blob: e6367b7303d1d1365b624ddb06f5ed07bf5731d7 [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:
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003317 void SetUp() override {
3318 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::KEYBOARD |
3319 InputDeviceClass::ALPHAKEY);
3320 }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003321 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003322 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003323 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003324
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003325 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003326 int32_t originalKeyCode, int32_t rotatedKeyCode,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003327 ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328};
3329
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003330/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3331 * orientation.
3332 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003333void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003334 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3335 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003336}
3337
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003338void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003339 int32_t originalScanCode, int32_t originalKeyCode,
Linnan Li13bf76a2024-05-05 19:18:02 +08003340 int32_t rotatedKeyCode,
3341 ui::LogicalDisplayId displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342 NotifyKeyArgs args;
3343
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3346 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3347 ASSERT_EQ(originalScanCode, args.scanCode);
3348 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003349 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3353 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3354 ASSERT_EQ(originalScanCode, args.scanCode);
3355 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003356 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357}
3358
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003360 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003361 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003363 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364}
3365
3366TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3367 const int32_t USAGE_A = 0x070004;
3368 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003369 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3370 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003371 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3372 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3373 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003374
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003375 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003376 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003377 // Initial metastate is AMETA_NONE.
3378 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003379
3380 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003382 NotifyKeyArgs args;
3383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3384 ASSERT_EQ(DEVICE_ID, args.deviceId);
3385 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3386 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3387 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3388 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3389 ASSERT_EQ(KEY_HOME, args.scanCode);
3390 ASSERT_EQ(AMETA_NONE, args.metaState);
3391 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3392 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3393 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3394
3395 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003396 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3398 ASSERT_EQ(DEVICE_ID, args.deviceId);
3399 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3400 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3401 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3402 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3403 ASSERT_EQ(KEY_HOME, args.scanCode);
3404 ASSERT_EQ(AMETA_NONE, args.metaState);
3405 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3406 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3407 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3408
3409 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3411 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3413 ASSERT_EQ(DEVICE_ID, args.deviceId);
3414 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3415 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3416 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3417 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3418 ASSERT_EQ(0, args.scanCode);
3419 ASSERT_EQ(AMETA_NONE, args.metaState);
3420 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3421 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3422 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3423
3424 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3426 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3428 ASSERT_EQ(DEVICE_ID, args.deviceId);
3429 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3430 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3431 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3432 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3433 ASSERT_EQ(0, args.scanCode);
3434 ASSERT_EQ(AMETA_NONE, args.metaState);
3435 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3436 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3437 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3438
3439 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3443 ASSERT_EQ(DEVICE_ID, args.deviceId);
3444 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3445 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3446 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3447 ASSERT_EQ(0, args.keyCode);
3448 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3449 ASSERT_EQ(AMETA_NONE, args.metaState);
3450 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3451 ASSERT_EQ(0U, args.policyFlags);
3452 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3453
3454 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3456 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3458 ASSERT_EQ(DEVICE_ID, args.deviceId);
3459 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3460 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3461 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3462 ASSERT_EQ(0, args.keyCode);
3463 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3464 ASSERT_EQ(AMETA_NONE, args.metaState);
3465 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3466 ASSERT_EQ(0U, args.policyFlags);
3467 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3468}
3469
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003470TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3471 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3472 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3473 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3474
3475 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003476 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003477
3478 // Key down by scan code.
3479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3480 NotifyKeyArgs args;
3481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3482 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3483
3484 // Key up by scan code.
3485 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3487 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3488}
3489
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003490/**
3491 * Ensure that the readTime is set to the time when the EV_KEY is received.
3492 */
3493TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3494 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3495
3496 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003497 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003498 NotifyKeyArgs args;
3499
3500 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003501 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3503 ASSERT_EQ(12, args.readTime);
3504
3505 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003506 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3508 ASSERT_EQ(15, args.readTime);
3509}
3510
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003512 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3513 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003514 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3515 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3516 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003518 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003519 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520
Arthur Hung95f68612022-04-07 14:08:22 +08003521 // Initial metastate is AMETA_NONE.
3522 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523
3524 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003526 NotifyKeyArgs args;
3527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3528 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003530 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531
3532 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003533 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3535 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003536 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537
3538 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003539 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3541 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003542 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543
3544 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003545 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3547 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003548 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003549 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550}
3551
3552TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003553 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3554 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3555 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3556 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003558 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003559 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003560
Michael Wrighta9cf4192022-12-01 23:46:39 +00003561 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003562 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3563 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3564 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3565 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3566 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3567 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3568 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3569 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3570}
3571
3572TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003573 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3574 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3575 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3576 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003577
Michael Wrightd02c5b62014-02-10 15:10:22 -08003578 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003579 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003580 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003581
Michael Wrighta9cf4192022-12-01 23:46:39 +00003582 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003583 ASSERT_NO_FATAL_FAILURE(
3584 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3586 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3587 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3588 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3589 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3590 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003591
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003592 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003593 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003594 ASSERT_NO_FATAL_FAILURE(
3595 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3597 AKEYCODE_DPAD_UP, DISPLAY_ID));
3598 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3599 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3600 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3601 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003603 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003604 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003605 ASSERT_NO_FATAL_FAILURE(
3606 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3607 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3608 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3609 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3610 AKEYCODE_DPAD_UP, DISPLAY_ID));
3611 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3612 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003614 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003615 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003616 ASSERT_NO_FATAL_FAILURE(
3617 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3618 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3619 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3620 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3621 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3622 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3623 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003624
3625 // Special case: if orientation changes while key is down, we still emit the same keycode
3626 // in the key up as we did in the key down.
3627 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003628 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003629 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3632 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3633 ASSERT_EQ(KEY_UP, args.scanCode);
3634 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3635
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003636 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003637 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003638 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3640 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3641 ASSERT_EQ(KEY_UP, args.scanCode);
3642 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3643}
3644
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003645TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3646 // If the keyboard is not orientation aware,
3647 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003648 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003649
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003650 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003651 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003652 NotifyKeyArgs args;
3653
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003654 // Display id should be LogicalDisplayId::INVALID without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003659 ASSERT_EQ(ui::LogicalDisplayId::INVALID, args.displayId);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003660
Michael Wrighta9cf4192022-12-01 23:46:39 +00003661 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003666 ASSERT_EQ(ui::LogicalDisplayId::INVALID, args.displayId);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003667}
3668
3669TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3670 // If the keyboard is orientation aware,
3671 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003672 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003673
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003674 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003675 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003676 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003677 NotifyKeyArgs args;
3678
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003679 // Display id should be LogicalDisplayId::INVALID without any display configuration.
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003680 // ^--- already checked by the previous test
3681
Michael Wrighta9cf4192022-12-01 23:46:39 +00003682 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003683 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3688 ASSERT_EQ(DISPLAY_ID, args.displayId);
3689
Linnan Li13bf76a2024-05-05 19:18:02 +08003690 constexpr ui::LogicalDisplayId newDisplayId = ui::LogicalDisplayId{2};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003691 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003692 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003693 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3698 ASSERT_EQ(newDisplayId, args.displayId);
3699}
3700
Michael Wrightd02c5b62014-02-10 15:10:22 -08003701TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003702 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003703 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003704
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003705 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003706 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003707
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003708 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003709 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003710}
3711
Philip Junker4af3b3d2021-12-14 10:36:55 +01003712TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3713 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003714 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Philip Junker4af3b3d2021-12-14 10:36:55 +01003715
3716 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3717 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3718 << "If a mapping is available, the result is equal to the mapping";
3719
3720 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3721 << "If no mapping is available, the result is the key location";
3722}
3723
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003725 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003726 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003728 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003729 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003730
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003731 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003732 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733}
3734
3735TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003736 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003737 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003739 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740
Michael Wrightd02c5b62014-02-10 15:10:22 -08003741 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003742 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743 ASSERT_TRUE(flags[0]);
3744 ASSERT_FALSE(flags[1]);
3745}
3746
3747TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003748 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3749 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3750 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3751 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3752 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3753 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003754
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003755 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003756 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003757 // Initial metastate is AMETA_NONE.
3758 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759
3760 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003761 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3762 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3763 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764
3765 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003768 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3769 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3770 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003771 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003772
3773 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003776 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3777 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3778 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003779 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003780
3781 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003784 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3785 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3786 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003787 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788
3789 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003792 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3793 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3794 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003795 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796
3797 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003800 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3801 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3802 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003803 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804
3805 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003808 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3809 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3810 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003811 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003812}
3813
Chris Yea52ade12020-08-27 16:49:20 -07003814TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3815 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3816 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3817 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3818 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3819
3820 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003821 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Chris Yea52ade12020-08-27 16:49:20 -07003822
Chris Yea52ade12020-08-27 16:49:20 -07003823 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003824 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003825 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3826 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3827 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3828 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3829
3830 NotifyKeyArgs args;
3831 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3834 ASSERT_EQ(AMETA_NONE, args.metaState);
3835 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3836 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3837 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3838
3839 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003840 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3842 ASSERT_EQ(AMETA_NONE, args.metaState);
3843 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3844 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3845 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3846}
3847
Arthur Hung2c9a3342019-07-23 14:18:59 +08003848TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3849 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003850 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3851 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3852 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3853 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003854
3855 // keyboard 2.
3856 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003857 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003858 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003859 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003860 std::shared_ptr<InputDevice> device2 =
3861 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003862 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003863
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003864 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3865 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3866 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3867 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003868
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003869 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003870 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003871
Arpit Singh67ca6842023-04-26 14:43:16 +00003872 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003873 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003874 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3875 mFakePolicy
3876 ->getReaderConfiguration(),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003877 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003878 std::list<NotifyArgs> unused =
3879 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003880 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003881 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003882
3883 // Prepared displays and associated info.
3884 constexpr uint8_t hdmi1 = 0;
3885 constexpr uint8_t hdmi2 = 1;
3886 const std::string SECONDARY_UNIQUE_ID = "local:1";
3887
3888 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3889 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3890
3891 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003892 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003893 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003894 ASSERT_FALSE(device2->isEnabled());
3895
3896 // Prepare second display.
Linnan Li13bf76a2024-05-05 19:18:02 +08003897 constexpr ui::LogicalDisplayId newDisplayId = ui::LogicalDisplayId{2};
Michael Wrighta9cf4192022-12-01 23:46:39 +00003898 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003899 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003900 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003901 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003902 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003903 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003904 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003905
3906 // Device should be enabled after the associated display is found.
3907 ASSERT_TRUE(mDevice->isEnabled());
3908 ASSERT_TRUE(device2->isEnabled());
3909
3910 // Test pad key events
3911 ASSERT_NO_FATAL_FAILURE(
3912 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3913 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3914 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3915 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3916 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3917 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3918 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3919
3920 ASSERT_NO_FATAL_FAILURE(
3921 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3922 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3923 AKEYCODE_DPAD_RIGHT, newDisplayId));
3924 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3925 AKEYCODE_DPAD_DOWN, newDisplayId));
3926 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3927 AKEYCODE_DPAD_LEFT, newDisplayId));
3928}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929
arthurhungc903df12020-08-11 15:08:42 +08003930TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3931 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3932 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3933 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3934 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3935 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3936 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3937
3938 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003939 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003940 // Initial metastate is AMETA_NONE.
3941 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003942
3943 // Initialization should have turned all of the lights off.
3944 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3945 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3946 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3947
3948 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003951 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3952 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3953
3954 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003957 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3958 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3959
3960 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003963 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3964 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3965
3966 mFakeEventHub->removeDevice(EVENTHUB_ID);
3967 mReader->loopOnce();
3968
3969 // keyboard 2 should default toggle keys.
3970 const std::string USB2 = "USB2";
3971 const std::string DEVICE_NAME2 = "KEYBOARD2";
3972 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3973 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3974 std::shared_ptr<InputDevice> device2 =
3975 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003976 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003977 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3978 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3979 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3980 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3981 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3982 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3983
Arpit Singh67ca6842023-04-26 14:43:16 +00003984 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003985 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003986 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3987 mFakePolicy
3988 ->getReaderConfiguration(),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003989 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003990 std::list<NotifyArgs> unused =
3991 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003992 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003993 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003994
3995 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3996 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3997 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003998 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3999 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004000}
4001
Arthur Hungcb40a002021-08-03 14:31:01 +00004002TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4003 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4004 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4005 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4006
4007 // Suppose we have two mappers. (DPAD + KEYBOARD)
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004008 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD);
Arthur Hungcb40a002021-08-03 14:31:01 +00004009 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004010 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08004011 // Initial metastate is AMETA_NONE.
4012 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004013
4014 mReader->toggleCapsLockState(DEVICE_ID);
4015 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4016}
4017
Arthur Hungfb3cc112022-04-13 07:39:50 +00004018TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4019 // keyboard 1.
4020 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4021 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4022 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4023 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4024 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4025 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4026
4027 KeyboardInputMapper& mapper1 =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004028 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004029
4030 // keyboard 2.
4031 const std::string USB2 = "USB2";
4032 const std::string DEVICE_NAME2 = "KEYBOARD2";
4033 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4034 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4035 std::shared_ptr<InputDevice> device2 =
4036 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4037 ftl::Flags<InputDeviceClass>(0));
4038 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4039 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4040 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4041 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4042 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4043 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4044
Arpit Singh67ca6842023-04-26 14:43:16 +00004045 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004046 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00004047 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
4048 mFakePolicy
4049 ->getReaderConfiguration(),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004050 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004051 std::list<NotifyArgs> unused =
4052 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004053 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004054 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004055
Arthur Hung95f68612022-04-07 14:08:22 +08004056 // Initial metastate is AMETA_NONE.
4057 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4058 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4059
4060 // Toggle num lock on and off.
4061 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4062 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004063 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4064 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4065 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4066
4067 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4068 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4069 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4070 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4071 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4072
4073 // Toggle caps lock on and off.
4074 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4075 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4076 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4077 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4078 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4079
4080 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4081 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4082 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4083 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4084 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4085
4086 // Toggle scroll lock on and off.
4087 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4088 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4089 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4090 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4091 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4092
4093 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4094 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4095 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4096 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4097 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4098}
4099
Arthur Hung2141d542022-08-23 07:45:21 +00004100TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4101 const int32_t USAGE_A = 0x070004;
4102 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4103 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4104
4105 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004106 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung2141d542022-08-23 07:45:21 +00004107 // Key down by scan code.
4108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4109 NotifyKeyArgs args;
4110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4111 ASSERT_EQ(DEVICE_ID, args.deviceId);
4112 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4113 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4114 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4115 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4116 ASSERT_EQ(KEY_HOME, args.scanCode);
4117 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4118
4119 // Disable device, it should synthesize cancellation events for down events.
4120 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004121 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00004122
4123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4124 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4125 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4126 ASSERT_EQ(KEY_HOME, args.scanCode);
4127 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4128}
4129
Zixuan Qufecb6062022-11-12 04:44:31 +00004130TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004131 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Zixuan Qufecb6062022-11-12 04:44:31 +00004132 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004133 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4134 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00004135
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004136 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00004137 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4138
4139 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004140 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00004141
4142 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
4143 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
4144 deviceInfo.getKeyboardLayoutInfo()->languageTag);
4145 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
4146 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004147 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
4148
4149 // Call change layout association with the same values: Generation shouldn't change
4150 generation = mReader->getContext()->getGeneration();
4151 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4152 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4153 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
4154 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00004155}
4156
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004157TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
4158 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
4159 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
4160
4161 // Configuration
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004162 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004163 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004164 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004165
4166 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4167 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4168}
4169
Justin Chung71ddb432023-03-27 04:29:07 +00004170TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4171 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4172 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004173 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Justin Chung71ddb432023-03-27 04:29:07 +00004174 NotifyKeyArgs args;
4175
4176 // Key down
4177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4179 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4180}
4181
Prabir Pradhan34f35702024-07-23 21:59:36 +00004182/**
4183 * When there is more than one KeyboardInputMapper for an InputDevice, each mapper should produce
4184 * events that use the shared keyboard source across all mappers. This is to ensure that each
4185 * input device generates key events in a consistent manner, regardless of which mapper produces
4186 * the event.
4187 */
4188TEST_F(KeyboardInputMapperTest, UsesSharedKeyboardSource) {
4189 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4190
4191 // Add a mapper with SOURCE_KEYBOARD
4192 KeyboardInputMapper& keyboardMapper =
4193 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
4194
4195 process(keyboardMapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 1);
4196 ASSERT_NO_FATAL_FAILURE(
4197 mFakeListener->assertNotifyKeyWasCalled(WithSource(AINPUT_SOURCE_KEYBOARD)));
4198 process(keyboardMapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 0);
4199 ASSERT_NO_FATAL_FAILURE(
4200 mFakeListener->assertNotifyKeyWasCalled(WithSource(AINPUT_SOURCE_KEYBOARD)));
4201
4202 // Add a mapper with SOURCE_DPAD
4203 KeyboardInputMapper& dpadMapper =
4204 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD);
4205 for (auto* mapper : {&keyboardMapper, &dpadMapper}) {
4206 process(*mapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 1);
4207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(
4208 WithSource(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_DPAD)));
4209 process(*mapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 0);
4210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(
4211 WithSource(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_DPAD)));
4212 }
4213
4214 // Add a mapper with SOURCE_GAMEPAD
4215 KeyboardInputMapper& gamepadMapper =
4216 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_GAMEPAD);
4217 for (auto* mapper : {&keyboardMapper, &dpadMapper, &gamepadMapper}) {
4218 process(*mapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 1);
4219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(
4220 WithSource(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_DPAD | AINPUT_SOURCE_GAMEPAD)));
4221 process(*mapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 0);
4222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(
4223 WithSource(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_DPAD | AINPUT_SOURCE_GAMEPAD)));
4224 }
4225}
4226
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004227// --- KeyboardInputMapperTest_ExternalAlphabeticDevice ---
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004228
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004229class KeyboardInputMapperTest_ExternalAlphabeticDevice : public InputMapperTest {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004230protected:
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004231 void SetUp() override {
4232 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::KEYBOARD |
4233 InputDeviceClass::ALPHAKEY | InputDeviceClass::EXTERNAL);
4234 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004235};
4236
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004237// --- KeyboardInputMapperTest_ExternalNonAlphabeticDevice ---
4238
4239class KeyboardInputMapperTest_ExternalNonAlphabeticDevice : public InputMapperTest {
4240protected:
4241 void SetUp() override {
4242 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::KEYBOARD |
4243 InputDeviceClass::EXTERNAL);
4244 }
4245};
4246
4247TEST_F(KeyboardInputMapperTest_ExternalAlphabeticDevice, WakeBehavior_AlphabeticKeyboard) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004248 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4249 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004250
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004251 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4252 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4253 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4254 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004255
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004256 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004257 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Powei Fengd041c5d2019-05-03 17:11:33 -07004258
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004260 NotifyKeyArgs args;
4261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4262 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4263
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004264 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4266 ASSERT_EQ(uint32_t(0), args.policyFlags);
4267
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004270 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004271
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004272 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4274 ASSERT_EQ(uint32_t(0), args.policyFlags);
4275
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4278 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4279
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004280 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4282 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4283}
4284
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004285TEST_F(KeyboardInputMapperTest_ExternalNonAlphabeticDevice, WakeBehavior_NonAlphabeticKeyboard) {
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004286 // For external devices, keys will trigger wake on key down. Media keys should not trigger
4287 // wake if triggered from external non-alphaebtic keyboard (e.g. headsets).
4288
4289 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4290 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4291 POLICY_FLAG_WAKE);
4292
4293 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004294 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004295
4296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
4297 NotifyKeyArgs args;
4298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4299 ASSERT_EQ(uint32_t(0), args.policyFlags);
4300
4301 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
4302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4303 ASSERT_EQ(uint32_t(0), args.policyFlags);
4304
4305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
4306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4307 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4308
4309 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
4310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4311 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4312}
4313
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004314TEST_F(KeyboardInputMapperTest_ExternalAlphabeticDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004315 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004316
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004317 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4318 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4319 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004320
Powei Fengd041c5d2019-05-03 17:11:33 -07004321 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004322 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004323 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Powei Fengd041c5d2019-05-03 17:11:33 -07004324
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004326 NotifyKeyArgs args;
4327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4328 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4329
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004330 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4332 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4333
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4336 ASSERT_EQ(uint32_t(0), args.policyFlags);
4337
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004338 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4340 ASSERT_EQ(uint32_t(0), args.policyFlags);
4341
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4344 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4345
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004346 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4348 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4349}
4350
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351// --- TouchInputMapperTest ---
4352
4353class TouchInputMapperTest : public InputMapperTest {
4354protected:
4355 static const int32_t RAW_X_MIN;
4356 static const int32_t RAW_X_MAX;
4357 static const int32_t RAW_Y_MIN;
4358 static const int32_t RAW_Y_MAX;
4359 static const int32_t RAW_TOUCH_MIN;
4360 static const int32_t RAW_TOUCH_MAX;
4361 static const int32_t RAW_TOOL_MIN;
4362 static const int32_t RAW_TOOL_MAX;
4363 static const int32_t RAW_PRESSURE_MIN;
4364 static const int32_t RAW_PRESSURE_MAX;
4365 static const int32_t RAW_ORIENTATION_MIN;
4366 static const int32_t RAW_ORIENTATION_MAX;
4367 static const int32_t RAW_DISTANCE_MIN;
4368 static const int32_t RAW_DISTANCE_MAX;
4369 static const int32_t RAW_TILT_MIN;
4370 static const int32_t RAW_TILT_MAX;
4371 static const int32_t RAW_ID_MIN;
4372 static const int32_t RAW_ID_MAX;
4373 static const int32_t RAW_SLOT_MIN;
4374 static const int32_t RAW_SLOT_MAX;
4375 static const float X_PRECISION;
4376 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004377 static const float X_PRECISION_VIRTUAL;
4378 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379
4380 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004381 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382
4383 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4384
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004385 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004386 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004387
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388 enum Axes {
4389 POSITION = 1 << 0,
4390 TOUCH = 1 << 1,
4391 TOOL = 1 << 2,
4392 PRESSURE = 1 << 3,
4393 ORIENTATION = 1 << 4,
4394 MINOR = 1 << 5,
4395 ID = 1 << 6,
4396 DISTANCE = 1 << 7,
4397 TILT = 1 << 8,
4398 SLOT = 1 << 9,
4399 TOOL_TYPE = 1 << 10,
4400 };
4401
Michael Wrighta9cf4192022-12-01 23:46:39 +00004402 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004403 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004404 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004406 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 int32_t toRawX(float displayX);
4408 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004409 int32_t toRotatedRawX(float displayX);
4410 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004411 float toCookedX(float rawX, float rawY);
4412 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004414 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004416 float toDisplayY(int32_t rawY, int32_t displayHeight);
4417
Michael Wrightd02c5b62014-02-10 15:10:22 -08004418};
4419
4420const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4421const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4422const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4423const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4424const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4425const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4426const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4427const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004428const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4429const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4431const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4432const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4433const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4434const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4435const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4436const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4437const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4438const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4439const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4440const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4441const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004442const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4443 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4444const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4445 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004446const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4447 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448
4449const float TouchInputMapperTest::GEOMETRIC_SCALE =
4450 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4451 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4452
4453const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4454 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4455 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4456};
4457
Michael Wrighta9cf4192022-12-01 23:46:39 +00004458void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004459 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4460 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004461}
4462
4463void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4464 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004465 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466}
4467
Michael Wrighta9cf4192022-12-01 23:46:39 +00004468void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004469 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4470 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4471 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004472}
4473
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004475 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4476 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4477 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4478 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479}
4480
Jason Gerecke489fda82012-09-07 17:19:40 -07004481void TouchInputMapperTest::prepareLocationCalibration() {
4482 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4483}
4484
Michael Wrightd02c5b62014-02-10 15:10:22 -08004485int32_t TouchInputMapperTest::toRawX(float displayX) {
4486 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4487}
4488
4489int32_t TouchInputMapperTest::toRawY(float displayY) {
4490 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4491}
4492
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004493int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4494 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4495}
4496
4497int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4498 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4499}
4500
Jason Gerecke489fda82012-09-07 17:19:40 -07004501float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4502 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4503 return rawX;
4504}
4505
4506float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4507 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4508 return rawY;
4509}
4510
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004512 return toDisplayX(rawX, DISPLAY_WIDTH);
4513}
4514
4515float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4516 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004517}
4518
4519float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004520 return toDisplayY(rawY, DISPLAY_HEIGHT);
4521}
4522
4523float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4524 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004525}
4526
4527
4528// --- SingleTouchInputMapperTest ---
4529
4530class SingleTouchInputMapperTest : public TouchInputMapperTest {
4531protected:
4532 void prepareButtons();
4533 void prepareAxes(int axes);
4534
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004535 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4536 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4537 void processUp(SingleTouchInputMapper& mappery);
4538 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4539 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4540 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4541 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4542 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4543 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544};
4545
4546void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004547 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548}
4549
4550void SingleTouchInputMapperTest::prepareAxes(int axes) {
4551 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004552 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4553 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004554 }
4555 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004556 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4557 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558 }
4559 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004560 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4561 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562 }
4563 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004564 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4565 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566 }
4567 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004568 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4569 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570 }
4571}
4572
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004573void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004574 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577}
4578
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004579void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4581 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582}
4583
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004584void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586}
4587
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004588void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004590}
4591
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004592void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4593 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595}
4596
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004597void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599}
4600
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004601void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4602 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004603 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4604 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605}
4606
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004607void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4608 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004609 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610}
4611
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004612void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004614}
4615
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617 prepareButtons();
4618 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00004619 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620
Josep del Río2d8c79a2023-01-23 19:33:50 +00004621 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622}
4623
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004625 prepareButtons();
4626 prepareAxes(POSITION);
4627 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00004628 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004629
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004630 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631}
4632
4633TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004634 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004635 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636 prepareButtons();
4637 prepareAxes(POSITION);
4638 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004639 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640
4641 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004642 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643
4644 // Virtual key is down.
4645 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4646 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4647 processDown(mapper, x, y);
4648 processSync(mapper);
4649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4650
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004651 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652
4653 // Virtual key is up.
4654 processUp(mapper);
4655 processSync(mapper);
4656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4657
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004658 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659}
4660
4661TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004663 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664 prepareButtons();
4665 prepareAxes(POSITION);
4666 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004667 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668
4669 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004670 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671
4672 // Virtual key is down.
4673 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4674 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4675 processDown(mapper, x, y);
4676 processSync(mapper);
4677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4678
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004679 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004680
4681 // Virtual key is up.
4682 processUp(mapper);
4683 processSync(mapper);
4684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4685
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004686 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687}
4688
4689TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004690 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004691 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692 prepareButtons();
4693 prepareAxes(POSITION);
4694 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004695 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696
Michael Wrightd02c5b62014-02-10 15:10:22 -08004697 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004698 ASSERT_TRUE(
4699 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 ASSERT_TRUE(flags[0]);
4701 ASSERT_FALSE(flags[1]);
4702}
4703
4704TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004706 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004707 prepareButtons();
4708 prepareAxes(POSITION);
4709 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004710 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711
arthurhungdcef2dc2020-08-11 14:47:50 +08004712 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713
4714 NotifyKeyArgs args;
4715
4716 // Press virtual key.
4717 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4718 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4719 processDown(mapper, x, y);
4720 processSync(mapper);
4721
4722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4723 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4724 ASSERT_EQ(DEVICE_ID, args.deviceId);
4725 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4726 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4727 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4728 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4729 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4730 ASSERT_EQ(KEY_HOME, args.scanCode);
4731 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4732 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4733
4734 // Release virtual key.
4735 processUp(mapper);
4736 processSync(mapper);
4737
4738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4739 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4740 ASSERT_EQ(DEVICE_ID, args.deviceId);
4741 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4742 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4743 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4744 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4745 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4746 ASSERT_EQ(KEY_HOME, args.scanCode);
4747 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4748 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4749
4750 // Should not have sent any motions.
4751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4752}
4753
4754TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004755 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004756 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 prepareButtons();
4758 prepareAxes(POSITION);
4759 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004760 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004761
arthurhungdcef2dc2020-08-11 14:47:50 +08004762 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763
4764 NotifyKeyArgs keyArgs;
4765
4766 // Press virtual key.
4767 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4768 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4769 processDown(mapper, x, y);
4770 processSync(mapper);
4771
4772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4773 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4774 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4775 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4776 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4777 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4778 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4779 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4780 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4781 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4782 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4783
4784 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4785 // into the display area.
4786 y -= 100;
4787 processMove(mapper, x, y);
4788 processSync(mapper);
4789
4790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4791 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4792 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4793 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4794 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4795 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4796 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4797 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4798 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4799 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4800 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4801 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4802
4803 NotifyMotionArgs motionArgs;
4804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4805 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4806 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4807 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4808 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4809 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4810 ASSERT_EQ(0, motionArgs.flags);
4811 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4812 ASSERT_EQ(0, motionArgs.buttonState);
4813 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004814 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004816 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4818 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4819 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4820 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4821 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4822
4823 // Keep moving out of bounds. Should generate a pointer move.
4824 y -= 50;
4825 processMove(mapper, x, y);
4826 processSync(mapper);
4827
4828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4829 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4830 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4831 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4832 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4834 ASSERT_EQ(0, motionArgs.flags);
4835 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4836 ASSERT_EQ(0, motionArgs.buttonState);
4837 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004838 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004840 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4842 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4843 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4844 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4845 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4846
4847 // Release out of bounds. Should generate a pointer up.
4848 processUp(mapper);
4849 processSync(mapper);
4850
4851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4852 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4853 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4854 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4855 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4856 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4857 ASSERT_EQ(0, motionArgs.flags);
4858 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4859 ASSERT_EQ(0, motionArgs.buttonState);
4860 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004861 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004863 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4865 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4866 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4867 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4868 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4869
4870 // Should not have sent any more keys or motions.
4871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4873}
4874
4875TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004876 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004877 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878 prepareButtons();
4879 prepareAxes(POSITION);
4880 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004881 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004882
arthurhungdcef2dc2020-08-11 14:47:50 +08004883 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884
4885 NotifyMotionArgs motionArgs;
4886
4887 // Initially go down out of bounds.
4888 int32_t x = -10;
4889 int32_t y = -10;
4890 processDown(mapper, x, y);
4891 processSync(mapper);
4892
4893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4894
4895 // Move into the display area. Should generate a pointer down.
4896 x = 50;
4897 y = 75;
4898 processMove(mapper, x, y);
4899 processSync(mapper);
4900
4901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4902 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4903 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4904 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4905 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4906 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4907 ASSERT_EQ(0, motionArgs.flags);
4908 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4909 ASSERT_EQ(0, motionArgs.buttonState);
4910 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004911 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004913 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4915 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4916 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4917 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4918 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4919
4920 // Release. Should generate a pointer up.
4921 processUp(mapper);
4922 processSync(mapper);
4923
4924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4925 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4926 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4927 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4928 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4929 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4930 ASSERT_EQ(0, motionArgs.flags);
4931 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4932 ASSERT_EQ(0, motionArgs.buttonState);
4933 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004934 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004936 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4938 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4939 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4940 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4941 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4942
4943 // Should not have sent any more keys or motions.
4944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4946}
4947
Santos Cordonfa5cf462017-04-05 10:37:00 -07004948TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004949 addConfigurationProperty("touch.deviceType", "touchScreen");
4950 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4951
Michael Wrighta9cf4192022-12-01 23:46:39 +00004952 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004953 prepareButtons();
4954 prepareAxes(POSITION);
4955 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004956 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004957
arthurhungdcef2dc2020-08-11 14:47:50 +08004958 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004959
4960 NotifyMotionArgs motionArgs;
4961
4962 // Down.
4963 int32_t x = 100;
4964 int32_t y = 125;
4965 processDown(mapper, x, y);
4966 processSync(mapper);
4967
4968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4969 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4970 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4971 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4972 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4973 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4974 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4975 ASSERT_EQ(0, motionArgs.flags);
4976 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4977 ASSERT_EQ(0, motionArgs.buttonState);
4978 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004979 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004980 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004981 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4983 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4984 1, 0, 0, 0, 0, 0, 0, 0));
4985 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4986 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4987 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4988
4989 // Move.
4990 x += 50;
4991 y += 75;
4992 processMove(mapper, x, y);
4993 processSync(mapper);
4994
4995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4996 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4997 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4998 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4999 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5000 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5001 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5002 ASSERT_EQ(0, motionArgs.flags);
5003 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5004 ASSERT_EQ(0, motionArgs.buttonState);
5005 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005006 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005007 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005008 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5010 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5011 1, 0, 0, 0, 0, 0, 0, 0));
5012 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5013 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5014 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5015
5016 // Up.
5017 processUp(mapper);
5018 processSync(mapper);
5019
5020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5021 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5022 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5023 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5024 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5025 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5026 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5027 ASSERT_EQ(0, motionArgs.flags);
5028 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5029 ASSERT_EQ(0, motionArgs.buttonState);
5030 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005031 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005032 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005033 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5035 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5036 1, 0, 0, 0, 0, 0, 0, 0));
5037 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5038 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5039 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5040
5041 // Should not have sent any more keys or motions.
5042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5044}
5045
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005048 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049 prepareButtons();
5050 prepareAxes(POSITION);
5051 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005052 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005053
arthurhungdcef2dc2020-08-11 14:47:50 +08005054 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055
5056 NotifyMotionArgs motionArgs;
5057
5058 // Down.
5059 int32_t x = 100;
5060 int32_t y = 125;
5061 processDown(mapper, x, y);
5062 processSync(mapper);
5063
5064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5065 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5066 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5067 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5068 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5069 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5070 ASSERT_EQ(0, motionArgs.flags);
5071 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5072 ASSERT_EQ(0, motionArgs.buttonState);
5073 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005074 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005076 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5078 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5079 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5080 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5081 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5082
5083 // Move.
5084 x += 50;
5085 y += 75;
5086 processMove(mapper, x, y);
5087 processSync(mapper);
5088
5089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5090 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5091 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5092 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5093 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5095 ASSERT_EQ(0, motionArgs.flags);
5096 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5097 ASSERT_EQ(0, motionArgs.buttonState);
5098 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005099 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005101 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5103 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5104 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5105 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5106 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5107
5108 // Up.
5109 processUp(mapper);
5110 processSync(mapper);
5111
5112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5113 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5114 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5115 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5116 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5117 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5118 ASSERT_EQ(0, motionArgs.flags);
5119 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5120 ASSERT_EQ(0, motionArgs.buttonState);
5121 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005122 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005124 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5126 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5127 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5128 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5129 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5130
5131 // Should not have sent any more keys or motions.
5132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5134}
5135
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005136TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137 addConfigurationProperty("touch.deviceType", "touchScreen");
5138 prepareButtons();
5139 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005140 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5141 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005142 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143
5144 NotifyMotionArgs args;
5145
5146 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005147 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148 processDown(mapper, toRawX(50), toRawY(75));
5149 processSync(mapper);
5150
5151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5152 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5153 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5154
5155 processUp(mapper);
5156 processSync(mapper);
5157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5158}
5159
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005160TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161 addConfigurationProperty("touch.deviceType", "touchScreen");
5162 prepareButtons();
5163 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005164 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5165 // orientation-aware are affected by display rotation.
5166 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005167 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005168
5169 NotifyMotionArgs args;
5170
5171 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005172 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005173 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005174 processDown(mapper, toRawX(50), toRawY(75));
5175 processSync(mapper);
5176
5177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5178 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5179 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5180
5181 processUp(mapper);
5182 processSync(mapper);
5183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5184
5185 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005186 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005187 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005188 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189 processSync(mapper);
5190
5191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5192 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5193 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5194
5195 processUp(mapper);
5196 processSync(mapper);
5197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5198
5199 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005200 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005201 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005202 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5203 processSync(mapper);
5204
5205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5206 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5207 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5208
5209 processUp(mapper);
5210 processSync(mapper);
5211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5212
5213 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005214 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005215 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005216 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217 processSync(mapper);
5218
5219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5220 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5221 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5222
5223 processUp(mapper);
5224 processSync(mapper);
5225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5226}
5227
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005228TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5229 addConfigurationProperty("touch.deviceType", "touchScreen");
5230 prepareButtons();
5231 prepareAxes(POSITION);
5232 addConfigurationProperty("touch.orientationAware", "1");
5233 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5234 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005235 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005236 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005237 NotifyMotionArgs args;
5238
5239 // Orientation 0.
5240 processDown(mapper, toRawX(50), toRawY(75));
5241 processSync(mapper);
5242
5243 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5244 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5245 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5246
5247 processUp(mapper);
5248 processSync(mapper);
5249 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5250}
5251
5252TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5253 addConfigurationProperty("touch.deviceType", "touchScreen");
5254 prepareButtons();
5255 prepareAxes(POSITION);
5256 addConfigurationProperty("touch.orientationAware", "1");
5257 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5258 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005259 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005260 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005261 NotifyMotionArgs args;
5262
5263 // Orientation 90.
5264 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5265 processSync(mapper);
5266
5267 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5268 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5269 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5270
5271 processUp(mapper);
5272 processSync(mapper);
5273 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5274}
5275
5276TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5277 addConfigurationProperty("touch.deviceType", "touchScreen");
5278 prepareButtons();
5279 prepareAxes(POSITION);
5280 addConfigurationProperty("touch.orientationAware", "1");
5281 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5282 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005283 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005284 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005285 NotifyMotionArgs args;
5286
5287 // Orientation 180.
5288 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5289 processSync(mapper);
5290
5291 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5292 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5293 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5294
5295 processUp(mapper);
5296 processSync(mapper);
5297 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5298}
5299
5300TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5301 addConfigurationProperty("touch.deviceType", "touchScreen");
5302 prepareButtons();
5303 prepareAxes(POSITION);
5304 addConfigurationProperty("touch.orientationAware", "1");
5305 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5306 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005307 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005308 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005309 NotifyMotionArgs args;
5310
5311 // Orientation 270.
5312 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5313 processSync(mapper);
5314
5315 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5316 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5317 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5318
5319 processUp(mapper);
5320 processSync(mapper);
5321 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5322}
5323
5324TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5325 addConfigurationProperty("touch.deviceType", "touchScreen");
5326 prepareButtons();
5327 prepareAxes(POSITION);
5328 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5329 // orientation-aware are affected by display rotation.
5330 addConfigurationProperty("touch.orientationAware", "0");
5331 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005332 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005333
5334 NotifyMotionArgs args;
5335
5336 // Orientation 90, Rotation 0.
5337 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005338 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005339 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5340 processSync(mapper);
5341
5342 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5343 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5344 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5345
5346 processUp(mapper);
5347 processSync(mapper);
5348 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5349
5350 // Orientation 90, Rotation 90.
5351 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005352 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005353 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005354 processSync(mapper);
5355
5356 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5357 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5358 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5359
5360 processUp(mapper);
5361 processSync(mapper);
5362 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5363
5364 // Orientation 90, Rotation 180.
5365 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005366 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005367 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5368 processSync(mapper);
5369
5370 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5371 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5372 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5373
5374 processUp(mapper);
5375 processSync(mapper);
5376 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5377
5378 // Orientation 90, Rotation 270.
5379 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005380 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005381 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 -07005382 processSync(mapper);
5383
5384 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5385 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5386 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5387
5388 processUp(mapper);
5389 processSync(mapper);
5390 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5391}
5392
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005393TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5394 addConfigurationProperty("touch.deviceType", "touchScreen");
5395 prepareButtons();
5396 prepareAxes(POSITION);
5397 addConfigurationProperty("touch.orientationAware", "1");
5398 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005399 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005400
5401 // Set a physical frame in the display viewport.
5402 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5403 viewport->physicalLeft = 20;
5404 viewport->physicalTop = 600;
5405 viewport->physicalRight = 30;
5406 viewport->physicalBottom = 610;
5407 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005408 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005409
5410 // Start the touch.
5411 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5412 processSync(mapper);
5413
5414 // Expect all input starting outside the physical frame to be ignored.
5415 const std::array<Point, 6> outsidePoints = {
5416 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5417 for (const auto& p : outsidePoints) {
5418 processMove(mapper, toRawX(p.x), toRawY(p.y));
5419 processSync(mapper);
5420 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5421 }
5422
5423 // Move the touch into the physical frame.
5424 processMove(mapper, toRawX(25), toRawY(605));
5425 processSync(mapper);
5426 NotifyMotionArgs args;
5427 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5428 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5429 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5430 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5431
5432 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5433 for (const auto& p : outsidePoints) {
5434 processMove(mapper, toRawX(p.x), toRawY(p.y));
5435 processSync(mapper);
5436 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5437 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5438 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5439 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5440 }
5441
5442 processUp(mapper);
5443 processSync(mapper);
5444 EXPECT_NO_FATAL_FAILURE(
5445 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5446}
5447
Harry Cutts1db43992023-06-19 17:05:07 +00005448TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
Harry Cutts1db43992023-06-19 17:05:07 +00005449 addConfigurationProperty("touch.deviceType", "pointer");
5450 prepareAxes(POSITION);
5451 prepareDisplay(ui::ROTATION_0);
5452 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5453
5454 // Set a physical frame in the display viewport.
5455 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5456 viewport->physicalLeft = 20;
5457 viewport->physicalTop = 600;
5458 viewport->physicalRight = 30;
5459 viewport->physicalBottom = 610;
5460 mFakePolicy->updateViewport(*viewport);
5461 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5462
5463 // Start the touch.
5464 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5465 processSync(mapper);
5466
5467 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5468 // produced.
5469 const std::array<Point, 6> outsidePoints = {
5470 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5471 for (const auto& p : outsidePoints) {
5472 processMove(mapper, toRawX(p.x), toRawY(p.y));
5473 processSync(mapper);
5474 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5475 }
5476}
5477
Michael Wrightd02c5b62014-02-10 15:10:22 -08005478TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005479 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005480 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005481 prepareButtons();
5482 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00005483 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005484
5485 // These calculations are based on the input device calibration documentation.
5486 int32_t rawX = 100;
5487 int32_t rawY = 200;
5488 int32_t rawPressure = 10;
5489 int32_t rawToolMajor = 12;
5490 int32_t rawDistance = 2;
5491 int32_t rawTiltX = 30;
5492 int32_t rawTiltY = 110;
5493
5494 float x = toDisplayX(rawX);
5495 float y = toDisplayY(rawY);
5496 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5497 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5498 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5499 float distance = float(rawDistance);
5500
5501 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5502 float tiltScale = M_PI / 180;
5503 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5504 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5505 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5506 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5507
5508 processDown(mapper, rawX, rawY);
5509 processPressure(mapper, rawPressure);
5510 processToolMajor(mapper, rawToolMajor);
5511 processDistance(mapper, rawDistance);
5512 processTilt(mapper, rawTiltX, rawTiltY);
5513 processSync(mapper);
5514
5515 NotifyMotionArgs args;
5516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5518 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5519 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
Prabir Pradhan9a53b552024-06-04 02:59:40 +00005520 ASSERT_EQ(args.flags,
5521 AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_ORIENTATION |
5522 AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_DIRECTIONAL_ORIENTATION);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005523}
5524
Jason Gerecke489fda82012-09-07 17:19:40 -07005525TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005526 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005527 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005528 prepareLocationCalibration();
5529 prepareButtons();
5530 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005531 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005532
5533 int32_t rawX = 100;
5534 int32_t rawY = 200;
5535
5536 float x = toDisplayX(toCookedX(rawX, rawY));
5537 float y = toDisplayY(toCookedY(rawX, rawY));
5538
5539 processDown(mapper, rawX, rawY);
5540 processSync(mapper);
5541
5542 NotifyMotionArgs args;
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5545 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5546}
5547
Michael Wrightd02c5b62014-02-10 15:10:22 -08005548TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005549 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005550 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005551 prepareButtons();
5552 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005553 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005554
5555 NotifyMotionArgs motionArgs;
5556 NotifyKeyArgs keyArgs;
5557
5558 processDown(mapper, 100, 200);
5559 processSync(mapper);
5560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5561 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5562 ASSERT_EQ(0, motionArgs.buttonState);
5563
5564 // press BTN_LEFT, release BTN_LEFT
5565 processKey(mapper, BTN_LEFT, 1);
5566 processSync(mapper);
5567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5569 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5570
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5572 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5573 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5574
Michael Wrightd02c5b62014-02-10 15:10:22 -08005575 processKey(mapper, BTN_LEFT, 0);
5576 processSync(mapper);
5577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005578 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005579 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005580
5581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005583 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584
5585 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5586 processKey(mapper, BTN_RIGHT, 1);
5587 processKey(mapper, BTN_MIDDLE, 1);
5588 processSync(mapper);
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5591 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5592 motionArgs.buttonState);
5593
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5595 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5596 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5597
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5599 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5600 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5601 motionArgs.buttonState);
5602
Michael Wrightd02c5b62014-02-10 15:10:22 -08005603 processKey(mapper, BTN_RIGHT, 0);
5604 processSync(mapper);
5605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005606 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005607 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005608
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005611 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612
5613 processKey(mapper, BTN_MIDDLE, 0);
5614 processSync(mapper);
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005616 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005618
5619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005621 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005622
5623 // press BTN_BACK, release BTN_BACK
5624 processKey(mapper, BTN_BACK, 1);
5625 processSync(mapper);
5626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5627 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5628 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005629
Michael Wrightd02c5b62014-02-10 15:10:22 -08005630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005632 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5633
5634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5635 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5636 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005637
5638 processKey(mapper, BTN_BACK, 0);
5639 processSync(mapper);
5640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005641 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005642 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005643
5644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005645 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005646 ASSERT_EQ(0, motionArgs.buttonState);
5647
Michael Wrightd02c5b62014-02-10 15:10:22 -08005648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5649 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5650 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5651
5652 // press BTN_SIDE, release BTN_SIDE
5653 processKey(mapper, BTN_SIDE, 1);
5654 processSync(mapper);
5655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5656 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5657 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005658
Michael Wrightd02c5b62014-02-10 15:10:22 -08005659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005661 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5662
5663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5664 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5665 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666
5667 processKey(mapper, BTN_SIDE, 0);
5668 processSync(mapper);
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005670 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005671 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005672
5673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005675 ASSERT_EQ(0, motionArgs.buttonState);
5676
Michael Wrightd02c5b62014-02-10 15:10:22 -08005677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5678 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5679 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5680
5681 // press BTN_FORWARD, release BTN_FORWARD
5682 processKey(mapper, BTN_FORWARD, 1);
5683 processSync(mapper);
5684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5685 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5686 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005687
Michael Wrightd02c5b62014-02-10 15:10:22 -08005688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005689 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005690 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5691
5692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5693 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5694 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005695
5696 processKey(mapper, BTN_FORWARD, 0);
5697 processSync(mapper);
5698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005699 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005700 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005701
5702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005703 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005704 ASSERT_EQ(0, motionArgs.buttonState);
5705
Michael Wrightd02c5b62014-02-10 15:10:22 -08005706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5707 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5708 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5709
5710 // press BTN_EXTRA, release BTN_EXTRA
5711 processKey(mapper, BTN_EXTRA, 1);
5712 processSync(mapper);
5713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5714 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5715 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005716
Michael Wrightd02c5b62014-02-10 15:10:22 -08005717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005719 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5720
5721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5722 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5723 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724
5725 processKey(mapper, BTN_EXTRA, 0);
5726 processSync(mapper);
5727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005728 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005729 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005730
5731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005732 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005733 ASSERT_EQ(0, motionArgs.buttonState);
5734
Michael Wrightd02c5b62014-02-10 15:10:22 -08005735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5736 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5737 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5738
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5740
Michael Wrightd02c5b62014-02-10 15:10:22 -08005741 // press BTN_STYLUS, release BTN_STYLUS
5742 processKey(mapper, BTN_STYLUS, 1);
5743 processSync(mapper);
5744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005746 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5747
5748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5749 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5750 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005751
5752 processKey(mapper, BTN_STYLUS, 0);
5753 processSync(mapper);
5754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005755 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005756 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005757
5758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005760 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005761
5762 // press BTN_STYLUS2, release BTN_STYLUS2
5763 processKey(mapper, BTN_STYLUS2, 1);
5764 processSync(mapper);
5765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005767 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5768
5769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5770 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5771 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005772
5773 processKey(mapper, BTN_STYLUS2, 0);
5774 processSync(mapper);
5775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005776 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005777 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005778
5779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005781 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005782
5783 // release touch
5784 processUp(mapper);
5785 processSync(mapper);
5786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5787 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5788 ASSERT_EQ(0, motionArgs.buttonState);
5789}
5790
5791TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005792 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005793 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005794 prepareButtons();
5795 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005796 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005797
5798 NotifyMotionArgs motionArgs;
5799
5800 // default tool type is finger
5801 processDown(mapper, 100, 200);
5802 processSync(mapper);
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5804 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005805 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005806
5807 // eraser
5808 processKey(mapper, BTN_TOOL_RUBBER, 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::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005813
5814 // stylus
5815 processKey(mapper, BTN_TOOL_RUBBER, 0);
5816 processKey(mapper, BTN_TOOL_PEN, 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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005821
5822 // brush
5823 processKey(mapper, BTN_TOOL_PEN, 0);
5824 processKey(mapper, BTN_TOOL_BRUSH, 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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005829
5830 // pencil
5831 processKey(mapper, BTN_TOOL_BRUSH, 0);
5832 processKey(mapper, BTN_TOOL_PENCIL, 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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005837
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005838 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839 processKey(mapper, BTN_TOOL_PENCIL, 0);
5840 processKey(mapper, BTN_TOOL_AIRBRUSH, 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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005845
5846 // mouse
5847 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5848 processKey(mapper, BTN_TOOL_MOUSE, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005853
5854 // lens
5855 processKey(mapper, BTN_TOOL_MOUSE, 0);
5856 processKey(mapper, BTN_TOOL_LENS, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005861
5862 // double-tap
5863 processKey(mapper, BTN_TOOL_LENS, 0);
5864 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5865 processSync(mapper);
5866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5867 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005868 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005869
5870 // triple-tap
5871 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5872 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5873 processSync(mapper);
5874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005876 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005877
5878 // quad-tap
5879 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5880 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5881 processSync(mapper);
5882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005884 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885
5886 // finger
5887 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5888 processKey(mapper, BTN_TOOL_FINGER, 1);
5889 processSync(mapper);
5890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5891 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005892 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005893
5894 // stylus trumps finger
5895 processKey(mapper, BTN_TOOL_PEN, 1);
5896 processSync(mapper);
5897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005899 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005900
5901 // eraser trumps stylus
5902 processKey(mapper, BTN_TOOL_RUBBER, 1);
5903 processSync(mapper);
5904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5905 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005906 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005907
5908 // mouse trumps eraser
5909 processKey(mapper, BTN_TOOL_MOUSE, 1);
5910 processSync(mapper);
5911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005913 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005914
5915 // back to default tool type
5916 processKey(mapper, BTN_TOOL_MOUSE, 0);
5917 processKey(mapper, BTN_TOOL_RUBBER, 0);
5918 processKey(mapper, BTN_TOOL_PEN, 0);
5919 processKey(mapper, BTN_TOOL_FINGER, 0);
5920 processSync(mapper);
5921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5922 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005923 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005924}
5925
5926TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005927 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005928 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005929 prepareButtons();
5930 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005931 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005932 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005933
5934 NotifyMotionArgs motionArgs;
5935
5936 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5937 processKey(mapper, BTN_TOOL_FINGER, 1);
5938 processMove(mapper, 100, 200);
5939 processSync(mapper);
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5941 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5943 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5944
5945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5946 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5948 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5949
5950 // move a little
5951 processMove(mapper, 150, 250);
5952 processSync(mapper);
5953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5954 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5956 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5957
5958 // down when BTN_TOUCH is pressed, pressure defaults to 1
5959 processKey(mapper, BTN_TOUCH, 1);
5960 processSync(mapper);
5961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5962 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5963 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5964 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5965
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5967 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5969 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5970
5971 // up when BTN_TOUCH is released, hover restored
5972 processKey(mapper, BTN_TOUCH, 0);
5973 processSync(mapper);
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5975 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5977 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5978
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(150), toDisplayY(250), 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(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5988
5989 // exit hover when pointer goes away
5990 processKey(mapper, BTN_TOOL_FINGER, 0);
5991 processSync(mapper);
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5993 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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
5998TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005999 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006000 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006001 prepareButtons();
6002 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006003 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006004
6005 NotifyMotionArgs motionArgs;
6006
6007 // initially hovering because pressure is 0
6008 processDown(mapper, 100, 200);
6009 processPressure(mapper, 0);
6010 processSync(mapper);
6011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6012 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6014 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6015
6016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6017 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6019 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6020
6021 // move a little
6022 processMove(mapper, 150, 250);
6023 processSync(mapper);
6024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6025 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6027 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6028
6029 // down when pressure is non-zero
6030 processPressure(mapper, RAW_PRESSURE_MAX);
6031 processSync(mapper);
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6033 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6035 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6036
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6038 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6039 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6040 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6041
6042 // up when pressure becomes 0, hover restored
6043 processPressure(mapper, 0);
6044 processSync(mapper);
6045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6046 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6048 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6049
6050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6051 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6053 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6054
6055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6056 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6057 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6058 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6059
6060 // exit hover when pointer goes away
6061 processUp(mapper);
6062 processSync(mapper);
6063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6064 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6066 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6067}
6068
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006069TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6070 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006071 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006072 prepareButtons();
6073 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006074 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006075
6076 // Touch down.
6077 processDown(mapper, 100, 200);
6078 processPressure(mapper, 1);
6079 processSync(mapper);
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6081 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6082
6083 // Reset the mapper. This should cancel the ongoing gesture.
6084 resetMapper(mapper, ARBITRARY_TIME);
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6086 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6087
6088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6089}
6090
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006091TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6092 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006093 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006094 prepareButtons();
6095 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006096 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006097
6098 // Set the initial state for the touch pointer.
6099 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6100 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6101 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6102 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6103
6104 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006105 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6106 // does not generate any events.
6107 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006108
6109 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6110 // the recreated touch state to generate a down event.
6111 processSync(mapper);
6112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6113 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6114
6115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6116}
6117
lilinnan687e58f2022-07-19 16:00:50 +08006118TEST_F(SingleTouchInputMapperTest,
6119 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6120 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006121 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006122 prepareButtons();
6123 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006124 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006125 NotifyMotionArgs motionArgs;
6126
6127 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006128 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006129 processSync(mapper);
6130
6131 // We should receive a down event
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6133 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6134
6135 // Change display id
6136 clearViewports();
6137 prepareSecondaryDisplay(ViewportType::INTERNAL);
6138
6139 // We should receive a cancel event
6140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6141 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6142 // Then receive reset called
6143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6144}
6145
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006146TEST_F(SingleTouchInputMapperTest,
6147 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6148 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006149 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006150 prepareButtons();
6151 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006152 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6154 NotifyMotionArgs motionArgs;
6155
6156 // Start a new gesture.
6157 processDown(mapper, 100, 200);
6158 processSync(mapper);
6159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6160 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6161
6162 // Make the viewport inactive. This will put the device in disabled mode.
6163 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6164 viewport->isActive = false;
6165 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006166 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006167
6168 // We should receive a cancel event for the ongoing gesture.
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6170 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6171 // Then we should be notified that the device was reset.
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6173
6174 // No events are generated while the viewport is inactive.
6175 processMove(mapper, 101, 201);
6176 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006177 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006178 processSync(mapper);
6179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6180
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006181 // Start a new gesture while the viewport is still inactive.
6182 processDown(mapper, 300, 400);
6183 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6184 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6185 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6186 processSync(mapper);
6187
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006188 // Make the viewport active again. The device should resume processing events.
6189 viewport->isActive = true;
6190 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006191 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006192
6193 // The device is reset because it changes back to direct mode, without generating any events.
6194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6196
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006197 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006198 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6200 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006201
6202 // No more events.
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6205}
6206
Prabir Pradhan211ba622022-10-31 21:09:21 +00006207TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6208 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006209 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006210 prepareButtons();
6211 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006212 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6214
6215 // Press a stylus button.
6216 processKey(mapper, BTN_STYLUS, 1);
6217 processSync(mapper);
6218
6219 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6220 processDown(mapper, 100, 200);
6221 processSync(mapper);
6222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6223 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6224 WithCoords(toDisplayX(100), toDisplayY(200)),
6225 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6227 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6228 WithCoords(toDisplayX(100), toDisplayY(200)),
6229 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6230
6231 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6232 // the button has not actually been released, since there will be no pointers through which the
6233 // button state can be reported. The event is generated at the location of the pointer before
6234 // it went up.
6235 processUp(mapper);
6236 processSync(mapper);
6237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6238 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6239 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6241 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6242 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6243}
6244
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006245TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6246 addConfigurationProperty("touch.deviceType", "touchScreen");
6247 prepareDisplay(ui::ROTATION_0);
6248 prepareButtons();
6249 prepareAxes(POSITION);
6250
6251 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6252
Arpit Singha8c236b2023-04-25 13:56:05 +00006253 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6255
6256 // Press a stylus button.
6257 processKey(mapper, BTN_STYLUS, 1);
6258 processSync(mapper);
6259
6260 // Start a touch gesture and ensure that the stylus button is not reported.
6261 processDown(mapper, 100, 200);
6262 processSync(mapper);
6263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6264 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6265
6266 // Release and press the stylus button again.
6267 processKey(mapper, BTN_STYLUS, 0);
6268 processSync(mapper);
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6270 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6271 processKey(mapper, BTN_STYLUS, 1);
6272 processSync(mapper);
6273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6274 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6275
6276 // Release the touch gesture.
6277 processUp(mapper);
6278 processSync(mapper);
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6280 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6281
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6283}
6284
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006285TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6286 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6287 prepareDisplay(ui::ROTATION_0);
6288 prepareButtons();
6289 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006290 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6292
6293 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6294}
6295
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006296TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6297 // Initialize the device without setting device source to touch navigation.
6298 addConfigurationProperty("touch.deviceType", "touchScreen");
6299 prepareDisplay(ui::ROTATION_0);
6300 prepareButtons();
6301 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006302 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006303
6304 // Ensure that the device is created as a touchscreen, not touch navigation.
6305 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6306
6307 // Add device type association after the device was created.
6308 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6309
6310 // Send update to the mapper.
6311 std::list<NotifyArgs> unused2 =
6312 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006313 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006314
6315 // Check whether device type update was successful.
6316 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6317}
6318
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006319TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6320 // Initialize the device without setting device source to touch navigation.
6321 addConfigurationProperty("touch.deviceType", "touchScreen");
6322 prepareDisplay(ui::ROTATION_0);
6323 prepareButtons();
6324 prepareAxes(POSITION);
6325 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6326
6327 // Set a physical frame in the display viewport.
6328 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6329 viewport->physicalLeft = 0;
6330 viewport->physicalTop = 0;
6331 viewport->physicalRight = DISPLAY_WIDTH / 2;
6332 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6333 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006334 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006335
Arpit Singha8c236b2023-04-25 13:56:05 +00006336 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006337
6338 // Hovering inside the physical frame produces events.
6339 processKey(mapper, BTN_TOOL_PEN, 1);
6340 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6341 processSync(mapper);
6342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6343 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6345 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6346
6347 // Leaving the physical frame ends the hovering gesture.
6348 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6349 processSync(mapper);
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6351 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6352
6353 // Moving outside the physical frame does not produce events.
6354 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6355 processSync(mapper);
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6357
6358 // Re-entering the physical frame produces events.
6359 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6360 processSync(mapper);
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6362 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6364 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6365}
6366
Prabir Pradhan5632d622021-09-06 07:57:20 -07006367// --- TouchDisplayProjectionTest ---
6368
6369class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6370public:
6371 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6372 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6373 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006374 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6375 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6376 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006377 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006378 auto rotatedWidth = naturalDisplayWidth;
6379 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006380 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006381 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006382 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006383 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006384 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006385 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006386 inverseRotationFlags = ui::Transform::ROT_180;
6387 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006388 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006389 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006390 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006391 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006392 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006393 inverseRotationFlags = ui::Transform::ROT_0;
6394 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006395 }
6396
Prabir Pradhana9df3162022-12-05 23:57:27 +00006397 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006398 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6399
6400 std::optional<DisplayViewport> internalViewport =
6401 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6402 DisplayViewport& v = *internalViewport;
6403 v.displayId = DISPLAY_ID;
6404 v.orientation = orientation;
6405
6406 v.logicalLeft = 0;
6407 v.logicalTop = 0;
6408 v.logicalRight = 100;
6409 v.logicalBottom = 100;
6410
6411 v.physicalLeft = rotatedPhysicalDisplay.left;
6412 v.physicalTop = rotatedPhysicalDisplay.top;
6413 v.physicalRight = rotatedPhysicalDisplay.right;
6414 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6415
Prabir Pradhana9df3162022-12-05 23:57:27 +00006416 v.deviceWidth = rotatedWidth;
6417 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006418
6419 v.isActive = true;
6420 v.uniqueId = UNIQUE_ID;
6421 v.type = ViewportType::INTERNAL;
6422 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006423 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006424 }
6425
6426 void assertReceivedMove(const Point& point) {
6427 NotifyMotionArgs motionArgs;
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006430 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6432 1, 0, 0, 0, 0, 0, 0, 0));
6433 }
6434};
6435
6436TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6437 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006438 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006439
6440 prepareButtons();
6441 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006442 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006443
6444 NotifyMotionArgs motionArgs;
6445
6446 // Configure the DisplayViewport such that the logical display maps to a subsection of
6447 // the display panel called the physical display. Here, the physical display is bounded by the
6448 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6449 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6450 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6451 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6452
Michael Wrighta9cf4192022-12-01 23:46:39 +00006453 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006454 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6455
6456 // Touches outside the physical display should be ignored, and should not generate any
6457 // events. Ensure touches at the following points that lie outside of the physical display
6458 // area do not generate any events.
6459 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6460 processDown(mapper, toRawX(point.x), toRawY(point.y));
6461 processSync(mapper);
6462 processUp(mapper);
6463 processSync(mapper);
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6465 << "Unexpected event generated for touch outside physical display at point: "
6466 << point.x << ", " << point.y;
6467 }
6468 }
6469}
6470
6471TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6472 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006473 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006474
6475 prepareButtons();
6476 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006477 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006478
6479 NotifyMotionArgs motionArgs;
6480
6481 // Configure the DisplayViewport such that the logical display maps to a subsection of
6482 // the display panel called the physical display. Here, the physical display is bounded by the
6483 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6484 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6485
Michael Wrighta9cf4192022-12-01 23:46:39 +00006486 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006487 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6488
6489 // Touches that start outside the physical display should be ignored until it enters the
6490 // physical display bounds, at which point it should generate a down event. Start a touch at
6491 // the point (5, 100), which is outside the physical display bounds.
6492 static const Point kOutsidePoint{5, 100};
6493 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6494 processSync(mapper);
6495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6496
6497 // Move the touch into the physical display area. This should generate a pointer down.
6498 processMove(mapper, toRawX(11), toRawY(21));
6499 processSync(mapper);
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6501 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006502 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006503 ASSERT_NO_FATAL_FAILURE(
6504 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6505
6506 // Move the touch inside the physical display area. This should generate a pointer move.
6507 processMove(mapper, toRawX(69), toRawY(159));
6508 processSync(mapper);
6509 assertReceivedMove({69, 159});
6510
6511 // Move outside the physical display area. Since the pointer is already down, this should
6512 // now continue generating events.
6513 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6514 processSync(mapper);
6515 assertReceivedMove(kOutsidePoint);
6516
6517 // Release. This should generate a pointer up.
6518 processUp(mapper);
6519 processSync(mapper);
6520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6521 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6523 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6524
6525 // Ensure no more events were generated.
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6528 }
6529}
6530
Prabir Pradhana9df3162022-12-05 23:57:27 +00006531// --- TouchscreenPrecisionTests ---
6532
6533// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6534// in various orientations and with different display rotations. We configure the touchscreen to
6535// have a higher resolution than that of the display by an integer scale factor in each axis so that
6536// we can enforce that coordinates match precisely as expected.
6537class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6538 public ::testing::WithParamInterface<ui::Rotation> {
6539public:
6540 void SetUp() override {
6541 SingleTouchInputMapperTest::SetUp();
6542
6543 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6544 // four times the resolution of the display in the Y axis.
6545 prepareButtons();
6546 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006547 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6548 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006549 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006550 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6551 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006552 }
6553
6554 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6555 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6556 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6557 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6558
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006559 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6560 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6561
6562 static const int32_t PRECISION_RAW_X_FLAT = 16;
6563 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6564
6565 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6566 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6567
Prabir Pradhana9df3162022-12-05 23:57:27 +00006568 static const std::array<Point, 4> kRawCorners;
6569};
6570
6571const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6572 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6573 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6574 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6575 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6576}};
6577
6578// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6579// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6580// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6581TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6582 enum class Orientation {
6583 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6584 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6585 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6586 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6587 ftl_last = ORIENTATION_270,
6588 };
6589 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6590 Orientation::ORIENTATION_270;
6591 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6592 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6593 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6594 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6595 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6596 };
6597
6598 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6599
6600 // Configure the touchscreen as being installed in the one of the four different orientations
6601 // relative to the display.
6602 addConfigurationProperty("touch.deviceType", "touchScreen");
6603 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6604 prepareDisplay(ui::ROTATION_0);
6605
Arpit Singha8c236b2023-04-25 13:56:05 +00006606 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00006607
6608 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6609 // orientations of either 90 or 270) this means the display's natural resolution will be
6610 // flipped.
6611 const bool displayRotated =
6612 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6613 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6614 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6615 const Rect physicalFrame{0, 0, width, height};
6616 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6617
6618 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6619 const float expectedPrecisionX = displayRotated ? 4 : 2;
6620 const float expectedPrecisionY = displayRotated ? 2 : 4;
6621
6622 // Test all four corners.
6623 for (int i = 0; i < 4; i++) {
6624 const auto& raw = kRawCorners[i];
6625 processDown(mapper, raw.x, raw.y);
6626 processSync(mapper);
6627 const auto& expected = expectedPoints[i];
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6629 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6630 WithCoords(expected.x, expected.y),
6631 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6632 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6633 << "with touchscreen orientation "
6634 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6635 << expected.x << ", " << expected.y << ").";
6636 processUp(mapper);
6637 processSync(mapper);
6638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6639 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6640 WithCoords(expected.x, expected.y))));
6641 }
6642}
6643
Prabir Pradhan82687402022-12-06 01:32:53 +00006644TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6645 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6646 kMappedCorners = {
6647 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6648 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6649 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6650 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
6651 };
6652
6653 const ui::Rotation displayRotation = GetParam();
6654
6655 addConfigurationProperty("touch.deviceType", "touchScreen");
6656 prepareDisplay(displayRotation);
6657
Arpit Singha8c236b2023-04-25 13:56:05 +00006658 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00006659
6660 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6661
6662 // Test all four corners.
6663 for (int i = 0; i < 4; i++) {
6664 const auto& expected = expectedPoints[i];
6665 const auto& raw = kRawCorners[i];
6666 processDown(mapper, raw.x, raw.y);
6667 processSync(mapper);
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6669 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6670 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
6671 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6672 << "with display rotation " << ui::toCString(displayRotation)
6673 << ", expected point (" << expected.x << ", " << expected.y << ").";
6674 processUp(mapper);
6675 processSync(mapper);
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6677 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6678 WithCoords(expected.x, expected.y))));
6679 }
6680}
6681
Prabir Pradhan3e798762022-12-02 21:02:11 +00006682TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
6683 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
6684 kMappedCorners = {
6685 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6686 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
6687 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
6688 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
6689 };
6690
6691 const ui::Rotation displayRotation = GetParam();
6692
6693 addConfigurationProperty("touch.deviceType", "touchScreen");
6694 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6695
Arpit Singha8c236b2023-04-25 13:56:05 +00006696 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00006697
6698 // Ori 270, so width and height swapped
6699 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
6700 prepareDisplay(displayRotation);
6701 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
6702
6703 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6704
6705 // Test all four corners.
6706 for (int i = 0; i < 4; i++) {
6707 const auto& expected = expectedPoints[i];
6708 const auto& raw = kRawCorners[i];
6709 processDown(mapper, raw.x, raw.y);
6710 processSync(mapper);
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6712 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6713 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
6714 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6715 << "with display rotation " << ui::toCString(displayRotation)
6716 << ", expected point (" << expected.x << ", " << expected.y << ").";
6717 processUp(mapper);
6718 processSync(mapper);
6719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6720 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6721 WithCoords(expected.x, expected.y))));
6722 }
6723}
6724
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006725TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
6726 const ui::Rotation displayRotation = GetParam();
6727
6728 addConfigurationProperty("touch.deviceType", "touchScreen");
6729 prepareDisplay(displayRotation);
6730
6731 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00006732 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006733
6734 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
6735 // MotionRanges use display pixels as their units
6736 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
6737 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
6738
6739 // The MotionRanges should be oriented in the rotated display's coordinate space
6740 const bool displayRotated =
6741 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
6742
6743 constexpr float MAX_X = 479.5;
6744 constexpr float MAX_Y = 799.75;
6745 EXPECT_EQ(xRange->min, 0.f);
6746 EXPECT_EQ(yRange->min, 0.f);
6747 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
6748 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
6749
6750 EXPECT_EQ(xRange->flat, 8.f);
6751 EXPECT_EQ(yRange->flat, 8.f);
6752
6753 EXPECT_EQ(xRange->fuzz, 2.f);
6754 EXPECT_EQ(yRange->fuzz, 2.f);
6755
6756 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
6757 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
6758}
6759
Prabir Pradhana9df3162022-12-05 23:57:27 +00006760// Run the precision tests for all rotations.
6761INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
6762 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6763 ui::ROTATION_270),
6764 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6765 return ftl::enum_string(testParamInfo.param);
6766 });
6767
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006768// --- ExternalStylusFusionTest ---
6769
6770class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6771public:
6772 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6773 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006774 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006775 prepareButtons();
6776 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006777 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006778
6779 mStylusState.when = ARBITRARY_TIME;
6780 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006781 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006782 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006783 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006784 processExternalStylusState(mapper);
6785 return mapper;
6786 }
6787
6788 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6789 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6790 for (const NotifyArgs& args : generatedArgs) {
6791 mFakeListener->notify(args);
6792 }
6793 // Loop the reader to flush the input listener queue.
6794 mReader->loopOnce();
6795 return generatedArgs;
6796 }
6797
6798protected:
6799 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006800
6801 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6802 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006803 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006804
6805 // The first pointer is withheld.
6806 processDown(mapper, 100, 200);
6807 processSync(mapper);
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6809 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6810 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6811
6812 // The external stylus reports pressure. The withheld finger pointer is released as a
6813 // stylus.
6814 mStylusState.pressure = 1.f;
6815 processExternalStylusState(mapper);
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6817 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6818 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6819
6820 // Subsequent pointer events are not withheld.
6821 processMove(mapper, 101, 201);
6822 processSync(mapper);
6823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6824 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6825
6826 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6828 }
6829
6830 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6831 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6832
6833 // Releasing the touch pointer ends the gesture.
6834 processUp(mapper);
6835 processSync(mapper);
6836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006837 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006838 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006839
6840 mStylusState.pressure = 0.f;
6841 processExternalStylusState(mapper);
6842 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6844 }
6845
6846 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006847 // When stylus fusion is not successful, events should be reported with the original source.
6848 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006849 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006850 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006851
6852 // The first pointer is withheld when an external stylus is connected,
6853 // and a timeout is requested.
6854 processDown(mapper, 100, 200);
6855 processSync(mapper);
6856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6857 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6858 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6859
6860 // If the timeout expires early, it is requested again.
6861 handleTimeout(mapper, ARBITRARY_TIME + 1);
6862 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6863 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6864
6865 // When the timeout expires, the withheld touch is released as a finger pointer.
6866 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6868 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6869
6870 // Subsequent pointer events are not withheld.
6871 processMove(mapper, 101, 201);
6872 processSync(mapper);
6873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6874 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6875 processUp(mapper);
6876 processSync(mapper);
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6878 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6879
6880 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6882 }
6883
6884private:
6885 InputDeviceInfo mExternalStylusDeviceInfo{};
6886};
6887
6888TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6889 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006890 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006891}
6892
6893TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6894 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6895 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6896}
6897
6898TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6899 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6900 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6901}
6902
6903// Test a successful stylus fusion gesture where the pressure is reported by the external
6904// before the touch is reported by the touchscreen.
6905TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6906 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006907 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006908
6909 // The external stylus reports pressure first. It is ignored for now.
6910 mStylusState.pressure = 1.f;
6911 processExternalStylusState(mapper);
6912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6913 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6914
6915 // When the touch goes down afterwards, it is reported as a stylus pointer.
6916 processDown(mapper, 100, 200);
6917 processSync(mapper);
6918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6919 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6920 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6921
6922 processMove(mapper, 101, 201);
6923 processSync(mapper);
6924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6925 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6926 processUp(mapper);
6927 processSync(mapper);
6928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6929 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6930
6931 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6933}
6934
6935TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6936 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6937
6938 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6939 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6940
6941 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6942 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6943 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6944 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6945}
6946
6947TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6948 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006949 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006950
6951 mStylusState.pressure = 0.8f;
6952 processExternalStylusState(mapper);
6953 processDown(mapper, 100, 200);
6954 processSync(mapper);
6955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6956 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6957 WithPressure(0.8f))));
6958 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6959
6960 // The external stylus reports a pressure change. We wait for some time for a touch event.
6961 mStylusState.pressure = 0.6f;
6962 processExternalStylusState(mapper);
6963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6964 ASSERT_NO_FATAL_FAILURE(
6965 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6966
6967 // If a touch is reported within the timeout, it reports the updated pressure.
6968 processMove(mapper, 101, 201);
6969 processSync(mapper);
6970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6971 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6972 WithPressure(0.6f))));
6973 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6974
6975 // There is another pressure change.
6976 mStylusState.pressure = 0.5f;
6977 processExternalStylusState(mapper);
6978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6979 ASSERT_NO_FATAL_FAILURE(
6980 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6981
6982 // If a touch is not reported within the timeout, a move event is generated to report
6983 // the new pressure.
6984 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6986 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6987 WithPressure(0.5f))));
6988
6989 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6990 // repeated indefinitely.
6991 mStylusState.pressure = 0.0f;
6992 processExternalStylusState(mapper);
6993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6994 ASSERT_NO_FATAL_FAILURE(
6995 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6996 processMove(mapper, 102, 202);
6997 processSync(mapper);
6998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6999 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7000 WithPressure(0.5f))));
7001 processMove(mapper, 103, 203);
7002 processSync(mapper);
7003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7004 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7005 WithPressure(0.5f))));
7006
7007 processUp(mapper);
7008 processSync(mapper);
7009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007010 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007011 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007012
7013 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7015}
7016
7017TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7018 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007019 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007020
7021 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007022 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007023 processExternalStylusState(mapper);
7024 processDown(mapper, 100, 200);
7025 processSync(mapper);
7026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7027 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007028 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007029 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7030
7031 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007032 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007033 processExternalStylusState(mapper);
7034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7035 ASSERT_NO_FATAL_FAILURE(
7036 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7037
7038 // If a touch is reported within the timeout, it reports the updated pressure.
7039 processMove(mapper, 101, 201);
7040 processSync(mapper);
7041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7042 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007043 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007044 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7045
7046 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007047 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007048 processExternalStylusState(mapper);
7049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7050 ASSERT_NO_FATAL_FAILURE(
7051 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7052
7053 // If a touch is not reported within the timeout, a move event is generated to report
7054 // the new tool type.
7055 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7057 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007058 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007059
7060 processUp(mapper);
7061 processSync(mapper);
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7063 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007064 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007065
7066 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7068}
7069
7070TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7071 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007072 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007073
7074 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7075
7076 // The external stylus reports a button change. We wait for some time for a touch event.
7077 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7078 processExternalStylusState(mapper);
7079 ASSERT_NO_FATAL_FAILURE(
7080 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7081
7082 // If a touch is reported within the timeout, it reports the updated button state.
7083 processMove(mapper, 101, 201);
7084 processSync(mapper);
7085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7086 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7087 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7089 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7090 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7091 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7092
7093 // The button is now released.
7094 mStylusState.buttons = 0;
7095 processExternalStylusState(mapper);
7096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7097 ASSERT_NO_FATAL_FAILURE(
7098 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7099
7100 // If a touch is not reported within the timeout, a move event is generated to report
7101 // the new button state.
7102 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7104 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7105 WithButtonState(0))));
7106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007107 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7108 WithButtonState(0))));
7109
7110 processUp(mapper);
7111 processSync(mapper);
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007113 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7114
7115 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7117}
7118
Michael Wrightd02c5b62014-02-10 15:10:22 -08007119// --- MultiTouchInputMapperTest ---
7120
7121class MultiTouchInputMapperTest : public TouchInputMapperTest {
7122protected:
7123 void prepareAxes(int axes);
7124
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007125 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7126 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7127 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7128 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7129 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7130 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7131 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7132 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7133 void processId(MultiTouchInputMapper& mapper, int32_t id);
7134 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7135 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7136 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007137 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007138 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007139 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7140 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007141};
7142
7143void MultiTouchInputMapperTest::prepareAxes(int axes) {
7144 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007145 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7146 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147 }
7148 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007149 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7150 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007151 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007152 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7153 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007154 }
7155 }
7156 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007157 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7158 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007159 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007160 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007161 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007162 }
7163 }
7164 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007165 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7166 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007167 }
7168 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007169 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7170 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007171 }
7172 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007173 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7174 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007175 }
7176 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007177 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7178 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007179 }
7180 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007181 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7182 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007183 }
7184 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007185 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007186 }
7187}
7188
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007189void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7190 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007193}
7194
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007195void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7196 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007198}
7199
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007200void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7201 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007203}
7204
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007205void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007207}
7208
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007209void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007211}
7212
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007213void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7214 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007216}
7217
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007218void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007220}
7221
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007222void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007224}
7225
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007226void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007228}
7229
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007230void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007232}
7233
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007234void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007236}
7237
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007238void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7239 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007241}
7242
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007243void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7244 int32_t value) {
7245 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7247}
7248
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007249void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007251}
7252
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007253void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7254 nsecs_t readTime) {
7255 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007256}
7257
Michael Wrightd02c5b62014-02-10 15:10:22 -08007258TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007259 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007260 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007261 prepareAxes(POSITION);
7262 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007263 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007264
arthurhungdcef2dc2020-08-11 14:47:50 +08007265 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007266
7267 NotifyMotionArgs motionArgs;
7268
7269 // Two fingers down at once.
7270 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7271 processPosition(mapper, x1, y1);
7272 processMTSync(mapper);
7273 processPosition(mapper, x2, y2);
7274 processMTSync(mapper);
7275 processSync(mapper);
7276
7277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7278 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7279 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7280 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7281 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7282 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7283 ASSERT_EQ(0, motionArgs.flags);
7284 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7285 ASSERT_EQ(0, motionArgs.buttonState);
7286 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007287 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007288 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007289 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7291 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7292 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7293 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7294 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7295
7296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7297 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7298 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7299 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7300 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007301 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007302 ASSERT_EQ(0, motionArgs.flags);
7303 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7304 ASSERT_EQ(0, motionArgs.buttonState);
7305 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007306 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007307 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007308 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007309 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007310 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007311 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7312 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7314 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7315 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7316 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7317 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7318
7319 // Move.
7320 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7321 processPosition(mapper, x1, y1);
7322 processMTSync(mapper);
7323 processPosition(mapper, x2, y2);
7324 processMTSync(mapper);
7325 processSync(mapper);
7326
7327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7328 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7329 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7330 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7331 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7333 ASSERT_EQ(0, motionArgs.flags);
7334 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7335 ASSERT_EQ(0, motionArgs.buttonState);
7336 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007337 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007338 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007339 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007340 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007341 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007342 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7343 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7345 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7346 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7347 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7348 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7349
7350 // First finger up.
7351 x2 += 15; y2 -= 20;
7352 processPosition(mapper, x2, y2);
7353 processMTSync(mapper);
7354 processSync(mapper);
7355
7356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7357 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7358 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7359 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7360 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007361 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007362 ASSERT_EQ(0, motionArgs.flags);
7363 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7364 ASSERT_EQ(0, motionArgs.buttonState);
7365 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007366 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007367 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007368 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007369 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007370 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7372 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7374 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7375 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7376 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7377 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7378
7379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7380 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7381 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7382 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7383 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7385 ASSERT_EQ(0, motionArgs.flags);
7386 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7387 ASSERT_EQ(0, motionArgs.buttonState);
7388 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007389 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007390 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007391 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7393 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7394 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7395 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7396 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7397
7398 // Move.
7399 x2 += 20; y2 -= 25;
7400 processPosition(mapper, x2, y2);
7401 processMTSync(mapper);
7402 processSync(mapper);
7403
7404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7405 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7406 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7407 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7408 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7410 ASSERT_EQ(0, motionArgs.flags);
7411 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7412 ASSERT_EQ(0, motionArgs.buttonState);
7413 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007414 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007415 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007416 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7418 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7419 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7420 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7421 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7422
7423 // New finger down.
7424 int32_t x3 = 700, y3 = 300;
7425 processPosition(mapper, x2, y2);
7426 processMTSync(mapper);
7427 processPosition(mapper, x3, y3);
7428 processMTSync(mapper);
7429 processSync(mapper);
7430
7431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7432 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7433 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7434 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7435 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007436 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007437 ASSERT_EQ(0, motionArgs.flags);
7438 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7439 ASSERT_EQ(0, motionArgs.buttonState);
7440 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007441 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007442 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007443 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007444 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007445 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7447 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7449 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7450 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7451 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7452 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7453
7454 // Second finger up.
7455 x3 += 30; y3 -= 20;
7456 processPosition(mapper, x3, y3);
7457 processMTSync(mapper);
7458 processSync(mapper);
7459
7460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7461 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7462 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7463 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7464 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007465 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007466 ASSERT_EQ(0, motionArgs.flags);
7467 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7468 ASSERT_EQ(0, motionArgs.buttonState);
7469 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007470 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007471 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007472 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007473 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007474 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7476 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7478 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7479 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7480 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7481 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7482
7483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7484 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7485 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7486 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7487 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7489 ASSERT_EQ(0, motionArgs.flags);
7490 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7491 ASSERT_EQ(0, motionArgs.buttonState);
7492 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007493 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007494 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007495 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7497 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7498 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7499 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7500 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7501
7502 // Last finger up.
7503 processMTSync(mapper);
7504 processSync(mapper);
7505
7506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7507 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7508 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7509 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7510 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7511 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7512 ASSERT_EQ(0, motionArgs.flags);
7513 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7514 ASSERT_EQ(0, motionArgs.buttonState);
7515 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007516 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007517 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007518 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7520 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7521 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7522 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7523 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7524
7525 // Should not have sent any more keys or motions.
7526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7528}
7529
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007530TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7531 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007532 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007533
7534 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7535 /*fuzz*/ 0, /*resolution*/ 10);
7536 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7537 /*fuzz*/ 0, /*resolution*/ 11);
7538 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7539 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7540 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7541 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7542 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7543 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7544 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7545 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7546
Arpit Singha8c236b2023-04-25 13:56:05 +00007547 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007548
7549 // X and Y axes
7550 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7551 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7552 // Touch major and minor
7553 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7554 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7555 // Tool major and minor
7556 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7557 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7558}
7559
7560TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7561 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007562 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007563
7564 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7565 /*fuzz*/ 0, /*resolution*/ 10);
7566 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7567 /*fuzz*/ 0, /*resolution*/ 11);
7568
7569 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7570
Arpit Singha8c236b2023-04-25 13:56:05 +00007571 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007572
7573 // Touch major and minor
7574 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7575 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7576 // Tool major and minor
7577 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7578 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7579}
7580
Michael Wrightd02c5b62014-02-10 15:10:22 -08007581TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007582 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007583 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007584 prepareAxes(POSITION | ID);
7585 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007586 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007587
arthurhungdcef2dc2020-08-11 14:47:50 +08007588 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007589
7590 NotifyMotionArgs motionArgs;
7591
7592 // Two fingers down at once.
7593 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7594 processPosition(mapper, x1, y1);
7595 processId(mapper, 1);
7596 processMTSync(mapper);
7597 processPosition(mapper, x2, y2);
7598 processId(mapper, 2);
7599 processMTSync(mapper);
7600 processSync(mapper);
7601
7602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7603 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007604 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007605 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007606 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].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
7610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007611 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007612 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007613 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007614 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007615 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007616 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7618 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7620 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7621
7622 // Move.
7623 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7624 processPosition(mapper, x1, y1);
7625 processId(mapper, 1);
7626 processMTSync(mapper);
7627 processPosition(mapper, x2, y2);
7628 processId(mapper, 2);
7629 processMTSync(mapper);
7630 processSync(mapper);
7631
7632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007634 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007635 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007636 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007637 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007638 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7640 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7642 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7643
7644 // First finger up.
7645 x2 += 15; y2 -= 20;
7646 processPosition(mapper, x2, y2);
7647 processId(mapper, 2);
7648 processMTSync(mapper);
7649 processSync(mapper);
7650
7651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007652 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007653 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007654 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007655 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007656 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007657 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7659 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7661 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7662
7663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007665 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007666 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007667 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7669 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7670
7671 // Move.
7672 x2 += 20; y2 -= 25;
7673 processPosition(mapper, x2, y2);
7674 processId(mapper, 2);
7675 processMTSync(mapper);
7676 processSync(mapper);
7677
7678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007680 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007681 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007682 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7684 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7685
7686 // New finger down.
7687 int32_t x3 = 700, y3 = 300;
7688 processPosition(mapper, x2, y2);
7689 processId(mapper, 2);
7690 processMTSync(mapper);
7691 processPosition(mapper, x3, y3);
7692 processId(mapper, 3);
7693 processMTSync(mapper);
7694 processSync(mapper);
7695
7696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007697 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007698 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007699 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007700 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007701 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007702 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7704 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7706 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7707
7708 // Second finger up.
7709 x3 += 30; y3 -= 20;
7710 processPosition(mapper, x3, y3);
7711 processId(mapper, 3);
7712 processMTSync(mapper);
7713 processSync(mapper);
7714
7715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007716 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007717 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007718 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007719 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007720 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007721 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7723 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7725 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7726
7727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007729 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007730 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007731 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7733 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7734
7735 // Last finger up.
7736 processMTSync(mapper);
7737 processSync(mapper);
7738
7739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7740 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007741 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007742 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007743 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7745 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7746
7747 // Should not have sent any more keys or motions.
7748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7750}
7751
7752TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007753 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007754 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007755 prepareAxes(POSITION | ID | SLOT);
7756 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007757 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007758
arthurhungdcef2dc2020-08-11 14:47:50 +08007759 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007760
7761 NotifyMotionArgs motionArgs;
7762
7763 // Two fingers down at once.
7764 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7765 processPosition(mapper, x1, y1);
7766 processId(mapper, 1);
7767 processSlot(mapper, 1);
7768 processPosition(mapper, x2, y2);
7769 processId(mapper, 2);
7770 processSync(mapper);
7771
7772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7773 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007774 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007775 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007776 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7778 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7779
7780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007781 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007782 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007783 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007784 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007785 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007786 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7788 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7790 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7791
7792 // Move.
7793 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7794 processSlot(mapper, 0);
7795 processPosition(mapper, x1, y1);
7796 processSlot(mapper, 1);
7797 processPosition(mapper, x2, y2);
7798 processSync(mapper);
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(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007803 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007804 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007805 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007806 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7808 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7810 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7811
7812 // First finger up.
7813 x2 += 15; y2 -= 20;
7814 processSlot(mapper, 0);
7815 processId(mapper, -1);
7816 processSlot(mapper, 1);
7817 processPosition(mapper, x2, y2);
7818 processSync(mapper);
7819
7820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007821 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007822 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007823 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007824 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007825 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007826 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7828 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7830 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7831
7832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007834 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007835 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007836 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7838 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7839
7840 // Move.
7841 x2 += 20; y2 -= 25;
7842 processPosition(mapper, x2, y2);
7843 processSync(mapper);
7844
7845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7846 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007847 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007848 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007849 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7851 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7852
7853 // New finger down.
7854 int32_t x3 = 700, y3 = 300;
7855 processPosition(mapper, x2, y2);
7856 processSlot(mapper, 0);
7857 processId(mapper, 3);
7858 processPosition(mapper, x3, y3);
7859 processSync(mapper);
7860
7861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007862 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007863 ASSERT_EQ(size_t(2), 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_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007867 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7869 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7871 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7872
7873 // Second finger up.
7874 x3 += 30; y3 -= 20;
7875 processSlot(mapper, 1);
7876 processId(mapper, -1);
7877 processSlot(mapper, 0);
7878 processPosition(mapper, x3, y3);
7879 processSync(mapper);
7880
7881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007882 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007883 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007884 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007885 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007886 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007887 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7889 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7891 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7892
7893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7894 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007895 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007896 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007897 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7899 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7900
7901 // Last finger up.
7902 processId(mapper, -1);
7903 processSync(mapper);
7904
7905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7906 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007907 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007908 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007909 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7911 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7912
7913 // Should not have sent any more keys or motions.
7914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7916}
7917
7918TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007919 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007920 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007921 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007922 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007923
7924 // These calculations are based on the input device calibration documentation.
7925 int32_t rawX = 100;
7926 int32_t rawY = 200;
7927 int32_t rawTouchMajor = 7;
7928 int32_t rawTouchMinor = 6;
7929 int32_t rawToolMajor = 9;
7930 int32_t rawToolMinor = 8;
7931 int32_t rawPressure = 11;
7932 int32_t rawDistance = 0;
7933 int32_t rawOrientation = 3;
7934 int32_t id = 5;
7935
7936 float x = toDisplayX(rawX);
7937 float y = toDisplayY(rawY);
7938 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7939 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7940 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7941 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7942 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7943 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7944 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7945 float distance = float(rawDistance);
7946
7947 processPosition(mapper, rawX, rawY);
7948 processTouchMajor(mapper, rawTouchMajor);
7949 processTouchMinor(mapper, rawTouchMinor);
7950 processToolMajor(mapper, rawToolMajor);
7951 processToolMinor(mapper, rawToolMinor);
7952 processPressure(mapper, rawPressure);
7953 processOrientation(mapper, rawOrientation);
7954 processDistance(mapper, rawDistance);
7955 processId(mapper, id);
7956 processMTSync(mapper);
7957 processSync(mapper);
7958
7959 NotifyMotionArgs args;
7960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7961 ASSERT_EQ(0, args.pointerProperties[0].id);
7962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7963 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7964 orientation, distance));
Prabir Pradhan9a53b552024-06-04 02:59:40 +00007965 ASSERT_EQ(args.flags, AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_ORIENTATION);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007966}
7967
7968TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007969 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007970 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007971 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7972 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00007973 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007974
7975 // These calculations are based on the input device calibration documentation.
7976 int32_t rawX = 100;
7977 int32_t rawY = 200;
7978 int32_t rawTouchMajor = 140;
7979 int32_t rawTouchMinor = 120;
7980 int32_t rawToolMajor = 180;
7981 int32_t rawToolMinor = 160;
7982
7983 float x = toDisplayX(rawX);
7984 float y = toDisplayY(rawY);
7985 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7986 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7987 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7988 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7989 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7990
7991 processPosition(mapper, rawX, rawY);
7992 processTouchMajor(mapper, rawTouchMajor);
7993 processTouchMinor(mapper, rawTouchMinor);
7994 processToolMajor(mapper, rawToolMajor);
7995 processToolMinor(mapper, rawToolMinor);
7996 processMTSync(mapper);
7997 processSync(mapper);
7998
7999 NotifyMotionArgs args;
8000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8002 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8003}
8004
8005TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008006 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008007 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008008 prepareAxes(POSITION | TOUCH | TOOL);
8009 addConfigurationProperty("touch.size.calibration", "diameter");
8010 addConfigurationProperty("touch.size.scale", "10");
8011 addConfigurationProperty("touch.size.bias", "160");
8012 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00008013 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008014
8015 // These calculations are based on the input device calibration documentation.
8016 // Note: We only provide a single common touch/tool value because the device is assumed
8017 // not to emit separate values for each pointer (isSummed = 1).
8018 int32_t rawX = 100;
8019 int32_t rawY = 200;
8020 int32_t rawX2 = 150;
8021 int32_t rawY2 = 250;
8022 int32_t rawTouchMajor = 5;
8023 int32_t rawToolMajor = 8;
8024
8025 float x = toDisplayX(rawX);
8026 float y = toDisplayY(rawY);
8027 float x2 = toDisplayX(rawX2);
8028 float y2 = toDisplayY(rawY2);
8029 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8030 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8031 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8032
8033 processPosition(mapper, rawX, rawY);
8034 processTouchMajor(mapper, rawTouchMajor);
8035 processToolMajor(mapper, rawToolMajor);
8036 processMTSync(mapper);
8037 processPosition(mapper, rawX2, rawY2);
8038 processTouchMajor(mapper, rawTouchMajor);
8039 processToolMajor(mapper, rawToolMajor);
8040 processMTSync(mapper);
8041 processSync(mapper);
8042
8043 NotifyMotionArgs args;
8044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8045 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8046
8047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008048 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008049 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8051 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8053 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8054}
8055
8056TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008058 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008059 prepareAxes(POSITION | TOUCH | TOOL);
8060 addConfigurationProperty("touch.size.calibration", "area");
8061 addConfigurationProperty("touch.size.scale", "43");
8062 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008063 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008064
8065 // These calculations are based on the input device calibration documentation.
8066 int32_t rawX = 100;
8067 int32_t rawY = 200;
8068 int32_t rawTouchMajor = 5;
8069 int32_t rawToolMajor = 8;
8070
8071 float x = toDisplayX(rawX);
8072 float y = toDisplayY(rawY);
8073 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8074 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8075 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8076
8077 processPosition(mapper, rawX, rawY);
8078 processTouchMajor(mapper, rawTouchMajor);
8079 processToolMajor(mapper, rawToolMajor);
8080 processMTSync(mapper);
8081 processSync(mapper);
8082
8083 NotifyMotionArgs args;
8084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8086 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8087}
8088
8089TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008091 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092 prepareAxes(POSITION | PRESSURE);
8093 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8094 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008095 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008096
Michael Wrightaa449c92017-12-13 21:21:43 +00008097 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008098 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008099 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8100 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8101 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8102
Michael Wrightd02c5b62014-02-10 15:10:22 -08008103 // These calculations are based on the input device calibration documentation.
8104 int32_t rawX = 100;
8105 int32_t rawY = 200;
8106 int32_t rawPressure = 60;
8107
8108 float x = toDisplayX(rawX);
8109 float y = toDisplayY(rawY);
8110 float pressure = float(rawPressure) * 0.01f;
8111
8112 processPosition(mapper, rawX, rawY);
8113 processPressure(mapper, rawPressure);
8114 processMTSync(mapper);
8115 processSync(mapper);
8116
8117 NotifyMotionArgs args;
8118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8119 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8120 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8121}
8122
8123TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008124 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008125 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008126 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008127 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008128
8129 NotifyMotionArgs motionArgs;
8130 NotifyKeyArgs keyArgs;
8131
8132 processId(mapper, 1);
8133 processPosition(mapper, 100, 200);
8134 processSync(mapper);
8135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8136 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8137 ASSERT_EQ(0, motionArgs.buttonState);
8138
8139 // press BTN_LEFT, release BTN_LEFT
8140 processKey(mapper, BTN_LEFT, 1);
8141 processSync(mapper);
8142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8144 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8145
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8147 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8148 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8149
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150 processKey(mapper, BTN_LEFT, 0);
8151 processSync(mapper);
8152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008153 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008154 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008155
8156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008158 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008159
8160 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8161 processKey(mapper, BTN_RIGHT, 1);
8162 processKey(mapper, BTN_MIDDLE, 1);
8163 processSync(mapper);
8164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8166 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8167 motionArgs.buttonState);
8168
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8170 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8171 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8172
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8174 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8175 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8176 motionArgs.buttonState);
8177
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178 processKey(mapper, BTN_RIGHT, 0);
8179 processSync(mapper);
8180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008181 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008182 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008183
8184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008186 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187
8188 processKey(mapper, BTN_MIDDLE, 0);
8189 processSync(mapper);
8190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008191 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008193
8194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008196 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008197
8198 // press BTN_BACK, release BTN_BACK
8199 processKey(mapper, BTN_BACK, 1);
8200 processSync(mapper);
8201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8202 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8203 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008204
Michael Wrightd02c5b62014-02-10 15:10:22 -08008205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008207 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8208
8209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8210 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8211 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008212
8213 processKey(mapper, BTN_BACK, 0);
8214 processSync(mapper);
8215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008216 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008217 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008218
8219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008221 ASSERT_EQ(0, motionArgs.buttonState);
8222
Michael Wrightd02c5b62014-02-10 15:10:22 -08008223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8224 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8225 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8226
8227 // press BTN_SIDE, release BTN_SIDE
8228 processKey(mapper, BTN_SIDE, 1);
8229 processSync(mapper);
8230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8231 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8232 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008233
Michael Wrightd02c5b62014-02-10 15:10:22 -08008234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008236 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8237
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8239 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8240 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008241
8242 processKey(mapper, BTN_SIDE, 0);
8243 processSync(mapper);
8244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008245 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008246 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008247
8248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008250 ASSERT_EQ(0, motionArgs.buttonState);
8251
Michael Wrightd02c5b62014-02-10 15:10:22 -08008252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8253 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8254 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8255
8256 // press BTN_FORWARD, release BTN_FORWARD
8257 processKey(mapper, BTN_FORWARD, 1);
8258 processSync(mapper);
8259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8260 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8261 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008262
Michael Wrightd02c5b62014-02-10 15:10:22 -08008263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008265 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8266
8267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8268 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8269 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008270
8271 processKey(mapper, BTN_FORWARD, 0);
8272 processSync(mapper);
8273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008274 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008275 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008276
8277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008279 ASSERT_EQ(0, motionArgs.buttonState);
8280
Michael Wrightd02c5b62014-02-10 15:10:22 -08008281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8282 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8283 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8284
8285 // press BTN_EXTRA, release BTN_EXTRA
8286 processKey(mapper, BTN_EXTRA, 1);
8287 processSync(mapper);
8288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8289 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8290 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008291
Michael Wrightd02c5b62014-02-10 15:10:22 -08008292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008294 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8295
8296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8298 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008299
8300 processKey(mapper, BTN_EXTRA, 0);
8301 processSync(mapper);
8302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008303 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008304 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008305
8306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008308 ASSERT_EQ(0, motionArgs.buttonState);
8309
Michael Wrightd02c5b62014-02-10 15:10:22 -08008310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8311 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8312 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8313
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8315
Michael Wrightd02c5b62014-02-10 15:10:22 -08008316 // press BTN_STYLUS, release BTN_STYLUS
8317 processKey(mapper, BTN_STYLUS, 1);
8318 processSync(mapper);
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008321 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8322
8323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8324 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8325 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008326
8327 processKey(mapper, BTN_STYLUS, 0);
8328 processSync(mapper);
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008330 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008331 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008332
8333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008335 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008336
8337 // press BTN_STYLUS2, release BTN_STYLUS2
8338 processKey(mapper, BTN_STYLUS2, 1);
8339 processSync(mapper);
8340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008342 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8343
8344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8345 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8346 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008347
8348 processKey(mapper, BTN_STYLUS2, 0);
8349 processSync(mapper);
8350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008351 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008352 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008353
8354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008356 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008357
8358 // release touch
8359 processId(mapper, -1);
8360 processSync(mapper);
8361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8362 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8363 ASSERT_EQ(0, motionArgs.buttonState);
8364}
8365
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008366TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8367 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008368 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008369 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008370 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008371
8372 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8373 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8374
8375 // Touch down.
8376 processId(mapper, 1);
8377 processPosition(mapper, 100, 200);
8378 processSync(mapper);
8379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8380 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8381
8382 // Press and release button mapped to the primary stylus button.
8383 processKey(mapper, BTN_A, 1);
8384 processSync(mapper);
8385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8386 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8387 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8389 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8390 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8391
8392 processKey(mapper, BTN_A, 0);
8393 processSync(mapper);
8394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8395 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8397 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8398
8399 // Press and release the HID usage mapped to the secondary stylus button.
8400 processHidUsage(mapper, 0xabcd, 1);
8401 processSync(mapper);
8402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8403 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8404 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8406 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8407 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8408
8409 processHidUsage(mapper, 0xabcd, 0);
8410 processSync(mapper);
8411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8412 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8414 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8415
8416 // Release touch.
8417 processId(mapper, -1);
8418 processSync(mapper);
8419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8420 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8421}
8422
Michael Wrightd02c5b62014-02-10 15:10:22 -08008423TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008424 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008425 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008426 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008427 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008428
8429 NotifyMotionArgs motionArgs;
8430
8431 // default tool type is finger
8432 processId(mapper, 1);
8433 processPosition(mapper, 100, 200);
8434 processSync(mapper);
8435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8436 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008437 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008438
8439 // eraser
8440 processKey(mapper, BTN_TOOL_RUBBER, 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::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008445
8446 // stylus
8447 processKey(mapper, BTN_TOOL_RUBBER, 0);
8448 processKey(mapper, BTN_TOOL_PEN, 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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008453
8454 // brush
8455 processKey(mapper, BTN_TOOL_PEN, 0);
8456 processKey(mapper, BTN_TOOL_BRUSH, 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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008461
8462 // pencil
8463 processKey(mapper, BTN_TOOL_BRUSH, 0);
8464 processKey(mapper, BTN_TOOL_PENCIL, 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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008469
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008470 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008471 processKey(mapper, BTN_TOOL_PENCIL, 0);
8472 processKey(mapper, BTN_TOOL_AIRBRUSH, 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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008477
8478 // mouse
8479 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8480 processKey(mapper, BTN_TOOL_MOUSE, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008485
8486 // lens
8487 processKey(mapper, BTN_TOOL_MOUSE, 0);
8488 processKey(mapper, BTN_TOOL_LENS, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008493
8494 // double-tap
8495 processKey(mapper, BTN_TOOL_LENS, 0);
8496 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8497 processSync(mapper);
8498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008500 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008501
8502 // triple-tap
8503 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8504 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8505 processSync(mapper);
8506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008508 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008509
8510 // quad-tap
8511 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8512 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8513 processSync(mapper);
8514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008516 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008517
8518 // finger
8519 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8520 processKey(mapper, BTN_TOOL_FINGER, 1);
8521 processSync(mapper);
8522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8523 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008524 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008525
8526 // stylus trumps finger
8527 processKey(mapper, BTN_TOOL_PEN, 1);
8528 processSync(mapper);
8529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008531 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008532
8533 // eraser trumps stylus
8534 processKey(mapper, BTN_TOOL_RUBBER, 1);
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::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008539
8540 // mouse trumps eraser
8541 processKey(mapper, BTN_TOOL_MOUSE, 1);
8542 processSync(mapper);
8543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008545 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008546
8547 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8548 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8549 processSync(mapper);
8550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008552 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008553
8554 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8555 processToolType(mapper, MT_TOOL_PEN);
8556 processSync(mapper);
8557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008559 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008560
8561 // back to default tool type
8562 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8563 processKey(mapper, BTN_TOOL_MOUSE, 0);
8564 processKey(mapper, BTN_TOOL_RUBBER, 0);
8565 processKey(mapper, BTN_TOOL_PEN, 0);
8566 processKey(mapper, BTN_TOOL_FINGER, 0);
8567 processSync(mapper);
8568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008570 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008571}
8572
8573TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008574 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008575 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008576 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008577 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008578 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008579
8580 NotifyMotionArgs motionArgs;
8581
8582 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8583 processId(mapper, 1);
8584 processPosition(mapper, 100, 200);
8585 processSync(mapper);
8586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8587 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8589 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8590
8591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8592 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8594 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8595
8596 // move a little
8597 processPosition(mapper, 150, 250);
8598 processSync(mapper);
8599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8600 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8602 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8603
8604 // down when BTN_TOUCH is pressed, pressure defaults to 1
8605 processKey(mapper, BTN_TOUCH, 1);
8606 processSync(mapper);
8607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8608 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8610 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8611
8612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8613 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8615 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8616
8617 // up when BTN_TOUCH is released, hover restored
8618 processKey(mapper, BTN_TOUCH, 0);
8619 processSync(mapper);
8620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8621 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8623 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8624
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(150), toDisplayY(250), 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(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8634
8635 // exit hover when pointer goes away
8636 processId(mapper, -1);
8637 processSync(mapper);
8638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8639 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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
8644TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008645 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008646 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008647 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008648 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008649
8650 NotifyMotionArgs motionArgs;
8651
8652 // initially hovering because pressure is 0
8653 processId(mapper, 1);
8654 processPosition(mapper, 100, 200);
8655 processPressure(mapper, 0);
8656 processSync(mapper);
8657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8658 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8660 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8661
8662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8663 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8665 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8666
8667 // move a little
8668 processPosition(mapper, 150, 250);
8669 processSync(mapper);
8670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8671 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8673 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8674
8675 // down when pressure becomes non-zero
8676 processPressure(mapper, RAW_PRESSURE_MAX);
8677 processSync(mapper);
8678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8679 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8681 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8682
8683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8684 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8686 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8687
8688 // up when pressure becomes 0, hover restored
8689 processPressure(mapper, 0);
8690 processSync(mapper);
8691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8692 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8694 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8695
8696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8697 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8699 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8700
8701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8702 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8704 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8705
8706 // exit hover when pointer goes away
8707 processId(mapper, -1);
8708 processSync(mapper);
8709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8710 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8712 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8713}
8714
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008715/**
8716 * Set the input device port <--> display port associations, and check that the
8717 * events are routed to the display that matches the display port.
8718 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8719 */
8720TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008721 const std::string usb2 = "USB2";
8722 const uint8_t hdmi1 = 0;
8723 const uint8_t hdmi2 = 1;
8724 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008725 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008726
8727 addConfigurationProperty("touch.deviceType", "touchScreen");
8728 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008729 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008730
8731 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8732 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8733
8734 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8735 // for this input device is specified, and the matching viewport is not present,
8736 // the input device should be disabled (at the mapper level).
8737
8738 // Add viewport for display 2 on hdmi2
8739 prepareSecondaryDisplay(type, hdmi2);
8740 // Send a touch event
8741 processPosition(mapper, 100, 100);
8742 processSync(mapper);
8743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8744
8745 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008746 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008747 // Send a touch event again
8748 processPosition(mapper, 100, 100);
8749 processSync(mapper);
8750
8751 NotifyMotionArgs args;
8752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8753 ASSERT_EQ(DISPLAY_ID, args.displayId);
8754}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008755
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008756TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8757 addConfigurationProperty("touch.deviceType", "touchScreen");
8758 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008759 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008760
8761 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8762
Michael Wrighta9cf4192022-12-01 23:46:39 +00008763 prepareDisplay(ui::ROTATION_0);
8764 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008765
8766 // Send a touch event
8767 processPosition(mapper, 100, 100);
8768 processSync(mapper);
8769
8770 NotifyMotionArgs args;
8771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8772 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8773}
8774
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008775TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008776 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008777
Michael Wrighta9cf4192022-12-01 23:46:39 +00008778 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008779 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008780 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008781
Josep del Río2d8c79a2023-01-23 19:33:50 +00008782 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008783
8784 NotifyMotionArgs motionArgs;
8785 processPosition(mapper, 100, 100);
8786 processSync(mapper);
8787
8788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8789 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008790 ASSERT_EQ(ui::LogicalDisplayId::INVALID, motionArgs.displayId);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008791}
8792
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008793/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008794 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8795 */
8796TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8797 addConfigurationProperty("touch.deviceType", "touchScreen");
8798 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008799 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008800
Michael Wrighta9cf4192022-12-01 23:46:39 +00008801 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00008802 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
8803 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
8804 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
8805 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008806
8807 NotifyMotionArgs args;
8808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8809 ASSERT_EQ(26, args.readTime);
8810
Harry Cutts33476232023-01-30 19:57:29 +00008811 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
8812 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
8813 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008814
8815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8816 ASSERT_EQ(33, args.readTime);
8817}
8818
8819/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008820 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8821 * events should not be delivered to the listener.
8822 */
8823TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8824 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008825 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008826 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008827 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008828 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008829 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008830 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008831
8832 NotifyMotionArgs motionArgs;
8833 processPosition(mapper, 100, 100);
8834 processSync(mapper);
8835
8836 mFakeListener->assertNotifyMotionWasNotCalled();
8837}
8838
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008839/**
8840 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8841 * the touch mapper can process the events and the events can be delivered to the listener.
8842 */
8843TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8844 addConfigurationProperty("touch.deviceType", "touchScreen");
8845 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008846 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008847 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008848 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008849 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008850 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008851
8852 NotifyMotionArgs motionArgs;
8853 processPosition(mapper, 100, 100);
8854 processSync(mapper);
8855
8856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8857 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8858}
8859
Josh Thielene986aed2023-06-01 14:17:30 +00008860/**
8861 * When the viewport is deactivated (isActive transitions from true to false),
8862 * and touch.enableForInactiveViewport is false, touches prior to the transition
8863 * should be cancelled.
8864 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08008865TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8866 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008867 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008868 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008869 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008870 std::optional<DisplayViewport> optionalDisplayViewport =
8871 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8872 ASSERT_TRUE(optionalDisplayViewport.has_value());
8873 DisplayViewport displayViewport = *optionalDisplayViewport;
8874
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008875 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008876 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008877 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08008878
8879 // Finger down
8880 int32_t x = 100, y = 100;
8881 processPosition(mapper, x, y);
8882 processSync(mapper);
8883
8884 NotifyMotionArgs motionArgs;
8885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8886 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8887
8888 // Deactivate display viewport
8889 displayViewport.isActive = false;
8890 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008891 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008892
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008893 // The ongoing touch should be canceled immediately
8894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8895 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8896
8897 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008898 x += 10, y += 10;
8899 processPosition(mapper, x, y);
8900 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008902
8903 // Reactivate display viewport
8904 displayViewport.isActive = true;
8905 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008906 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008907
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008908 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008909 x += 10, y += 10;
8910 processPosition(mapper, x, y);
8911 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8913 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008914}
8915
Josh Thielene986aed2023-06-01 14:17:30 +00008916/**
8917 * When the viewport is deactivated (isActive transitions from true to false),
8918 * and touch.enableForInactiveViewport is true, touches prior to the transition
8919 * should not be cancelled.
8920 */
8921TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
8922 addConfigurationProperty("touch.deviceType", "touchScreen");
8923 addConfigurationProperty("touch.enableForInactiveViewport", "1");
8924 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8925 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
8926 std::optional<DisplayViewport> optionalDisplayViewport =
8927 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8928 ASSERT_TRUE(optionalDisplayViewport.has_value());
8929 DisplayViewport displayViewport = *optionalDisplayViewport;
8930
8931 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8932 prepareAxes(POSITION);
8933 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
8934
8935 // Finger down
8936 int32_t x = 100, y = 100;
8937 processPosition(mapper, x, y);
8938 processSync(mapper);
8939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8940 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
8941
8942 // Deactivate display viewport
8943 displayViewport.isActive = false;
8944 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8945 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8946
8947 // The ongoing touch should not be canceled
8948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8949
8950 // Finger move is not ignored
8951 x += 10, y += 10;
8952 processPosition(mapper, x, y);
8953 processSync(mapper);
8954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8955 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8956
8957 // Reactivate display viewport
8958 displayViewport.isActive = true;
8959 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8960 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8961
8962 // Finger move continues and does not start new gesture
8963 x += 10, y += 10;
8964 processPosition(mapper, x, y);
8965 processSync(mapper);
8966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8967 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8968}
8969
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008970TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008971 prepareAxes(POSITION);
8972 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008973 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008974 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008975
8976 NotifyMotionArgs motionArgs;
8977 // Unrotated video frame
8978 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8979 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008980 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008981 processPosition(mapper, 100, 200);
8982 processSync(mapper);
8983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8984 ASSERT_EQ(frames, motionArgs.videoFrames);
8985
8986 // Subsequent touch events should not have any videoframes
8987 // This is implemented separately in FakeEventHub,
8988 // but that should match the behaviour of TouchVideoDevice.
8989 processPosition(mapper, 200, 200);
8990 processSync(mapper);
8991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8992 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8993}
8994
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008995TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008996 prepareAxes(POSITION);
8997 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008998 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008999 // Unrotated video frame
9000 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9001 NotifyMotionArgs motionArgs;
9002
9003 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009004 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00009005 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009006 clearViewports();
9007 prepareDisplay(orientation);
9008 std::vector<TouchVideoFrame> frames{frame};
9009 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9010 processPosition(mapper, 100, 200);
9011 processSync(mapper);
9012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9013 ASSERT_EQ(frames, motionArgs.videoFrames);
9014 }
9015}
9016
9017TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9018 prepareAxes(POSITION);
9019 addConfigurationProperty("touch.deviceType", "touchScreen");
9020 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9021 // orientation-aware are affected by display rotation.
9022 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009023 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009024 // Unrotated video frame
9025 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9026 NotifyMotionArgs motionArgs;
9027
9028 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009029 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00009030 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009031 clearViewports();
9032 prepareDisplay(orientation);
9033 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009034 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009035 processPosition(mapper, 100, 200);
9036 processSync(mapper);
9037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009038 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9039 // compared to the display. This is so that when the window transform (which contains the
9040 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9041 // window's coordinate space.
9042 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009043 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009044
9045 // Release finger.
9046 processSync(mapper);
9047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009048 }
9049}
9050
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009051TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009052 prepareAxes(POSITION);
9053 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009054 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009055 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9056 // so mix these.
9057 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9058 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9059 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9060 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9061 NotifyMotionArgs motionArgs;
9062
Michael Wrighta9cf4192022-12-01 23:46:39 +00009063 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009064 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009065 processPosition(mapper, 100, 200);
9066 processSync(mapper);
9067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009068 ASSERT_EQ(frames, motionArgs.videoFrames);
9069}
9070
9071TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9072 prepareAxes(POSITION);
9073 addConfigurationProperty("touch.deviceType", "touchScreen");
9074 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9075 // orientation-aware are affected by display rotation.
9076 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009077 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009078 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9079 // so mix these.
9080 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9081 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9082 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9083 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9084 NotifyMotionArgs motionArgs;
9085
Michael Wrighta9cf4192022-12-01 23:46:39 +00009086 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009087 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9088 processPosition(mapper, 100, 200);
9089 processSync(mapper);
9090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9091 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9092 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9093 // compared to the display. This is so that when the window transform (which contains the
9094 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9095 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009096 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009097 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009098 ASSERT_EQ(frames, motionArgs.videoFrames);
9099}
9100
Arthur Hung9da14732019-09-02 16:16:58 +08009101/**
9102 * If we had defined port associations, but the viewport is not ready, the touch device would be
9103 * expected to be disabled, and it should be enabled after the viewport has found.
9104 */
9105TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009106 constexpr uint8_t hdmi2 = 1;
9107 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009108 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009109
9110 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9111
9112 addConfigurationProperty("touch.deviceType", "touchScreen");
9113 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009114 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009115
9116 ASSERT_EQ(mDevice->isEnabled(), false);
9117
9118 // Add display on hdmi2, the device should be enabled and can receive touch event.
9119 prepareSecondaryDisplay(type, hdmi2);
9120 ASSERT_EQ(mDevice->isEnabled(), true);
9121
9122 // Send a touch event.
9123 processPosition(mapper, 100, 100);
9124 processSync(mapper);
9125
9126 NotifyMotionArgs args;
9127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9128 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9129}
9130
Arthur Hung421eb1c2020-01-16 00:09:42 +08009131TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009132 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009133 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009134 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009135 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009136
9137 NotifyMotionArgs motionArgs;
9138
9139 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9140 // finger down
9141 processId(mapper, 1);
9142 processPosition(mapper, x1, y1);
9143 processSync(mapper);
9144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9145 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009146 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009147
9148 // finger move
9149 processId(mapper, 1);
9150 processPosition(mapper, x2, y2);
9151 processSync(mapper);
9152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009154 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009155
9156 // finger up.
9157 processId(mapper, -1);
9158 processSync(mapper);
9159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9160 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009161 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009162
9163 // new finger down
9164 processId(mapper, 1);
9165 processPosition(mapper, x3, y3);
9166 processSync(mapper);
9167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9168 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009169 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009170}
9171
9172/**
arthurhungcc7f9802020-04-30 17:55:40 +08009173 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9174 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009175 */
arthurhungcc7f9802020-04-30 17:55:40 +08009176TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009177 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009178 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009179 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009180 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009181
9182 NotifyMotionArgs motionArgs;
9183
9184 // default tool type is finger
9185 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009186 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009187 processPosition(mapper, x1, y1);
9188 processSync(mapper);
9189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9190 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009191 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009192
9193 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9194 processToolType(mapper, MT_TOOL_PALM);
9195 processSync(mapper);
9196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9197 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9198
9199 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009200 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009201 processPosition(mapper, x2, y2);
9202 processSync(mapper);
9203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9204
9205 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009206 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009207 processSync(mapper);
9208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9209
9210 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009211 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009212 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009213 processPosition(mapper, x3, y3);
9214 processSync(mapper);
9215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9216 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009217 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009218}
9219
arthurhungbf89a482020-04-17 17:37:55 +08009220/**
arthurhungcc7f9802020-04-30 17:55:40 +08009221 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9222 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009223 */
arthurhungcc7f9802020-04-30 17:55:40 +08009224TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009225 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009226 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009227 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009228 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009229
9230 NotifyMotionArgs motionArgs;
9231
9232 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009233 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9234 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009235 processPosition(mapper, x1, y1);
9236 processSync(mapper);
9237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9238 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009239 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009240
9241 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009242 processSlot(mapper, SECOND_SLOT);
9243 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009244 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009245 processSync(mapper);
9246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009247 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009248 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009249
9250 // If the tool type of the first finger changes to MT_TOOL_PALM,
9251 // we expect to receive ACTION_POINTER_UP with cancel flag.
9252 processSlot(mapper, FIRST_SLOT);
9253 processId(mapper, FIRST_TRACKING_ID);
9254 processToolType(mapper, MT_TOOL_PALM);
9255 processSync(mapper);
9256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009257 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009258 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9259
9260 // The following MOVE events of second finger should be processed.
9261 processSlot(mapper, SECOND_SLOT);
9262 processId(mapper, SECOND_TRACKING_ID);
9263 processPosition(mapper, x2 + 1, y2 + 1);
9264 processSync(mapper);
9265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009267 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009268
9269 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9270 // it. Second finger receive move.
9271 processSlot(mapper, FIRST_SLOT);
9272 processId(mapper, INVALID_TRACKING_ID);
9273 processSync(mapper);
9274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009276 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009277
9278 // Second finger keeps moving.
9279 processSlot(mapper, SECOND_SLOT);
9280 processId(mapper, SECOND_TRACKING_ID);
9281 processPosition(mapper, x2 + 2, y2 + 2);
9282 processSync(mapper);
9283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009285 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009286
9287 // Second finger up.
9288 processId(mapper, INVALID_TRACKING_ID);
9289 processSync(mapper);
9290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9291 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9292 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9293}
9294
9295/**
9296 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9297 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9298 */
9299TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9300 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009301 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009302 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009303 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009304
9305 NotifyMotionArgs motionArgs;
9306
9307 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9308 // First finger down.
9309 processId(mapper, FIRST_TRACKING_ID);
9310 processPosition(mapper, x1, y1);
9311 processSync(mapper);
9312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9313 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009314 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009315
9316 // Second finger down.
9317 processSlot(mapper, SECOND_SLOT);
9318 processId(mapper, SECOND_TRACKING_ID);
9319 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009320 processSync(mapper);
9321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009322 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009323 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009324
arthurhungcc7f9802020-04-30 17:55:40 +08009325 // If the tool type of the first finger changes to MT_TOOL_PALM,
9326 // we expect to receive ACTION_POINTER_UP with cancel flag.
9327 processSlot(mapper, FIRST_SLOT);
9328 processId(mapper, FIRST_TRACKING_ID);
9329 processToolType(mapper, MT_TOOL_PALM);
9330 processSync(mapper);
9331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009332 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009333 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9334
9335 // Second finger keeps moving.
9336 processSlot(mapper, SECOND_SLOT);
9337 processId(mapper, SECOND_TRACKING_ID);
9338 processPosition(mapper, x2 + 1, y2 + 1);
9339 processSync(mapper);
9340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9342
9343 // second finger becomes palm, receive cancel due to only 1 finger is active.
9344 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009345 processToolType(mapper, MT_TOOL_PALM);
9346 processSync(mapper);
9347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9348 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9349
arthurhungcc7f9802020-04-30 17:55:40 +08009350 // third finger down.
9351 processSlot(mapper, THIRD_SLOT);
9352 processId(mapper, THIRD_TRACKING_ID);
9353 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009354 processPosition(mapper, x3, y3);
9355 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9357 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009358 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009359 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009360
9361 // third finger move
9362 processId(mapper, THIRD_TRACKING_ID);
9363 processPosition(mapper, x3 + 1, y3 + 1);
9364 processSync(mapper);
9365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9367
9368 // first finger up, third finger receive move.
9369 processSlot(mapper, FIRST_SLOT);
9370 processId(mapper, INVALID_TRACKING_ID);
9371 processSync(mapper);
9372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009374 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009375
9376 // second finger up, third finger receive move.
9377 processSlot(mapper, SECOND_SLOT);
9378 processId(mapper, INVALID_TRACKING_ID);
9379 processSync(mapper);
9380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009382 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009383
9384 // third finger up.
9385 processSlot(mapper, THIRD_SLOT);
9386 processId(mapper, INVALID_TRACKING_ID);
9387 processSync(mapper);
9388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9389 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9390 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9391}
9392
9393/**
9394 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9395 * and the active finger could still be allowed to receive the events
9396 */
9397TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9398 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009399 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009400 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009401 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009402
9403 NotifyMotionArgs motionArgs;
9404
9405 // default tool type is finger
9406 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9407 processId(mapper, FIRST_TRACKING_ID);
9408 processPosition(mapper, x1, y1);
9409 processSync(mapper);
9410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9411 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009412 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009413
9414 // Second finger down.
9415 processSlot(mapper, SECOND_SLOT);
9416 processId(mapper, SECOND_TRACKING_ID);
9417 processPosition(mapper, x2, y2);
9418 processSync(mapper);
9419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009420 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009421 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009422
9423 // If the tool type of the second finger changes to MT_TOOL_PALM,
9424 // we expect to receive ACTION_POINTER_UP with cancel flag.
9425 processId(mapper, SECOND_TRACKING_ID);
9426 processToolType(mapper, MT_TOOL_PALM);
9427 processSync(mapper);
9428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009429 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009430 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9431
9432 // The following MOVE event should be processed.
9433 processSlot(mapper, FIRST_SLOT);
9434 processId(mapper, FIRST_TRACKING_ID);
9435 processPosition(mapper, x1 + 1, y1 + 1);
9436 processSync(mapper);
9437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009439 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009440
9441 // second finger up.
9442 processSlot(mapper, SECOND_SLOT);
9443 processId(mapper, INVALID_TRACKING_ID);
9444 processSync(mapper);
9445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9447
9448 // first finger keep moving
9449 processSlot(mapper, FIRST_SLOT);
9450 processId(mapper, FIRST_TRACKING_ID);
9451 processPosition(mapper, x1 + 2, y1 + 2);
9452 processSync(mapper);
9453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9455
9456 // first finger up.
9457 processId(mapper, INVALID_TRACKING_ID);
9458 processSync(mapper);
9459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9460 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9461 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009462}
9463
Arthur Hung9ad18942021-06-19 02:04:46 +00009464/**
9465 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9466 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9467 * cause slot be valid again.
9468 */
9469TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9470 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009471 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009472 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009473 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +00009474
9475 NotifyMotionArgs motionArgs;
9476
9477 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9478 // First finger down.
9479 processId(mapper, FIRST_TRACKING_ID);
9480 processPosition(mapper, x1, y1);
9481 processPressure(mapper, RAW_PRESSURE_MAX);
9482 processSync(mapper);
9483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9484 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009485 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009486
9487 // First finger move.
9488 processId(mapper, FIRST_TRACKING_ID);
9489 processPosition(mapper, x1 + 1, y1 + 1);
9490 processPressure(mapper, RAW_PRESSURE_MAX);
9491 processSync(mapper);
9492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009494 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009495
9496 // Second finger down.
9497 processSlot(mapper, SECOND_SLOT);
9498 processId(mapper, SECOND_TRACKING_ID);
9499 processPosition(mapper, x2, y2);
9500 processPressure(mapper, RAW_PRESSURE_MAX);
9501 processSync(mapper);
9502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009503 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009504 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009505
9506 // second finger up with some unexpected data.
9507 processSlot(mapper, SECOND_SLOT);
9508 processId(mapper, INVALID_TRACKING_ID);
9509 processPosition(mapper, x2, y2);
9510 processSync(mapper);
9511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009512 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009513 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009514
9515 // first finger up with some unexpected data.
9516 processSlot(mapper, FIRST_SLOT);
9517 processId(mapper, INVALID_TRACKING_ID);
9518 processPosition(mapper, x2, y2);
9519 processPressure(mapper, RAW_PRESSURE_MAX);
9520 processSync(mapper);
9521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9522 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009523 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009524}
9525
Arpit Singh4b4a4572023-11-24 18:19:56 +00009526TEST_F(MultiTouchInputMapperTest, Reset_RepopulatesMultiTouchState) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009527 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009528 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009529 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009530 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009531
9532 // First finger down.
Arpit Singh4b4a4572023-11-24 18:19:56 +00009533 constexpr int32_t x1 = 100, y1 = 200, x2 = 300, y2 = 400;
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009534 processId(mapper, FIRST_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009535 processPosition(mapper, x1, y1);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009536 processPressure(mapper, RAW_PRESSURE_MAX);
9537 processSync(mapper);
9538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9539 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9540
9541 // Second finger down.
9542 processSlot(mapper, SECOND_SLOT);
9543 processId(mapper, SECOND_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009544 processPosition(mapper, x2, y2);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009545 processPressure(mapper, RAW_PRESSURE_MAX);
9546 processSync(mapper);
9547 ASSERT_NO_FATAL_FAILURE(
9548 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9549
Arpit Singh4b4a4572023-11-24 18:19:56 +00009550 // Set MT Slot state to be repopulated for the required slots
9551 std::vector<int32_t> mtSlotValues(RAW_SLOT_MAX + 1, -1);
9552 mtSlotValues[0] = FIRST_TRACKING_ID;
9553 mtSlotValues[1] = SECOND_TRACKING_ID;
9554 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_TRACKING_ID, mtSlotValues);
9555
9556 mtSlotValues[0] = x1;
9557 mtSlotValues[1] = x2;
9558 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_X, mtSlotValues);
9559
9560 mtSlotValues[0] = y1;
9561 mtSlotValues[1] = y2;
9562 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_Y, mtSlotValues);
9563
9564 mtSlotValues[0] = RAW_PRESSURE_MAX;
9565 mtSlotValues[1] = RAW_PRESSURE_MAX;
9566 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_PRESSURE, mtSlotValues);
9567
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009568 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Arpit Singh4b4a4572023-11-24 18:19:56 +00009569 // repopulated. Resetting should cancel the ongoing gesture.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009570 resetMapper(mapper, ARBITRARY_TIME);
9571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9572 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009573
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009574 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9575 // the existing touch state to generate a down event.
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009576 processPosition(mapper, 301, 302);
9577 processSync(mapper);
9578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9579 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9581 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009582
9583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9584}
9585
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009586TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009587 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009588 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009589 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009590 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009591
9592 // First finger touches down and releases.
9593 processId(mapper, FIRST_TRACKING_ID);
9594 processPosition(mapper, 100, 200);
9595 processPressure(mapper, RAW_PRESSURE_MAX);
9596 processSync(mapper);
9597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9598 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9599 processId(mapper, INVALID_TRACKING_ID);
9600 processSync(mapper);
9601 ASSERT_NO_FATAL_FAILURE(
9602 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9603
9604 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9605 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009606 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9608
9609 // Send an empty sync frame. Since there are no pointers, no events are generated.
9610 processSync(mapper);
9611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9612}
9613
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009614TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009615 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009616 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009617 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009618 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009620
9621 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9622 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9623 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9624 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9625 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9626
9627 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009628 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009629 processId(mapper, FIRST_TRACKING_ID);
9630 processToolType(mapper, MT_TOOL_PEN);
9631 processPosition(mapper, 100, 200);
9632 processPressure(mapper, RAW_PRESSURE_MAX);
9633 processSync(mapper);
9634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9635 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9636 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009637 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009638
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009639 // Now that we know the device supports styluses, ensure that the device is re-configured with
9640 // the stylus source.
9641 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9642 {
9643 const auto& devices = mReader->getInputDevices();
9644 auto deviceInfo =
9645 std::find_if(devices.begin(), devices.end(),
9646 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9647 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9648 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9649 }
9650
9651 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9653
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009654 processId(mapper, INVALID_TRACKING_ID);
9655 processSync(mapper);
9656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9657 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9658 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009659 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009660}
9661
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009662// --- MultiTouchInputMapperTest_ExternalDevice ---
9663
9664class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9665protected:
Chris Yea52ade12020-08-27 16:49:20 -07009666 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009667};
9668
9669/**
9670 * Expect fallback to internal viewport if device is external and external viewport is not present.
9671 */
9672TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9673 prepareAxes(POSITION);
9674 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009675 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009676 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009677
9678 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9679
9680 NotifyMotionArgs motionArgs;
9681
9682 // Expect the event to be sent to the internal viewport,
9683 // because an external viewport is not present.
9684 processPosition(mapper, 100, 100);
9685 processSync(mapper);
9686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009687 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, motionArgs.displayId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009688
9689 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009690 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009691 processPosition(mapper, 100, 100);
9692 processSync(mapper);
9693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9694 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9695}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009696
Prabir Pradhan3ed7e352024-05-03 23:59:43 +00009697// TODO(b/281840344): Remove the test when the old touchpad stack is removed. It is currently
9698// unclear what the behavior of the touchpad logic in TouchInputMapper should do after the
9699// PointerChoreographer refactor.
9700TEST_F(MultiTouchInputMapperTest, DISABLED_Process_TouchpadPointer) {
Harry Cutts8722be92024-04-05 14:46:05 +00009701 // prepare device
Michael Wrighta9cf4192022-12-01 23:46:39 +00009702 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009703 prepareAxes(POSITION | ID | SLOT);
9704 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9705 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009706 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009707 // run uncaptured pointer tests - pushes out generic events
9708 // FINGER 0 DOWN
9709 processId(mapper, 3);
9710 processPosition(mapper, 100, 100);
9711 processKey(mapper, BTN_TOUCH, 1);
9712 processSync(mapper);
9713
9714 // start at (100,100), cursor should be at (0,0) * scale
9715 NotifyMotionArgs args;
9716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9717 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9718 ASSERT_NO_FATAL_FAILURE(
9719 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9720
9721 // FINGER 0 MOVE
9722 processPosition(mapper, 200, 200);
9723 processSync(mapper);
9724
9725 // compute scaling to help with touch position checking
9726 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9727 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9728 float scale =
9729 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9730
9731 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9733 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9735 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +08009736
9737 // BUTTON DOWN
9738 processKey(mapper, BTN_LEFT, 1);
9739 processSync(mapper);
9740
9741 // touchinputmapper design sends a move before button press
9742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9743 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9745 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9746
9747 // BUTTON UP
9748 processKey(mapper, BTN_LEFT, 0);
9749 processSync(mapper);
9750
9751 // touchinputmapper design sends a move after button release
9752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9753 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9755 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009756}
9757
Harry Cutts8722be92024-04-05 14:46:05 +00009758TEST_F(MultiTouchInputMapperTest, Touchpad_GetSources) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00009759 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009760 prepareAxes(POSITION | ID | SLOT);
9761 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Hiroki Sato25040232024-02-22 17:21:22 +09009762 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Arpit Singha8c236b2023-04-25 13:56:05 +00009763 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009764
Josep del Río2d8c79a2023-01-23 19:33:50 +00009765 // uncaptured touchpad should be a pointer device
9766 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009767}
9768
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009769// --- BluetoothMultiTouchInputMapperTest ---
9770
9771class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9772protected:
9773 void SetUp() override {
9774 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9775 }
9776};
9777
9778TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9779 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009780 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009781 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009782 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009783
9784 nsecs_t kernelEventTime = ARBITRARY_TIME;
9785 nsecs_t expectedEventTime = ARBITRARY_TIME;
9786 // Touch down.
9787 processId(mapper, FIRST_TRACKING_ID);
9788 processPosition(mapper, 100, 200);
9789 processPressure(mapper, RAW_PRESSURE_MAX);
9790 processSync(mapper, ARBITRARY_TIME);
9791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9792 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9793
9794 // Process several events that come in quick succession, according to their timestamps.
9795 for (int i = 0; i < 3; i++) {
9796 constexpr static nsecs_t delta = ms2ns(1);
9797 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9798 kernelEventTime += delta;
9799 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
9800
9801 processPosition(mapper, 101 + i, 201 + i);
9802 processSync(mapper, kernelEventTime);
9803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9804 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9805 WithEventTime(expectedEventTime))));
9806 }
9807
9808 // Release the touch.
9809 processId(mapper, INVALID_TRACKING_ID);
9810 processPressure(mapper, RAW_PRESSURE_MIN);
9811 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
9812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9813 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9814 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
9815}
9816
9817// --- MultiTouchPointerModeTest ---
9818
HQ Liue6983c72022-04-19 22:14:56 +00009819class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9820protected:
9821 float mPointerMovementScale;
9822 float mPointerXZoomScale;
9823 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9824 addConfigurationProperty("touch.deviceType", "pointer");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009825 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +00009826
9827 prepareAxes(POSITION);
9828 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9829 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9830 // needs to be disabled, and the pointer gesture needs to be enabled.
Hiroki Sato25040232024-02-22 17:21:22 +09009831 mFakePolicy->setPointerCapture(/*window=*/nullptr);
HQ Liue6983c72022-04-19 22:14:56 +00009832 mFakePolicy->setPointerGestureEnabled(true);
HQ Liue6983c72022-04-19 22:14:56 +00009833
9834 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9835 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9836 mPointerMovementScale =
9837 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9838 mPointerXZoomScale =
9839 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9840 }
9841
9842 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9843 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9844 /*flat*/ 0,
9845 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9846 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9847 /*flat*/ 0,
9848 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9849 }
9850};
9851
9852/**
9853 * Two fingers down on a pointer mode touch pad. The width
9854 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9855 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9856 * be greater than the both value to be freeform gesture, so that after two
9857 * fingers start to move downwards, the gesture should be swipe.
9858 */
9859TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9860 // The min freeform gesture width is 25units/mm x 30mm = 750
9861 // which is greater than fraction of the diagnal length of the touchpad (349).
9862 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +00009863 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009864 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009865 NotifyMotionArgs motionArgs;
9866
9867 // Two fingers down at once.
9868 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9869 // Pointer's initial position is used the [0,0] coordinate.
9870 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9871
9872 processId(mapper, FIRST_TRACKING_ID);
9873 processPosition(mapper, x1, y1);
9874 processMTSync(mapper);
9875 processId(mapper, SECOND_TRACKING_ID);
9876 processPosition(mapper, x2, y2);
9877 processMTSync(mapper);
9878 processSync(mapper);
9879
9880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009881 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009882 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009883 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009884 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009885 ASSERT_NO_FATAL_FAILURE(
9886 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9887
9888 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9889 // that there should be 1 pointer.
9890 int32_t movingDistance = 200;
9891 y1 += movingDistance;
9892 y2 += movingDistance;
9893
9894 processId(mapper, FIRST_TRACKING_ID);
9895 processPosition(mapper, x1, y1);
9896 processMTSync(mapper);
9897 processId(mapper, SECOND_TRACKING_ID);
9898 processPosition(mapper, x2, y2);
9899 processMTSync(mapper);
9900 processSync(mapper);
9901
9902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009903 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009905 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009906 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9908 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9909 0, 0, 0, 0));
9910}
9911
9912/**
9913 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9914 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9915 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9916 * value to be freeform gesture, so that after two fingers start to move downwards,
9917 * the gesture should be swipe.
9918 */
9919TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9920 // The min freeform gesture width is 5units/mm x 30mm = 150
9921 // which is greater than fraction of the diagnal length of the touchpad (349).
9922 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +00009923 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +00009924 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009925 NotifyMotionArgs motionArgs;
9926
9927 // Two fingers down at once.
9928 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9929 // Pointer's initial position is used the [0,0] coordinate.
9930 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9931
9932 processId(mapper, FIRST_TRACKING_ID);
9933 processPosition(mapper, x1, y1);
9934 processMTSync(mapper);
9935 processId(mapper, SECOND_TRACKING_ID);
9936 processPosition(mapper, x2, y2);
9937 processMTSync(mapper);
9938 processSync(mapper);
9939
9940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009941 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009942 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009943 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009944 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009945 ASSERT_NO_FATAL_FAILURE(
9946 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9947
9948 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9949 // and there should be 1 pointer.
9950 int32_t movingDistance = 200;
9951 y1 += movingDistance;
9952 y2 += movingDistance;
9953
9954 processId(mapper, FIRST_TRACKING_ID);
9955 processPosition(mapper, x1, y1);
9956 processMTSync(mapper);
9957 processId(mapper, SECOND_TRACKING_ID);
9958 processPosition(mapper, x2, y2);
9959 processMTSync(mapper);
9960 processSync(mapper);
9961
9962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009963 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009965 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009966 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009967 // New coordinate is the scaled relative coordinate from the initial coordinate.
9968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9969 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9970 0, 0, 0, 0));
9971}
9972
9973/**
9974 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9975 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9976 * freeform gestures after two fingers start to move downwards.
9977 */
9978TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +00009979 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009980 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009981
9982 NotifyMotionArgs motionArgs;
9983
9984 // Two fingers down at once. Wider than the max swipe width.
9985 // The gesture is expected to be PRESS, then transformed to FREEFORM
9986 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9987
9988 processId(mapper, FIRST_TRACKING_ID);
9989 processPosition(mapper, x1, y1);
9990 processMTSync(mapper);
9991 processId(mapper, SECOND_TRACKING_ID);
9992 processPosition(mapper, x2, y2);
9993 processMTSync(mapper);
9994 processSync(mapper);
9995
9996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009997 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009998 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009999 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010000 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010001 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10002 ASSERT_NO_FATAL_FAILURE(
10003 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10004
10005 int32_t movingDistance = 200;
10006
10007 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10008 // then two down events for two pointers.
10009 y1 += movingDistance;
10010 y2 += movingDistance;
10011
10012 processId(mapper, FIRST_TRACKING_ID);
10013 processPosition(mapper, x1, y1);
10014 processMTSync(mapper);
10015 processId(mapper, SECOND_TRACKING_ID);
10016 processPosition(mapper, x2, y2);
10017 processMTSync(mapper);
10018 processSync(mapper);
10019
10020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10021 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010022 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010023 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010025 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010026 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010027 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010029 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010030 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010031 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010032 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010033 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010034 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010035 // Two pointers' scaled relative coordinates from their initial centroid.
10036 // Initial y coordinates are 0 as y1 and y2 have the same value.
10037 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10038 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10039 // When pointers move, the new coordinates equal to the initial coordinates plus
10040 // scaled moving distance.
10041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10042 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10043 0, 0, 0, 0));
10044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10045 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10046 0, 0, 0, 0));
10047
10048 // Move two fingers down again, expect one MOVE motion event.
10049 y1 += movingDistance;
10050 y2 += movingDistance;
10051
10052 processId(mapper, FIRST_TRACKING_ID);
10053 processPosition(mapper, x1, y1);
10054 processMTSync(mapper);
10055 processId(mapper, SECOND_TRACKING_ID);
10056 processPosition(mapper, x2, y2);
10057 processMTSync(mapper);
10058 processSync(mapper);
10059
10060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010061 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010063 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010064 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10066 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10067 0, 0, 0, 0, 0));
10068 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10069 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10070 0, 0, 0, 0, 0));
10071}
10072
Harry Cutts39b7ca22022-10-05 15:55:48 +000010073TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010074 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010075 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010076 NotifyMotionArgs motionArgs;
10077
10078 // Place two fingers down.
10079 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10080
10081 processId(mapper, FIRST_TRACKING_ID);
10082 processPosition(mapper, x1, y1);
10083 processMTSync(mapper);
10084 processId(mapper, SECOND_TRACKING_ID);
10085 processPosition(mapper, x2, y2);
10086 processMTSync(mapper);
10087 processSync(mapper);
10088
10089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010090 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010091 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10092 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10093 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10094 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10095
10096 // Move the two fingers down and to the left.
10097 int32_t movingDistance = 200;
10098 x1 -= movingDistance;
10099 y1 += movingDistance;
10100 x2 -= movingDistance;
10101 y2 += movingDistance;
10102
10103 processId(mapper, FIRST_TRACKING_ID);
10104 processPosition(mapper, x1, y1);
10105 processMTSync(mapper);
10106 processId(mapper, SECOND_TRACKING_ID);
10107 processPosition(mapper, x2, y2);
10108 processMTSync(mapper);
10109 processSync(mapper);
10110
10111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010112 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010113 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10114 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10115 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10116 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10117}
10118
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010119TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010120 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010121 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010122 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10124
10125 // Start a stylus gesture.
10126 processKey(mapper, BTN_TOOL_PEN, 1);
10127 processId(mapper, FIRST_TRACKING_ID);
10128 processPosition(mapper, 100, 200);
10129 processSync(mapper);
10130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10131 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10132 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010133 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010134 // TODO(b/257078296): Pointer mode generates extra event.
10135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10136 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10137 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010138 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10140
10141 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10142 // gesture should be disabled.
10143 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10144 viewport->isActive = false;
10145 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010146 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10148 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10149 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010150 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010151 // TODO(b/257078296): Pointer mode generates extra event.
10152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10153 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10154 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010155 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10157}
10158
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010159// --- JoystickInputMapperTest ---
10160
10161class JoystickInputMapperTest : public InputMapperTest {
10162protected:
10163 static const int32_t RAW_X_MIN;
10164 static const int32_t RAW_X_MAX;
10165 static const int32_t RAW_Y_MIN;
10166 static const int32_t RAW_Y_MAX;
10167
10168 void SetUp() override {
10169 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10170 }
10171 void prepareAxes() {
10172 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10173 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10174 }
10175
10176 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10178 }
10179
10180 void processSync(JoystickInputMapper& mapper) {
10181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10182 }
10183
Michael Wrighta9cf4192022-12-01 23:46:39 +000010184 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010185 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10186 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10187 NO_PORT, ViewportType::VIRTUAL);
10188 }
10189};
10190
10191const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10192const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10193const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10194const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10195
10196TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10197 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000010198 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010199
10200 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10201
Michael Wrighta9cf4192022-12-01 23:46:39 +000010202 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010203
10204 // Send an axis event
10205 processAxis(mapper, ABS_X, 100);
10206 processSync(mapper);
10207
10208 NotifyMotionArgs args;
10209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10210 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10211
10212 // Send another axis event
10213 processAxis(mapper, ABS_Y, 100);
10214 processSync(mapper);
10215
10216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10217 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10218}
10219
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010220// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010221
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010222class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010223protected:
10224 static const char* DEVICE_NAME;
10225 static const char* DEVICE_LOCATION;
10226 static const int32_t DEVICE_ID;
10227 static const int32_t DEVICE_GENERATION;
10228 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010229 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010230 static const int32_t EVENTHUB_ID;
10231
10232 std::shared_ptr<FakeEventHub> mFakeEventHub;
10233 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010234 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010235 std::unique_ptr<InstrumentedInputReader> mReader;
10236 std::shared_ptr<InputDevice> mDevice;
10237
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010238 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010239 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010240 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010241 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010242 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010243 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010244 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10245 }
10246
10247 void SetUp() override { SetUp(DEVICE_CLASSES); }
10248
10249 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010250 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010251 mFakePolicy.clear();
10252 }
10253
Chris Yee2b1e5c2021-03-10 22:45:12 -080010254 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10255 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010256 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010257 InputDeviceIdentifier identifier;
10258 identifier.name = name;
10259 identifier.location = location;
10260 std::shared_ptr<InputDevice> device =
10261 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10262 identifier);
10263 mReader->pushNextDevice(device);
10264 mFakeEventHub->addDevice(eventHubId, name, classes);
10265 mReader->loopOnce();
10266 return device;
10267 }
10268
10269 template <class T, typename... Args>
10270 T& addControllerAndConfigure(Args... args) {
10271 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10272
10273 return controller;
10274 }
10275};
10276
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010277const char* PeripheralControllerTest::DEVICE_NAME = "device";
10278const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10279const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10280const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10281const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010282const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10283 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010284const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010285
10286// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010287class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010288protected:
10289 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010290 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010291 }
10292};
10293
10294TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010295 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010296
Harry Cuttsa5b71292022-11-28 12:56:17 +000010297 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10298 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10299 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010300}
10301
10302TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010303 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010304
Harry Cuttsa5b71292022-11-28 12:56:17 +000010305 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10306 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10307 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010308}
10309
10310// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010311class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010312protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010313 void SetUp() override {
10314 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10315 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010316};
10317
Chris Ye85758332021-05-16 23:05:17 -070010318TEST_F(LightControllerTest, MonoLight) {
10319 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010320 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010321 .maxBrightness = 255,
10322 .flags = InputLightClass::BRIGHTNESS,
10323 .path = ""};
10324 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010325
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010326 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010327 InputDeviceInfo info;
10328 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010329 std::vector<InputDeviceLightInfo> lights = info.getLights();
10330 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010331 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10332 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10333
10334 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10335 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10336}
10337
DingYong99f2c3c2023-12-20 15:46:06 +080010338TEST_F(LightControllerTest, MonoKeyboardMuteLight) {
10339 RawLightInfo infoMono = {.id = 1,
10340 .name = "mono_keyboard_mute",
10341 .maxBrightness = 255,
10342 .flags = InputLightClass::BRIGHTNESS |
10343 InputLightClass::KEYBOARD_MIC_MUTE,
10344 .path = ""};
10345 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10346
10347 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10348 std::list<NotifyArgs> unused =
10349 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10350 /*changes=*/{});
10351
10352 InputDeviceInfo info;
10353 controller.populateDeviceInfo(&info);
10354 std::vector<InputDeviceLightInfo> lights = info.getLights();
10355 ASSERT_EQ(1U, lights.size());
10356 ASSERT_EQ(InputDeviceLightType::KEYBOARD_MIC_MUTE, lights[0].type);
10357 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10358}
10359
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010360TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10361 RawLightInfo infoMono = {.id = 1,
10362 .name = "mono_keyboard_backlight",
10363 .maxBrightness = 255,
10364 .flags = InputLightClass::BRIGHTNESS |
10365 InputLightClass::KEYBOARD_BACKLIGHT,
10366 .path = ""};
10367 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10368
10369 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10370 InputDeviceInfo info;
10371 controller.populateDeviceInfo(&info);
10372 std::vector<InputDeviceLightInfo> lights = info.getLights();
10373 ASSERT_EQ(1U, lights.size());
10374 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10375 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010376
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010377 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10378 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010379}
10380
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000010381TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
10382 RawLightInfo infoMono = {.id = 1,
10383 .name = "mono_light",
10384 .maxBrightness = 255,
10385 .flags = InputLightClass::BRIGHTNESS,
10386 .path = ""};
10387 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10388 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10389 "0,100,200");
10390
10391 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10392 std::list<NotifyArgs> unused =
10393 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10394 /*changes=*/{});
10395
10396 InputDeviceInfo info;
10397 controller.populateDeviceInfo(&info);
10398 std::vector<InputDeviceLightInfo> lights = info.getLights();
10399 ASSERT_EQ(1U, lights.size());
10400 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10401}
10402
10403TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
10404 RawLightInfo infoMono = {.id = 1,
10405 .name = "mono_keyboard_backlight",
10406 .maxBrightness = 255,
10407 .flags = InputLightClass::BRIGHTNESS |
10408 InputLightClass::KEYBOARD_BACKLIGHT,
10409 .path = ""};
10410 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10411
10412 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10413 std::list<NotifyArgs> unused =
10414 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10415 /*changes=*/{});
10416
10417 InputDeviceInfo info;
10418 controller.populateDeviceInfo(&info);
10419 std::vector<InputDeviceLightInfo> lights = info.getLights();
10420 ASSERT_EQ(1U, lights.size());
10421 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10422}
10423
10424TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
10425 RawLightInfo infoMono = {.id = 1,
10426 .name = "mono_keyboard_backlight",
10427 .maxBrightness = 255,
10428 .flags = InputLightClass::BRIGHTNESS |
10429 InputLightClass::KEYBOARD_BACKLIGHT,
10430 .path = ""};
10431 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10432 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10433 "0,100,200");
10434
10435 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10436 std::list<NotifyArgs> unused =
10437 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10438 /*changes=*/{});
10439
10440 InputDeviceInfo info;
10441 controller.populateDeviceInfo(&info);
10442 std::vector<InputDeviceLightInfo> lights = info.getLights();
10443 ASSERT_EQ(1U, lights.size());
10444 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
10445 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
10446 ASSERT_EQ(BrightnessLevel(0), *it);
10447 std::advance(it, 1);
10448 ASSERT_EQ(BrightnessLevel(100), *it);
10449 std::advance(it, 1);
10450 ASSERT_EQ(BrightnessLevel(200), *it);
10451}
10452
10453TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
10454 RawLightInfo infoMono = {.id = 1,
10455 .name = "mono_keyboard_backlight",
10456 .maxBrightness = 255,
10457 .flags = InputLightClass::BRIGHTNESS |
10458 InputLightClass::KEYBOARD_BACKLIGHT,
10459 .path = ""};
10460 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10461 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10462 "0,100,200,300,400,500");
10463
10464 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10465 std::list<NotifyArgs> unused =
10466 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10467 /*changes=*/{});
10468
10469 InputDeviceInfo info;
10470 controller.populateDeviceInfo(&info);
10471 std::vector<InputDeviceLightInfo> lights = info.getLights();
10472 ASSERT_EQ(1U, lights.size());
10473 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10474}
10475
Chris Yee2b1e5c2021-03-10 22:45:12 -080010476TEST_F(LightControllerTest, RGBLight) {
10477 RawLightInfo infoRed = {.id = 1,
10478 .name = "red",
10479 .maxBrightness = 255,
10480 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10481 .path = ""};
10482 RawLightInfo infoGreen = {.id = 2,
10483 .name = "green",
10484 .maxBrightness = 255,
10485 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10486 .path = ""};
10487 RawLightInfo infoBlue = {.id = 3,
10488 .name = "blue",
10489 .maxBrightness = 255,
10490 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10491 .path = ""};
10492 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10493 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10494 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10495
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010496 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010497 InputDeviceInfo info;
10498 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010499 std::vector<InputDeviceLightInfo> lights = info.getLights();
10500 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010501 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10502 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10503 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10504
10505 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10506 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10507}
10508
10509TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10510 RawLightInfo infoRed = {.id = 1,
10511 .name = "red_keyboard_backlight",
10512 .maxBrightness = 255,
10513 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10514 InputLightClass::KEYBOARD_BACKLIGHT,
10515 .path = ""};
10516 RawLightInfo infoGreen = {.id = 2,
10517 .name = "green_keyboard_backlight",
10518 .maxBrightness = 255,
10519 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10520 InputLightClass::KEYBOARD_BACKLIGHT,
10521 .path = ""};
10522 RawLightInfo infoBlue = {.id = 3,
10523 .name = "blue_keyboard_backlight",
10524 .maxBrightness = 255,
10525 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10526 InputLightClass::KEYBOARD_BACKLIGHT,
10527 .path = ""};
10528 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10529 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10530 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10531
10532 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10533 InputDeviceInfo info;
10534 controller.populateDeviceInfo(&info);
10535 std::vector<InputDeviceLightInfo> lights = info.getLights();
10536 ASSERT_EQ(1U, lights.size());
10537 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10538 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10539 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10540
10541 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10542 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10543}
10544
10545TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10546 RawLightInfo infoRed = {.id = 1,
10547 .name = "red",
10548 .maxBrightness = 255,
10549 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10550 .path = ""};
10551 RawLightInfo infoGreen = {.id = 2,
10552 .name = "green",
10553 .maxBrightness = 255,
10554 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10555 .path = ""};
10556 RawLightInfo infoBlue = {.id = 3,
10557 .name = "blue",
10558 .maxBrightness = 255,
10559 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10560 .path = ""};
10561 RawLightInfo infoGlobal = {.id = 3,
10562 .name = "global_keyboard_backlight",
10563 .maxBrightness = 255,
10564 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10565 InputLightClass::KEYBOARD_BACKLIGHT,
10566 .path = ""};
10567 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10568 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10569 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10570 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10571
10572 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10573 InputDeviceInfo info;
10574 controller.populateDeviceInfo(&info);
10575 std::vector<InputDeviceLightInfo> lights = info.getLights();
10576 ASSERT_EQ(1U, lights.size());
10577 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10578 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10579 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010580
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010581 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10582 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010583}
10584
10585TEST_F(LightControllerTest, MultiColorRGBLight) {
10586 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010587 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010588 .maxBrightness = 255,
10589 .flags = InputLightClass::BRIGHTNESS |
10590 InputLightClass::MULTI_INTENSITY |
10591 InputLightClass::MULTI_INDEX,
10592 .path = ""};
10593
10594 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10595
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010596 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010597 InputDeviceInfo info;
10598 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010599 std::vector<InputDeviceLightInfo> lights = info.getLights();
10600 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010601 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10602 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10603 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10604
10605 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10606 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10607}
10608
10609TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10610 RawLightInfo infoColor = {.id = 1,
10611 .name = "multi_color_keyboard_backlight",
10612 .maxBrightness = 255,
10613 .flags = InputLightClass::BRIGHTNESS |
10614 InputLightClass::MULTI_INTENSITY |
10615 InputLightClass::MULTI_INDEX |
10616 InputLightClass::KEYBOARD_BACKLIGHT,
10617 .path = ""};
10618
10619 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10620
10621 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10622 InputDeviceInfo info;
10623 controller.populateDeviceInfo(&info);
10624 std::vector<InputDeviceLightInfo> lights = info.getLights();
10625 ASSERT_EQ(1U, lights.size());
10626 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10627 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10628 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010629
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010630 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10631 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010632}
10633
Josep del Rioa1046a82023-08-24 19:57:27 +000010634TEST_F(LightControllerTest, SonyPlayerIdLight) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010635 RawLightInfo info1 = {.id = 1,
Josep del Rioa1046a82023-08-24 19:57:27 +000010636 .name = "sony1",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010637 .maxBrightness = 255,
10638 .flags = InputLightClass::BRIGHTNESS,
10639 .path = ""};
10640 RawLightInfo info2 = {.id = 2,
Josep del Rioa1046a82023-08-24 19:57:27 +000010641 .name = "sony2",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010642 .maxBrightness = 255,
10643 .flags = InputLightClass::BRIGHTNESS,
10644 .path = ""};
10645 RawLightInfo info3 = {.id = 3,
Josep del Rioa1046a82023-08-24 19:57:27 +000010646 .name = "sony3",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010647 .maxBrightness = 255,
10648 .flags = InputLightClass::BRIGHTNESS,
10649 .path = ""};
10650 RawLightInfo info4 = {.id = 4,
Josep del Rioa1046a82023-08-24 19:57:27 +000010651 .name = "sony4",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010652 .maxBrightness = 255,
10653 .flags = InputLightClass::BRIGHTNESS,
10654 .path = ""};
10655 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10656 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10657 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10658 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10659
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010660 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010661 InputDeviceInfo info;
10662 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010663 std::vector<InputDeviceLightInfo> lights = info.getLights();
10664 ASSERT_EQ(1U, lights.size());
Josep del Rioa1046a82023-08-24 19:57:27 +000010665 ASSERT_STREQ("sony", lights[0].name.c_str());
10666 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
10667 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10668 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10669
10670 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10671 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10672 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
10673 ASSERT_STREQ("sony", lights[0].name.c_str());
10674}
10675
10676TEST_F(LightControllerTest, PlayerIdLight) {
10677 RawLightInfo info1 = {.id = 1,
10678 .name = "player-1",
10679 .maxBrightness = 255,
10680 .flags = InputLightClass::BRIGHTNESS,
10681 .path = ""};
10682 RawLightInfo info2 = {.id = 2,
10683 .name = "player-2",
10684 .maxBrightness = 255,
10685 .flags = InputLightClass::BRIGHTNESS,
10686 .path = ""};
10687 RawLightInfo info3 = {.id = 3,
10688 .name = "player-3",
10689 .maxBrightness = 255,
10690 .flags = InputLightClass::BRIGHTNESS,
10691 .path = ""};
10692 RawLightInfo info4 = {.id = 4,
10693 .name = "player-4",
10694 .maxBrightness = 255,
10695 .flags = InputLightClass::BRIGHTNESS,
10696 .path = ""};
10697 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10698 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10699 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10700 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10701
10702 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10703 InputDeviceInfo info;
10704 controller.populateDeviceInfo(&info);
10705 std::vector<InputDeviceLightInfo> lights = info.getLights();
10706 ASSERT_EQ(1U, lights.size());
10707 ASSERT_STREQ("player", lights[0].name.c_str());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010708 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010709 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10710 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010711
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010712 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10713 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10714 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010715}
10716
Michael Wrightd02c5b62014-02-10 15:10:22 -080010717} // namespace android