blob: 8536ff0676d4b4252c8d7fad598886ef2eec612f [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
Harry Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020
Prabir Pradhan2770d242019-09-02 18:07:11 -070021#include <CursorInputMapper.h>
22#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080025#include <InputReaderBase.h>
26#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000027#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <KeyboardInputMapper.h>
29#include <MultiTouchInputMapper.h>
Prabir Pradhan018faea2024-05-08 21:52:54 +000030#include <NotifyArgsBuilders.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070031#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070032#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070033#include <SingleTouchInputMapper.h>
34#include <SwitchInputMapper.h>
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000035#include <TestEventMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070036#include <TestInputListener.h>
37#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080038#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000039#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070040#include <android-base/thread_annotations.h>
Byoungho Jungda10dd32023-10-06 17:03:45 +090041#include <com_android_input_flags.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000042#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080043#include <gtest/gtest.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000044#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080045
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070046#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000047#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000048#include "FakeInputReaderPolicy.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000049#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000050#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000051#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000052#include "input/DisplayViewport.h"
53#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010054
Michael Wrightd02c5b62014-02-10 15:10:22 -080055namespace android {
56
Dominik Laskowski2f01d772022-03-23 16:01:29 -070057using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000058using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070059using std::chrono_literals::operator""ms;
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -080060using std::chrono_literals::operator""s;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070061
Michael Wrightd02c5b62014-02-10 15:10:22 -080062// Arbitrary display properties.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070063static constexpr ui::LogicalDisplayId DISPLAY_ID = ui::LogicalDisplayId::DEFAULT;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000064static const std::string DISPLAY_UNIQUE_ID = "local:1";
Linnan Li13bf76a2024-05-05 19:18:02 +080065static constexpr ui::LogicalDisplayId SECONDARY_DISPLAY_ID =
66 ui::LogicalDisplayId{DISPLAY_ID.val() + 1};
arthurhungcc7f9802020-04-30 17:55:40 +080067static constexpr int32_t DISPLAY_WIDTH = 480;
68static constexpr int32_t DISPLAY_HEIGHT = 800;
Linnan Li13bf76a2024-05-05 19:18:02 +080069static constexpr ui::LogicalDisplayId VIRTUAL_DISPLAY_ID = ui::LogicalDisplayId{1};
arthurhungcc7f9802020-04-30 17:55:40 +080070static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
71static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070072static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070073static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080074
arthurhungcc7f9802020-04-30 17:55:40 +080075static constexpr int32_t FIRST_SLOT = 0;
76static constexpr int32_t SECOND_SLOT = 1;
77static constexpr int32_t THIRD_SLOT = 2;
78static constexpr int32_t INVALID_TRACKING_ID = -1;
79static constexpr int32_t FIRST_TRACKING_ID = 0;
80static constexpr int32_t SECOND_TRACKING_ID = 1;
81static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080082static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
83static constexpr int32_t LIGHT_COLOR = 0x7F448866;
84static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080085
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080086static constexpr int32_t ACTION_POINTER_0_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_0_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90static constexpr int32_t ACTION_POINTER_1_DOWN =
91 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92static constexpr int32_t ACTION_POINTER_1_UP =
93 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
94
Prabir Pradhanb08a0e82023-09-14 22:28:32 +000095static constexpr uint32_t STYLUS_FUSION_SOURCE =
96 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
97
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000098// Minimum timestamp separation between subsequent input events from a Bluetooth device.
99static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +0000100
Byoungho Jungda10dd32023-10-06 17:03:45 +0900101namespace input_flags = com::android::input::flags;
102
Michael Wrightd02c5b62014-02-10 15:10:22 -0800103template<typename T>
104static inline T min(T a, T b) {
105 return a < b ? a : b;
106}
107
108static inline float avg(float x, float y) {
109 return (x + y) / 2;
110}
111
Chris Ye3fdbfef2021-01-06 18:45:18 -0800112// Mapping for light color name and the light color
113const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
114 {"green", LightColor::GREEN},
115 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116
Michael Wrighta9cf4192022-12-01 23:46:39 +0000117static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700118 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000119 case ui::ROTATION_90:
120 return ui::ROTATION_270;
121 case ui::ROTATION_270:
122 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700123 default:
124 return orientation;
125 }
126}
127
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800128static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
129 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000130 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800131
132 const InputDeviceInfo::MotionRange* motionRange =
133 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
134 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
135}
136
137static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
138 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000139 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800140
141 const InputDeviceInfo::MotionRange* motionRange =
142 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
143 ASSERT_EQ(nullptr, motionRange);
144}
145
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700146[[maybe_unused]] static void dumpReader(InputReader& reader) {
147 std::string dump;
148 reader.dump(dump);
149 std::istringstream iss(dump);
150 for (std::string line; std::getline(iss, line);) {
151 ALOGE("%s", line.c_str());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -0800152 std::this_thread::sleep_for(1ms);
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700153 }
154}
155
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156// --- FakeInputMapper ---
157
158class FakeInputMapper : public InputMapper {
159 uint32_t mSources;
160 int32_t mKeyboardType;
161 int32_t mMetaState;
162 KeyedVector<int32_t, int32_t> mKeyCodeStates;
163 KeyedVector<int32_t, int32_t> mScanCodeStates;
164 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100165 // fake mapping which would normally come from keyCharacterMap
166 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800167 std::vector<int32_t> mSupportedKeyCodes;
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700168 std::list<NotifyArgs> mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700170 std::mutex mLock;
171 std::condition_variable mStateChangedCondition;
172 bool mConfigureWasCalled GUARDED_BY(mLock);
173 bool mResetWasCalled GUARDED_BY(mLock);
174 bool mProcessWasCalled GUARDED_BY(mLock);
175 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176
Arthur Hungc23540e2018-11-29 20:42:11 +0800177 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000179 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
180 uint32_t sources)
181 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800182 mSources(sources),
183 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800185 mConfigureWasCalled(false),
186 mResetWasCalled(false),
187 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Chris Yea52ade12020-08-27 16:49:20 -0700189 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
191 void setKeyboardType(int32_t keyboardType) {
192 mKeyboardType = keyboardType;
193 }
194
195 void setMetaState(int32_t metaState) {
196 mMetaState = metaState;
197 }
198
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700199 // Sets the return value for the `process` call.
200 void setProcessResult(std::list<NotifyArgs> notifyArgs) {
201 mProcessResult.clear();
202 for (auto notifyArg : notifyArgs) {
203 mProcessResult.push_back(notifyArg);
204 }
205 }
206
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208 std::unique_lock<std::mutex> lock(mLock);
209 base::ScopedLockAssertion assumeLocked(mLock);
210 const bool configureCalled =
211 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
212 return mConfigureWasCalled;
213 });
214 if (!configureCalled) {
215 FAIL() << "Expected configure() to have been called.";
216 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 mConfigureWasCalled = false;
218 }
219
220 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 std::unique_lock<std::mutex> lock(mLock);
222 base::ScopedLockAssertion assumeLocked(mLock);
223 const bool resetCalled =
224 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
225 return mResetWasCalled;
226 });
227 if (!resetCalled) {
228 FAIL() << "Expected reset() to have been called.";
229 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 mResetWasCalled = false;
231 }
232
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000233 void assertResetWasNotCalled() {
234 std::scoped_lock lock(mLock);
235 ASSERT_FALSE(mResetWasCalled) << "Expected reset to not have been called.";
236 }
237
Yi Kong9b14ac62018-07-17 13:48:38 -0700238 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700239 std::unique_lock<std::mutex> lock(mLock);
240 base::ScopedLockAssertion assumeLocked(mLock);
241 const bool processCalled =
242 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
243 return mProcessWasCalled;
244 });
245 if (!processCalled) {
246 FAIL() << "Expected process() to have been called.";
247 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248 if (outLastEvent) {
249 *outLastEvent = mLastEvent;
250 }
251 mProcessWasCalled = false;
252 }
253
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000254 void assertProcessWasNotCalled() {
255 std::scoped_lock lock(mLock);
256 ASSERT_FALSE(mProcessWasCalled) << "Expected process to not have been called.";
257 }
258
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 void setKeyCodeState(int32_t keyCode, int32_t state) {
260 mKeyCodeStates.replaceValueFor(keyCode, state);
261 }
262
263 void setScanCodeState(int32_t scanCode, int32_t state) {
264 mScanCodeStates.replaceValueFor(scanCode, state);
265 }
266
267 void setSwitchState(int32_t switchCode, int32_t state) {
268 mSwitchStates.replaceValueFor(switchCode, state);
269 }
270
271 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800272 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273 }
274
Philip Junker4af3b3d2021-12-14 10:36:55 +0100275 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
276 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
277 }
278
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100280 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281
Harry Cuttsd02ea102023-03-17 18:21:30 +0000282 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283 InputMapper::populateDeviceInfo(deviceInfo);
284
285 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000286 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 }
288 }
289
Arpit Singhed6c3de2023-04-05 19:24:37 +0000290 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000291 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700292 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800294
295 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800296 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000297 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000298 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800299 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700300
301 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700302 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 }
304
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700305 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700306 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700308 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700309 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310 }
311
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700312 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700313 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314 mLastEvent = *rawEvent;
315 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700316 mStateChangedCondition.notify_all();
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700317 return mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318 }
319
Chris Yea52ade12020-08-27 16:49:20 -0700320 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
322 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
323 }
324
Philip Junker4af3b3d2021-12-14 10:36:55 +0100325 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
326 auto it = mKeyCodeMapping.find(locationKeyCode);
327 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
328 }
329
Chris Yea52ade12020-08-27 16:49:20 -0700330 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
332 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
333 }
334
Chris Yea52ade12020-08-27 16:49:20 -0700335 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336 ssize_t index = mSwitchStates.indexOfKey(switchCode);
337 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
338 }
339
Chris Yea52ade12020-08-27 16:49:20 -0700340 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700341 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700342 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700343 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
345 if (keyCodes[i] == mSupportedKeyCodes[j]) {
346 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 }
348 }
349 }
Chris Yea52ade12020-08-27 16:49:20 -0700350 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 return result;
352 }
353
354 virtual int32_t getMetaState() {
355 return mMetaState;
356 }
357
358 virtual void fadePointer() {
359 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800360
Linnan Li13bf76a2024-05-05 19:18:02 +0800361 virtual std::optional<ui::LogicalDisplayId> getAssociatedDisplay() {
Arthur Hungc23540e2018-11-29 20:42:11 +0800362 if (mViewport) {
363 return std::make_optional(mViewport->displayId);
364 }
365 return std::nullopt;
366 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367};
368
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700369// --- InputReaderPolicyTest ---
370class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700371protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700372 sp<FakeInputReaderPolicy> mFakePolicy;
373
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700374 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700375 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700376};
377
378/**
379 * Check that empty set of viewports is an acceptable configuration.
380 * Also try to get internal viewport two different ways - by type and by uniqueId.
381 *
382 * There will be confusion if two viewports with empty uniqueId and identical type are present.
383 * Such configuration is not currently allowed.
384 */
385TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700386 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700387
388 // We didn't add any viewports yet, so there shouldn't be any.
389 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100390 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700391 ASSERT_FALSE(internalViewport);
392
393 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000394 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000395 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700396
397 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700398 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700399 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100400 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700401
402 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100403 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700404 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700405 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700406
407 mFakePolicy->clearViewports();
408 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700409 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700410 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100411 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700412 ASSERT_FALSE(internalViewport);
413}
414
415TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
416 const std::string internalUniqueId = "local:0";
417 const std::string externalUniqueId = "local:1";
418 const std::string virtualUniqueId1 = "virtual:2";
419 const std::string virtualUniqueId2 = "virtual:3";
Linnan Li13bf76a2024-05-05 19:18:02 +0800420 constexpr ui::LogicalDisplayId virtualDisplayId1 = ui::LogicalDisplayId{2};
421 constexpr ui::LogicalDisplayId virtualDisplayId2 = ui::LogicalDisplayId{3};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700422
423 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000424 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000425 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000426 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700427 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000428 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000429 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000430 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700431 // Add an virtual viewport
432 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000433 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000434 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700435 // Add another virtual viewport
436 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000437 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000438 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700439
440 // Check matching by type for internal
441 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100442 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700443 ASSERT_TRUE(internalViewport);
444 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
445
446 // Check matching by type for external
447 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100448 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700449 ASSERT_TRUE(externalViewport);
450 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
451
452 // Check matching by uniqueId for virtual viewport #1
453 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700454 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700455 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100456 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700457 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
458 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
459
460 // Check matching by uniqueId for virtual viewport #2
461 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700462 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700463 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100464 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700465 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
466 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
467}
468
469
470/**
471 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
472 * that lookup works by checking display id.
473 * Check that 2 viewports of each kind is possible, for all existing viewport types.
474 */
475TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
476 const std::string uniqueId1 = "uniqueId1";
477 const std::string uniqueId2 = "uniqueId2";
Linnan Li13bf76a2024-05-05 19:18:02 +0800478 constexpr ui::LogicalDisplayId displayId1 = ui::LogicalDisplayId{2};
479 constexpr ui::LogicalDisplayId displayId2 = ui::LogicalDisplayId{3};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700480
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100481 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
482 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700483 for (const ViewportType& type : types) {
484 mFakePolicy->clearViewports();
485 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000486 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000487 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700488 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000489 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000490 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700491
492 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700493 std::optional<DisplayViewport> viewport1 =
494 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700495 ASSERT_TRUE(viewport1);
496 ASSERT_EQ(displayId1, viewport1->displayId);
497 ASSERT_EQ(type, viewport1->type);
498
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700499 std::optional<DisplayViewport> viewport2 =
500 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700501 ASSERT_TRUE(viewport2);
502 ASSERT_EQ(displayId2, viewport2->displayId);
503 ASSERT_EQ(type, viewport2->type);
504
505 // When there are multiple viewports of the same kind, and uniqueId is not specified
506 // in the call to getDisplayViewport, then that situation is not supported.
507 // The viewports can be stored in any order, so we cannot rely on the order, since that
508 // is just implementation detail.
509 // However, we can check that it still returns *a* viewport, we just cannot assert
510 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700511 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700512 ASSERT_TRUE(someViewport);
513 }
514}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800515
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700516/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000517 * When we have multiple internal displays make sure we always return the default display when
518 * querying by type.
519 */
520TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
521 const std::string uniqueId1 = "uniqueId1";
522 const std::string uniqueId2 = "uniqueId2";
Linnan Li13bf76a2024-05-05 19:18:02 +0800523 constexpr ui::LogicalDisplayId nonDefaultDisplayId = ui::LogicalDisplayId{2};
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700524 ASSERT_NE(nonDefaultDisplayId, ui::LogicalDisplayId::DEFAULT)
525 << "Test display ID should not be ui::LogicalDisplayId::DEFAULT ";
Michael Wrightdde67b82020-10-27 16:09:22 +0000526
527 // Add the default display first and ensure it gets returned.
528 mFakePolicy->clearViewports();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700529 mFakePolicy->addDisplayViewport(ui::LogicalDisplayId::DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000530 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000531 ViewportType::INTERNAL);
532 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000533 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000534 ViewportType::INTERNAL);
535
536 std::optional<DisplayViewport> viewport =
537 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
538 ASSERT_TRUE(viewport);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700539 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, viewport->displayId);
Michael Wrightdde67b82020-10-27 16:09:22 +0000540 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
541
542 // Add the default display second to make sure order doesn't matter.
543 mFakePolicy->clearViewports();
544 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000545 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000546 ViewportType::INTERNAL);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700547 mFakePolicy->addDisplayViewport(ui::LogicalDisplayId::DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000548 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000549 ViewportType::INTERNAL);
550
551 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
552 ASSERT_TRUE(viewport);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700553 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, viewport->displayId);
Michael Wrightdde67b82020-10-27 16:09:22 +0000554 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
555}
556
557/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700558 * Check getDisplayViewportByPort
559 */
560TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100561 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700562 const std::string uniqueId1 = "uniqueId1";
563 const std::string uniqueId2 = "uniqueId2";
Linnan Li13bf76a2024-05-05 19:18:02 +0800564 constexpr ui::LogicalDisplayId displayId1 = ui::LogicalDisplayId{1};
565 constexpr ui::LogicalDisplayId displayId2 = ui::LogicalDisplayId{2};
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700566 const uint8_t hdmi1 = 0;
567 const uint8_t hdmi2 = 1;
568 const uint8_t hdmi3 = 2;
569
570 mFakePolicy->clearViewports();
571 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000572 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000573 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700574 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000575 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000576 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700577
578 // Check that correct display viewport was returned by comparing the display ports.
579 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
580 ASSERT_TRUE(hdmi1Viewport);
581 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
582 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
583
584 // Check that we can still get the same viewport using the uniqueId
585 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
586 ASSERT_TRUE(hdmi1Viewport);
587 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
588 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
589 ASSERT_EQ(type, hdmi1Viewport->type);
590
591 // Check that we cannot find a port with "HDMI2", because we never added one
592 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
593 ASSERT_FALSE(hdmi2Viewport);
594}
595
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596// --- InputReaderTest ---
597
598class InputReaderTest : public testing::Test {
599protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700600 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700602 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000603 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604
Chris Yea52ade12020-08-27 16:49:20 -0700605 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700606 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700607 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700608 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609
Prabir Pradhan28efc192019-11-05 01:10:04 +0000610 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700611 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 }
613
Chris Yea52ade12020-08-27 16:49:20 -0700614 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700615 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 }
618
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700619 void addDevice(int32_t eventHubId, const std::string& name,
620 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622
623 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800624 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800625 }
626 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000627 mReader->loopOnce();
628 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700629 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700631 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 }
633
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800634 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700635 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000636 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700637 }
638
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800639 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700640 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000641 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700642 }
643
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800644 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700645 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700646 ftl::Flags<InputDeviceClass> classes,
647 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800648 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800649 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000650 FakeInputMapper& mapper =
651 device->addMapper<FakeInputMapper>(eventHubId,
652 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800653 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800654 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 return mapper;
656 }
657};
658
Chris Ye98d3f532020-10-01 21:48:59 -0700659TEST_F(InputReaderTest, PolicyGetInputDevices) {
660 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700661 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700662 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663
664 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700665 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800667 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100668 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
670 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000671 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672}
673
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000674TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
675 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
676 mFakeEventHub->setSysfsRootPath(1, "xyz");
677
678 // Should also have received a notification describing the new input device.
679 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
680 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
681 ASSERT_EQ(0U, inputDevice.getLights().size());
682
683 RawLightInfo infoMonolight = {.id = 123,
684 .name = "mono_keyboard_backlight",
685 .maxBrightness = 255,
686 .flags = InputLightClass::BRIGHTNESS,
687 .path = ""};
688 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
689 mReader->sysfsNodeChanged("xyz");
690 mReader->loopOnce();
691
692 // Should also have received a notification describing the new recreated input device.
693 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
694 inputDevice = mFakePolicy->getInputDevices()[0];
695 ASSERT_EQ(1U, inputDevice.getLights().size());
696}
697
Chris Yee7310032020-09-22 15:36:28 -0700698TEST_F(InputReaderTest, GetMergedInputDevices) {
699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
700 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
701 // Add two subdevices to device
702 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
703 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000704 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
705 AINPUT_SOURCE_KEYBOARD);
706 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
707 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700708
709 // Push same device instance for next device to be added, so they'll have same identifier.
710 mReader->pushNextDevice(device);
711 mReader->pushNextDevice(device);
712 ASSERT_NO_FATAL_FAILURE(
713 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
714 ASSERT_NO_FATAL_FAILURE(
715 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
716
717 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000718 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700719}
720
Chris Yee14523a2020-12-19 13:46:00 -0800721TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
722 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
723 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
724 // Add two subdevices to device
725 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
726 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000727 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
728 AINPUT_SOURCE_KEYBOARD);
729 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
730 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800731
732 // Push same device instance for next device to be added, so they'll have same identifier.
733 mReader->pushNextDevice(device);
734 mReader->pushNextDevice(device);
735 // Sensor device is initially disabled
736 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
737 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
738 nullptr));
739 // Device is disabled because the only sub device is a sensor device and disabled initially.
740 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
741 ASSERT_FALSE(device->isEnabled());
742 ASSERT_NO_FATAL_FAILURE(
743 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
744 // The merged device is enabled if any sub device is enabled
745 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
746 ASSERT_TRUE(device->isEnabled());
747}
748
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700749TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800750 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700751 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800752 constexpr int32_t eventHubId = 1;
753 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700754 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000755 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
756 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800757 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800758 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700759
Yi Kong9b14ac62018-07-17 13:48:38 -0700760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700761
762 NotifyDeviceResetArgs resetArgs;
763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700764 ASSERT_EQ(deviceId, resetArgs.deviceId);
765
766 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800767 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000768 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700769
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700771 ASSERT_EQ(deviceId, resetArgs.deviceId);
772 ASSERT_EQ(device->isEnabled(), false);
773
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800774 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000775 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700778 ASSERT_EQ(device->isEnabled(), false);
779
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800780 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000781 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700783 ASSERT_EQ(deviceId, resetArgs.deviceId);
784 ASSERT_EQ(device->isEnabled(), true);
785}
786
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800788 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700789 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800790 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800791 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800792 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800793 AINPUT_SOURCE_KEYBOARD, nullptr);
794 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795
796 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
797 AINPUT_SOURCE_ANY, AKEYCODE_A))
798 << "Should return unknown when the device id is >= 0 but unknown.";
799
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800800 ASSERT_EQ(AKEY_STATE_UNKNOWN,
801 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
802 << "Should return unknown when the device id is valid but the sources are not "
803 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800805 ASSERT_EQ(AKEY_STATE_DOWN,
806 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
807 AKEYCODE_A))
808 << "Should return value provided by mapper when device id is valid and the device "
809 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810
811 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
812 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
813 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
814
815 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
816 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
817 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
818}
819
Philip Junker4af3b3d2021-12-14 10:36:55 +0100820TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
821 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
822 constexpr int32_t eventHubId = 1;
823 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
824 InputDeviceClass::KEYBOARD,
825 AINPUT_SOURCE_KEYBOARD, nullptr);
826 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
827
828 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
829 << "Should return unknown when the device with the specified id is not found.";
830
831 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
832 << "Should return correct mapping when device id is valid and mapping exists.";
833
834 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
835 << "Should return the location key code when device id is valid and there's no "
836 "mapping.";
837}
838
839TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
840 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
841 constexpr int32_t eventHubId = 1;
842 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
843 InputDeviceClass::JOYSTICK,
844 AINPUT_SOURCE_GAMEPAD, nullptr);
845 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
846
847 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
848 << "Should return unknown when the device id is valid but there is no keyboard mapper";
849}
850
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800852 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700853 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800854 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800855 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800856 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800857 AINPUT_SOURCE_KEYBOARD, nullptr);
858 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800859
860 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
861 AINPUT_SOURCE_ANY, KEY_A))
862 << "Should return unknown when the device id is >= 0 but unknown.";
863
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 ASSERT_EQ(AKEY_STATE_UNKNOWN,
865 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
866 << "Should return unknown when the device id is valid but the sources are not "
867 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800869 ASSERT_EQ(AKEY_STATE_DOWN,
870 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
871 KEY_A))
872 << "Should return value provided by mapper when device id is valid and the device "
873 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800874
875 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
876 AINPUT_SOURCE_TRACKBALL, KEY_A))
877 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
878
879 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
880 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
881 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
882}
883
884TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800885 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700886 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800887 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800888 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800889 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800890 AINPUT_SOURCE_KEYBOARD, nullptr);
891 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892
893 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
894 AINPUT_SOURCE_ANY, SW_LID))
895 << "Should return unknown when the device id is >= 0 but unknown.";
896
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800897 ASSERT_EQ(AKEY_STATE_UNKNOWN,
898 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
899 << "Should return unknown when the device id is valid but the sources are not "
900 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800902 ASSERT_EQ(AKEY_STATE_DOWN,
903 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
904 SW_LID))
905 << "Should return value provided by mapper when device id is valid and the device "
906 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907
908 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
909 AINPUT_SOURCE_TRACKBALL, SW_LID))
910 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
911
912 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
913 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
914 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
915}
916
917TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800918 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700919 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800920 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800921 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800922 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800923 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100924
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800925 mapper.addSupportedKeyCode(AKEYCODE_A);
926 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700928 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 uint8_t flags[4] = { 0, 0, 0, 1 };
930
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700931 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932 << "Should return false when device id is >= 0 but unknown.";
933 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
934
935 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700936 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800937 << "Should return false when device id is valid but the sources are not supported by "
938 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
940
941 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700942 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800943 keyCodes, flags))
944 << "Should return value provided by mapper when device id is valid and the device "
945 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
947
948 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700949 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
950 << "Should return false when the device id is < 0 but the sources are not supported by "
951 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
953
954 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700955 ASSERT_TRUE(
956 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
957 << "Should return value provided by mapper when device id is < 0 and one of the "
958 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
960}
961
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000962TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800963 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700964 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
966 NotifyConfigurationChangedArgs args;
967
968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
969 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
970}
971
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000972TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800973 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700974 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000975 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800976 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000977 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800978 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800979 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800980 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000982 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000983 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
985
986 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800987 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000988 ASSERT_EQ(when, event.when);
989 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800990 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800991 ASSERT_EQ(EV_KEY, event.type);
992 ASSERT_EQ(KEY_A, event.code);
993 ASSERT_EQ(1, event.value);
994}
995
Garfield Tan1c7bc862020-01-28 13:24:04 -0800996TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800997 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700998 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800999 constexpr int32_t eventHubId = 1;
1000 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001001 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001002 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1003 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001004 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001005 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001006
1007 NotifyDeviceResetArgs resetArgs;
1008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001009 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001011 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001012 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001014 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001015 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001016
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001017 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001018 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001020 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001021 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001023 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001024 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001026 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001027 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001028}
1029
Garfield Tan1c7bc862020-01-28 13:24:04 -08001030TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1031 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001032 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001033 constexpr int32_t eventHubId = 1;
1034 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1035 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001036 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1037 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001038 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001039 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1040
1041 NotifyDeviceResetArgs resetArgs;
1042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1043 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1044}
1045
Arthur Hungc23540e2018-11-29 20:42:11 +08001046TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001047 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001048 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001049 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001050 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001051 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1052 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001053 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1054 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001055 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001056
1057 const uint8_t hdmi1 = 1;
1058
1059 // Associated touch screen with second display.
1060 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1061
1062 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001063 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001064 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001065 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001066 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001067 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001068 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001069 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001070 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001071
1072 // Add the device, and make sure all of the callbacks are triggered.
1073 // The device is added after the input port associations are processed since
1074 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001075 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001078 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001079
Arthur Hung2c9a3342019-07-23 14:18:59 +08001080 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001081 ASSERT_EQ(deviceId, device->getId());
1082 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1083 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001084
1085 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001086 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001087 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001088 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001089}
1090
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001091TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1092 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001093 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001094 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1095 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1096 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001097 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1098 AINPUT_SOURCE_KEYBOARD);
1099 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1100 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001101 mReader->pushNextDevice(device);
1102 mReader->pushNextDevice(device);
1103 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1104 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1105
1106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1107
1108 NotifyDeviceResetArgs resetArgs;
1109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1110 ASSERT_EQ(deviceId, resetArgs.deviceId);
1111 ASSERT_TRUE(device->isEnabled());
1112 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1113 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1114
1115 disableDevice(deviceId);
1116 mReader->loopOnce();
1117
1118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1119 ASSERT_EQ(deviceId, resetArgs.deviceId);
1120 ASSERT_FALSE(device->isEnabled());
1121 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1122 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1123
1124 enableDevice(deviceId);
1125 mReader->loopOnce();
1126
1127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1128 ASSERT_EQ(deviceId, resetArgs.deviceId);
1129 ASSERT_TRUE(device->isEnabled());
1130 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1131 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1132}
1133
1134TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1135 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001136 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001137 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1138 // Add two subdevices to device
1139 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1140 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001141 device->addMapper<FakeInputMapper>(eventHubIds[0],
1142 mFakePolicy->getReaderConfiguration(),
1143 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001144 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001145 device->addMapper<FakeInputMapper>(eventHubIds[1],
1146 mFakePolicy->getReaderConfiguration(),
1147 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001148 mReader->pushNextDevice(device);
1149 mReader->pushNextDevice(device);
1150 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1151 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1152
1153 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1154 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1155
1156 ASSERT_EQ(AKEY_STATE_DOWN,
1157 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1158 ASSERT_EQ(AKEY_STATE_DOWN,
1159 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1160 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1161 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1162}
1163
Prabir Pradhan7e186182020-11-10 13:56:45 -08001164TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1165 NotifyPointerCaptureChangedArgs args;
1166
Hiroki Sato25040232024-02-22 17:21:22 +09001167 auto request = mFakePolicy->setPointerCapture(/*window=*/sp<BBinder>::make());
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001168 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001169 mReader->loopOnce();
1170 mFakeListener->assertNotifyCaptureWasCalled(&args);
Hiroki Sato25040232024-02-22 17:21:22 +09001171 ASSERT_TRUE(args.request.isEnable()) << "Pointer Capture should be enabled.";
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001172 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001173
Hiroki Sato25040232024-02-22 17:21:22 +09001174 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001175 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001176 mReader->loopOnce();
1177 mFakeListener->assertNotifyCaptureWasCalled(&args);
Hiroki Sato25040232024-02-22 17:21:22 +09001178 ASSERT_FALSE(args.request.isEnable()) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001179
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001180 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001181 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001182 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001183 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001184 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001185}
1186
Prabir Pradhan018faea2024-05-08 21:52:54 +00001187TEST_F(InputReaderTest, GetLastUsedInputDeviceId) {
1188 constexpr int32_t FIRST_DEVICE_ID = END_RESERVED_ID + 1000;
1189 constexpr int32_t SECOND_DEVICE_ID = FIRST_DEVICE_ID + 1;
1190 FakeInputMapper& firstMapper =
1191 addDeviceWithFakeInputMapper(FIRST_DEVICE_ID, FIRST_DEVICE_ID, "first",
1192 InputDeviceClass::KEYBOARD, AINPUT_SOURCE_KEYBOARD,
1193 /*configuration=*/nullptr);
1194 FakeInputMapper& secondMapper =
1195 addDeviceWithFakeInputMapper(SECOND_DEVICE_ID, SECOND_DEVICE_ID, "second",
1196 InputDeviceClass::TOUCH_MT, AINPUT_SOURCE_STYLUS,
1197 /*configuration=*/nullptr);
1198
1199 ASSERT_EQ(ReservedInputDeviceId::INVALID_INPUT_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1200
1201 // Start a new key gesture from the first device
1202 firstMapper.setProcessResult({KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
1203 .deviceId(FIRST_DEVICE_ID)
1204 .build()});
1205 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, FIRST_DEVICE_ID, 0, 0, 0);
1206 mReader->loopOnce();
1207 ASSERT_EQ(firstMapper.getDeviceId(), mReader->getLastUsedInputDeviceId());
1208
1209 // Start a new touch gesture from the second device
1210 secondMapper.setProcessResult(
1211 {MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS)
1212 .deviceId(SECOND_DEVICE_ID)
1213 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER))
1214 .build()});
1215 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1216 mReader->loopOnce();
1217 ASSERT_EQ(SECOND_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1218
1219 // Releasing the key is not a new gesture, so it does not update the last used device
1220 firstMapper.setProcessResult({KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD)
1221 .deviceId(FIRST_DEVICE_ID)
1222 .build()});
1223 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, FIRST_DEVICE_ID, 0, 0, 0);
1224 mReader->loopOnce();
1225 ASSERT_EQ(SECOND_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1226
1227 // But pressing a new key does start a new gesture
1228 firstMapper.setProcessResult({KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
1229 .deviceId(FIRST_DEVICE_ID)
1230 .build()});
1231 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, FIRST_DEVICE_ID, 0, 0, 0);
1232 mReader->loopOnce();
1233 ASSERT_EQ(FIRST_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1234
1235 // Moving or ending a touch gesture does not update the last used device
1236 secondMapper.setProcessResult(
1237 {MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
1238 .deviceId(SECOND_DEVICE_ID)
1239 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS))
1240 .build()});
1241 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1242 mReader->loopOnce();
1243 ASSERT_EQ(FIRST_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1244 secondMapper.setProcessResult({MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
1245 .deviceId(SECOND_DEVICE_ID)
1246 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS))
1247 .build()});
1248 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1249 mReader->loopOnce();
1250 ASSERT_EQ(FIRST_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1251
1252 // Starting a new hover gesture updates the last used device
1253 secondMapper.setProcessResult(
1254 {MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
1255 .deviceId(SECOND_DEVICE_ID)
1256 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS))
1257 .build()});
1258 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1259 mReader->loopOnce();
1260 ASSERT_EQ(SECOND_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1261}
1262
Chris Ye87143712020-11-10 05:05:58 +00001263class FakeVibratorInputMapper : public FakeInputMapper {
1264public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001265 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1266 const InputReaderConfiguration& readerConfig, uint32_t sources)
1267 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001268
1269 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1270};
1271
1272TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1273 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001274 ftl::Flags<InputDeviceClass> deviceClass =
1275 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001276 constexpr int32_t eventHubId = 1;
1277 const char* DEVICE_LOCATION = "BLUETOOTH";
1278 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1279 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001280 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1281 mFakePolicy->getReaderConfiguration(),
1282 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001283 mReader->pushNextDevice(device);
1284
1285 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1286 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1287
1288 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1289 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1290}
1291
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001292// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001293
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001294class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001295public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001296 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001297
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001298 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001299
Andy Chenf9f1a022022-08-29 20:07:10 -04001300 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1301
Chris Yee2b1e5c2021-03-10 22:45:12 -08001302 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1303
1304 void dump(std::string& dump) override {}
1305
1306 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1307 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001308 }
1309
Chris Yee2b1e5c2021-03-10 22:45:12 -08001310 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1311 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001312 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001313
1314 bool setLightColor(int32_t lightId, int32_t color) override {
1315 getDeviceContext().setLightBrightness(lightId, color >> 24);
1316 return true;
1317 }
1318
1319 std::optional<int32_t> getLightColor(int32_t lightId) override {
1320 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1321 if (!result.has_value()) {
1322 return std::nullopt;
1323 }
1324 return result.value() << 24;
1325 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001326
1327 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1328
1329 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1330
1331private:
1332 InputDeviceContext& mDeviceContext;
1333 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1334 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001335 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001336};
1337
Chris Yee2b1e5c2021-03-10 22:45:12 -08001338TEST_F(InputReaderTest, BatteryGetCapacity) {
1339 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001340 ftl::Flags<InputDeviceClass> deviceClass =
1341 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001342 constexpr int32_t eventHubId = 1;
1343 const char* DEVICE_LOCATION = "BLUETOOTH";
1344 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001345 FakePeripheralController& controller =
1346 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001347 mReader->pushNextDevice(device);
1348
1349 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1350
Harry Cuttsa5b71292022-11-28 12:56:17 +00001351 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1352 FakeEventHub::BATTERY_CAPACITY);
1353 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001354}
1355
1356TEST_F(InputReaderTest, BatteryGetStatus) {
1357 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001358 ftl::Flags<InputDeviceClass> deviceClass =
1359 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001360 constexpr int32_t eventHubId = 1;
1361 const char* DEVICE_LOCATION = "BLUETOOTH";
1362 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001363 FakePeripheralController& controller =
1364 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001365 mReader->pushNextDevice(device);
1366
1367 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1368
Harry Cuttsa5b71292022-11-28 12:56:17 +00001369 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1370 FakeEventHub::BATTERY_STATUS);
1371 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001372}
1373
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001374TEST_F(InputReaderTest, BatteryGetDevicePath) {
1375 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1376 ftl::Flags<InputDeviceClass> deviceClass =
1377 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1378 constexpr int32_t eventHubId = 1;
1379 const char* DEVICE_LOCATION = "BLUETOOTH";
1380 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1381 device->addController<FakePeripheralController>(eventHubId);
1382 mReader->pushNextDevice(device);
1383
1384 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1385
Harry Cuttsa5b71292022-11-28 12:56:17 +00001386 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001387}
1388
Chris Ye3fdbfef2021-01-06 18:45:18 -08001389TEST_F(InputReaderTest, LightGetColor) {
1390 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001391 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001392 constexpr int32_t eventHubId = 1;
1393 const char* DEVICE_LOCATION = "BLUETOOTH";
1394 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001395 FakePeripheralController& controller =
1396 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001397 mReader->pushNextDevice(device);
1398 RawLightInfo info = {.id = 1,
1399 .name = "Mono",
1400 .maxBrightness = 255,
1401 .flags = InputLightClass::BRIGHTNESS,
1402 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001403 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1404 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001405
1406 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001407
Harry Cutts33476232023-01-30 19:57:29 +00001408 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1409 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1410 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1411 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001412}
1413
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001414// --- InputReaderIntegrationTest ---
1415
1416// These tests create and interact with the InputReader only through its interface.
1417// The InputReader is started during SetUp(), which starts its processing in its own
1418// thread. The tests use linux uinput to emulate input devices.
1419// NOTE: Interacting with the physical device while these tests are running may cause
1420// the tests to fail.
1421class InputReaderIntegrationTest : public testing::Test {
1422protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001423 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001424 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001425 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001426
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001427 constexpr static auto EVENT_HAPPENED_TIMEOUT = 2000ms;
1428 constexpr static auto EVENT_DID_NOT_HAPPEN_TIMEOUT = 30ms;
1429
Chris Yea52ade12020-08-27 16:49:20 -07001430 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001431#if !defined(__ANDROID__)
1432 GTEST_SKIP();
1433#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001434 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001435
Arpit Singh440bf652023-08-09 09:23:43 +00001436 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001437 }
1438
Chris Yea52ade12020-08-27 16:49:20 -07001439 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001440#if !defined(__ANDROID__)
1441 return;
1442#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001443 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001444 mReader.reset();
1445 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001446 mFakePolicy.clear();
1447 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001448
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001449 std::optional<InputDeviceInfo> waitForDevice(const std::string& deviceName) {
1450 std::chrono::time_point start = std::chrono::steady_clock::now();
1451 while (true) {
1452 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1453 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1454 [&deviceName](const InputDeviceInfo& info) {
1455 return info.getIdentifier().name == deviceName;
1456 });
1457 if (it != inputDevices.end()) {
1458 return std::make_optional(*it);
1459 }
1460 std::this_thread::sleep_for(1ms);
1461 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
1462 if (elapsed > 5s) {
1463 return {};
1464 }
1465 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001466 }
Arpit Singh440bf652023-08-09 09:23:43 +00001467
1468 void setupInputReader() {
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001469 mTestListener = std::make_unique<TestInputListener>(EVENT_HAPPENED_TIMEOUT,
1470 EVENT_DID_NOT_HAPPEN_TIMEOUT);
Arpit Singh440bf652023-08-09 09:23:43 +00001471
1472 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1473 *mTestListener);
1474 ASSERT_EQ(mReader->start(), OK);
1475
1476 // Since this test is run on a real device, all the input devices connected
1477 // to the test device will show up in mReader. We wait for those input devices to
1478 // show up before beginning the tests.
1479 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1480 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1481 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1482 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001483};
1484
1485TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1486 // An invalid input device that is only used for this test.
1487 class InvalidUinputDevice : public UinputDevice {
1488 public:
Harry Cutts33476232023-01-30 19:57:29 +00001489 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001490
1491 private:
1492 void configureDevice(int fd, uinput_user_dev* device) override {}
1493 };
1494
1495 const size_t numDevices = mFakePolicy->getInputDevices().size();
1496
1497 // UinputDevice does not set any event or key bits, so InputReader should not
1498 // consider it as a valid device.
1499 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1500 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1501 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1502 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1503
1504 invalidDevice.reset();
1505 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1506 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1507 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1508}
1509
1510TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1511 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1512
1513 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1514 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1515 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1516 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1517
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001518 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001519 ASSERT_TRUE(device.has_value());
1520 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1521 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1522 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001523
1524 keyboard.reset();
1525 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1526 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1527 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1528}
1529
1530TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1531 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1532 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1533
1534 NotifyConfigurationChangedArgs configChangedArgs;
1535 ASSERT_NO_FATAL_FAILURE(
1536 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001537 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001538 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1539
1540 NotifyKeyArgs keyArgs;
1541 keyboard->pressAndReleaseHomeKey();
1542 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1543 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001544 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001545 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001546 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001547 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001548 prevTimestamp = keyArgs.eventTime;
1549
1550 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1551 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001552 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001553 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001554 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001555}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001556
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001557TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1558 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1559 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1560
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001561 const auto device = waitForDevice(stylus->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001562 ASSERT_TRUE(device.has_value());
1563
Prabir Pradhana3621852022-10-14 18:57:23 +00001564 // An external stylus with buttons should also be recognized as a keyboard.
1565 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001566 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1567 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1568
1569 const auto DOWN =
1570 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1571 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1572
1573 stylus->pressAndReleaseKey(BTN_STYLUS);
1574 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1575 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1576 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1577 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1578
1579 stylus->pressAndReleaseKey(BTN_STYLUS2);
1580 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1581 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1582 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1583 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1584
1585 stylus->pressAndReleaseKey(BTN_STYLUS3);
1586 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1587 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1588 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1589 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1590}
1591
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001592TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1593 std::unique_ptr<UinputKeyboard> keyboard =
1594 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1595 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1596 KEY_R, KEY_T, KEY_Y,
1597 BTN_STYLUS, BTN_STYLUS2,
1598 BTN_STYLUS3});
1599 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1600
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001601 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001602 ASSERT_TRUE(device.has_value());
1603
1604 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1605 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1606 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1607 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1608}
1609
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001610TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1611 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1612 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1613 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1614 createUinputDevice<UinputKeyboardWithHidUsage>(
1615 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1616 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1617
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001618 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001619 ASSERT_TRUE(device.has_value());
1620
1621 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1622 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1623
1624 // If a device supports reporting HID usage codes, it shouldn't automatically support
1625 // stylus keys.
1626 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1627 uint8_t outFlags[] = {0};
1628 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1629 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1630}
1631
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001632/**
1633 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1634 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1635 * are passed to the listener.
1636 */
1637static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1638TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1639 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1640 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1641 NotifyKeyArgs keyArgs;
1642
1643 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1644 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1645 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1646 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1647
1648 controller->pressAndReleaseKey(BTN_GEAR_UP);
1649 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1650 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1651 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1652}
1653
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001654// --- TouchIntegrationTest ---
1655
Arpit Singh440bf652023-08-09 09:23:43 +00001656class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001657protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001658 const std::string UNIQUE_ID = "local:0";
1659
Chris Yea52ade12020-08-27 16:49:20 -07001660 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001661#if !defined(__ANDROID__)
1662 GTEST_SKIP();
1663#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001664 InputReaderIntegrationTest::SetUp();
1665 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001666 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1667 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001668
1669 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1670 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1671 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001672 const auto info = waitForDevice(mDevice->getName());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001673 ASSERT_TRUE(info);
1674 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001675 }
1676
Linnan Li13bf76a2024-05-05 19:18:02 +08001677 void setDisplayInfoAndReconfigure(ui::LogicalDisplayId displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001678 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001679 std::optional<uint8_t> physicalPort,
1680 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001681 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001682 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001683 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001684 }
1685
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001686 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1687 NotifyMotionArgs args;
1688 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1689 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001690 ASSERT_EQ(points.size(), args.getPointerCount());
1691 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001692 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1693 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1694 }
1695 }
1696
Arthur Hungaab25622020-01-16 11:22:11 +08001697 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001698 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001699};
1700
Arpit Singh440bf652023-08-09 09:23:43 +00001701enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1702
1703class TouchIntegrationTest : public BaseTouchIntegrationTest,
1704 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1705protected:
1706 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1707 const std::string INPUT_PORT = "uinput_touch/input0";
1708
1709 void SetUp() override {
1710#if !defined(__ANDROID__)
1711 GTEST_SKIP();
1712#endif
1713 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1714 BaseTouchIntegrationTest::SetUp();
1715 return;
1716 }
1717
1718 // setup policy with a input-port or UniqueId association to the display
1719 bool isInputPortAssociation =
1720 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1721
1722 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1723 if (isInputPortAssociation) {
1724 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1725 } else {
1726 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1727 }
Arpit Singh440bf652023-08-09 09:23:43 +00001728
1729 InputReaderIntegrationTest::setupInputReader();
1730
1731 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1732 INPUT_PORT);
1733 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1734
1735 // Add a display linked to a physical port or UniqueId.
1736 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1737 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1738 ViewportType::INTERNAL);
1739 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1740 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001741 const auto info = waitForDevice(mDevice->getName());
Arpit Singh440bf652023-08-09 09:23:43 +00001742 ASSERT_TRUE(info);
1743 mDeviceInfo = *info;
1744 }
1745};
1746
1747TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001748 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1749 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1750 // presses).
1751 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1752 mDeviceInfo.getSources());
1753}
1754
Arpit Singh440bf652023-08-09 09:23:43 +00001755TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001756 NotifyMotionArgs args;
1757 const Point centerPoint = mDevice->getCenterPoint();
1758
1759 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001760 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001761 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001762 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001763 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1764 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1765
1766 // ACTION_MOVE
1767 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001768 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001769 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1770 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1771
1772 // ACTION_UP
1773 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001774 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001775 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1776 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1777}
1778
Arpit Singh440bf652023-08-09 09:23:43 +00001779TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001780 NotifyMotionArgs args;
1781 const Point centerPoint = mDevice->getCenterPoint();
1782
1783 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001784 mDevice->sendSlot(FIRST_SLOT);
1785 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001786 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001787 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001788 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1789 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1790
1791 // ACTION_POINTER_DOWN (Second slot)
1792 const Point secondPoint = centerPoint + Point(100, 100);
1793 mDevice->sendSlot(SECOND_SLOT);
1794 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001795 mDevice->sendDown(secondPoint);
1796 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001797 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001798 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001799
1800 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001801 mDevice->sendMove(secondPoint + Point(1, 1));
1802 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001803 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1805
1806 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001807 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001808 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001809 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001810 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001811
1812 // ACTION_UP
1813 mDevice->sendSlot(FIRST_SLOT);
1814 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001815 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001816 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1817 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1818}
1819
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001820/**
1821 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1822 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1823 * data?
1824 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1825 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1826 * for Pointer 0 only is generated after.
1827 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1828 * events, we will not miss any information.
1829 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1830 * event generated afterwards that contains the newest movement of pointer 0.
1831 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1832 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1833 * losing information about non-palm pointers.
1834 */
Arpit Singh440bf652023-08-09 09:23:43 +00001835TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001836 NotifyMotionArgs args;
1837 const Point centerPoint = mDevice->getCenterPoint();
1838
1839 // ACTION_DOWN
1840 mDevice->sendSlot(FIRST_SLOT);
1841 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1842 mDevice->sendDown(centerPoint);
1843 mDevice->sendSync();
1844 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1845
1846 // ACTION_POINTER_DOWN (Second slot)
1847 const Point secondPoint = centerPoint + Point(100, 100);
1848 mDevice->sendSlot(SECOND_SLOT);
1849 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1850 mDevice->sendDown(secondPoint);
1851 mDevice->sendSync();
1852 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1853
1854 // ACTION_MOVE (First slot)
1855 mDevice->sendSlot(FIRST_SLOT);
1856 mDevice->sendMove(centerPoint + Point(5, 5));
1857 // ACTION_POINTER_UP (Second slot)
1858 mDevice->sendSlot(SECOND_SLOT);
1859 mDevice->sendPointerUp();
1860 // Send a single sync for the above 2 pointer updates
1861 mDevice->sendSync();
1862
1863 // First, we should get POINTER_UP for the second pointer
1864 assertReceivedMotion(ACTION_POINTER_1_UP,
1865 {/*first pointer */ centerPoint + Point(5, 5),
1866 /*second pointer*/ secondPoint});
1867
1868 // Next, the MOVE event for the first pointer
1869 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1870}
1871
1872/**
1873 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1874 * move, and then it will go up, all in the same frame.
1875 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1876 * gets sent to the listener.
1877 */
Arpit Singh440bf652023-08-09 09:23:43 +00001878TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001879 NotifyMotionArgs args;
1880 const Point centerPoint = mDevice->getCenterPoint();
1881
1882 // ACTION_DOWN
1883 mDevice->sendSlot(FIRST_SLOT);
1884 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1885 mDevice->sendDown(centerPoint);
1886 mDevice->sendSync();
1887 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1888
1889 // ACTION_POINTER_DOWN (Second slot)
1890 const Point secondPoint = centerPoint + Point(100, 100);
1891 mDevice->sendSlot(SECOND_SLOT);
1892 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1893 mDevice->sendDown(secondPoint);
1894 mDevice->sendSync();
1895 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1896
1897 // ACTION_MOVE (First slot)
1898 mDevice->sendSlot(FIRST_SLOT);
1899 mDevice->sendMove(centerPoint + Point(5, 5));
1900 // ACTION_POINTER_UP (Second slot)
1901 mDevice->sendSlot(SECOND_SLOT);
1902 mDevice->sendMove(secondPoint + Point(6, 6));
1903 mDevice->sendPointerUp();
1904 // Send a single sync for the above 2 pointer updates
1905 mDevice->sendSync();
1906
1907 // First, we should get POINTER_UP for the second pointer
1908 // The movement of the second pointer during the liftoff frame is ignored.
1909 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1910 assertReceivedMotion(ACTION_POINTER_1_UP,
1911 {/*first pointer */ centerPoint + Point(5, 5),
1912 /*second pointer*/ secondPoint});
1913
1914 // Next, the MOVE event for the first pointer
1915 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1916}
1917
Arpit Singh440bf652023-08-09 09:23:43 +00001918TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001919 NotifyMotionArgs args;
1920 const Point centerPoint = mDevice->getCenterPoint();
1921
1922 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001923 mDevice->sendSlot(FIRST_SLOT);
1924 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001925 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001926 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001927 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1928 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1929
arthurhungcc7f9802020-04-30 17:55:40 +08001930 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001931 const Point secondPoint = centerPoint + Point(100, 100);
1932 mDevice->sendSlot(SECOND_SLOT);
1933 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1934 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001935 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001936 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001937 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001938
arthurhungcc7f9802020-04-30 17:55:40 +08001939 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001940 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001941 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001942 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1944
arthurhungcc7f9802020-04-30 17:55:40 +08001945 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1946 // a palm event.
1947 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001948 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001949 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001950 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001951 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001952 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001953
arthurhungcc7f9802020-04-30 17:55:40 +08001954 // Send up to second slot, expect first slot send moving.
1955 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001956 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001957 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001959
arthurhungcc7f9802020-04-30 17:55:40 +08001960 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001961 mDevice->sendSlot(FIRST_SLOT);
1962 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001963 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001964
arthurhungcc7f9802020-04-30 17:55:40 +08001965 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1966 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001967}
1968
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001969/**
1970 * Some drivers historically have reported axis values outside of the range specified in the
1971 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1972 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1973 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1974 * and no units for pressure (resolution) is specified by the evdev documentation.
1975 */
1976TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1977 const Point centerPoint = mDevice->getCenterPoint();
1978
1979 // Down with pressure outside the reported range
1980 mDevice->sendSlot(FIRST_SLOT);
1981 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1982 mDevice->sendDown(centerPoint);
1983 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1984 mDevice->sendSync();
1985 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1986 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1987
1988 // Move to a point outside the reported range
1989 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1990 mDevice->sendSync();
1991 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1992 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1993
1994 // Up
1995 mDevice->sendUp();
1996 mDevice->sendSync();
1997 ASSERT_NO_FATAL_FAILURE(
1998 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1999}
2000
Arpit Singh440bf652023-08-09 09:23:43 +00002001TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00002002 const Point centerPoint = mDevice->getCenterPoint();
2003
2004 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
2005 mDevice->sendSlot(FIRST_SLOT);
2006 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2007 mDevice->sendToolType(MT_TOOL_PEN);
2008 mDevice->sendDown(centerPoint);
2009 mDevice->sendSync();
2010 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2011 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002012 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00002013
2014 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2015
2016 // Release the stylus touch.
2017 mDevice->sendUp();
2018 mDevice->sendSync();
2019 ASSERT_NO_FATAL_FAILURE(
2020 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2021
2022 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2023
2024 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2025 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2026 mDevice->sendToolType(MT_TOOL_FINGER);
2027 mDevice->sendDown(centerPoint);
2028 mDevice->sendSync();
2029 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2030 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002031 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00002032
2033 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2034
2035 mDevice->sendUp();
2036 mDevice->sendSync();
2037 ASSERT_NO_FATAL_FAILURE(
2038 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2039
2040 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2041 // The policy should be notified of the stylus presence.
2042 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2043 mDevice->sendToolType(MT_TOOL_PEN);
2044 mDevice->sendMove(centerPoint);
2045 mDevice->sendSync();
2046 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2047 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002048 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00002049
2050 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2051}
2052
Arpit Singh440bf652023-08-09 09:23:43 +00002053TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002054 const Point centerPoint = mDevice->getCenterPoint();
2055
2056 // Down
2057 mDevice->sendSlot(FIRST_SLOT);
2058 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2059 mDevice->sendDown(centerPoint);
2060 mDevice->sendSync();
2061 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2062 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
2063
2064 // Move
2065 mDevice->sendMove(centerPoint + Point(1, 1));
2066 mDevice->sendSync();
2067 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2068 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2069
2070 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2071 auto externalStylus = createUinputDevice<UinputExternalStylus>();
2072 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2073 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002074 const auto stylusInfo = waitForDevice(externalStylus->getName());
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002075 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002076
2077 // Move
2078 mDevice->sendMove(centerPoint + Point(2, 2));
2079 mDevice->sendSync();
2080 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2081 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2082
2083 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2084 externalStylus.reset();
2085 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2086 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2087 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2088
2089 // Up
2090 mDevice->sendUp();
2091 mDevice->sendSync();
2092 ASSERT_NO_FATAL_FAILURE(
2093 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2094
2095 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2096}
2097
Arpit Singh440bf652023-08-09 09:23:43 +00002098INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
2099 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
2100 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
2101 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
2102
Prabir Pradhan124ea442022-10-28 20:27:44 +00002103// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002104
Prabir Pradhan124ea442022-10-28 20:27:44 +00002105// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2106// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2107// stylus.
2108template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00002109class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002110protected:
2111 void SetUp() override {
2112#if !defined(__ANDROID__)
2113 GTEST_SKIP();
2114#endif
Arpit Singh440bf652023-08-09 09:23:43 +00002115 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00002116 mTouchscreen = mDevice.get();
2117 mTouchscreenInfo = mDeviceInfo;
2118
2119 setUpStylusDevice();
2120 }
2121
2122 UinputStylusDevice* mStylus{nullptr};
2123 InputDeviceInfo mStylusInfo{};
2124
2125 UinputTouchScreen* mTouchscreen{nullptr};
2126 InputDeviceInfo mTouchscreenInfo{};
2127
2128private:
2129 // When we are attempting to test stylus button events that are sent from the touchscreen,
2130 // use the same Uinput device for the touchscreen and the stylus.
2131 template <typename T = UinputStylusDevice>
2132 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2133 mStylus = mDevice.get();
2134 mStylusInfo = mDeviceInfo;
2135 }
2136
2137 // When we are attempting to stylus buttons from an external stylus being merged with touches
2138 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2139 template <typename T = UinputStylusDevice>
2140 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2141 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2142 mStylus = mStylusDeviceLifecycleTracker.get();
2143 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2144 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002145 const auto info = waitForDevice(mStylus->getName());
Prabir Pradhan124ea442022-10-28 20:27:44 +00002146 ASSERT_TRUE(info);
2147 mStylusInfo = *info;
2148 }
2149
2150 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2151
2152 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002153 using BaseTouchIntegrationTest::mDevice;
2154 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002155};
2156
2157using StylusButtonIntegrationTestTypes =
2158 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2159TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2160
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002161TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002162 const auto stylusId = TestFixture::mStylusInfo.getId();
2163
2164 TestFixture::mStylus->pressKey(BTN_STYLUS);
2165 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2166 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2167 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2168
2169 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2170 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002171 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002172 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002173}
2174
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002175TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002176 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2177 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2178 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002179
2180 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002181 TestFixture::mStylus->pressKey(BTN_STYLUS);
2182 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002183 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002184 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002185
2186 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002187 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2188 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2189 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2190 TestFixture::mTouchscreen->sendDown(centerPoint);
2191 TestFixture::mTouchscreen->sendSync();
2192 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002193 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002194 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002195 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2196 WithDeviceId(touchscreenId))));
2197 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002198 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002199 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002200 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2201 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002202
Prabir Pradhan124ea442022-10-28 20:27:44 +00002203 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2204 TestFixture::mTouchscreen->sendSync();
2205 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002206 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002207 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002208 WithDeviceId(touchscreenId))));
2209 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002210 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002211 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002212 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002213
2214 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002215 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2216 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002217 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002218 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002219}
2220
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002221TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002222 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2223 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2224 const auto stylusId = TestFixture::mStylusInfo.getId();
2225 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002226 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002227
2228 // Press the stylus button.
2229 TestFixture::mStylus->pressKey(BTN_STYLUS);
2230 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2231 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2232 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2233
2234 // Start hovering with the stylus.
2235 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2236 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2237 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2238 TestFixture::mTouchscreen->sendMove(centerPoint);
2239 TestFixture::mTouchscreen->sendSync();
2240 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2241 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2242 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2243 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2244 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2245 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2246 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2247 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2248 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2249
2250 // Touch down with the stylus.
2251 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2252 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2253 TestFixture::mTouchscreen->sendDown(centerPoint);
2254 TestFixture::mTouchscreen->sendSync();
2255 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2256 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2257 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2258
2259 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2260 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2261 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2262
2263 // Stop touching with the stylus, and start hovering.
2264 TestFixture::mTouchscreen->sendUp();
2265 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2266 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2267 TestFixture::mTouchscreen->sendMove(centerPoint);
2268 TestFixture::mTouchscreen->sendSync();
2269 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2270 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2271 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2272 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2273 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2274 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2275 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2276 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2277 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2278
2279 // Stop hovering.
2280 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2281 TestFixture::mTouchscreen->sendSync();
2282 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2283 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2284 WithButtonState(0))));
2285 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2286 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2287 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2288 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2289
2290 // Release the stylus button.
2291 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2292 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2293 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2294 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2295}
2296
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002297TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002298 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2299 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2300 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002301
2302 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002303 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2304 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2305 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2306 TestFixture::mTouchscreen->sendDown(centerPoint);
2307 TestFixture::mTouchscreen->sendSync();
2308 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002309 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002310 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002311 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002312
2313 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002314 TestFixture::mStylus->pressKey(BTN_STYLUS);
2315 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002316 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002317 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2318 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002319 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002320 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002321 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2322 WithDeviceId(touchscreenId))));
2323 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002324 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002325 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002326 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2327 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002328
Prabir Pradhan124ea442022-10-28 20:27:44 +00002329 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2330 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002331 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002332 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2333 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002334 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002335 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002336 WithDeviceId(touchscreenId))));
2337 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002338 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002339 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002340 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002341
2342 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002343 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2344 TestFixture::mTouchscreen->sendSync();
2345 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002346 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002347 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002348 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002349}
2350
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002351TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002352 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2353 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002354 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002355
2356 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2357 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2358 const auto stylusId = TestFixture::mStylusInfo.getId();
2359
2360 // Start a stylus gesture. By the time this event is processed, the configuration change that
2361 // was requested is guaranteed to be completed.
2362 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2363 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2364 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2365 TestFixture::mTouchscreen->sendDown(centerPoint);
2366 TestFixture::mTouchscreen->sendSync();
2367 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2368 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002369 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002370 WithDeviceId(touchscreenId))));
2371
2372 // Press and release a stylus button. Each change only generates a MOVE motion event.
2373 // Key events are unaffected.
2374 TestFixture::mStylus->pressKey(BTN_STYLUS);
2375 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2376 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2377 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2378 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2379 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002380 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002381 WithDeviceId(touchscreenId))));
2382
2383 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2384 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2385 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2386 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2387 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2388 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002389 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002390 WithDeviceId(touchscreenId))));
2391
2392 // Finish the stylus gesture.
2393 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2394 TestFixture::mTouchscreen->sendSync();
2395 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2396 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002397 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002398 WithDeviceId(touchscreenId))));
2399}
2400
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002401// --- ExternalStylusIntegrationTest ---
2402
2403// Verify the behavior of an external stylus. An external stylus can report pressure or button
2404// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2405// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002406using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002407
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002408TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreenSource) {
2409 // Create an external stylus capable of reporting pressure data that
2410 // should be fused with a touch pointer.
2411 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2412 createUinputDevice<UinputExternalStylusWithPressure>();
2413 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2414 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002415 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002416 ASSERT_TRUE(stylusInfo);
2417
2418 // Connecting an external stylus changes the source of the touchscreen.
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002419 const auto deviceInfo = waitForDevice(mDevice->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002420 ASSERT_TRUE(deviceInfo);
2421 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
2422}
2423
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002424TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002425 const Point centerPoint = mDevice->getCenterPoint();
2426
2427 // Create an external stylus capable of reporting pressure data that
2428 // should be fused with a touch pointer.
2429 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2430 createUinputDevice<UinputExternalStylusWithPressure>();
2431 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2432 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002433 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002434 ASSERT_TRUE(stylusInfo);
2435
2436 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2437
2438 const auto touchscreenId = mDeviceInfo.getId();
2439
2440 // Set a pressure value on the stylus. It doesn't generate any events.
2441 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2442 stylus->setPressure(100);
2443 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2444
2445 // Start a finger gesture, and ensure it shows up as stylus gesture
2446 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002447 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002448 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002449 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002450 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002451 mDevice->sendSync();
2452 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002453 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithToolType(ToolType::STYLUS),
2454 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2455 WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002456
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002457 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2458 // event with the updated pressure.
2459 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002460 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002461 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithToolType(ToolType::STYLUS),
2462 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2463 WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002464
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002465 // The external stylus did not generate any events.
2466 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2468}
2469
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002470TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002471 const Point centerPoint = mDevice->getCenterPoint();
2472
2473 // Create an external stylus capable of reporting pressure data that
2474 // should be fused with a touch pointer.
2475 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2476 createUinputDevice<UinputExternalStylusWithPressure>();
2477 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2478 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002479 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002480 ASSERT_TRUE(stylusInfo);
2481
2482 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2483
2484 const auto touchscreenId = mDeviceInfo.getId();
2485
2486 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2487 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002488 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2489 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002490 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002491 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002492
2493 // Start a finger gesture. The touch device will withhold generating any touches for
2494 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2495 mDevice->sendSlot(FIRST_SLOT);
2496 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2497 mDevice->sendToolType(MT_TOOL_FINGER);
2498 mDevice->sendDown(centerPoint);
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002499 const auto syncTime = std::chrono::system_clock::now();
2500 // After 72 ms, the event *will* be generated. If we wait the full 72 ms to check that NO event
2501 // is generated in that period, there will be a race condition between the event being generated
2502 // and the test's wait timeout expiring. Thus, we wait for a shorter duration in the test, which
2503 // will reduce the liklihood of the race condition occurring.
2504 const auto waitUntilTimeForNoEvent =
2505 syncTime + std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT / 2));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002506 mDevice->sendSync();
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002507 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntilTimeForNoEvent));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002508
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002509 // Since the external stylus did not report a pressure value within the timeout,
2510 // it shows up as a finger pointer.
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002511 const auto waitUntilTimeForEvent = syncTime +
2512 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT)) + EVENT_HAPPENED_TIMEOUT;
2513 ASSERT_NO_FATAL_FAILURE(
2514 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2515 AMOTION_EVENT_ACTION_DOWN),
2516 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2517 AINPUT_SOURCE_STYLUS),
2518 WithToolType(ToolType::FINGER),
2519 WithDeviceId(touchscreenId),
2520 WithPressure(1.f)),
2521 waitUntilTimeForEvent));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002522
2523 // Change the pressure on the external stylus. Since the pressure was not present at the start
2524 // of the gesture, it is ignored for now.
2525 stylus->setPressure(200);
2526 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2527
2528 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002529 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2530 mDevice->sendSync();
2531 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2532 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002533 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002534 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002535
2536 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2537 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2538 mDevice->sendToolType(MT_TOOL_FINGER);
2539 mDevice->sendDown(centerPoint);
2540 mDevice->sendSync();
2541 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002542 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithSource(STYLUS_FUSION_SOURCE),
2543 WithToolType(ToolType::STYLUS), WithButtonState(0), WithDeviceId(touchscreenId),
2544 WithPressure(200.f / RAW_PRESSURE_MAX))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002545
2546 // The external stylus did not generate any events.
2547 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2548 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002549}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002550
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002551TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002552 const Point centerPoint = mDevice->getCenterPoint();
2553
2554 // Create an external stylus device that does not support pressure. It should not affect any
2555 // touch pointers.
2556 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2557 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2558 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002559 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002560 ASSERT_TRUE(stylusInfo);
2561
2562 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2563
2564 const auto touchscreenId = mDeviceInfo.getId();
2565
2566 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2567 // pressure data from the external stylus.
2568 mDevice->sendSlot(FIRST_SLOT);
2569 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2570 mDevice->sendToolType(MT_TOOL_FINGER);
2571 mDevice->sendDown(centerPoint);
2572 auto waitUntil = std::chrono::system_clock::now() +
2573 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2574 mDevice->sendSync();
2575 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002576 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2577 AMOTION_EVENT_ACTION_DOWN),
2578 WithToolType(ToolType::FINGER),
2579 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2580 AINPUT_SOURCE_STYLUS),
2581 WithButtonState(0),
2582 WithDeviceId(touchscreenId),
2583 WithPressure(1.f)),
2584 waitUntil));
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002585
2586 // The external stylus did not generate any events.
2587 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2588 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2589}
2590
Michael Wrightd02c5b62014-02-10 15:10:22 -08002591// --- InputDeviceTest ---
2592class InputDeviceTest : public testing::Test {
2593protected:
2594 static const char* DEVICE_NAME;
2595 static const char* DEVICE_LOCATION;
2596 static const int32_t DEVICE_ID;
2597 static const int32_t DEVICE_GENERATION;
2598 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002599 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002600 static const int32_t EVENTHUB_ID;
2601 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2602
2603 std::shared_ptr<FakeEventHub> mFakeEventHub;
2604 sp<FakeInputReaderPolicy> mFakePolicy;
2605 std::unique_ptr<TestInputListener> mFakeListener;
2606 std::unique_ptr<InstrumentedInputReader> mReader;
2607 std::shared_ptr<InputDevice> mDevice;
2608
2609 void SetUp() override {
2610 mFakeEventHub = std::make_unique<FakeEventHub>();
2611 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2612 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002613 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002614 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002615 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616 identifier.name = DEVICE_NAME;
2617 identifier.location = DEVICE_LOCATION;
2618 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2619 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2620 identifier);
2621 mReader->pushNextDevice(mDevice);
2622 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002623 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002624 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002625
2626 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002627 mFakeListener.reset();
2628 mFakePolicy.clear();
2629 }
2630};
2631
2632const char* InputDeviceTest::DEVICE_NAME = "device";
2633const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2634const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2635const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002636const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002637const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2638 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002639const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2641
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002642TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002643 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002644 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2645 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002646}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002647
Michael Wrightd02c5b62014-02-10 15:10:22 -08002648TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2649 ASSERT_EQ(mDevice->isEnabled(), false);
2650}
2651
2652TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2653 // Configuration.
2654 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002655 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656
2657 // Reset.
2658 unused += mDevice->reset(ARBITRARY_TIME);
2659
2660 NotifyDeviceResetArgs resetArgs;
2661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2662 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2663 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2664
2665 // Metadata.
2666 ASSERT_TRUE(mDevice->isIgnored());
2667 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2668
2669 InputDeviceInfo info = mDevice->getDeviceInfo();
2670 ASSERT_EQ(DEVICE_ID, info.getId());
2671 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2672 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2673 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2674
2675 // State queries.
2676 ASSERT_EQ(0, mDevice->getMetaState());
2677
2678 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2679 << "Ignored device should return unknown key code state.";
2680 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2681 << "Ignored device should return unknown scan code state.";
2682 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2683 << "Ignored device should return unknown switch state.";
2684
2685 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2686 uint8_t flags[2] = { 0, 1 };
2687 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2688 << "Ignored device should never mark any key codes.";
2689 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2690 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2691}
2692
2693TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2694 // Configuration.
2695 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2696
2697 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002698 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2699 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2701 mapper1.setMetaState(AMETA_ALT_ON);
2702 mapper1.addSupportedKeyCode(AKEYCODE_A);
2703 mapper1.addSupportedKeyCode(AKEYCODE_B);
2704 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2705 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2706 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2707 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2708 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2709
2710 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002711 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2712 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713 mapper2.setMetaState(AMETA_SHIFT_ON);
2714
2715 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002716 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002717
Harry Cuttsf13161a2023-03-08 14:15:49 +00002718 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2719 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002720 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002721 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002722
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002723 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2724 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002725
2726 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002727 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002728 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2729 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002730
2731 NotifyDeviceResetArgs resetArgs;
2732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2733 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2734 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2735
2736 // Metadata.
2737 ASSERT_FALSE(mDevice->isIgnored());
2738 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2739
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002740 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002741 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002742 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002743 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2744 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2745
2746 // State queries.
2747 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2748 << "Should query mappers and combine meta states.";
2749
2750 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2751 << "Should return unknown key code state when source not supported.";
2752 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2753 << "Should return unknown scan code state when source not supported.";
2754 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2755 << "Should return unknown switch state when source not supported.";
2756
2757 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2758 << "Should query mapper when source is supported.";
2759 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2760 << "Should query mapper when source is supported.";
2761 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2762 << "Should query mapper when source is supported.";
2763
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002764 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002765 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002766 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002767 << "Should do nothing when source is unsupported.";
2768 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2769 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2770 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2771 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2772
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002773 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002774 << "Should query mapper when source is supported.";
2775 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2776 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2777 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2778 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2779
2780 // Event handling.
2781 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002782 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002783 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002785 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2786 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787}
2788
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -08002789TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorNotSet) {
2790 // Set some behavior to force the configuration to be update.
2791 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2792 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2793 AINPUT_SOURCE_KEYBOARD);
2794
2795 std::list<NotifyArgs> unused =
2796 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2797 /*changes=*/{});
2798
2799 ASSERT_FALSE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.has_value());
2800}
2801
2802TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorEnabled) {
2803 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.viewBehavior_smoothScroll", "1");
2804 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2805 AINPUT_SOURCE_KEYBOARD);
2806
2807 std::list<NotifyArgs> unused =
2808 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2809 /*changes=*/{});
2810
2811 ASSERT_TRUE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.value_or(false));
2812}
2813
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002814TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2815 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2816 FakeInputMapper& mapper =
2817 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2818 AINPUT_SOURCE_KEYBOARD);
2819 NotifyMotionArgs args1;
2820 NotifySwitchArgs args2;
2821 NotifyKeyArgs args3;
2822 mapper.setProcessResult({args1, args2, args3});
2823
2824 InputReaderConfiguration config;
2825 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2826
2827 RawEvent event;
2828 event.deviceId = EVENTHUB_ID;
2829 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2830
2831 for (auto& arg : notifyArgs) {
2832 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2833 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2834 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2835 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2836 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2837 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2838 }
2839 }
2840}
2841
2842TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2843 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2844 FakeInputMapper& mapper =
2845 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2846 AINPUT_SOURCE_KEYBOARD);
2847 NotifyMotionArgs args;
2848 mapper.setProcessResult({args});
2849
2850 InputReaderConfiguration config;
2851 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2852
2853 RawEvent event;
2854 event.deviceId = EVENTHUB_ID;
2855 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2856
2857 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2858 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2859}
2860
2861TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2862 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2863 FakeInputMapper& mapper =
2864 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2865 AINPUT_SOURCE_KEYBOARD);
2866 NotifyMotionArgs args;
2867 args.policyFlags = POLICY_FLAG_WAKE;
2868 mapper.setProcessResult({args});
2869
2870 InputReaderConfiguration config;
2871 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2872
2873 RawEvent event;
2874 event.deviceId = EVENTHUB_ID;
2875 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2876
2877 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2878 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2879}
2880
Arthur Hung2c9a3342019-07-23 14:18:59 +08002881// A single input device is associated with a specific display. Check that:
2882// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002883// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002884TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002885 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2886 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002887
2888 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002889 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002890 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2891 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002892
2893 // Device should be enabled by default.
2894 ASSERT_TRUE(mDevice->isEnabled());
2895
2896 // Prepare associated info.
2897 constexpr uint8_t hdmi = 1;
2898 const std::string UNIQUE_ID = "local:1";
2899
2900 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002901 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002902 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002903 // Device should be disabled because it is associated with a specific display via
2904 // input port <-> display port association, but the corresponding display is not found
2905 ASSERT_FALSE(mDevice->isEnabled());
2906
2907 // Prepare displays.
2908 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002909 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002910 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002911 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002912 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002913 ASSERT_TRUE(mDevice->isEnabled());
2914
2915 // Device should be disabled after set disable.
2916 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002917 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002918 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002919 ASSERT_FALSE(mDevice->isEnabled());
2920
2921 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002922 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002923 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002924 ASSERT_FALSE(mDevice->isEnabled());
2925}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002926
Christine Franks1ba71cc2021-04-07 14:37:42 -07002927TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2928 // Device should be enabled by default.
2929 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002930 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2931 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002932 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002933 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2934 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002935 ASSERT_TRUE(mDevice->isEnabled());
2936
2937 // Device should be disabled because it is associated with a specific display, but the
2938 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002939 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002940 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002941 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002942 ASSERT_FALSE(mDevice->isEnabled());
2943
2944 // Device should be enabled when a display is found.
2945 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002946 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002947 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002948 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002949 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002950 ASSERT_TRUE(mDevice->isEnabled());
2951
2952 // Device should be disabled after set disable.
2953 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002954 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002955 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002956 ASSERT_FALSE(mDevice->isEnabled());
2957
2958 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002959 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002960 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002961 ASSERT_FALSE(mDevice->isEnabled());
2962}
2963
Christine Franks2a2293c2022-01-18 11:51:16 -08002964TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2965 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002966 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2967 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002968 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002969 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2970 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002971
Christine Franks2a2293c2022-01-18 11:51:16 -08002972 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2973 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002974 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002975 NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002976 const auto initialGeneration = mDevice->getGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002977 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002978 InputReaderConfiguration::Change::DISPLAY_INFO);
Antonio Kantek0ac5e092024-04-22 17:10:27 +00002979 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueIdByPort());
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002980 ASSERT_GT(mDevice->getGeneration(), initialGeneration);
2981 ASSERT_EQ(mDevice->getDeviceInfo().getAssociatedDisplayId(), SECONDARY_DISPLAY_ID);
Christine Franks2a2293c2022-01-18 11:51:16 -08002982}
2983
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002984/**
2985 * This test reproduces a crash caused by a dangling reference that remains after device is added
2986 * and removed. The reference is accessed in InputDevice::dump(..);
2987 */
2988TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2989 constexpr int32_t TEST_EVENTHUB_ID = 10;
2990 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2991
Harry Cutts33476232023-01-30 19:57:29 +00002992 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh82f29a12023-06-13 15:05:53 +00002993 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2994 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002995 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2996 std::string dumpStr, eventHubDevStr;
2997 device.dump(dumpStr, eventHubDevStr);
2998}
2999
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003000TEST_F(InputDeviceTest, GetBluetoothAddress) {
3001 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
3002 ASSERT_TRUE(address);
3003 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
3004}
3005
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003006TEST_F(InputDeviceTest, KernelBufferOverflowResetsMappers) {
3007 mFakePolicy->clearViewports();
3008 FakeInputMapper& mapper =
3009 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
3010 AINPUT_SOURCE_KEYBOARD);
3011 std::list<NotifyArgs> unused =
3012 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3013 /*changes=*/{});
3014
3015 mapper.assertConfigureWasCalled();
3016 mapper.assertResetWasNotCalled();
3017
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08003018 RawEvent event{.when = ARBITRARY_TIME,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003019 .readTime = ARBITRARY_TIME,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08003020 .deviceId = EVENTHUB_ID,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003021 .type = EV_SYN,
3022 .code = SYN_REPORT,
3023 .value = 0};
3024
3025 // Events are processed normally.
3026 unused = mDevice->process(&event, /*count=*/1);
3027 mapper.assertProcessWasCalled();
3028
3029 // Simulate a kernel buffer overflow, which generates a SYN_DROPPED event.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003030 event.type = EV_SYN;
3031 event.code = SYN_DROPPED;
3032 event.value = 0;
3033 unused = mDevice->process(&event, /*count=*/1);
3034 mapper.assertProcessWasNotCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003035
3036 // All events until the next SYN_REPORT should be dropped.
3037 event.type = EV_KEY;
3038 event.code = KEY_A;
3039 event.value = 1;
3040 unused = mDevice->process(&event, /*count=*/1);
3041 mapper.assertProcessWasNotCalled();
3042
3043 // We get the SYN_REPORT event now, which is not forwarded to mappers.
Arpit Singh4b4a4572023-11-24 18:19:56 +00003044 // This should reset the mapper.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003045 event.type = EV_SYN;
3046 event.code = SYN_REPORT;
3047 event.value = 0;
3048 unused = mDevice->process(&event, /*count=*/1);
3049 mapper.assertProcessWasNotCalled();
Arpit Singh4b4a4572023-11-24 18:19:56 +00003050 mapper.assertResetWasCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003051
3052 // The mapper receives events normally now.
3053 event.type = EV_KEY;
3054 event.code = KEY_B;
3055 event.value = 1;
3056 unused = mDevice->process(&event, /*count=*/1);
3057 mapper.assertProcessWasCalled();
3058}
3059
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060// --- SwitchInputMapperTest ---
3061
3062class SwitchInputMapperTest : public InputMapperTest {
3063protected:
3064};
3065
3066TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003067 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003069 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070}
3071
3072TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003073 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003075 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003076 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003078 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003079 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080}
3081
3082TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003083 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003084 std::list<NotifyArgs> out;
3085 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3086 ASSERT_TRUE(out.empty());
3087 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3088 ASSERT_TRUE(out.empty());
3089 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3090 ASSERT_TRUE(out.empty());
3091 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003093 ASSERT_EQ(1u, out.size());
3094 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003096 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3097 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098 args.switchMask);
3099 ASSERT_EQ(uint32_t(0), args.policyFlags);
3100}
3101
Chris Ye87143712020-11-10 05:05:58 +00003102// --- VibratorInputMapperTest ---
3103class VibratorInputMapperTest : public InputMapperTest {
3104protected:
3105 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3106};
3107
3108TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003109 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003110
3111 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3112}
3113
3114TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003115 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003116
3117 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3118}
3119
3120TEST_F(VibratorInputMapperTest, Vibrate) {
3121 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003122 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00003123 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003124
3125 VibrationElement pattern(2);
3126 VibrationSequence sequence(2);
3127 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00003128 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
3129 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003130 sequence.addElement(pattern);
3131 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00003132 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
3133 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003134 sequence.addElement(pattern);
3135
3136 std::vector<int64_t> timings = {0, 1};
3137 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3138
3139 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003140 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00003141 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003142 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003143 // Verify vibrator state listener was notified.
3144 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003145 ASSERT_EQ(1u, out.size());
3146 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3147 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3148 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003149 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003150 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003151 ASSERT_FALSE(mapper.isVibrating());
3152 // Verify vibrator state listener was notified.
3153 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003154 ASSERT_EQ(1u, out.size());
3155 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3156 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3157 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003158}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159
Chris Yef59a2f42020-10-16 12:55:26 -07003160// --- SensorInputMapperTest ---
3161
3162class SensorInputMapperTest : public InputMapperTest {
3163protected:
3164 static const int32_t ACCEL_RAW_MIN;
3165 static const int32_t ACCEL_RAW_MAX;
3166 static const int32_t ACCEL_RAW_FUZZ;
3167 static const int32_t ACCEL_RAW_FLAT;
3168 static const int32_t ACCEL_RAW_RESOLUTION;
3169
3170 static const int32_t GYRO_RAW_MIN;
3171 static const int32_t GYRO_RAW_MAX;
3172 static const int32_t GYRO_RAW_FUZZ;
3173 static const int32_t GYRO_RAW_FLAT;
3174 static const int32_t GYRO_RAW_RESOLUTION;
3175
3176 static const float GRAVITY_MS2_UNIT;
3177 static const float DEGREE_RADIAN_UNIT;
3178
3179 void prepareAccelAxes();
3180 void prepareGyroAxes();
3181 void setAccelProperties();
3182 void setGyroProperties();
3183 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3184};
3185
3186const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3187const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3188const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3189const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3190const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3191
3192const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3193const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3194const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3195const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3196const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3197
3198const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3199const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3200
3201void SensorInputMapperTest::prepareAccelAxes() {
3202 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3203 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3204 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3205 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3206 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3207 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3208}
3209
3210void SensorInputMapperTest::prepareGyroAxes() {
3211 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3212 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3213 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3214 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3215 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3216 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3217}
3218
3219void SensorInputMapperTest::setAccelProperties() {
3220 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3221 /* sensorDataIndex */ 0);
3222 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3223 /* sensorDataIndex */ 1);
3224 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3225 /* sensorDataIndex */ 2);
3226 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3227 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3228 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3229 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3230 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3231}
3232
3233void SensorInputMapperTest::setGyroProperties() {
3234 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3235 /* sensorDataIndex */ 0);
3236 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3237 /* sensorDataIndex */ 1);
3238 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3239 /* sensorDataIndex */ 2);
3240 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3241 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3242 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3243 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3244 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3245}
3246
3247TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003248 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003249
3250 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3251}
3252
3253TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3254 setAccelProperties();
3255 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003256 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003257
3258 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3259 std::chrono::microseconds(10000),
3260 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003261 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3265 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003267
3268 NotifySensorArgs args;
3269 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3270 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3271 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3272
3273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3274 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3275 ASSERT_EQ(args.deviceId, DEVICE_ID);
3276 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3277 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3278 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3279 ASSERT_EQ(args.values, values);
3280 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3281}
3282
3283TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3284 setGyroProperties();
3285 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003286 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003287
3288 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3289 std::chrono::microseconds(10000),
3290 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003291 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003297
3298 NotifySensorArgs args;
3299 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3300 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3301 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3302
3303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3304 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3305 ASSERT_EQ(args.deviceId, DEVICE_ID);
3306 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3307 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3308 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3309 ASSERT_EQ(args.values, values);
3310 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3311}
3312
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313// --- KeyboardInputMapperTest ---
3314
3315class KeyboardInputMapperTest : public InputMapperTest {
3316protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003317 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003318 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003319 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003320
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003321 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003322 int32_t originalKeyCode, int32_t rotatedKeyCode,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003323 ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324};
3325
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003326/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3327 * orientation.
3328 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003329void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003330 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3331 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003332}
3333
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003334void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003335 int32_t originalScanCode, int32_t originalKeyCode,
Linnan Li13bf76a2024-05-05 19:18:02 +08003336 int32_t rotatedKeyCode,
3337 ui::LogicalDisplayId displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338 NotifyKeyArgs args;
3339
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3342 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3343 ASSERT_EQ(originalScanCode, args.scanCode);
3344 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003345 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3349 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3350 ASSERT_EQ(originalScanCode, args.scanCode);
3351 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003352 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353}
3354
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003356 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003357 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003358 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003360 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361}
3362
3363TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3364 const int32_t USAGE_A = 0x070004;
3365 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003366 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3367 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003368 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3369 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3370 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003372 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003373 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003374 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003375 // Initial metastate is AMETA_NONE.
3376 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377
3378 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003380 NotifyKeyArgs args;
3381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3382 ASSERT_EQ(DEVICE_ID, args.deviceId);
3383 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3384 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3385 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3386 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3387 ASSERT_EQ(KEY_HOME, args.scanCode);
3388 ASSERT_EQ(AMETA_NONE, args.metaState);
3389 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3390 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3391 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3392
3393 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003394 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3396 ASSERT_EQ(DEVICE_ID, args.deviceId);
3397 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3398 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3399 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3400 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3401 ASSERT_EQ(KEY_HOME, args.scanCode);
3402 ASSERT_EQ(AMETA_NONE, args.metaState);
3403 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3404 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3405 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3406
3407 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3411 ASSERT_EQ(DEVICE_ID, args.deviceId);
3412 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3413 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3414 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3415 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3416 ASSERT_EQ(0, args.scanCode);
3417 ASSERT_EQ(AMETA_NONE, args.metaState);
3418 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3419 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3420 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3421
3422 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3424 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3426 ASSERT_EQ(DEVICE_ID, args.deviceId);
3427 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3428 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3429 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3430 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3431 ASSERT_EQ(0, args.scanCode);
3432 ASSERT_EQ(AMETA_NONE, args.metaState);
3433 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3434 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3435 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3436
3437 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3441 ASSERT_EQ(DEVICE_ID, args.deviceId);
3442 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3443 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3444 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3445 ASSERT_EQ(0, args.keyCode);
3446 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3447 ASSERT_EQ(AMETA_NONE, args.metaState);
3448 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3449 ASSERT_EQ(0U, args.policyFlags);
3450 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3451
3452 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3454 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3456 ASSERT_EQ(DEVICE_ID, args.deviceId);
3457 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3458 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3459 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3460 ASSERT_EQ(0, args.keyCode);
3461 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3462 ASSERT_EQ(AMETA_NONE, args.metaState);
3463 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3464 ASSERT_EQ(0U, args.policyFlags);
3465 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3466}
3467
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003468TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3469 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3470 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3471 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3472
3473 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003474 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003475 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3476
3477 // Key down by scan code.
3478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3479 NotifyKeyArgs args;
3480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3481 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3482
3483 // Key up by scan code.
3484 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3486 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3487}
3488
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003489/**
3490 * Ensure that the readTime is set to the time when the EV_KEY is received.
3491 */
3492TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3493 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3494
3495 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003496 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003497 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3498 NotifyKeyArgs args;
3499
3500 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003501 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3503 ASSERT_EQ(12, args.readTime);
3504
3505 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003506 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3508 ASSERT_EQ(15, args.readTime);
3509}
3510
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003512 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3513 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003514 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3515 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3516 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003518 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003519 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003520 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521
Arthur Hung95f68612022-04-07 14:08:22 +08003522 // Initial metastate is AMETA_NONE.
3523 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524
3525 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527 NotifyKeyArgs args;
3528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003530 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003531 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003532
3533 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003534 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3536 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003537 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003538
3539 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003540 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3542 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003543 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544
3545 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003546 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3548 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003549 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003550 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551}
3552
3553TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003554 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3555 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3556 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3557 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003559 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003560 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003561 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003562
Michael Wrighta9cf4192022-12-01 23:46:39 +00003563 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3565 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3566 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3567 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3568 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3569 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3570 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3571 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3572}
3573
3574TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003575 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3576 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3577 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3578 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003581 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003582 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003583 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584
Michael Wrighta9cf4192022-12-01 23:46:39 +00003585 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003586 ASSERT_NO_FATAL_FAILURE(
3587 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3588 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3589 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3590 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3591 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3592 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3593 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003594
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003595 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003596 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003597 ASSERT_NO_FATAL_FAILURE(
3598 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3599 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3600 AKEYCODE_DPAD_UP, DISPLAY_ID));
3601 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3602 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3603 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3604 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003605
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003606 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003607 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003608 ASSERT_NO_FATAL_FAILURE(
3609 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3610 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3611 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3612 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3613 AKEYCODE_DPAD_UP, DISPLAY_ID));
3614 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3615 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003616
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003617 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003618 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003619 ASSERT_NO_FATAL_FAILURE(
3620 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3621 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3622 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3624 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3625 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3626 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627
3628 // Special case: if orientation changes while key is down, we still emit the same keycode
3629 // in the key up as we did in the key down.
3630 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003631 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003632 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003633 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3635 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3636 ASSERT_EQ(KEY_UP, args.scanCode);
3637 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3638
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003639 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003640 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3643 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3644 ASSERT_EQ(KEY_UP, args.scanCode);
3645 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3646}
3647
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003648TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3649 // If the keyboard is not orientation aware,
3650 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003651 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003652
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003653 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003654 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003655 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003656 NotifyKeyArgs args;
3657
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003658 // Display id should be LogicalDisplayId::INVALID without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003663 ASSERT_EQ(ui::LogicalDisplayId::INVALID, args.displayId);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003664
Michael Wrighta9cf4192022-12-01 23:46:39 +00003665 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003670 ASSERT_EQ(ui::LogicalDisplayId::INVALID, args.displayId);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003671}
3672
3673TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3674 // If the keyboard is orientation aware,
3675 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003676 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003677
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003678 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003679 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003680 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003681 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003682 NotifyKeyArgs args;
3683
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003684 // Display id should be LogicalDisplayId::INVALID without any display configuration.
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003685 // ^--- already checked by the previous test
3686
Michael Wrighta9cf4192022-12-01 23:46:39 +00003687 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003688 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003689 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3693 ASSERT_EQ(DISPLAY_ID, args.displayId);
3694
Linnan Li13bf76a2024-05-05 19:18:02 +08003695 constexpr ui::LogicalDisplayId newDisplayId = ui::LogicalDisplayId{2};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003696 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003697 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003698 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003701 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3703 ASSERT_EQ(newDisplayId, args.displayId);
3704}
3705
Michael Wrightd02c5b62014-02-10 15:10:22 -08003706TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003707 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003708 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003709 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003710
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003711 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003712 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003713
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003714 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003715 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003716}
3717
Philip Junker4af3b3d2021-12-14 10:36:55 +01003718TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3719 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003720 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003721 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3722
3723 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3724 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3725 << "If a mapping is available, the result is equal to the mapping";
3726
3727 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3728 << "If no mapping is available, the result is the key location";
3729}
3730
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003732 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003733 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003734 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003736 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003737 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003739 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003740 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003741}
3742
3743TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003744 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003745 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003746 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003747
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003748 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003749
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003751 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003752 ASSERT_TRUE(flags[0]);
3753 ASSERT_FALSE(flags[1]);
3754}
3755
3756TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003757 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3758 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3759 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3760 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3761 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3762 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003763
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003764 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003765 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003766 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003767 // Initial metastate is AMETA_NONE.
3768 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769
3770 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003771 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3772 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3773 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003774
3775 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003778 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3779 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3780 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003781 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003782
3783 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003786 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3787 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3788 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003789 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790
3791 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003794 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3795 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3796 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003797 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003798
3799 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003802 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3803 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3804 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003805 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003806
3807 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003810 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3811 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3812 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003813 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003814
3815 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003818 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3819 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3820 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003821 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822}
3823
Chris Yea52ade12020-08-27 16:49:20 -07003824TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3825 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3826 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3827 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3828 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3829
3830 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003831 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003832 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3833
Chris Yea52ade12020-08-27 16:49:20 -07003834 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003835 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003836 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3837 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3838 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3839 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3840
3841 NotifyKeyArgs args;
3842 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3845 ASSERT_EQ(AMETA_NONE, args.metaState);
3846 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3847 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3848 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3849
3850 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003851 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3853 ASSERT_EQ(AMETA_NONE, args.metaState);
3854 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3855 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3856 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3857}
3858
Arthur Hung2c9a3342019-07-23 14:18:59 +08003859TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3860 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003861 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3862 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3863 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3864 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003865
3866 // keyboard 2.
3867 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003868 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003869 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003870 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003871 std::shared_ptr<InputDevice> device2 =
3872 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003873 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003874
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003875 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3876 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3877 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3878 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003879
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003880 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003881 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003882 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003883
Arpit Singh67ca6842023-04-26 14:43:16 +00003884 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003885 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003886 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3887 mFakePolicy
3888 ->getReaderConfiguration(),
3889 AINPUT_SOURCE_KEYBOARD,
3890 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003891 std::list<NotifyArgs> unused =
3892 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003893 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003894 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003895
3896 // Prepared displays and associated info.
3897 constexpr uint8_t hdmi1 = 0;
3898 constexpr uint8_t hdmi2 = 1;
3899 const std::string SECONDARY_UNIQUE_ID = "local:1";
3900
3901 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3902 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3903
3904 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003905 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003906 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003907 ASSERT_FALSE(device2->isEnabled());
3908
3909 // Prepare second display.
Linnan Li13bf76a2024-05-05 19:18:02 +08003910 constexpr ui::LogicalDisplayId newDisplayId = ui::LogicalDisplayId{2};
Michael Wrighta9cf4192022-12-01 23:46:39 +00003911 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003912 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003913 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003914 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003915 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003916 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003917 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003918
3919 // Device should be enabled after the associated display is found.
3920 ASSERT_TRUE(mDevice->isEnabled());
3921 ASSERT_TRUE(device2->isEnabled());
3922
3923 // Test pad key events
3924 ASSERT_NO_FATAL_FAILURE(
3925 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3926 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3927 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3928 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3929 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3930 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3931 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3932
3933 ASSERT_NO_FATAL_FAILURE(
3934 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3935 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3936 AKEYCODE_DPAD_RIGHT, newDisplayId));
3937 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3938 AKEYCODE_DPAD_DOWN, newDisplayId));
3939 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3940 AKEYCODE_DPAD_LEFT, newDisplayId));
3941}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942
arthurhungc903df12020-08-11 15:08:42 +08003943TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3944 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3945 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3946 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3947 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3948 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3949 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3950
3951 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003952 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003953 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003954 // Initial metastate is AMETA_NONE.
3955 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003956
3957 // Initialization should have turned all of the lights off.
3958 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3959 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3960 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3961
3962 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003965 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3966 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3967
3968 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3970 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003971 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3972 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3973
3974 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003977 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3978 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3979
3980 mFakeEventHub->removeDevice(EVENTHUB_ID);
3981 mReader->loopOnce();
3982
3983 // keyboard 2 should default toggle keys.
3984 const std::string USB2 = "USB2";
3985 const std::string DEVICE_NAME2 = "KEYBOARD2";
3986 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3987 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3988 std::shared_ptr<InputDevice> device2 =
3989 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003990 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003991 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3992 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3993 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3994 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3995 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3996 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3997
Arpit Singh67ca6842023-04-26 14:43:16 +00003998 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003999 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00004000 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
4001 mFakePolicy
4002 ->getReaderConfiguration(),
4003 AINPUT_SOURCE_KEYBOARD,
4004 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004005 std::list<NotifyArgs> unused =
4006 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004007 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004008 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004009
4010 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4011 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4012 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004013 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4014 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004015}
4016
Arthur Hungcb40a002021-08-03 14:31:01 +00004017TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4018 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4019 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4020 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4021
4022 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00004023 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00004024 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4025 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004026 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00004027 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004028 // Initial metastate is AMETA_NONE.
4029 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004030
4031 mReader->toggleCapsLockState(DEVICE_ID);
4032 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4033}
4034
Arthur Hungfb3cc112022-04-13 07:39:50 +00004035TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4036 // keyboard 1.
4037 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4038 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4039 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4040 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4041 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4042 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4043
4044 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00004045 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00004046 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4047
4048 // keyboard 2.
4049 const std::string USB2 = "USB2";
4050 const std::string DEVICE_NAME2 = "KEYBOARD2";
4051 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4052 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4053 std::shared_ptr<InputDevice> device2 =
4054 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4055 ftl::Flags<InputDeviceClass>(0));
4056 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4057 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4058 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4059 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4060 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4061 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4062
Arpit Singh67ca6842023-04-26 14:43:16 +00004063 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004064 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00004065 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
4066 mFakePolicy
4067 ->getReaderConfiguration(),
4068 AINPUT_SOURCE_KEYBOARD,
4069 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004070 std::list<NotifyArgs> unused =
4071 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004072 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004073 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004074
Arthur Hung95f68612022-04-07 14:08:22 +08004075 // Initial metastate is AMETA_NONE.
4076 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4077 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4078
4079 // Toggle num lock on and off.
4080 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4081 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004082 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4083 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4084 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4085
4086 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4087 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4088 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4089 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4090 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4091
4092 // Toggle caps lock on and off.
4093 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4094 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4095 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4096 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4097 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4098
4099 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4100 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4101 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4102 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4103 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4104
4105 // Toggle scroll lock on and off.
4106 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4107 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4108 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4109 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4110 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4111
4112 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4113 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4114 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4115 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4116 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4117}
4118
Arthur Hung2141d542022-08-23 07:45:21 +00004119TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4120 const int32_t USAGE_A = 0x070004;
4121 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4122 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4123
4124 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004125 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00004126 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4127 // Key down by scan code.
4128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4129 NotifyKeyArgs args;
4130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4131 ASSERT_EQ(DEVICE_ID, args.deviceId);
4132 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4133 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4134 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4135 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4136 ASSERT_EQ(KEY_HOME, args.scanCode);
4137 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4138
4139 // Disable device, it should synthesize cancellation events for down events.
4140 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004141 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00004142
4143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4144 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4145 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4146 ASSERT_EQ(KEY_HOME, args.scanCode);
4147 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4148}
4149
Zixuan Qufecb6062022-11-12 04:44:31 +00004150TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00004151 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4152 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00004153 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004154 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4155 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00004156
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004157 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00004158 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4159
4160 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004161 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00004162
4163 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
4164 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
4165 deviceInfo.getKeyboardLayoutInfo()->languageTag);
4166 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
4167 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004168 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
4169
4170 // Call change layout association with the same values: Generation shouldn't change
4171 generation = mReader->getContext()->getGeneration();
4172 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4173 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4174 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
4175 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00004176}
4177
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004178TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
4179 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
4180 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
4181
4182 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00004183 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004184 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4185 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004186 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004187
4188 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4189 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4190}
4191
Justin Chung71ddb432023-03-27 04:29:07 +00004192TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4193 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4194 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004195 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00004196 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4197 NotifyKeyArgs args;
4198
4199 // Key down
4200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4202 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4203}
4204
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004205// --- KeyboardInputMapperTest_ExternalDevice ---
4206
4207class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4208protected:
Chris Yea52ade12020-08-27 16:49:20 -07004209 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004210};
4211
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004212TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_AlphabeticKeyboard) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004213 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4214 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004215
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004216 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4217 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4218 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4219 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004220
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004221 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004222 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004223 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004224
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004226 NotifyKeyArgs args;
4227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4228 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4229
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004230 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4232 ASSERT_EQ(uint32_t(0), args.policyFlags);
4233
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004236 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004237
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004238 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4240 ASSERT_EQ(uint32_t(0), args.policyFlags);
4241
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004242 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4244 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4245
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004246 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4248 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4249}
4250
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004251TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_NoneAlphabeticKeyboard) {
4252 // For external devices, keys will trigger wake on key down. Media keys should not trigger
4253 // wake if triggered from external non-alphaebtic keyboard (e.g. headsets).
4254
4255 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4256 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4257 POLICY_FLAG_WAKE);
4258
4259 KeyboardInputMapper& mapper =
4260 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4261 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4262
4263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
4264 NotifyKeyArgs args;
4265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4266 ASSERT_EQ(uint32_t(0), args.policyFlags);
4267
4268 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
4269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4270 ASSERT_EQ(uint32_t(0), args.policyFlags);
4271
4272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
4273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4274 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4275
4276 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
4277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4278 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4279}
4280
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004281TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004282 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004283
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004284 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4285 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4286 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004287
Powei Fengd041c5d2019-05-03 17:11:33 -07004288 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004289 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004290 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004291 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004292
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004294 NotifyKeyArgs args;
4295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4296 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4297
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004298 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4300 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4301
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4304 ASSERT_EQ(uint32_t(0), args.policyFlags);
4305
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004306 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4308 ASSERT_EQ(uint32_t(0), args.policyFlags);
4309
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4312 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4313
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004314 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4316 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4317}
4318
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319// --- TouchInputMapperTest ---
4320
4321class TouchInputMapperTest : public InputMapperTest {
4322protected:
4323 static const int32_t RAW_X_MIN;
4324 static const int32_t RAW_X_MAX;
4325 static const int32_t RAW_Y_MIN;
4326 static const int32_t RAW_Y_MAX;
4327 static const int32_t RAW_TOUCH_MIN;
4328 static const int32_t RAW_TOUCH_MAX;
4329 static const int32_t RAW_TOOL_MIN;
4330 static const int32_t RAW_TOOL_MAX;
4331 static const int32_t RAW_PRESSURE_MIN;
4332 static const int32_t RAW_PRESSURE_MAX;
4333 static const int32_t RAW_ORIENTATION_MIN;
4334 static const int32_t RAW_ORIENTATION_MAX;
4335 static const int32_t RAW_DISTANCE_MIN;
4336 static const int32_t RAW_DISTANCE_MAX;
4337 static const int32_t RAW_TILT_MIN;
4338 static const int32_t RAW_TILT_MAX;
4339 static const int32_t RAW_ID_MIN;
4340 static const int32_t RAW_ID_MAX;
4341 static const int32_t RAW_SLOT_MIN;
4342 static const int32_t RAW_SLOT_MAX;
4343 static const float X_PRECISION;
4344 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004345 static const float X_PRECISION_VIRTUAL;
4346 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347
4348 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004349 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350
4351 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4352
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004353 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004354 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004355
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 enum Axes {
4357 POSITION = 1 << 0,
4358 TOUCH = 1 << 1,
4359 TOOL = 1 << 2,
4360 PRESSURE = 1 << 3,
4361 ORIENTATION = 1 << 4,
4362 MINOR = 1 << 5,
4363 ID = 1 << 6,
4364 DISTANCE = 1 << 7,
4365 TILT = 1 << 8,
4366 SLOT = 1 << 9,
4367 TOOL_TYPE = 1 << 10,
4368 };
4369
Michael Wrighta9cf4192022-12-01 23:46:39 +00004370 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004371 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004372 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004374 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 int32_t toRawX(float displayX);
4376 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004377 int32_t toRotatedRawX(float displayX);
4378 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004379 float toCookedX(float rawX, float rawY);
4380 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004382 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004384 float toDisplayY(int32_t rawY, int32_t displayHeight);
4385
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386};
4387
4388const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4389const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4390const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4391const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4392const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4393const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4394const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4395const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004396const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4397const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4399const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4400const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4401const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4402const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4403const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4404const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4405const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4406const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4407const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4408const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4409const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004410const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4411 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4412const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4413 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004414const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4415 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416
4417const float TouchInputMapperTest::GEOMETRIC_SCALE =
4418 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4419 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4420
4421const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4422 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4423 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4424};
4425
Michael Wrighta9cf4192022-12-01 23:46:39 +00004426void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004427 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4428 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004429}
4430
4431void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4432 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004433 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434}
4435
Michael Wrighta9cf4192022-12-01 23:46:39 +00004436void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004437 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4438 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4439 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004440}
4441
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004443 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4444 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4445 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4446 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447}
4448
Jason Gerecke489fda82012-09-07 17:19:40 -07004449void TouchInputMapperTest::prepareLocationCalibration() {
4450 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4451}
4452
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453int32_t TouchInputMapperTest::toRawX(float displayX) {
4454 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4455}
4456
4457int32_t TouchInputMapperTest::toRawY(float displayY) {
4458 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4459}
4460
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004461int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4462 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4463}
4464
4465int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4466 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4467}
4468
Jason Gerecke489fda82012-09-07 17:19:40 -07004469float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4470 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4471 return rawX;
4472}
4473
4474float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4475 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4476 return rawY;
4477}
4478
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004480 return toDisplayX(rawX, DISPLAY_WIDTH);
4481}
4482
4483float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4484 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004485}
4486
4487float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004488 return toDisplayY(rawY, DISPLAY_HEIGHT);
4489}
4490
4491float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4492 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493}
4494
4495
4496// --- SingleTouchInputMapperTest ---
4497
4498class SingleTouchInputMapperTest : public TouchInputMapperTest {
4499protected:
4500 void prepareButtons();
4501 void prepareAxes(int axes);
4502
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004503 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4504 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4505 void processUp(SingleTouchInputMapper& mappery);
4506 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4507 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4508 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4509 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4510 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4511 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512};
4513
4514void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004515 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516}
4517
4518void SingleTouchInputMapperTest::prepareAxes(int axes) {
4519 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004520 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4521 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522 }
4523 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004524 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4525 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526 }
4527 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004528 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4529 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004530 }
4531 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004532 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4533 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534 }
4535 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004536 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4537 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538 }
4539}
4540
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004541void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545}
4546
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004547void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550}
4551
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004552void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004554}
4555
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004556void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558}
4559
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004560void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4561 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004563}
4564
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004565void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567}
4568
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004569void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4570 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573}
4574
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004575void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4576 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578}
4579
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004580void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004581 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582}
4583
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585 prepareButtons();
4586 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00004587 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588
Josep del Río2d8c79a2023-01-23 19:33:50 +00004589 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004590}
4591
Michael Wrightd02c5b62014-02-10 15:10:22 -08004592TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593 prepareButtons();
4594 prepareAxes(POSITION);
4595 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00004596 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004597
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004598 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599}
4600
4601TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004603 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604 prepareButtons();
4605 prepareAxes(POSITION);
4606 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004607 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004608
4609 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004610 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611
4612 // Virtual key is down.
4613 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4614 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4615 processDown(mapper, x, y);
4616 processSync(mapper);
4617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4618
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004619 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620
4621 // Virtual key is up.
4622 processUp(mapper);
4623 processSync(mapper);
4624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4625
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004626 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627}
4628
4629TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004630 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004631 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632 prepareButtons();
4633 prepareAxes(POSITION);
4634 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004635 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636
4637 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004638 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639
4640 // Virtual key is down.
4641 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4642 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4643 processDown(mapper, x, y);
4644 processSync(mapper);
4645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4646
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004647 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648
4649 // Virtual key is up.
4650 processUp(mapper);
4651 processSync(mapper);
4652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4653
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004654 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655}
4656
4657TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004659 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660 prepareButtons();
4661 prepareAxes(POSITION);
4662 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004663 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004666 ASSERT_TRUE(
4667 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668 ASSERT_TRUE(flags[0]);
4669 ASSERT_FALSE(flags[1]);
4670}
4671
4672TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004674 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675 prepareButtons();
4676 prepareAxes(POSITION);
4677 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004678 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004679
arthurhungdcef2dc2020-08-11 14:47:50 +08004680 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681
4682 NotifyKeyArgs args;
4683
4684 // Press virtual key.
4685 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4686 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4687 processDown(mapper, x, y);
4688 processSync(mapper);
4689
4690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4691 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4692 ASSERT_EQ(DEVICE_ID, args.deviceId);
4693 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4694 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4695 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4696 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4697 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4698 ASSERT_EQ(KEY_HOME, args.scanCode);
4699 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4700 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4701
4702 // Release virtual key.
4703 processUp(mapper);
4704 processSync(mapper);
4705
4706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4707 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4708 ASSERT_EQ(DEVICE_ID, args.deviceId);
4709 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4710 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4711 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4712 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4713 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4714 ASSERT_EQ(KEY_HOME, args.scanCode);
4715 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4716 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4717
4718 // Should not have sent any motions.
4719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4720}
4721
4722TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004724 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725 prepareButtons();
4726 prepareAxes(POSITION);
4727 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004728 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729
arthurhungdcef2dc2020-08-11 14:47:50 +08004730 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731
4732 NotifyKeyArgs keyArgs;
4733
4734 // Press virtual key.
4735 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4736 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4737 processDown(mapper, x, y);
4738 processSync(mapper);
4739
4740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4741 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4742 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4743 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4744 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4745 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4746 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4747 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4748 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4749 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4750 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4751
4752 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4753 // into the display area.
4754 y -= 100;
4755 processMove(mapper, x, y);
4756 processSync(mapper);
4757
4758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4759 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4760 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4761 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4762 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4763 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4764 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4765 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4766 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4767 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4768 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4769 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4770
4771 NotifyMotionArgs motionArgs;
4772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4773 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4774 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4775 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4776 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4777 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4778 ASSERT_EQ(0, motionArgs.flags);
4779 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4780 ASSERT_EQ(0, motionArgs.buttonState);
4781 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004782 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004784 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4786 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4787 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4788 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4789 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4790
4791 // Keep moving out of bounds. Should generate a pointer move.
4792 y -= 50;
4793 processMove(mapper, x, y);
4794 processSync(mapper);
4795
4796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4797 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4798 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4799 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4800 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4801 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4802 ASSERT_EQ(0, motionArgs.flags);
4803 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4804 ASSERT_EQ(0, motionArgs.buttonState);
4805 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004806 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004808 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4810 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4811 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4812 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4813 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4814
4815 // Release out of bounds. Should generate a pointer up.
4816 processUp(mapper);
4817 processSync(mapper);
4818
4819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4820 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4821 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4822 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4823 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4824 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4825 ASSERT_EQ(0, motionArgs.flags);
4826 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4827 ASSERT_EQ(0, motionArgs.buttonState);
4828 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004829 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004830 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004831 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4833 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4834 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4835 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4836 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4837
4838 // Should not have sent any more keys or motions.
4839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4841}
4842
4843TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004844 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004845 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846 prepareButtons();
4847 prepareAxes(POSITION);
4848 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004849 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850
arthurhungdcef2dc2020-08-11 14:47:50 +08004851 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004852
4853 NotifyMotionArgs motionArgs;
4854
4855 // Initially go down out of bounds.
4856 int32_t x = -10;
4857 int32_t y = -10;
4858 processDown(mapper, x, y);
4859 processSync(mapper);
4860
4861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4862
4863 // Move into the display area. Should generate a pointer down.
4864 x = 50;
4865 y = 75;
4866 processMove(mapper, x, y);
4867 processSync(mapper);
4868
4869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4870 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4871 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4872 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4873 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4874 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4875 ASSERT_EQ(0, motionArgs.flags);
4876 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4877 ASSERT_EQ(0, motionArgs.buttonState);
4878 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004879 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004881 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4883 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4884 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4885 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4886 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4887
4888 // Release. Should generate a pointer up.
4889 processUp(mapper);
4890 processSync(mapper);
4891
4892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4893 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4894 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4895 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4896 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4897 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4898 ASSERT_EQ(0, motionArgs.flags);
4899 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4900 ASSERT_EQ(0, motionArgs.buttonState);
4901 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004902 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004904 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4906 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4907 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4908 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4909 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4910
4911 // Should not have sent any more keys or motions.
4912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4914}
4915
Santos Cordonfa5cf462017-04-05 10:37:00 -07004916TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004917 addConfigurationProperty("touch.deviceType", "touchScreen");
4918 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4919
Michael Wrighta9cf4192022-12-01 23:46:39 +00004920 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004921 prepareButtons();
4922 prepareAxes(POSITION);
4923 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004924 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004925
arthurhungdcef2dc2020-08-11 14:47:50 +08004926 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004927
4928 NotifyMotionArgs motionArgs;
4929
4930 // Down.
4931 int32_t x = 100;
4932 int32_t y = 125;
4933 processDown(mapper, x, y);
4934 processSync(mapper);
4935
4936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4937 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4938 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4939 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4940 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4941 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4942 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4943 ASSERT_EQ(0, motionArgs.flags);
4944 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4945 ASSERT_EQ(0, motionArgs.buttonState);
4946 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004947 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004948 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004949 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4951 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4952 1, 0, 0, 0, 0, 0, 0, 0));
4953 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4954 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4955 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4956
4957 // Move.
4958 x += 50;
4959 y += 75;
4960 processMove(mapper, x, y);
4961 processSync(mapper);
4962
4963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4964 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4965 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4966 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4967 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4968 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4970 ASSERT_EQ(0, motionArgs.flags);
4971 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4972 ASSERT_EQ(0, motionArgs.buttonState);
4973 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004974 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004975 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004976 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4978 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4979 1, 0, 0, 0, 0, 0, 0, 0));
4980 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4981 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4982 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4983
4984 // Up.
4985 processUp(mapper);
4986 processSync(mapper);
4987
4988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4989 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4990 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4991 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4992 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4993 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4994 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4995 ASSERT_EQ(0, motionArgs.flags);
4996 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4997 ASSERT_EQ(0, motionArgs.buttonState);
4998 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004999 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005000 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005001 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5003 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5004 1, 0, 0, 0, 0, 0, 0, 0));
5005 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5006 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5007 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5008
5009 // Should not have sent any more keys or motions.
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5012}
5013
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005016 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017 prepareButtons();
5018 prepareAxes(POSITION);
5019 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005020 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021
arthurhungdcef2dc2020-08-11 14:47:50 +08005022 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023
5024 NotifyMotionArgs motionArgs;
5025
5026 // Down.
5027 int32_t x = 100;
5028 int32_t y = 125;
5029 processDown(mapper, x, y);
5030 processSync(mapper);
5031
5032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5033 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5034 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5035 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5036 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5037 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5038 ASSERT_EQ(0, motionArgs.flags);
5039 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5040 ASSERT_EQ(0, motionArgs.buttonState);
5041 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005042 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005044 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5046 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5047 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5048 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5049 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5050
5051 // Move.
5052 x += 50;
5053 y += 75;
5054 processMove(mapper, x, y);
5055 processSync(mapper);
5056
5057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5058 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5059 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5060 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5061 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5063 ASSERT_EQ(0, motionArgs.flags);
5064 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5065 ASSERT_EQ(0, motionArgs.buttonState);
5066 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005067 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005069 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5071 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5072 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5073 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5074 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5075
5076 // Up.
5077 processUp(mapper);
5078 processSync(mapper);
5079
5080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5081 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5082 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5083 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5084 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5085 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5086 ASSERT_EQ(0, motionArgs.flags);
5087 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5088 ASSERT_EQ(0, motionArgs.buttonState);
5089 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005090 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005091 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005092 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5094 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5095 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5096 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5097 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5098
5099 // Should not have sent any more keys or motions.
5100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5102}
5103
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005104TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105 addConfigurationProperty("touch.deviceType", "touchScreen");
5106 prepareButtons();
5107 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005108 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5109 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005110 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111
5112 NotifyMotionArgs args;
5113
5114 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005115 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116 processDown(mapper, toRawX(50), toRawY(75));
5117 processSync(mapper);
5118
5119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5120 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5121 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5122
5123 processUp(mapper);
5124 processSync(mapper);
5125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5126}
5127
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005128TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129 addConfigurationProperty("touch.deviceType", "touchScreen");
5130 prepareButtons();
5131 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005132 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5133 // orientation-aware are affected by display rotation.
5134 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005135 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005136
5137 NotifyMotionArgs args;
5138
5139 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005140 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005141 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005142 processDown(mapper, toRawX(50), toRawY(75));
5143 processSync(mapper);
5144
5145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5146 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5147 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5148
5149 processUp(mapper);
5150 processSync(mapper);
5151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5152
5153 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005154 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005155 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005156 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157 processSync(mapper);
5158
5159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5160 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5161 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5162
5163 processUp(mapper);
5164 processSync(mapper);
5165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5166
5167 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005168 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005169 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5171 processSync(mapper);
5172
5173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5174 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5175 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5176
5177 processUp(mapper);
5178 processSync(mapper);
5179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5180
5181 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005182 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005183 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005184 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185 processSync(mapper);
5186
5187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5188 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5189 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5190
5191 processUp(mapper);
5192 processSync(mapper);
5193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5194}
5195
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005196TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5197 addConfigurationProperty("touch.deviceType", "touchScreen");
5198 prepareButtons();
5199 prepareAxes(POSITION);
5200 addConfigurationProperty("touch.orientationAware", "1");
5201 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5202 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005203 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005204 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005205 NotifyMotionArgs args;
5206
5207 // Orientation 0.
5208 processDown(mapper, toRawX(50), toRawY(75));
5209 processSync(mapper);
5210
5211 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5212 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5213 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5214
5215 processUp(mapper);
5216 processSync(mapper);
5217 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5218}
5219
5220TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5221 addConfigurationProperty("touch.deviceType", "touchScreen");
5222 prepareButtons();
5223 prepareAxes(POSITION);
5224 addConfigurationProperty("touch.orientationAware", "1");
5225 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5226 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005227 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005228 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005229 NotifyMotionArgs args;
5230
5231 // Orientation 90.
5232 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5233 processSync(mapper);
5234
5235 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5236 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5237 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5238
5239 processUp(mapper);
5240 processSync(mapper);
5241 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5242}
5243
5244TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5245 addConfigurationProperty("touch.deviceType", "touchScreen");
5246 prepareButtons();
5247 prepareAxes(POSITION);
5248 addConfigurationProperty("touch.orientationAware", "1");
5249 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5250 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005251 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005252 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005253 NotifyMotionArgs args;
5254
5255 // Orientation 180.
5256 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5257 processSync(mapper);
5258
5259 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5260 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5261 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5262
5263 processUp(mapper);
5264 processSync(mapper);
5265 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5266}
5267
5268TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5269 addConfigurationProperty("touch.deviceType", "touchScreen");
5270 prepareButtons();
5271 prepareAxes(POSITION);
5272 addConfigurationProperty("touch.orientationAware", "1");
5273 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5274 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005275 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005276 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005277 NotifyMotionArgs args;
5278
5279 // Orientation 270.
5280 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5281 processSync(mapper);
5282
5283 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5284 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5285 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5286
5287 processUp(mapper);
5288 processSync(mapper);
5289 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5290}
5291
5292TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5293 addConfigurationProperty("touch.deviceType", "touchScreen");
5294 prepareButtons();
5295 prepareAxes(POSITION);
5296 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5297 // orientation-aware are affected by display rotation.
5298 addConfigurationProperty("touch.orientationAware", "0");
5299 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005300 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005301
5302 NotifyMotionArgs args;
5303
5304 // Orientation 90, Rotation 0.
5305 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005306 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005307 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5308 processSync(mapper);
5309
5310 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5311 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5312 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5313
5314 processUp(mapper);
5315 processSync(mapper);
5316 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5317
5318 // Orientation 90, Rotation 90.
5319 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005320 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005321 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005322 processSync(mapper);
5323
5324 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5325 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5326 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5327
5328 processUp(mapper);
5329 processSync(mapper);
5330 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5331
5332 // Orientation 90, Rotation 180.
5333 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005334 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005335 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5336 processSync(mapper);
5337
5338 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5339 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5340 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5341
5342 processUp(mapper);
5343 processSync(mapper);
5344 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5345
5346 // Orientation 90, Rotation 270.
5347 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005348 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005349 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005350 processSync(mapper);
5351
5352 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5353 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5354 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5355
5356 processUp(mapper);
5357 processSync(mapper);
5358 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5359}
5360
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005361TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5362 addConfigurationProperty("touch.deviceType", "touchScreen");
5363 prepareButtons();
5364 prepareAxes(POSITION);
5365 addConfigurationProperty("touch.orientationAware", "1");
5366 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005367 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005368
5369 // Set a physical frame in the display viewport.
5370 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5371 viewport->physicalLeft = 20;
5372 viewport->physicalTop = 600;
5373 viewport->physicalRight = 30;
5374 viewport->physicalBottom = 610;
5375 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005376 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005377
5378 // Start the touch.
5379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5380 processSync(mapper);
5381
5382 // Expect all input starting outside the physical frame to be ignored.
5383 const std::array<Point, 6> outsidePoints = {
5384 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5385 for (const auto& p : outsidePoints) {
5386 processMove(mapper, toRawX(p.x), toRawY(p.y));
5387 processSync(mapper);
5388 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5389 }
5390
5391 // Move the touch into the physical frame.
5392 processMove(mapper, toRawX(25), toRawY(605));
5393 processSync(mapper);
5394 NotifyMotionArgs args;
5395 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5396 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5397 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5398 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5399
5400 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5401 for (const auto& p : outsidePoints) {
5402 processMove(mapper, toRawX(p.x), toRawY(p.y));
5403 processSync(mapper);
5404 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5405 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5406 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5407 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5408 }
5409
5410 processUp(mapper);
5411 processSync(mapper);
5412 EXPECT_NO_FATAL_FAILURE(
5413 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5414}
5415
Harry Cutts1db43992023-06-19 17:05:07 +00005416TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
Harry Cutts1db43992023-06-19 17:05:07 +00005417 addConfigurationProperty("touch.deviceType", "pointer");
5418 prepareAxes(POSITION);
5419 prepareDisplay(ui::ROTATION_0);
5420 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5421
5422 // Set a physical frame in the display viewport.
5423 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5424 viewport->physicalLeft = 20;
5425 viewport->physicalTop = 600;
5426 viewport->physicalRight = 30;
5427 viewport->physicalBottom = 610;
5428 mFakePolicy->updateViewport(*viewport);
5429 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5430
5431 // Start the touch.
5432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5433 processSync(mapper);
5434
5435 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5436 // produced.
5437 const std::array<Point, 6> outsidePoints = {
5438 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5439 for (const auto& p : outsidePoints) {
5440 processMove(mapper, toRawX(p.x), toRawY(p.y));
5441 processSync(mapper);
5442 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5443 }
5444}
5445
Michael Wrightd02c5b62014-02-10 15:10:22 -08005446TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005447 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005448 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005449 prepareButtons();
5450 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00005451 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005452
5453 // These calculations are based on the input device calibration documentation.
5454 int32_t rawX = 100;
5455 int32_t rawY = 200;
5456 int32_t rawPressure = 10;
5457 int32_t rawToolMajor = 12;
5458 int32_t rawDistance = 2;
5459 int32_t rawTiltX = 30;
5460 int32_t rawTiltY = 110;
5461
5462 float x = toDisplayX(rawX);
5463 float y = toDisplayY(rawY);
5464 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5465 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5466 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5467 float distance = float(rawDistance);
5468
5469 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5470 float tiltScale = M_PI / 180;
5471 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5472 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5473 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5474 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5475
5476 processDown(mapper, rawX, rawY);
5477 processPressure(mapper, rawPressure);
5478 processToolMajor(mapper, rawToolMajor);
5479 processDistance(mapper, rawDistance);
5480 processTilt(mapper, rawTiltX, rawTiltY);
5481 processSync(mapper);
5482
5483 NotifyMotionArgs args;
5484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5486 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5487 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5488}
5489
Jason Gerecke489fda82012-09-07 17:19:40 -07005490TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005491 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005492 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005493 prepareLocationCalibration();
5494 prepareButtons();
5495 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005496 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005497
5498 int32_t rawX = 100;
5499 int32_t rawY = 200;
5500
5501 float x = toDisplayX(toCookedX(rawX, rawY));
5502 float y = toDisplayY(toCookedY(rawX, rawY));
5503
5504 processDown(mapper, rawX, rawY);
5505 processSync(mapper);
5506
5507 NotifyMotionArgs args;
5508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5510 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5511}
5512
Michael Wrightd02c5b62014-02-10 15:10:22 -08005513TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005514 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005515 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005516 prepareButtons();
5517 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005518 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519
5520 NotifyMotionArgs motionArgs;
5521 NotifyKeyArgs keyArgs;
5522
5523 processDown(mapper, 100, 200);
5524 processSync(mapper);
5525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5526 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5527 ASSERT_EQ(0, motionArgs.buttonState);
5528
5529 // press BTN_LEFT, release BTN_LEFT
5530 processKey(mapper, BTN_LEFT, 1);
5531 processSync(mapper);
5532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5534 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5535
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5537 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5538 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5539
Michael Wrightd02c5b62014-02-10 15:10:22 -08005540 processKey(mapper, BTN_LEFT, 0);
5541 processSync(mapper);
5542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005543 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005544 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005545
5546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005547 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005548 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005549
5550 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5551 processKey(mapper, BTN_RIGHT, 1);
5552 processKey(mapper, BTN_MIDDLE, 1);
5553 processSync(mapper);
5554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5556 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5557 motionArgs.buttonState);
5558
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5560 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5561 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5562
5563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5564 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5565 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5566 motionArgs.buttonState);
5567
Michael Wrightd02c5b62014-02-10 15:10:22 -08005568 processKey(mapper, BTN_RIGHT, 0);
5569 processSync(mapper);
5570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005571 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005572 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005573
5574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005576 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005577
5578 processKey(mapper, BTN_MIDDLE, 0);
5579 processSync(mapper);
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005581 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005582 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005583
5584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005586 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005587
5588 // press BTN_BACK, release BTN_BACK
5589 processKey(mapper, BTN_BACK, 1);
5590 processSync(mapper);
5591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5592 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5593 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005594
Michael Wrightd02c5b62014-02-10 15:10:22 -08005595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005596 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005597 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5598
5599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5600 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5601 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005602
5603 processKey(mapper, BTN_BACK, 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(0, 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(0, motionArgs.buttonState);
5612
Michael Wrightd02c5b62014-02-10 15:10:22 -08005613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5614 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5615 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5616
5617 // press BTN_SIDE, release BTN_SIDE
5618 processKey(mapper, BTN_SIDE, 1);
5619 processSync(mapper);
5620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5621 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5622 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005623
Michael Wrightd02c5b62014-02-10 15:10:22 -08005624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005626 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5627
5628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5629 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5630 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005631
5632 processKey(mapper, BTN_SIDE, 0);
5633 processSync(mapper);
5634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005635 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005636 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005637
5638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005640 ASSERT_EQ(0, motionArgs.buttonState);
5641
Michael Wrightd02c5b62014-02-10 15:10:22 -08005642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5643 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5644 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5645
5646 // press BTN_FORWARD, release BTN_FORWARD
5647 processKey(mapper, BTN_FORWARD, 1);
5648 processSync(mapper);
5649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5650 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5651 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005652
Michael Wrightd02c5b62014-02-10 15:10:22 -08005653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005655 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5656
5657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5658 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5659 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005660
5661 processKey(mapper, BTN_FORWARD, 0);
5662 processSync(mapper);
5663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005664 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005665 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005666
5667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005669 ASSERT_EQ(0, motionArgs.buttonState);
5670
Michael Wrightd02c5b62014-02-10 15:10:22 -08005671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5672 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5673 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5674
5675 // press BTN_EXTRA, release BTN_EXTRA
5676 processKey(mapper, BTN_EXTRA, 1);
5677 processSync(mapper);
5678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5679 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5680 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005681
Michael Wrightd02c5b62014-02-10 15:10:22 -08005682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005684 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5685
5686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5687 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5688 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005689
5690 processKey(mapper, BTN_EXTRA, 0);
5691 processSync(mapper);
5692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005693 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005694 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005695
5696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005698 ASSERT_EQ(0, motionArgs.buttonState);
5699
Michael Wrightd02c5b62014-02-10 15:10:22 -08005700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5701 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5702 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5703
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5705
Michael Wrightd02c5b62014-02-10 15:10:22 -08005706 // press BTN_STYLUS, release BTN_STYLUS
5707 processKey(mapper, BTN_STYLUS, 1);
5708 processSync(mapper);
5709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005711 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5712
5713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5714 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5715 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005716
5717 processKey(mapper, BTN_STYLUS, 0);
5718 processSync(mapper);
5719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005720 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005721 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005722
5723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005725 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005726
5727 // press BTN_STYLUS2, release BTN_STYLUS2
5728 processKey(mapper, BTN_STYLUS2, 1);
5729 processSync(mapper);
5730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5731 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005732 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5733
5734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5735 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5736 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005737
5738 processKey(mapper, BTN_STYLUS2, 0);
5739 processSync(mapper);
5740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005741 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005742 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005743
5744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005746 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005747
5748 // release touch
5749 processUp(mapper);
5750 processSync(mapper);
5751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5752 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5753 ASSERT_EQ(0, motionArgs.buttonState);
5754}
5755
5756TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005757 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005758 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005759 prepareButtons();
5760 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005761 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005762
5763 NotifyMotionArgs motionArgs;
5764
5765 // default tool type is finger
5766 processDown(mapper, 100, 200);
5767 processSync(mapper);
5768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5769 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005770 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005771
5772 // eraser
5773 processKey(mapper, BTN_TOOL_RUBBER, 1);
5774 processSync(mapper);
5775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005777 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005778
5779 // stylus
5780 processKey(mapper, BTN_TOOL_RUBBER, 0);
5781 processKey(mapper, BTN_TOOL_PEN, 1);
5782 processSync(mapper);
5783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5784 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005785 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005786
5787 // brush
5788 processKey(mapper, BTN_TOOL_PEN, 0);
5789 processKey(mapper, BTN_TOOL_BRUSH, 1);
5790 processSync(mapper);
5791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005793 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005794
5795 // pencil
5796 processKey(mapper, BTN_TOOL_BRUSH, 0);
5797 processKey(mapper, BTN_TOOL_PENCIL, 1);
5798 processSync(mapper);
5799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005801 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005802
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005803 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005804 processKey(mapper, BTN_TOOL_PENCIL, 0);
5805 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5806 processSync(mapper);
5807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5808 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005809 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005810
5811 // mouse
5812 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5813 processKey(mapper, BTN_TOOL_MOUSE, 1);
5814 processSync(mapper);
5815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5816 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005817 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005818
5819 // lens
5820 processKey(mapper, BTN_TOOL_MOUSE, 0);
5821 processKey(mapper, BTN_TOOL_LENS, 1);
5822 processSync(mapper);
5823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5824 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005825 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005826
5827 // double-tap
5828 processKey(mapper, BTN_TOOL_LENS, 0);
5829 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5830 processSync(mapper);
5831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005833 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005834
5835 // triple-tap
5836 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5837 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5838 processSync(mapper);
5839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005841 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005842
5843 // quad-tap
5844 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5845 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5846 processSync(mapper);
5847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005849 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005850
5851 // finger
5852 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5853 processKey(mapper, BTN_TOOL_FINGER, 1);
5854 processSync(mapper);
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5856 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005857 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005858
5859 // stylus trumps finger
5860 processKey(mapper, BTN_TOOL_PEN, 1);
5861 processSync(mapper);
5862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005864 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005865
5866 // eraser trumps stylus
5867 processKey(mapper, BTN_TOOL_RUBBER, 1);
5868 processSync(mapper);
5869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5870 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005871 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005872
5873 // mouse trumps eraser
5874 processKey(mapper, BTN_TOOL_MOUSE, 1);
5875 processSync(mapper);
5876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5877 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005878 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879
5880 // back to default tool type
5881 processKey(mapper, BTN_TOOL_MOUSE, 0);
5882 processKey(mapper, BTN_TOOL_RUBBER, 0);
5883 processKey(mapper, BTN_TOOL_PEN, 0);
5884 processKey(mapper, BTN_TOOL_FINGER, 0);
5885 processSync(mapper);
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005888 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005889}
5890
5891TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005892 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005893 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005894 prepareButtons();
5895 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005896 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005897 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005898
5899 NotifyMotionArgs motionArgs;
5900
5901 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5902 processKey(mapper, BTN_TOOL_FINGER, 1);
5903 processMove(mapper, 100, 200);
5904 processSync(mapper);
5905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5906 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5908 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5909
5910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5911 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5913 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5914
5915 // move a little
5916 processMove(mapper, 150, 250);
5917 processSync(mapper);
5918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5919 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5920 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5921 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5922
5923 // down when BTN_TOUCH is pressed, pressure defaults to 1
5924 processKey(mapper, BTN_TOUCH, 1);
5925 processSync(mapper);
5926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5927 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5929 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5930
5931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5932 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5934 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5935
5936 // up when BTN_TOUCH is released, hover restored
5937 processKey(mapper, BTN_TOUCH, 0);
5938 processSync(mapper);
5939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5940 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5942 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5943
5944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5945 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5947 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5948
5949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5950 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5952 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5953
5954 // exit hover when pointer goes away
5955 processKey(mapper, BTN_TOOL_FINGER, 0);
5956 processSync(mapper);
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5958 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5960 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5961}
5962
5963TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005964 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005965 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005966 prepareButtons();
5967 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005968 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005969
5970 NotifyMotionArgs motionArgs;
5971
5972 // initially hovering because pressure is 0
5973 processDown(mapper, 100, 200);
5974 processPressure(mapper, 0);
5975 processSync(mapper);
5976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5977 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5979 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5980
5981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5982 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5984 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5985
5986 // move a little
5987 processMove(mapper, 150, 250);
5988 processSync(mapper);
5989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5990 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5992 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5993
5994 // down when pressure is non-zero
5995 processPressure(mapper, RAW_PRESSURE_MAX);
5996 processSync(mapper);
5997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5998 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5999 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6000 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6001
6002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6003 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6005 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6006
6007 // up when pressure becomes 0, hover restored
6008 processPressure(mapper, 0);
6009 processSync(mapper);
6010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6011 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6012 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6013 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6014
6015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6016 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6018 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6019
6020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6021 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6023 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6024
6025 // exit hover when pointer goes away
6026 processUp(mapper);
6027 processSync(mapper);
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6029 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6030 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6031 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6032}
6033
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006034TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6035 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006036 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006037 prepareButtons();
6038 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006039 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006040
6041 // Touch down.
6042 processDown(mapper, 100, 200);
6043 processPressure(mapper, 1);
6044 processSync(mapper);
6045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6046 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6047
6048 // Reset the mapper. This should cancel the ongoing gesture.
6049 resetMapper(mapper, ARBITRARY_TIME);
6050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6051 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6052
6053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6054}
6055
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006056TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6057 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006058 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006059 prepareButtons();
6060 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006061 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006062
6063 // Set the initial state for the touch pointer.
6064 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6065 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6066 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6067 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6068
6069 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006070 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6071 // does not generate any events.
6072 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006073
6074 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6075 // the recreated touch state to generate a down event.
6076 processSync(mapper);
6077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6078 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6079
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6081}
6082
lilinnan687e58f2022-07-19 16:00:50 +08006083TEST_F(SingleTouchInputMapperTest,
6084 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6085 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006086 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006087 prepareButtons();
6088 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006089 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006090 NotifyMotionArgs motionArgs;
6091
6092 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006093 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006094 processSync(mapper);
6095
6096 // We should receive a down event
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6098 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6099
6100 // Change display id
6101 clearViewports();
6102 prepareSecondaryDisplay(ViewportType::INTERNAL);
6103
6104 // We should receive a cancel event
6105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6106 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6107 // Then receive reset called
6108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6109}
6110
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006111TEST_F(SingleTouchInputMapperTest,
6112 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6113 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006114 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006115 prepareButtons();
6116 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006117 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6119 NotifyMotionArgs motionArgs;
6120
6121 // Start a new gesture.
6122 processDown(mapper, 100, 200);
6123 processSync(mapper);
6124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6125 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6126
6127 // Make the viewport inactive. This will put the device in disabled mode.
6128 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6129 viewport->isActive = false;
6130 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006131 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006132
6133 // We should receive a cancel event for the ongoing gesture.
6134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6135 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6136 // Then we should be notified that the device was reset.
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6138
6139 // No events are generated while the viewport is inactive.
6140 processMove(mapper, 101, 201);
6141 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006142 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006143 processSync(mapper);
6144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6145
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006146 // Start a new gesture while the viewport is still inactive.
6147 processDown(mapper, 300, 400);
6148 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6149 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6150 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6151 processSync(mapper);
6152
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006153 // Make the viewport active again. The device should resume processing events.
6154 viewport->isActive = true;
6155 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006156 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006157
6158 // The device is reset because it changes back to direct mode, without generating any events.
6159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6161
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006162 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006163 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6165 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006166
6167 // No more events.
6168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6170}
6171
Prabir Pradhan211ba622022-10-31 21:09:21 +00006172TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6173 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006174 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006175 prepareButtons();
6176 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006177 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6179
6180 // Press a stylus button.
6181 processKey(mapper, BTN_STYLUS, 1);
6182 processSync(mapper);
6183
6184 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6185 processDown(mapper, 100, 200);
6186 processSync(mapper);
6187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6188 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6189 WithCoords(toDisplayX(100), toDisplayY(200)),
6190 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6192 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6193 WithCoords(toDisplayX(100), toDisplayY(200)),
6194 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6195
6196 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6197 // the button has not actually been released, since there will be no pointers through which the
6198 // button state can be reported. The event is generated at the location of the pointer before
6199 // it went up.
6200 processUp(mapper);
6201 processSync(mapper);
6202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6203 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6204 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6206 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6207 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6208}
6209
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006210TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6211 addConfigurationProperty("touch.deviceType", "touchScreen");
6212 prepareDisplay(ui::ROTATION_0);
6213 prepareButtons();
6214 prepareAxes(POSITION);
6215
6216 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6217
Arpit Singha8c236b2023-04-25 13:56:05 +00006218 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6220
6221 // Press a stylus button.
6222 processKey(mapper, BTN_STYLUS, 1);
6223 processSync(mapper);
6224
6225 // Start a touch gesture and ensure that the stylus button is not reported.
6226 processDown(mapper, 100, 200);
6227 processSync(mapper);
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6229 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6230
6231 // Release and press the stylus button again.
6232 processKey(mapper, BTN_STYLUS, 0);
6233 processSync(mapper);
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6235 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6236 processKey(mapper, BTN_STYLUS, 1);
6237 processSync(mapper);
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6239 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6240
6241 // Release the touch gesture.
6242 processUp(mapper);
6243 processSync(mapper);
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6245 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6246
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6248}
6249
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006250TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6251 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6252 prepareDisplay(ui::ROTATION_0);
6253 prepareButtons();
6254 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006255 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6257
6258 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6259}
6260
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006261TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6262 // Initialize the device without setting device source to touch navigation.
6263 addConfigurationProperty("touch.deviceType", "touchScreen");
6264 prepareDisplay(ui::ROTATION_0);
6265 prepareButtons();
6266 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006267 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006268
6269 // Ensure that the device is created as a touchscreen, not touch navigation.
6270 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6271
6272 // Add device type association after the device was created.
6273 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6274
6275 // Send update to the mapper.
6276 std::list<NotifyArgs> unused2 =
6277 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006278 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006279
6280 // Check whether device type update was successful.
6281 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6282}
6283
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006284TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6285 // Initialize the device without setting device source to touch navigation.
6286 addConfigurationProperty("touch.deviceType", "touchScreen");
6287 prepareDisplay(ui::ROTATION_0);
6288 prepareButtons();
6289 prepareAxes(POSITION);
6290 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6291
6292 // Set a physical frame in the display viewport.
6293 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6294 viewport->physicalLeft = 0;
6295 viewport->physicalTop = 0;
6296 viewport->physicalRight = DISPLAY_WIDTH / 2;
6297 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6298 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006299 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006300
Arpit Singha8c236b2023-04-25 13:56:05 +00006301 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006302
6303 // Hovering inside the physical frame produces events.
6304 processKey(mapper, BTN_TOOL_PEN, 1);
6305 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6306 processSync(mapper);
6307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6308 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6310 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6311
6312 // Leaving the physical frame ends the hovering gesture.
6313 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6314 processSync(mapper);
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6316 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6317
6318 // Moving outside the physical frame does not produce events.
6319 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6320 processSync(mapper);
6321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6322
6323 // Re-entering the physical frame produces events.
6324 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6325 processSync(mapper);
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6327 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6329 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6330}
6331
Prabir Pradhan5632d622021-09-06 07:57:20 -07006332// --- TouchDisplayProjectionTest ---
6333
6334class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6335public:
6336 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6337 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6338 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006339 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6340 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6341 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006342 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006343 auto rotatedWidth = naturalDisplayWidth;
6344 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006345 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006346 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006347 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006348 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006349 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006350 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006351 inverseRotationFlags = ui::Transform::ROT_180;
6352 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006353 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006354 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006355 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006356 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006357 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006358 inverseRotationFlags = ui::Transform::ROT_0;
6359 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006360 }
6361
Prabir Pradhana9df3162022-12-05 23:57:27 +00006362 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006363 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6364
6365 std::optional<DisplayViewport> internalViewport =
6366 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6367 DisplayViewport& v = *internalViewport;
6368 v.displayId = DISPLAY_ID;
6369 v.orientation = orientation;
6370
6371 v.logicalLeft = 0;
6372 v.logicalTop = 0;
6373 v.logicalRight = 100;
6374 v.logicalBottom = 100;
6375
6376 v.physicalLeft = rotatedPhysicalDisplay.left;
6377 v.physicalTop = rotatedPhysicalDisplay.top;
6378 v.physicalRight = rotatedPhysicalDisplay.right;
6379 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6380
Prabir Pradhana9df3162022-12-05 23:57:27 +00006381 v.deviceWidth = rotatedWidth;
6382 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006383
6384 v.isActive = true;
6385 v.uniqueId = UNIQUE_ID;
6386 v.type = ViewportType::INTERNAL;
6387 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006388 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006389 }
6390
6391 void assertReceivedMove(const Point& point) {
6392 NotifyMotionArgs motionArgs;
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006395 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6397 1, 0, 0, 0, 0, 0, 0, 0));
6398 }
6399};
6400
6401TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6402 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006403 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006404
6405 prepareButtons();
6406 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006407 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006408
6409 NotifyMotionArgs motionArgs;
6410
6411 // Configure the DisplayViewport such that the logical display maps to a subsection of
6412 // the display panel called the physical display. Here, the physical display is bounded by the
6413 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6414 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6415 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6416 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6417
Michael Wrighta9cf4192022-12-01 23:46:39 +00006418 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006419 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6420
6421 // Touches outside the physical display should be ignored, and should not generate any
6422 // events. Ensure touches at the following points that lie outside of the physical display
6423 // area do not generate any events.
6424 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6425 processDown(mapper, toRawX(point.x), toRawY(point.y));
6426 processSync(mapper);
6427 processUp(mapper);
6428 processSync(mapper);
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6430 << "Unexpected event generated for touch outside physical display at point: "
6431 << point.x << ", " << point.y;
6432 }
6433 }
6434}
6435
6436TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
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
Michael Wrighta9cf4192022-12-01 23:46:39 +00006451 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006452 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6453
6454 // Touches that start outside the physical display should be ignored until it enters the
6455 // physical display bounds, at which point it should generate a down event. Start a touch at
6456 // the point (5, 100), which is outside the physical display bounds.
6457 static const Point kOutsidePoint{5, 100};
6458 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6459 processSync(mapper);
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6461
6462 // Move the touch into the physical display area. This should generate a pointer down.
6463 processMove(mapper, toRawX(11), toRawY(21));
6464 processSync(mapper);
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6466 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006467 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006468 ASSERT_NO_FATAL_FAILURE(
6469 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6470
6471 // Move the touch inside the physical display area. This should generate a pointer move.
6472 processMove(mapper, toRawX(69), toRawY(159));
6473 processSync(mapper);
6474 assertReceivedMove({69, 159});
6475
6476 // Move outside the physical display area. Since the pointer is already down, this should
6477 // now continue generating events.
6478 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6479 processSync(mapper);
6480 assertReceivedMove(kOutsidePoint);
6481
6482 // Release. This should generate a pointer up.
6483 processUp(mapper);
6484 processSync(mapper);
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6486 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6488 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6489
6490 // Ensure no more events were generated.
6491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6493 }
6494}
6495
Prabir Pradhana9df3162022-12-05 23:57:27 +00006496// --- TouchscreenPrecisionTests ---
6497
6498// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6499// in various orientations and with different display rotations. We configure the touchscreen to
6500// have a higher resolution than that of the display by an integer scale factor in each axis so that
6501// we can enforce that coordinates match precisely as expected.
6502class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6503 public ::testing::WithParamInterface<ui::Rotation> {
6504public:
6505 void SetUp() override {
6506 SingleTouchInputMapperTest::SetUp();
6507
6508 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6509 // four times the resolution of the display in the Y axis.
6510 prepareButtons();
6511 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006512 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6513 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006514 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006515 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6516 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006517 }
6518
6519 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6520 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6521 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6522 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6523
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006524 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6525 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6526
6527 static const int32_t PRECISION_RAW_X_FLAT = 16;
6528 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6529
6530 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6531 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6532
Prabir Pradhana9df3162022-12-05 23:57:27 +00006533 static const std::array<Point, 4> kRawCorners;
6534};
6535
6536const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6537 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6538 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6539 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6540 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6541}};
6542
6543// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6544// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6545// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6546TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6547 enum class Orientation {
6548 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6549 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6550 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6551 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6552 ftl_last = ORIENTATION_270,
6553 };
6554 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6555 Orientation::ORIENTATION_270;
6556 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6557 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6558 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6559 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6560 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6561 };
6562
6563 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6564
6565 // Configure the touchscreen as being installed in the one of the four different orientations
6566 // relative to the display.
6567 addConfigurationProperty("touch.deviceType", "touchScreen");
6568 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6569 prepareDisplay(ui::ROTATION_0);
6570
Arpit Singha8c236b2023-04-25 13:56:05 +00006571 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00006572
6573 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6574 // orientations of either 90 or 270) this means the display's natural resolution will be
6575 // flipped.
6576 const bool displayRotated =
6577 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6578 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6579 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6580 const Rect physicalFrame{0, 0, width, height};
6581 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6582
6583 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6584 const float expectedPrecisionX = displayRotated ? 4 : 2;
6585 const float expectedPrecisionY = displayRotated ? 2 : 4;
6586
6587 // Test all four corners.
6588 for (int i = 0; i < 4; i++) {
6589 const auto& raw = kRawCorners[i];
6590 processDown(mapper, raw.x, raw.y);
6591 processSync(mapper);
6592 const auto& expected = expectedPoints[i];
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6594 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6595 WithCoords(expected.x, expected.y),
6596 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6597 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6598 << "with touchscreen orientation "
6599 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6600 << expected.x << ", " << expected.y << ").";
6601 processUp(mapper);
6602 processSync(mapper);
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6604 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6605 WithCoords(expected.x, expected.y))));
6606 }
6607}
6608
Prabir Pradhan82687402022-12-06 01:32:53 +00006609TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6610 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6611 kMappedCorners = {
6612 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6613 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6614 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6615 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
6616 };
6617
6618 const ui::Rotation displayRotation = GetParam();
6619
6620 addConfigurationProperty("touch.deviceType", "touchScreen");
6621 prepareDisplay(displayRotation);
6622
Arpit Singha8c236b2023-04-25 13:56:05 +00006623 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00006624
6625 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6626
6627 // Test all four corners.
6628 for (int i = 0; i < 4; i++) {
6629 const auto& expected = expectedPoints[i];
6630 const auto& raw = kRawCorners[i];
6631 processDown(mapper, raw.x, raw.y);
6632 processSync(mapper);
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6634 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6635 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
6636 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6637 << "with display rotation " << ui::toCString(displayRotation)
6638 << ", expected point (" << expected.x << ", " << expected.y << ").";
6639 processUp(mapper);
6640 processSync(mapper);
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6642 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6643 WithCoords(expected.x, expected.y))));
6644 }
6645}
6646
Prabir Pradhan3e798762022-12-02 21:02:11 +00006647TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
6648 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
6649 kMappedCorners = {
6650 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6651 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
6652 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
6653 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
6654 };
6655
6656 const ui::Rotation displayRotation = GetParam();
6657
6658 addConfigurationProperty("touch.deviceType", "touchScreen");
6659 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6660
Arpit Singha8c236b2023-04-25 13:56:05 +00006661 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00006662
6663 // Ori 270, so width and height swapped
6664 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
6665 prepareDisplay(displayRotation);
6666 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
6667
6668 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6669
6670 // Test all four corners.
6671 for (int i = 0; i < 4; i++) {
6672 const auto& expected = expectedPoints[i];
6673 const auto& raw = kRawCorners[i];
6674 processDown(mapper, raw.x, raw.y);
6675 processSync(mapper);
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6677 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6678 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
6679 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6680 << "with display rotation " << ui::toCString(displayRotation)
6681 << ", expected point (" << expected.x << ", " << expected.y << ").";
6682 processUp(mapper);
6683 processSync(mapper);
6684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6685 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6686 WithCoords(expected.x, expected.y))));
6687 }
6688}
6689
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006690TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
6691 const ui::Rotation displayRotation = GetParam();
6692
6693 addConfigurationProperty("touch.deviceType", "touchScreen");
6694 prepareDisplay(displayRotation);
6695
6696 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00006697 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006698
6699 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
6700 // MotionRanges use display pixels as their units
6701 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
6702 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
6703
6704 // The MotionRanges should be oriented in the rotated display's coordinate space
6705 const bool displayRotated =
6706 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
6707
6708 constexpr float MAX_X = 479.5;
6709 constexpr float MAX_Y = 799.75;
6710 EXPECT_EQ(xRange->min, 0.f);
6711 EXPECT_EQ(yRange->min, 0.f);
6712 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
6713 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
6714
6715 EXPECT_EQ(xRange->flat, 8.f);
6716 EXPECT_EQ(yRange->flat, 8.f);
6717
6718 EXPECT_EQ(xRange->fuzz, 2.f);
6719 EXPECT_EQ(yRange->fuzz, 2.f);
6720
6721 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
6722 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
6723}
6724
Prabir Pradhana9df3162022-12-05 23:57:27 +00006725// Run the precision tests for all rotations.
6726INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
6727 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6728 ui::ROTATION_270),
6729 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6730 return ftl::enum_string(testParamInfo.param);
6731 });
6732
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006733// --- ExternalStylusFusionTest ---
6734
6735class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6736public:
6737 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6738 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006739 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006740 prepareButtons();
6741 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006742 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006743
6744 mStylusState.when = ARBITRARY_TIME;
6745 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006746 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006747 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006748 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006749 processExternalStylusState(mapper);
6750 return mapper;
6751 }
6752
6753 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6754 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6755 for (const NotifyArgs& args : generatedArgs) {
6756 mFakeListener->notify(args);
6757 }
6758 // Loop the reader to flush the input listener queue.
6759 mReader->loopOnce();
6760 return generatedArgs;
6761 }
6762
6763protected:
6764 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006765
6766 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6767 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006768 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006769
6770 // The first pointer is withheld.
6771 processDown(mapper, 100, 200);
6772 processSync(mapper);
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6774 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6775 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6776
6777 // The external stylus reports pressure. The withheld finger pointer is released as a
6778 // stylus.
6779 mStylusState.pressure = 1.f;
6780 processExternalStylusState(mapper);
6781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6782 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6783 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6784
6785 // Subsequent pointer events are not withheld.
6786 processMove(mapper, 101, 201);
6787 processSync(mapper);
6788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6789 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6790
6791 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6793 }
6794
6795 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6796 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6797
6798 // Releasing the touch pointer ends the gesture.
6799 processUp(mapper);
6800 processSync(mapper);
6801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006802 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006803 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006804
6805 mStylusState.pressure = 0.f;
6806 processExternalStylusState(mapper);
6807 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6809 }
6810
6811 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006812 // When stylus fusion is not successful, events should be reported with the original source.
6813 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006814 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006815 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006816
6817 // The first pointer is withheld when an external stylus is connected,
6818 // and a timeout is requested.
6819 processDown(mapper, 100, 200);
6820 processSync(mapper);
6821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6822 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6823 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6824
6825 // If the timeout expires early, it is requested again.
6826 handleTimeout(mapper, ARBITRARY_TIME + 1);
6827 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6828 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6829
6830 // When the timeout expires, the withheld touch is released as a finger pointer.
6831 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6833 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6834
6835 // Subsequent pointer events are not withheld.
6836 processMove(mapper, 101, 201);
6837 processSync(mapper);
6838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6839 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6840 processUp(mapper);
6841 processSync(mapper);
6842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6843 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6844
6845 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6847 }
6848
6849private:
6850 InputDeviceInfo mExternalStylusDeviceInfo{};
6851};
6852
6853TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6854 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006855 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006856}
6857
6858TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6859 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6860 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6861}
6862
6863TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6864 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6865 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6866}
6867
6868// Test a successful stylus fusion gesture where the pressure is reported by the external
6869// before the touch is reported by the touchscreen.
6870TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6871 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006872 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006873
6874 // The external stylus reports pressure first. It is ignored for now.
6875 mStylusState.pressure = 1.f;
6876 processExternalStylusState(mapper);
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6878 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6879
6880 // When the touch goes down afterwards, it is reported as a stylus pointer.
6881 processDown(mapper, 100, 200);
6882 processSync(mapper);
6883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6884 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6885 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6886
6887 processMove(mapper, 101, 201);
6888 processSync(mapper);
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6890 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6891 processUp(mapper);
6892 processSync(mapper);
6893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6894 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6895
6896 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6898}
6899
6900TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6901 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6902
6903 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6904 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6905
6906 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6907 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6908 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6909 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6910}
6911
6912TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6913 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006914 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006915
6916 mStylusState.pressure = 0.8f;
6917 processExternalStylusState(mapper);
6918 processDown(mapper, 100, 200);
6919 processSync(mapper);
6920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6921 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6922 WithPressure(0.8f))));
6923 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6924
6925 // The external stylus reports a pressure change. We wait for some time for a touch event.
6926 mStylusState.pressure = 0.6f;
6927 processExternalStylusState(mapper);
6928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6929 ASSERT_NO_FATAL_FAILURE(
6930 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6931
6932 // If a touch is reported within the timeout, it reports the updated pressure.
6933 processMove(mapper, 101, 201);
6934 processSync(mapper);
6935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6936 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6937 WithPressure(0.6f))));
6938 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6939
6940 // There is another pressure change.
6941 mStylusState.pressure = 0.5f;
6942 processExternalStylusState(mapper);
6943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6944 ASSERT_NO_FATAL_FAILURE(
6945 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6946
6947 // If a touch is not reported within the timeout, a move event is generated to report
6948 // the new pressure.
6949 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6951 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6952 WithPressure(0.5f))));
6953
6954 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6955 // repeated indefinitely.
6956 mStylusState.pressure = 0.0f;
6957 processExternalStylusState(mapper);
6958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6959 ASSERT_NO_FATAL_FAILURE(
6960 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6961 processMove(mapper, 102, 202);
6962 processSync(mapper);
6963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6964 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6965 WithPressure(0.5f))));
6966 processMove(mapper, 103, 203);
6967 processSync(mapper);
6968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6969 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6970 WithPressure(0.5f))));
6971
6972 processUp(mapper);
6973 processSync(mapper);
6974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006975 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006976 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006977
6978 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6980}
6981
6982TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6983 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006984 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006985
6986 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006987 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006988 processExternalStylusState(mapper);
6989 processDown(mapper, 100, 200);
6990 processSync(mapper);
6991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6992 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006993 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006994 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6995
6996 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006997 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006998 processExternalStylusState(mapper);
6999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7000 ASSERT_NO_FATAL_FAILURE(
7001 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7002
7003 // If a touch is reported within the timeout, it reports the updated pressure.
7004 processMove(mapper, 101, 201);
7005 processSync(mapper);
7006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7007 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007008 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007009 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7010
7011 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007012 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007013 processExternalStylusState(mapper);
7014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7015 ASSERT_NO_FATAL_FAILURE(
7016 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7017
7018 // If a touch is not reported within the timeout, a move event is generated to report
7019 // the new tool type.
7020 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7022 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007023 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007024
7025 processUp(mapper);
7026 processSync(mapper);
7027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7028 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007029 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007030
7031 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7033}
7034
7035TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7036 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007037 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007038
7039 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7040
7041 // The external stylus reports a button change. We wait for some time for a touch event.
7042 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7043 processExternalStylusState(mapper);
7044 ASSERT_NO_FATAL_FAILURE(
7045 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7046
7047 // If a touch is reported within the timeout, it reports the updated button state.
7048 processMove(mapper, 101, 201);
7049 processSync(mapper);
7050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7051 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7052 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7054 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7055 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7056 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7057
7058 // The button is now released.
7059 mStylusState.buttons = 0;
7060 processExternalStylusState(mapper);
7061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7062 ASSERT_NO_FATAL_FAILURE(
7063 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7064
7065 // If a touch is not reported within the timeout, a move event is generated to report
7066 // the new button state.
7067 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7069 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7070 WithButtonState(0))));
7071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007072 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7073 WithButtonState(0))));
7074
7075 processUp(mapper);
7076 processSync(mapper);
7077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007078 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7079
7080 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7082}
7083
Michael Wrightd02c5b62014-02-10 15:10:22 -08007084// --- MultiTouchInputMapperTest ---
7085
7086class MultiTouchInputMapperTest : public TouchInputMapperTest {
7087protected:
7088 void prepareAxes(int axes);
7089
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007090 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7091 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7092 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7093 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7094 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7095 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7096 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7097 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7098 void processId(MultiTouchInputMapper& mapper, int32_t id);
7099 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7100 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7101 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007102 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007103 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007104 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7105 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007106};
7107
7108void MultiTouchInputMapperTest::prepareAxes(int axes) {
7109 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007110 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7111 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007112 }
7113 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007114 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7115 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007116 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007117 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7118 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007119 }
7120 }
7121 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007122 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7123 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007124 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007125 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007126 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007127 }
7128 }
7129 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007130 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7131 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132 }
7133 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007134 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7135 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007136 }
7137 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007138 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7139 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007140 }
7141 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007142 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7143 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007144 }
7145 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007146 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7147 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007148 }
7149 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007150 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007151 }
7152}
7153
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007154void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7155 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007158}
7159
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007160void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7161 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007163}
7164
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007165void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7166 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007168}
7169
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007170void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007172}
7173
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007174void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007176}
7177
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007178void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7179 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007181}
7182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007183void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007185}
7186
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007187void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007189}
7190
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007191void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007193}
7194
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007195void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007197}
7198
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007199void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007201}
7202
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007203void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7204 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007206}
7207
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007208void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7209 int32_t value) {
7210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7212}
7213
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007214void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007216}
7217
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007218void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7219 nsecs_t readTime) {
7220 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007221}
7222
Michael Wrightd02c5b62014-02-10 15:10:22 -08007223TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007224 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007225 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007226 prepareAxes(POSITION);
7227 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007228 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007229
arthurhungdcef2dc2020-08-11 14:47:50 +08007230 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007231
7232 NotifyMotionArgs motionArgs;
7233
7234 // Two fingers down at once.
7235 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7236 processPosition(mapper, x1, y1);
7237 processMTSync(mapper);
7238 processPosition(mapper, x2, y2);
7239 processMTSync(mapper);
7240 processSync(mapper);
7241
7242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7243 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7244 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7245 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7246 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7247 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7248 ASSERT_EQ(0, motionArgs.flags);
7249 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7250 ASSERT_EQ(0, motionArgs.buttonState);
7251 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007252 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007253 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007254 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7256 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7257 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7258 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7259 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7260
7261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7262 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7263 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7264 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7265 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007266 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007267 ASSERT_EQ(0, motionArgs.flags);
7268 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7269 ASSERT_EQ(0, motionArgs.buttonState);
7270 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007271 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007272 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007273 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007274 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007275 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7277 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7279 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7280 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7281 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7282 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7283
7284 // Move.
7285 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7286 processPosition(mapper, x1, y1);
7287 processMTSync(mapper);
7288 processPosition(mapper, x2, y2);
7289 processMTSync(mapper);
7290 processSync(mapper);
7291
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7293 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7294 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7295 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7296 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7298 ASSERT_EQ(0, motionArgs.flags);
7299 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7300 ASSERT_EQ(0, motionArgs.buttonState);
7301 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007302 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007303 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007304 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007305 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007306 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7308 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7310 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7311 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7312 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7314
7315 // First finger up.
7316 x2 += 15; y2 -= 20;
7317 processPosition(mapper, x2, y2);
7318 processMTSync(mapper);
7319 processSync(mapper);
7320
7321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7322 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7323 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7324 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7325 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007326 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007327 ASSERT_EQ(0, motionArgs.flags);
7328 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7329 ASSERT_EQ(0, motionArgs.buttonState);
7330 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007331 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007332 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007333 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007334 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007335 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7337 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7339 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7340 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7341 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7342 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7343
7344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7345 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7346 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7347 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7348 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7350 ASSERT_EQ(0, motionArgs.flags);
7351 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7352 ASSERT_EQ(0, motionArgs.buttonState);
7353 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007354 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007355 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007356 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7358 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7359 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7360 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7361 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7362
7363 // Move.
7364 x2 += 20; y2 -= 25;
7365 processPosition(mapper, x2, y2);
7366 processMTSync(mapper);
7367 processSync(mapper);
7368
7369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7370 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7371 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7372 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7373 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7375 ASSERT_EQ(0, motionArgs.flags);
7376 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7377 ASSERT_EQ(0, motionArgs.buttonState);
7378 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007379 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007380 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007381 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7383 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7384 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7385 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7386 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7387
7388 // New finger down.
7389 int32_t x3 = 700, y3 = 300;
7390 processPosition(mapper, x2, y2);
7391 processMTSync(mapper);
7392 processPosition(mapper, x3, y3);
7393 processMTSync(mapper);
7394 processSync(mapper);
7395
7396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7397 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7398 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7399 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7400 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007401 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007402 ASSERT_EQ(0, motionArgs.flags);
7403 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7404 ASSERT_EQ(0, motionArgs.buttonState);
7405 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007406 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007407 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007408 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007409 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007410 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7412 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7414 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7415 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7416 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7417 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7418
7419 // Second finger up.
7420 x3 += 30; y3 -= 20;
7421 processPosition(mapper, x3, y3);
7422 processMTSync(mapper);
7423 processSync(mapper);
7424
7425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7426 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7427 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7428 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7429 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007430 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007431 ASSERT_EQ(0, motionArgs.flags);
7432 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7433 ASSERT_EQ(0, motionArgs.buttonState);
7434 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007435 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007436 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007437 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007438 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007439 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7441 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7443 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7444 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7445 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7446 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7447
7448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7449 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7450 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7451 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7452 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7454 ASSERT_EQ(0, motionArgs.flags);
7455 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7456 ASSERT_EQ(0, motionArgs.buttonState);
7457 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007458 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007459 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007460 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7462 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7463 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7464 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7465 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7466
7467 // Last finger up.
7468 processMTSync(mapper);
7469 processSync(mapper);
7470
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7472 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7473 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7474 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7475 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7476 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7477 ASSERT_EQ(0, motionArgs.flags);
7478 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7479 ASSERT_EQ(0, motionArgs.buttonState);
7480 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007481 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007482 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007483 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7485 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7486 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7487 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7488 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7489
7490 // Should not have sent any more keys or motions.
7491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7493}
7494
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007495TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7496 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007497 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007498
7499 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7500 /*fuzz*/ 0, /*resolution*/ 10);
7501 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7502 /*fuzz*/ 0, /*resolution*/ 11);
7503 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7504 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7505 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7506 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7507 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7508 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7509 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7510 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7511
Arpit Singha8c236b2023-04-25 13:56:05 +00007512 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007513
7514 // X and Y axes
7515 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7516 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7517 // Touch major and minor
7518 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7519 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7520 // Tool major and minor
7521 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7522 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7523}
7524
7525TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7526 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007527 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007528
7529 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7530 /*fuzz*/ 0, /*resolution*/ 10);
7531 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7532 /*fuzz*/ 0, /*resolution*/ 11);
7533
7534 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7535
Arpit Singha8c236b2023-04-25 13:56:05 +00007536 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007537
7538 // Touch major and minor
7539 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7540 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7541 // Tool major and minor
7542 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7543 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7544}
7545
Michael Wrightd02c5b62014-02-10 15:10:22 -08007546TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007547 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007548 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007549 prepareAxes(POSITION | ID);
7550 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007551 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007552
arthurhungdcef2dc2020-08-11 14:47:50 +08007553 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007554
7555 NotifyMotionArgs motionArgs;
7556
7557 // Two fingers down at once.
7558 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7559 processPosition(mapper, x1, y1);
7560 processId(mapper, 1);
7561 processMTSync(mapper);
7562 processPosition(mapper, x2, y2);
7563 processId(mapper, 2);
7564 processMTSync(mapper);
7565 processSync(mapper);
7566
7567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7568 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007569 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007570 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007571 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7573 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7574
7575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007576 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007577 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007578 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007579 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007580 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007581 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7583 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7585 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7586
7587 // Move.
7588 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7589 processPosition(mapper, x1, y1);
7590 processId(mapper, 1);
7591 processMTSync(mapper);
7592 processPosition(mapper, x2, y2);
7593 processId(mapper, 2);
7594 processMTSync(mapper);
7595 processSync(mapper);
7596
7597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007599 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007600 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007601 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007602 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007603 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7605 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7607 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7608
7609 // First finger up.
7610 x2 += 15; y2 -= 20;
7611 processPosition(mapper, x2, y2);
7612 processId(mapper, 2);
7613 processMTSync(mapper);
7614 processSync(mapper);
7615
7616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007617 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007618 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007619 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007620 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007621 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007622 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7624 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7626 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7627
7628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007630 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007631 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007632 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7634 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7635
7636 // Move.
7637 x2 += 20; y2 -= 25;
7638 processPosition(mapper, x2, y2);
7639 processId(mapper, 2);
7640 processMTSync(mapper);
7641 processSync(mapper);
7642
7643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007645 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007646 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007647 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7649 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7650
7651 // New finger down.
7652 int32_t x3 = 700, y3 = 300;
7653 processPosition(mapper, x2, y2);
7654 processId(mapper, 2);
7655 processMTSync(mapper);
7656 processPosition(mapper, x3, y3);
7657 processId(mapper, 3);
7658 processMTSync(mapper);
7659 processSync(mapper);
7660
7661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007662 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007663 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007664 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007665 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007666 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007667 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7669 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7671 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7672
7673 // Second finger up.
7674 x3 += 30; y3 -= 20;
7675 processPosition(mapper, x3, y3);
7676 processId(mapper, 3);
7677 processMTSync(mapper);
7678 processSync(mapper);
7679
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007681 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007682 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007683 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007684 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007685 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007686 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7688 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7690 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7691
7692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007694 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007695 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007696 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7698 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7699
7700 // Last finger up.
7701 processMTSync(mapper);
7702 processSync(mapper);
7703
7704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7705 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007706 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007707 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007708 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7710 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7711
7712 // Should not have sent any more keys or motions.
7713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7715}
7716
7717TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007718 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007719 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007720 prepareAxes(POSITION | ID | SLOT);
7721 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007722 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007723
arthurhungdcef2dc2020-08-11 14:47:50 +08007724 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007725
7726 NotifyMotionArgs motionArgs;
7727
7728 // Two fingers down at once.
7729 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7730 processPosition(mapper, x1, y1);
7731 processId(mapper, 1);
7732 processSlot(mapper, 1);
7733 processPosition(mapper, x2, y2);
7734 processId(mapper, 2);
7735 processSync(mapper);
7736
7737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7738 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007739 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007740 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007741 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7743 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7744
7745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007746 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007747 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007748 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007749 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007750 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007751 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7753 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7755 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7756
7757 // Move.
7758 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7759 processSlot(mapper, 0);
7760 processPosition(mapper, x1, y1);
7761 processSlot(mapper, 1);
7762 processPosition(mapper, x2, y2);
7763 processSync(mapper);
7764
7765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007767 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007768 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007769 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007770 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007771 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7773 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7775 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7776
7777 // First finger up.
7778 x2 += 15; y2 -= 20;
7779 processSlot(mapper, 0);
7780 processId(mapper, -1);
7781 processSlot(mapper, 1);
7782 processPosition(mapper, x2, y2);
7783 processSync(mapper);
7784
7785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007786 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007787 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007788 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007789 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007790 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007791 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7793 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7795 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7796
7797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7798 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007799 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007800 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007801 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7803 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7804
7805 // Move.
7806 x2 += 20; y2 -= 25;
7807 processPosition(mapper, x2, y2);
7808 processSync(mapper);
7809
7810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007812 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007813 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007814 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7816 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7817
7818 // New finger down.
7819 int32_t x3 = 700, y3 = 300;
7820 processPosition(mapper, x2, y2);
7821 processSlot(mapper, 0);
7822 processId(mapper, 3);
7823 processPosition(mapper, x3, y3);
7824 processSync(mapper);
7825
7826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007827 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007828 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007829 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007830 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007831 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007832 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7834 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7836 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7837
7838 // Second finger up.
7839 x3 += 30; y3 -= 20;
7840 processSlot(mapper, 1);
7841 processId(mapper, -1);
7842 processSlot(mapper, 0);
7843 processPosition(mapper, x3, y3);
7844 processSync(mapper);
7845
7846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007847 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007848 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007849 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007850 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007851 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007852 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7854 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7856 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7857
7858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007860 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007861 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007862 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7864 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7865
7866 // Last finger up.
7867 processId(mapper, -1);
7868 processSync(mapper);
7869
7870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7871 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007872 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007873 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007874 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7876 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7877
7878 // Should not have sent any more keys or motions.
7879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7881}
7882
7883TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007884 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007885 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007886 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007887 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007888
7889 // These calculations are based on the input device calibration documentation.
7890 int32_t rawX = 100;
7891 int32_t rawY = 200;
7892 int32_t rawTouchMajor = 7;
7893 int32_t rawTouchMinor = 6;
7894 int32_t rawToolMajor = 9;
7895 int32_t rawToolMinor = 8;
7896 int32_t rawPressure = 11;
7897 int32_t rawDistance = 0;
7898 int32_t rawOrientation = 3;
7899 int32_t id = 5;
7900
7901 float x = toDisplayX(rawX);
7902 float y = toDisplayY(rawY);
7903 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7904 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7905 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7906 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7907 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7908 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7909 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7910 float distance = float(rawDistance);
7911
7912 processPosition(mapper, rawX, rawY);
7913 processTouchMajor(mapper, rawTouchMajor);
7914 processTouchMinor(mapper, rawTouchMinor);
7915 processToolMajor(mapper, rawToolMajor);
7916 processToolMinor(mapper, rawToolMinor);
7917 processPressure(mapper, rawPressure);
7918 processOrientation(mapper, rawOrientation);
7919 processDistance(mapper, rawDistance);
7920 processId(mapper, id);
7921 processMTSync(mapper);
7922 processSync(mapper);
7923
7924 NotifyMotionArgs args;
7925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7926 ASSERT_EQ(0, args.pointerProperties[0].id);
7927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7928 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7929 orientation, distance));
7930}
7931
7932TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007933 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007934 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007935 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7936 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00007937 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007938
7939 // These calculations are based on the input device calibration documentation.
7940 int32_t rawX = 100;
7941 int32_t rawY = 200;
7942 int32_t rawTouchMajor = 140;
7943 int32_t rawTouchMinor = 120;
7944 int32_t rawToolMajor = 180;
7945 int32_t rawToolMinor = 160;
7946
7947 float x = toDisplayX(rawX);
7948 float y = toDisplayY(rawY);
7949 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7950 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7951 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7952 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7953 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7954
7955 processPosition(mapper, rawX, rawY);
7956 processTouchMajor(mapper, rawTouchMajor);
7957 processTouchMinor(mapper, rawTouchMinor);
7958 processToolMajor(mapper, rawToolMajor);
7959 processToolMinor(mapper, rawToolMinor);
7960 processMTSync(mapper);
7961 processSync(mapper);
7962
7963 NotifyMotionArgs args;
7964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7966 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7967}
7968
7969TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007970 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007971 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007972 prepareAxes(POSITION | TOUCH | TOOL);
7973 addConfigurationProperty("touch.size.calibration", "diameter");
7974 addConfigurationProperty("touch.size.scale", "10");
7975 addConfigurationProperty("touch.size.bias", "160");
7976 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00007977 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007978
7979 // These calculations are based on the input device calibration documentation.
7980 // Note: We only provide a single common touch/tool value because the device is assumed
7981 // not to emit separate values for each pointer (isSummed = 1).
7982 int32_t rawX = 100;
7983 int32_t rawY = 200;
7984 int32_t rawX2 = 150;
7985 int32_t rawY2 = 250;
7986 int32_t rawTouchMajor = 5;
7987 int32_t rawToolMajor = 8;
7988
7989 float x = toDisplayX(rawX);
7990 float y = toDisplayY(rawY);
7991 float x2 = toDisplayX(rawX2);
7992 float y2 = toDisplayY(rawY2);
7993 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7994 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7995 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7996
7997 processPosition(mapper, rawX, rawY);
7998 processTouchMajor(mapper, rawTouchMajor);
7999 processToolMajor(mapper, rawToolMajor);
8000 processMTSync(mapper);
8001 processPosition(mapper, rawX2, rawY2);
8002 processTouchMajor(mapper, rawTouchMajor);
8003 processToolMajor(mapper, rawToolMajor);
8004 processMTSync(mapper);
8005 processSync(mapper);
8006
8007 NotifyMotionArgs args;
8008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8009 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8010
8011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008012 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008013 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8015 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8017 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8018}
8019
8020TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008021 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008022 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008023 prepareAxes(POSITION | TOUCH | TOOL);
8024 addConfigurationProperty("touch.size.calibration", "area");
8025 addConfigurationProperty("touch.size.scale", "43");
8026 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008027 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008028
8029 // These calculations are based on the input device calibration documentation.
8030 int32_t rawX = 100;
8031 int32_t rawY = 200;
8032 int32_t rawTouchMajor = 5;
8033 int32_t rawToolMajor = 8;
8034
8035 float x = toDisplayX(rawX);
8036 float y = toDisplayY(rawY);
8037 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8038 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8039 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8040
8041 processPosition(mapper, rawX, rawY);
8042 processTouchMajor(mapper, rawTouchMajor);
8043 processToolMajor(mapper, rawToolMajor);
8044 processMTSync(mapper);
8045 processSync(mapper);
8046
8047 NotifyMotionArgs args;
8048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8050 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8051}
8052
8053TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008054 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008055 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008056 prepareAxes(POSITION | PRESSURE);
8057 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8058 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008059 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008060
Michael Wrightaa449c92017-12-13 21:21:43 +00008061 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008062 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008063 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8064 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8065 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8066
Michael Wrightd02c5b62014-02-10 15:10:22 -08008067 // These calculations are based on the input device calibration documentation.
8068 int32_t rawX = 100;
8069 int32_t rawY = 200;
8070 int32_t rawPressure = 60;
8071
8072 float x = toDisplayX(rawX);
8073 float y = toDisplayY(rawY);
8074 float pressure = float(rawPressure) * 0.01f;
8075
8076 processPosition(mapper, rawX, rawY);
8077 processPressure(mapper, rawPressure);
8078 processMTSync(mapper);
8079 processSync(mapper);
8080
8081 NotifyMotionArgs args;
8082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8084 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8085}
8086
8087TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008088 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008089 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008091 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092
8093 NotifyMotionArgs motionArgs;
8094 NotifyKeyArgs keyArgs;
8095
8096 processId(mapper, 1);
8097 processPosition(mapper, 100, 200);
8098 processSync(mapper);
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8100 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8101 ASSERT_EQ(0, motionArgs.buttonState);
8102
8103 // press BTN_LEFT, release BTN_LEFT
8104 processKey(mapper, BTN_LEFT, 1);
8105 processSync(mapper);
8106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8108 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8109
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8112 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8113
Michael Wrightd02c5b62014-02-10 15:10:22 -08008114 processKey(mapper, BTN_LEFT, 0);
8115 processSync(mapper);
8116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008117 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008118 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008119
8120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008122 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008123
8124 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8125 processKey(mapper, BTN_RIGHT, 1);
8126 processKey(mapper, BTN_MIDDLE, 1);
8127 processSync(mapper);
8128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8130 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8131 motionArgs.buttonState);
8132
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8134 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8135 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8136
8137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8138 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8139 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8140 motionArgs.buttonState);
8141
Michael Wrightd02c5b62014-02-10 15:10:22 -08008142 processKey(mapper, BTN_RIGHT, 0);
8143 processSync(mapper);
8144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008145 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008146 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008147
8148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008150 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008151
8152 processKey(mapper, BTN_MIDDLE, 0);
8153 processSync(mapper);
8154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008155 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008156 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008157
8158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008160 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161
8162 // press BTN_BACK, release BTN_BACK
8163 processKey(mapper, BTN_BACK, 1);
8164 processSync(mapper);
8165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8166 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8167 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008168
Michael Wrightd02c5b62014-02-10 15:10:22 -08008169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008171 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8172
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8174 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8175 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008176
8177 processKey(mapper, BTN_BACK, 0);
8178 processSync(mapper);
8179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008180 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008181 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008182
8183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008185 ASSERT_EQ(0, motionArgs.buttonState);
8186
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8188 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8189 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8190
8191 // press BTN_SIDE, release BTN_SIDE
8192 processKey(mapper, BTN_SIDE, 1);
8193 processSync(mapper);
8194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8195 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8196 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008197
Michael Wrightd02c5b62014-02-10 15:10:22 -08008198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008199 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008200 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8201
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8203 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8204 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008205
8206 processKey(mapper, BTN_SIDE, 0);
8207 processSync(mapper);
8208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008209 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008210 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008211
8212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008214 ASSERT_EQ(0, motionArgs.buttonState);
8215
Michael Wrightd02c5b62014-02-10 15:10:22 -08008216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8217 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8218 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8219
8220 // press BTN_FORWARD, release BTN_FORWARD
8221 processKey(mapper, BTN_FORWARD, 1);
8222 processSync(mapper);
8223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8224 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8225 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008226
Michael Wrightd02c5b62014-02-10 15:10:22 -08008227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008229 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8230
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8232 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8233 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008234
8235 processKey(mapper, BTN_FORWARD, 0);
8236 processSync(mapper);
8237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008238 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008240
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008243 ASSERT_EQ(0, motionArgs.buttonState);
8244
Michael Wrightd02c5b62014-02-10 15:10:22 -08008245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8246 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8247 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8248
8249 // press BTN_EXTRA, release BTN_EXTRA
8250 processKey(mapper, BTN_EXTRA, 1);
8251 processSync(mapper);
8252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8253 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8254 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008255
Michael Wrightd02c5b62014-02-10 15:10:22 -08008256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008258 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8259
8260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8262 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008263
8264 processKey(mapper, BTN_EXTRA, 0);
8265 processSync(mapper);
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008268 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008269
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008272 ASSERT_EQ(0, motionArgs.buttonState);
8273
Michael Wrightd02c5b62014-02-10 15:10:22 -08008274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8275 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8276 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8277
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8279
Michael Wrightd02c5b62014-02-10 15:10:22 -08008280 // press BTN_STYLUS, release BTN_STYLUS
8281 processKey(mapper, BTN_STYLUS, 1);
8282 processSync(mapper);
8283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008285 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8286
8287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8288 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8289 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008290
8291 processKey(mapper, BTN_STYLUS, 0);
8292 processSync(mapper);
8293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008295 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008296
8297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008299 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008300
8301 // press BTN_STYLUS2, release BTN_STYLUS2
8302 processKey(mapper, BTN_STYLUS2, 1);
8303 processSync(mapper);
8304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008306 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8307
8308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8309 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8310 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008311
8312 processKey(mapper, BTN_STYLUS2, 0);
8313 processSync(mapper);
8314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008315 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008316 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008317
8318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008320 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008321
8322 // release touch
8323 processId(mapper, -1);
8324 processSync(mapper);
8325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8326 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8327 ASSERT_EQ(0, motionArgs.buttonState);
8328}
8329
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008330TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8331 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008332 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008333 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008334 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008335
8336 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8337 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8338
8339 // Touch down.
8340 processId(mapper, 1);
8341 processPosition(mapper, 100, 200);
8342 processSync(mapper);
8343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8344 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8345
8346 // Press and release button mapped to the primary stylus button.
8347 processKey(mapper, BTN_A, 1);
8348 processSync(mapper);
8349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8350 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8351 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8353 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8354 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8355
8356 processKey(mapper, BTN_A, 0);
8357 processSync(mapper);
8358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8359 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8361 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8362
8363 // Press and release the HID usage mapped to the secondary stylus button.
8364 processHidUsage(mapper, 0xabcd, 1);
8365 processSync(mapper);
8366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8367 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8368 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8370 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8371 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8372
8373 processHidUsage(mapper, 0xabcd, 0);
8374 processSync(mapper);
8375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8376 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8378 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8379
8380 // Release touch.
8381 processId(mapper, -1);
8382 processSync(mapper);
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8384 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8385}
8386
Michael Wrightd02c5b62014-02-10 15:10:22 -08008387TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008388 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008389 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008390 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008391 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008392
8393 NotifyMotionArgs motionArgs;
8394
8395 // default tool type is finger
8396 processId(mapper, 1);
8397 processPosition(mapper, 100, 200);
8398 processSync(mapper);
8399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8400 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008401 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008402
8403 // eraser
8404 processKey(mapper, BTN_TOOL_RUBBER, 1);
8405 processSync(mapper);
8406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008408 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008409
8410 // stylus
8411 processKey(mapper, BTN_TOOL_RUBBER, 0);
8412 processKey(mapper, BTN_TOOL_PEN, 1);
8413 processSync(mapper);
8414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008416 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008417
8418 // brush
8419 processKey(mapper, BTN_TOOL_PEN, 0);
8420 processKey(mapper, BTN_TOOL_BRUSH, 1);
8421 processSync(mapper);
8422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008424 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008425
8426 // pencil
8427 processKey(mapper, BTN_TOOL_BRUSH, 0);
8428 processKey(mapper, BTN_TOOL_PENCIL, 1);
8429 processSync(mapper);
8430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008432 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008433
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008434 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008435 processKey(mapper, BTN_TOOL_PENCIL, 0);
8436 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8437 processSync(mapper);
8438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008440 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008441
8442 // mouse
8443 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8444 processKey(mapper, BTN_TOOL_MOUSE, 1);
8445 processSync(mapper);
8446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008448 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008449
8450 // lens
8451 processKey(mapper, BTN_TOOL_MOUSE, 0);
8452 processKey(mapper, BTN_TOOL_LENS, 1);
8453 processSync(mapper);
8454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008456 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008457
8458 // double-tap
8459 processKey(mapper, BTN_TOOL_LENS, 0);
8460 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8461 processSync(mapper);
8462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008464 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008465
8466 // triple-tap
8467 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8468 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8469 processSync(mapper);
8470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008472 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008473
8474 // quad-tap
8475 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8476 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8477 processSync(mapper);
8478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008480 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008481
8482 // finger
8483 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8484 processKey(mapper, BTN_TOOL_FINGER, 1);
8485 processSync(mapper);
8486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008488 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008489
8490 // stylus trumps finger
8491 processKey(mapper, BTN_TOOL_PEN, 1);
8492 processSync(mapper);
8493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008495 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008496
8497 // eraser trumps stylus
8498 processKey(mapper, BTN_TOOL_RUBBER, 1);
8499 processSync(mapper);
8500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008502 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008503
8504 // mouse trumps eraser
8505 processKey(mapper, BTN_TOOL_MOUSE, 1);
8506 processSync(mapper);
8507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8508 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008509 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008510
8511 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8512 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8513 processSync(mapper);
8514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008516 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008517
8518 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8519 processToolType(mapper, MT_TOOL_PEN);
8520 processSync(mapper);
8521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008523 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008524
8525 // back to default tool type
8526 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8527 processKey(mapper, BTN_TOOL_MOUSE, 0);
8528 processKey(mapper, BTN_TOOL_RUBBER, 0);
8529 processKey(mapper, BTN_TOOL_PEN, 0);
8530 processKey(mapper, BTN_TOOL_FINGER, 0);
8531 processSync(mapper);
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008534 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008535}
8536
8537TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008538 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008539 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008540 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008541 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008542 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008543
8544 NotifyMotionArgs motionArgs;
8545
8546 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8547 processId(mapper, 1);
8548 processPosition(mapper, 100, 200);
8549 processSync(mapper);
8550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8551 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8553 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8554
8555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8556 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8558 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8559
8560 // move a little
8561 processPosition(mapper, 150, 250);
8562 processSync(mapper);
8563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8564 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8566 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8567
8568 // down when BTN_TOUCH is pressed, pressure defaults to 1
8569 processKey(mapper, BTN_TOUCH, 1);
8570 processSync(mapper);
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8572 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8574 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8575
8576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8577 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8579 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8580
8581 // up when BTN_TOUCH is released, hover restored
8582 processKey(mapper, BTN_TOUCH, 0);
8583 processSync(mapper);
8584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8585 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8587 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8588
8589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8590 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8592 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8593
8594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8595 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8597 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8598
8599 // exit hover when pointer goes away
8600 processId(mapper, -1);
8601 processSync(mapper);
8602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8603 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8605 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8606}
8607
8608TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008609 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008610 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008611 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008612 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008613
8614 NotifyMotionArgs motionArgs;
8615
8616 // initially hovering because pressure is 0
8617 processId(mapper, 1);
8618 processPosition(mapper, 100, 200);
8619 processPressure(mapper, 0);
8620 processSync(mapper);
8621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8622 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8624 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8625
8626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8627 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8629 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8630
8631 // move a little
8632 processPosition(mapper, 150, 250);
8633 processSync(mapper);
8634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8635 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8637 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8638
8639 // down when pressure becomes non-zero
8640 processPressure(mapper, RAW_PRESSURE_MAX);
8641 processSync(mapper);
8642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8643 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8645 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8646
8647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8648 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8650 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8651
8652 // up when pressure becomes 0, hover restored
8653 processPressure(mapper, 0);
8654 processSync(mapper);
8655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8656 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8658 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8659
8660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8661 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8663 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8664
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8666 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8668 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8669
8670 // exit hover when pointer goes away
8671 processId(mapper, -1);
8672 processSync(mapper);
8673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8674 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8676 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8677}
8678
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008679/**
8680 * Set the input device port <--> display port associations, and check that the
8681 * events are routed to the display that matches the display port.
8682 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8683 */
8684TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008685 const std::string usb2 = "USB2";
8686 const uint8_t hdmi1 = 0;
8687 const uint8_t hdmi2 = 1;
8688 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008689 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008690
8691 addConfigurationProperty("touch.deviceType", "touchScreen");
8692 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008693 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008694
8695 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8696 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8697
8698 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8699 // for this input device is specified, and the matching viewport is not present,
8700 // the input device should be disabled (at the mapper level).
8701
8702 // Add viewport for display 2 on hdmi2
8703 prepareSecondaryDisplay(type, hdmi2);
8704 // Send a touch event
8705 processPosition(mapper, 100, 100);
8706 processSync(mapper);
8707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8708
8709 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008710 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008711 // Send a touch event again
8712 processPosition(mapper, 100, 100);
8713 processSync(mapper);
8714
8715 NotifyMotionArgs args;
8716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8717 ASSERT_EQ(DISPLAY_ID, args.displayId);
8718}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008719
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008720TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8721 addConfigurationProperty("touch.deviceType", "touchScreen");
8722 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008723 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008724
8725 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8726
Michael Wrighta9cf4192022-12-01 23:46:39 +00008727 prepareDisplay(ui::ROTATION_0);
8728 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008729
8730 // Send a touch event
8731 processPosition(mapper, 100, 100);
8732 processSync(mapper);
8733
8734 NotifyMotionArgs args;
8735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8736 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8737}
8738
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008739TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008740 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008741
Michael Wrighta9cf4192022-12-01 23:46:39 +00008742 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008743 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008744 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008745
Josep del Río2d8c79a2023-01-23 19:33:50 +00008746 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008747
8748 NotifyMotionArgs motionArgs;
8749 processPosition(mapper, 100, 100);
8750 processSync(mapper);
8751
8752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8753 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008754 ASSERT_EQ(ui::LogicalDisplayId::INVALID, motionArgs.displayId);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008755}
8756
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008757/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008758 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8759 */
8760TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8761 addConfigurationProperty("touch.deviceType", "touchScreen");
8762 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008763 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008764
Michael Wrighta9cf4192022-12-01 23:46:39 +00008765 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00008766 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
8767 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
8768 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
8769 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008770
8771 NotifyMotionArgs args;
8772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8773 ASSERT_EQ(26, args.readTime);
8774
Harry Cutts33476232023-01-30 19:57:29 +00008775 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
8776 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
8777 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008778
8779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8780 ASSERT_EQ(33, args.readTime);
8781}
8782
8783/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008784 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8785 * events should not be delivered to the listener.
8786 */
8787TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8788 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008789 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008790 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008791 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008792 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008793 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008794 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008795
8796 NotifyMotionArgs motionArgs;
8797 processPosition(mapper, 100, 100);
8798 processSync(mapper);
8799
8800 mFakeListener->assertNotifyMotionWasNotCalled();
8801}
8802
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008803/**
8804 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8805 * the touch mapper can process the events and the events can be delivered to the listener.
8806 */
8807TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8808 addConfigurationProperty("touch.deviceType", "touchScreen");
8809 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008810 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008811 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008812 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008813 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008814 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008815
8816 NotifyMotionArgs motionArgs;
8817 processPosition(mapper, 100, 100);
8818 processSync(mapper);
8819
8820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8821 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8822}
8823
Josh Thielene986aed2023-06-01 14:17:30 +00008824/**
8825 * When the viewport is deactivated (isActive transitions from true to false),
8826 * and touch.enableForInactiveViewport is false, touches prior to the transition
8827 * should be cancelled.
8828 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08008829TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8830 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008831 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008832 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008833 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008834 std::optional<DisplayViewport> optionalDisplayViewport =
8835 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8836 ASSERT_TRUE(optionalDisplayViewport.has_value());
8837 DisplayViewport displayViewport = *optionalDisplayViewport;
8838
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008839 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008840 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008841 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08008842
8843 // Finger down
8844 int32_t x = 100, y = 100;
8845 processPosition(mapper, x, y);
8846 processSync(mapper);
8847
8848 NotifyMotionArgs motionArgs;
8849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8850 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8851
8852 // Deactivate display viewport
8853 displayViewport.isActive = false;
8854 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008855 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008856
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008857 // The ongoing touch should be canceled immediately
8858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8859 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8860
8861 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008862 x += 10, y += 10;
8863 processPosition(mapper, x, y);
8864 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008866
8867 // Reactivate display viewport
8868 displayViewport.isActive = true;
8869 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008870 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008871
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008872 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008873 x += 10, y += 10;
8874 processPosition(mapper, x, y);
8875 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8877 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008878}
8879
Josh Thielene986aed2023-06-01 14:17:30 +00008880/**
8881 * When the viewport is deactivated (isActive transitions from true to false),
8882 * and touch.enableForInactiveViewport is true, touches prior to the transition
8883 * should not be cancelled.
8884 */
8885TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
8886 addConfigurationProperty("touch.deviceType", "touchScreen");
8887 addConfigurationProperty("touch.enableForInactiveViewport", "1");
8888 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8889 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
8890 std::optional<DisplayViewport> optionalDisplayViewport =
8891 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8892 ASSERT_TRUE(optionalDisplayViewport.has_value());
8893 DisplayViewport displayViewport = *optionalDisplayViewport;
8894
8895 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8896 prepareAxes(POSITION);
8897 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
8898
8899 // Finger down
8900 int32_t x = 100, y = 100;
8901 processPosition(mapper, x, y);
8902 processSync(mapper);
8903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8904 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
8905
8906 // Deactivate display viewport
8907 displayViewport.isActive = false;
8908 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8909 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8910
8911 // The ongoing touch should not be canceled
8912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8913
8914 // Finger move is not ignored
8915 x += 10, y += 10;
8916 processPosition(mapper, x, y);
8917 processSync(mapper);
8918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8919 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8920
8921 // Reactivate display viewport
8922 displayViewport.isActive = true;
8923 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8924 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8925
8926 // Finger move continues and does not start new gesture
8927 x += 10, y += 10;
8928 processPosition(mapper, x, y);
8929 processSync(mapper);
8930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8931 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8932}
8933
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008934TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008935 prepareAxes(POSITION);
8936 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008937 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008938 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008939
8940 NotifyMotionArgs motionArgs;
8941 // Unrotated video frame
8942 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8943 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008944 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008945 processPosition(mapper, 100, 200);
8946 processSync(mapper);
8947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8948 ASSERT_EQ(frames, motionArgs.videoFrames);
8949
8950 // Subsequent touch events should not have any videoframes
8951 // This is implemented separately in FakeEventHub,
8952 // but that should match the behaviour of TouchVideoDevice.
8953 processPosition(mapper, 200, 200);
8954 processSync(mapper);
8955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8956 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8957}
8958
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008959TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008960 prepareAxes(POSITION);
8961 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008962 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008963 // Unrotated video frame
8964 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8965 NotifyMotionArgs motionArgs;
8966
8967 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008968 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008969 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008970 clearViewports();
8971 prepareDisplay(orientation);
8972 std::vector<TouchVideoFrame> frames{frame};
8973 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8974 processPosition(mapper, 100, 200);
8975 processSync(mapper);
8976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8977 ASSERT_EQ(frames, motionArgs.videoFrames);
8978 }
8979}
8980
8981TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8982 prepareAxes(POSITION);
8983 addConfigurationProperty("touch.deviceType", "touchScreen");
8984 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8985 // orientation-aware are affected by display rotation.
8986 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00008987 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008988 // Unrotated video frame
8989 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8990 NotifyMotionArgs motionArgs;
8991
8992 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008993 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008994 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008995 clearViewports();
8996 prepareDisplay(orientation);
8997 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008998 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008999 processPosition(mapper, 100, 200);
9000 processSync(mapper);
9001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009002 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9003 // compared to the display. This is so that when the window transform (which contains the
9004 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9005 // window's coordinate space.
9006 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009007 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009008
9009 // Release finger.
9010 processSync(mapper);
9011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009012 }
9013}
9014
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009015TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009016 prepareAxes(POSITION);
9017 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009018 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009019 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9020 // so mix these.
9021 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9022 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9023 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9024 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9025 NotifyMotionArgs motionArgs;
9026
Michael Wrighta9cf4192022-12-01 23:46:39 +00009027 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009028 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009029 processPosition(mapper, 100, 200);
9030 processSync(mapper);
9031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009032 ASSERT_EQ(frames, motionArgs.videoFrames);
9033}
9034
9035TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9036 prepareAxes(POSITION);
9037 addConfigurationProperty("touch.deviceType", "touchScreen");
9038 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9039 // orientation-aware are affected by display rotation.
9040 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009041 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009042 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9043 // so mix these.
9044 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9045 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9046 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9047 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9048 NotifyMotionArgs motionArgs;
9049
Michael Wrighta9cf4192022-12-01 23:46:39 +00009050 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009051 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9052 processPosition(mapper, 100, 200);
9053 processSync(mapper);
9054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9055 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9056 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9057 // compared to the display. This is so that when the window transform (which contains the
9058 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9059 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009060 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009061 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009062 ASSERT_EQ(frames, motionArgs.videoFrames);
9063}
9064
Arthur Hung9da14732019-09-02 16:16:58 +08009065/**
9066 * If we had defined port associations, but the viewport is not ready, the touch device would be
9067 * expected to be disabled, and it should be enabled after the viewport has found.
9068 */
9069TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009070 constexpr uint8_t hdmi2 = 1;
9071 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009072 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009073
9074 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9075
9076 addConfigurationProperty("touch.deviceType", "touchScreen");
9077 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009078 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009079
9080 ASSERT_EQ(mDevice->isEnabled(), false);
9081
9082 // Add display on hdmi2, the device should be enabled and can receive touch event.
9083 prepareSecondaryDisplay(type, hdmi2);
9084 ASSERT_EQ(mDevice->isEnabled(), true);
9085
9086 // Send a touch event.
9087 processPosition(mapper, 100, 100);
9088 processSync(mapper);
9089
9090 NotifyMotionArgs args;
9091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9092 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9093}
9094
Arthur Hung421eb1c2020-01-16 00:09:42 +08009095TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009096 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009097 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009098 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009099 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009100
9101 NotifyMotionArgs motionArgs;
9102
9103 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9104 // finger down
9105 processId(mapper, 1);
9106 processPosition(mapper, x1, y1);
9107 processSync(mapper);
9108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9109 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009110 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009111
9112 // finger move
9113 processId(mapper, 1);
9114 processPosition(mapper, x2, y2);
9115 processSync(mapper);
9116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009118 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009119
9120 // finger up.
9121 processId(mapper, -1);
9122 processSync(mapper);
9123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9124 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009125 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009126
9127 // new finger down
9128 processId(mapper, 1);
9129 processPosition(mapper, x3, y3);
9130 processSync(mapper);
9131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9132 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009133 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009134}
9135
9136/**
arthurhungcc7f9802020-04-30 17:55:40 +08009137 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9138 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009139 */
arthurhungcc7f9802020-04-30 17:55:40 +08009140TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009141 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009142 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009143 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009144 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009145
9146 NotifyMotionArgs motionArgs;
9147
9148 // default tool type is finger
9149 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009150 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009151 processPosition(mapper, x1, y1);
9152 processSync(mapper);
9153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9154 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009155 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009156
9157 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9158 processToolType(mapper, MT_TOOL_PALM);
9159 processSync(mapper);
9160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9161 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9162
9163 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009164 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009165 processPosition(mapper, x2, y2);
9166 processSync(mapper);
9167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9168
9169 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009170 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009171 processSync(mapper);
9172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9173
9174 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009175 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009176 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009177 processPosition(mapper, x3, y3);
9178 processSync(mapper);
9179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9180 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009181 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009182}
9183
arthurhungbf89a482020-04-17 17:37:55 +08009184/**
arthurhungcc7f9802020-04-30 17:55:40 +08009185 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9186 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009187 */
arthurhungcc7f9802020-04-30 17:55:40 +08009188TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009189 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009190 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009191 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009192 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009193
9194 NotifyMotionArgs motionArgs;
9195
9196 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009197 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9198 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009199 processPosition(mapper, x1, y1);
9200 processSync(mapper);
9201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9202 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009203 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009204
9205 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009206 processSlot(mapper, SECOND_SLOT);
9207 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009208 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009209 processSync(mapper);
9210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009211 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009212 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009213
9214 // If the tool type of the first finger changes to MT_TOOL_PALM,
9215 // we expect to receive ACTION_POINTER_UP with cancel flag.
9216 processSlot(mapper, FIRST_SLOT);
9217 processId(mapper, FIRST_TRACKING_ID);
9218 processToolType(mapper, MT_TOOL_PALM);
9219 processSync(mapper);
9220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009221 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009222 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9223
9224 // The following MOVE events of second finger should be processed.
9225 processSlot(mapper, SECOND_SLOT);
9226 processId(mapper, SECOND_TRACKING_ID);
9227 processPosition(mapper, x2 + 1, y2 + 1);
9228 processSync(mapper);
9229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009231 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009232
9233 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9234 // it. Second finger receive move.
9235 processSlot(mapper, FIRST_SLOT);
9236 processId(mapper, INVALID_TRACKING_ID);
9237 processSync(mapper);
9238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009240 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009241
9242 // Second finger keeps moving.
9243 processSlot(mapper, SECOND_SLOT);
9244 processId(mapper, SECOND_TRACKING_ID);
9245 processPosition(mapper, x2 + 2, y2 + 2);
9246 processSync(mapper);
9247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009249 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009250
9251 // Second finger up.
9252 processId(mapper, INVALID_TRACKING_ID);
9253 processSync(mapper);
9254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9255 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9256 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9257}
9258
9259/**
9260 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9261 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9262 */
9263TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9264 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009265 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009266 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009267 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009268
9269 NotifyMotionArgs motionArgs;
9270
9271 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9272 // First finger down.
9273 processId(mapper, FIRST_TRACKING_ID);
9274 processPosition(mapper, x1, y1);
9275 processSync(mapper);
9276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9277 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009278 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009279
9280 // Second finger down.
9281 processSlot(mapper, SECOND_SLOT);
9282 processId(mapper, SECOND_TRACKING_ID);
9283 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009284 processSync(mapper);
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009286 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009287 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009288
arthurhungcc7f9802020-04-30 17:55:40 +08009289 // If the tool type of the first finger changes to MT_TOOL_PALM,
9290 // we expect to receive ACTION_POINTER_UP with cancel flag.
9291 processSlot(mapper, FIRST_SLOT);
9292 processId(mapper, FIRST_TRACKING_ID);
9293 processToolType(mapper, MT_TOOL_PALM);
9294 processSync(mapper);
9295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009296 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009297 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9298
9299 // Second finger keeps moving.
9300 processSlot(mapper, SECOND_SLOT);
9301 processId(mapper, SECOND_TRACKING_ID);
9302 processPosition(mapper, x2 + 1, y2 + 1);
9303 processSync(mapper);
9304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9306
9307 // second finger becomes palm, receive cancel due to only 1 finger is active.
9308 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009309 processToolType(mapper, MT_TOOL_PALM);
9310 processSync(mapper);
9311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9312 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9313
arthurhungcc7f9802020-04-30 17:55:40 +08009314 // third finger down.
9315 processSlot(mapper, THIRD_SLOT);
9316 processId(mapper, THIRD_TRACKING_ID);
9317 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009318 processPosition(mapper, x3, y3);
9319 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9321 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009322 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009323 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009324
9325 // third finger move
9326 processId(mapper, THIRD_TRACKING_ID);
9327 processPosition(mapper, x3 + 1, y3 + 1);
9328 processSync(mapper);
9329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9331
9332 // first finger up, third finger receive move.
9333 processSlot(mapper, FIRST_SLOT);
9334 processId(mapper, INVALID_TRACKING_ID);
9335 processSync(mapper);
9336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009338 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009339
9340 // second finger up, third finger receive move.
9341 processSlot(mapper, SECOND_SLOT);
9342 processId(mapper, INVALID_TRACKING_ID);
9343 processSync(mapper);
9344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009346 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009347
9348 // third finger up.
9349 processSlot(mapper, THIRD_SLOT);
9350 processId(mapper, INVALID_TRACKING_ID);
9351 processSync(mapper);
9352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9353 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9354 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9355}
9356
9357/**
9358 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9359 * and the active finger could still be allowed to receive the events
9360 */
9361TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9362 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009363 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009364 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009365 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009366
9367 NotifyMotionArgs motionArgs;
9368
9369 // default tool type is finger
9370 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9371 processId(mapper, FIRST_TRACKING_ID);
9372 processPosition(mapper, x1, y1);
9373 processSync(mapper);
9374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9375 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009376 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009377
9378 // Second finger down.
9379 processSlot(mapper, SECOND_SLOT);
9380 processId(mapper, SECOND_TRACKING_ID);
9381 processPosition(mapper, x2, y2);
9382 processSync(mapper);
9383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009384 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009385 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009386
9387 // If the tool type of the second finger changes to MT_TOOL_PALM,
9388 // we expect to receive ACTION_POINTER_UP with cancel flag.
9389 processId(mapper, SECOND_TRACKING_ID);
9390 processToolType(mapper, MT_TOOL_PALM);
9391 processSync(mapper);
9392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009393 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009394 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9395
9396 // The following MOVE event should be processed.
9397 processSlot(mapper, FIRST_SLOT);
9398 processId(mapper, FIRST_TRACKING_ID);
9399 processPosition(mapper, x1 + 1, y1 + 1);
9400 processSync(mapper);
9401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009403 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009404
9405 // second finger up.
9406 processSlot(mapper, SECOND_SLOT);
9407 processId(mapper, INVALID_TRACKING_ID);
9408 processSync(mapper);
9409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9411
9412 // first finger keep moving
9413 processSlot(mapper, FIRST_SLOT);
9414 processId(mapper, FIRST_TRACKING_ID);
9415 processPosition(mapper, x1 + 2, y1 + 2);
9416 processSync(mapper);
9417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9419
9420 // first finger up.
9421 processId(mapper, INVALID_TRACKING_ID);
9422 processSync(mapper);
9423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9424 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9425 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009426}
9427
Arthur Hung9ad18942021-06-19 02:04:46 +00009428/**
9429 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9430 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9431 * cause slot be valid again.
9432 */
9433TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9434 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009435 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009436 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009437 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +00009438
9439 NotifyMotionArgs motionArgs;
9440
9441 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9442 // First finger down.
9443 processId(mapper, FIRST_TRACKING_ID);
9444 processPosition(mapper, x1, y1);
9445 processPressure(mapper, RAW_PRESSURE_MAX);
9446 processSync(mapper);
9447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9448 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009449 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009450
9451 // First finger move.
9452 processId(mapper, FIRST_TRACKING_ID);
9453 processPosition(mapper, x1 + 1, y1 + 1);
9454 processPressure(mapper, RAW_PRESSURE_MAX);
9455 processSync(mapper);
9456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009458 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009459
9460 // Second finger down.
9461 processSlot(mapper, SECOND_SLOT);
9462 processId(mapper, SECOND_TRACKING_ID);
9463 processPosition(mapper, x2, y2);
9464 processPressure(mapper, RAW_PRESSURE_MAX);
9465 processSync(mapper);
9466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009467 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009468 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009469
9470 // second finger up with some unexpected data.
9471 processSlot(mapper, SECOND_SLOT);
9472 processId(mapper, INVALID_TRACKING_ID);
9473 processPosition(mapper, x2, y2);
9474 processSync(mapper);
9475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009476 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009477 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009478
9479 // first finger up with some unexpected data.
9480 processSlot(mapper, FIRST_SLOT);
9481 processId(mapper, INVALID_TRACKING_ID);
9482 processPosition(mapper, x2, y2);
9483 processPressure(mapper, RAW_PRESSURE_MAX);
9484 processSync(mapper);
9485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9486 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009487 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009488}
9489
Arpit Singh4b4a4572023-11-24 18:19:56 +00009490TEST_F(MultiTouchInputMapperTest, Reset_RepopulatesMultiTouchState) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009491 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009492 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009493 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009494 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009495
9496 // First finger down.
Arpit Singh4b4a4572023-11-24 18:19:56 +00009497 constexpr int32_t x1 = 100, y1 = 200, x2 = 300, y2 = 400;
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009498 processId(mapper, FIRST_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009499 processPosition(mapper, x1, y1);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009500 processPressure(mapper, RAW_PRESSURE_MAX);
9501 processSync(mapper);
9502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9503 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9504
9505 // Second finger down.
9506 processSlot(mapper, SECOND_SLOT);
9507 processId(mapper, SECOND_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009508 processPosition(mapper, x2, y2);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009509 processPressure(mapper, RAW_PRESSURE_MAX);
9510 processSync(mapper);
9511 ASSERT_NO_FATAL_FAILURE(
9512 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9513
Arpit Singh4b4a4572023-11-24 18:19:56 +00009514 // Set MT Slot state to be repopulated for the required slots
9515 std::vector<int32_t> mtSlotValues(RAW_SLOT_MAX + 1, -1);
9516 mtSlotValues[0] = FIRST_TRACKING_ID;
9517 mtSlotValues[1] = SECOND_TRACKING_ID;
9518 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_TRACKING_ID, mtSlotValues);
9519
9520 mtSlotValues[0] = x1;
9521 mtSlotValues[1] = x2;
9522 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_X, mtSlotValues);
9523
9524 mtSlotValues[0] = y1;
9525 mtSlotValues[1] = y2;
9526 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_Y, mtSlotValues);
9527
9528 mtSlotValues[0] = RAW_PRESSURE_MAX;
9529 mtSlotValues[1] = RAW_PRESSURE_MAX;
9530 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_PRESSURE, mtSlotValues);
9531
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009532 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Arpit Singh4b4a4572023-11-24 18:19:56 +00009533 // repopulated. Resetting should cancel the ongoing gesture.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009534 resetMapper(mapper, ARBITRARY_TIME);
9535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9536 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009537
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009538 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9539 // the existing touch state to generate a down event.
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009540 processPosition(mapper, 301, 302);
9541 processSync(mapper);
9542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9543 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9545 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009546
9547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9548}
9549
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009550TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009551 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009552 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009553 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009554 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009555
9556 // First finger touches down and releases.
9557 processId(mapper, FIRST_TRACKING_ID);
9558 processPosition(mapper, 100, 200);
9559 processPressure(mapper, RAW_PRESSURE_MAX);
9560 processSync(mapper);
9561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9562 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9563 processId(mapper, INVALID_TRACKING_ID);
9564 processSync(mapper);
9565 ASSERT_NO_FATAL_FAILURE(
9566 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9567
9568 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9569 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009570 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9572
9573 // Send an empty sync frame. Since there are no pointers, no events are generated.
9574 processSync(mapper);
9575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9576}
9577
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009578TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009579 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009580 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009581 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009582 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009584
9585 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9586 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9587 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9588 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9589 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9590
9591 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009592 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009593 processId(mapper, FIRST_TRACKING_ID);
9594 processToolType(mapper, MT_TOOL_PEN);
9595 processPosition(mapper, 100, 200);
9596 processPressure(mapper, RAW_PRESSURE_MAX);
9597 processSync(mapper);
9598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9599 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9600 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009601 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009602
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009603 // Now that we know the device supports styluses, ensure that the device is re-configured with
9604 // the stylus source.
9605 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9606 {
9607 const auto& devices = mReader->getInputDevices();
9608 auto deviceInfo =
9609 std::find_if(devices.begin(), devices.end(),
9610 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9611 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9612 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9613 }
9614
9615 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9617
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009618 processId(mapper, INVALID_TRACKING_ID);
9619 processSync(mapper);
9620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9621 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9622 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009623 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009624}
9625
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009626// --- MultiTouchInputMapperTest_ExternalDevice ---
9627
9628class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9629protected:
Chris Yea52ade12020-08-27 16:49:20 -07009630 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009631};
9632
9633/**
9634 * Expect fallback to internal viewport if device is external and external viewport is not present.
9635 */
9636TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9637 prepareAxes(POSITION);
9638 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009639 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009640 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009641
9642 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9643
9644 NotifyMotionArgs motionArgs;
9645
9646 // Expect the event to be sent to the internal viewport,
9647 // because an external viewport is not present.
9648 processPosition(mapper, 100, 100);
9649 processSync(mapper);
9650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009651 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, motionArgs.displayId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009652
9653 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009654 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009655 processPosition(mapper, 100, 100);
9656 processSync(mapper);
9657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9658 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9659}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009660
Prabir Pradhan3ed7e352024-05-03 23:59:43 +00009661// TODO(b/281840344): Remove the test when the old touchpad stack is removed. It is currently
9662// unclear what the behavior of the touchpad logic in TouchInputMapper should do after the
9663// PointerChoreographer refactor.
9664TEST_F(MultiTouchInputMapperTest, DISABLED_Process_TouchpadPointer) {
Harry Cutts8722be92024-04-05 14:46:05 +00009665 // prepare device
Michael Wrighta9cf4192022-12-01 23:46:39 +00009666 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009667 prepareAxes(POSITION | ID | SLOT);
9668 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9669 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009670 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009671 // run uncaptured pointer tests - pushes out generic events
9672 // FINGER 0 DOWN
9673 processId(mapper, 3);
9674 processPosition(mapper, 100, 100);
9675 processKey(mapper, BTN_TOUCH, 1);
9676 processSync(mapper);
9677
9678 // start at (100,100), cursor should be at (0,0) * scale
9679 NotifyMotionArgs args;
9680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9681 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9682 ASSERT_NO_FATAL_FAILURE(
9683 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9684
9685 // FINGER 0 MOVE
9686 processPosition(mapper, 200, 200);
9687 processSync(mapper);
9688
9689 // compute scaling to help with touch position checking
9690 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9691 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9692 float scale =
9693 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9694
9695 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9697 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9699 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +08009700
9701 // BUTTON DOWN
9702 processKey(mapper, BTN_LEFT, 1);
9703 processSync(mapper);
9704
9705 // touchinputmapper design sends a move before button press
9706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9707 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9709 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9710
9711 // BUTTON UP
9712 processKey(mapper, BTN_LEFT, 0);
9713 processSync(mapper);
9714
9715 // touchinputmapper design sends a move after button release
9716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9717 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9719 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009720}
9721
Harry Cutts8722be92024-04-05 14:46:05 +00009722TEST_F(MultiTouchInputMapperTest, Touchpad_GetSources) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00009723 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009724 prepareAxes(POSITION | ID | SLOT);
9725 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Hiroki Sato25040232024-02-22 17:21:22 +09009726 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Arpit Singha8c236b2023-04-25 13:56:05 +00009727 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009728
Josep del Río2d8c79a2023-01-23 19:33:50 +00009729 // uncaptured touchpad should be a pointer device
9730 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009731}
9732
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009733// --- BluetoothMultiTouchInputMapperTest ---
9734
9735class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9736protected:
9737 void SetUp() override {
9738 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9739 }
9740};
9741
9742TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9743 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009744 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009745 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009746 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009747
9748 nsecs_t kernelEventTime = ARBITRARY_TIME;
9749 nsecs_t expectedEventTime = ARBITRARY_TIME;
9750 // Touch down.
9751 processId(mapper, FIRST_TRACKING_ID);
9752 processPosition(mapper, 100, 200);
9753 processPressure(mapper, RAW_PRESSURE_MAX);
9754 processSync(mapper, ARBITRARY_TIME);
9755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9756 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9757
9758 // Process several events that come in quick succession, according to their timestamps.
9759 for (int i = 0; i < 3; i++) {
9760 constexpr static nsecs_t delta = ms2ns(1);
9761 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9762 kernelEventTime += delta;
9763 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
9764
9765 processPosition(mapper, 101 + i, 201 + i);
9766 processSync(mapper, kernelEventTime);
9767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9768 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9769 WithEventTime(expectedEventTime))));
9770 }
9771
9772 // Release the touch.
9773 processId(mapper, INVALID_TRACKING_ID);
9774 processPressure(mapper, RAW_PRESSURE_MIN);
9775 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
9776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9777 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9778 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
9779}
9780
9781// --- MultiTouchPointerModeTest ---
9782
HQ Liue6983c72022-04-19 22:14:56 +00009783class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9784protected:
9785 float mPointerMovementScale;
9786 float mPointerXZoomScale;
9787 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9788 addConfigurationProperty("touch.deviceType", "pointer");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009789 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +00009790
9791 prepareAxes(POSITION);
9792 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9793 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9794 // needs to be disabled, and the pointer gesture needs to be enabled.
Hiroki Sato25040232024-02-22 17:21:22 +09009795 mFakePolicy->setPointerCapture(/*window=*/nullptr);
HQ Liue6983c72022-04-19 22:14:56 +00009796 mFakePolicy->setPointerGestureEnabled(true);
HQ Liue6983c72022-04-19 22:14:56 +00009797
9798 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9799 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9800 mPointerMovementScale =
9801 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9802 mPointerXZoomScale =
9803 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9804 }
9805
9806 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9807 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9808 /*flat*/ 0,
9809 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9810 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9811 /*flat*/ 0,
9812 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9813 }
9814};
9815
9816/**
9817 * Two fingers down on a pointer mode touch pad. The width
9818 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9819 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9820 * be greater than the both value to be freeform gesture, so that after two
9821 * fingers start to move downwards, the gesture should be swipe.
9822 */
9823TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9824 // The min freeform gesture width is 25units/mm x 30mm = 750
9825 // which is greater than fraction of the diagnal length of the touchpad (349).
9826 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +00009827 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009828 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009829 NotifyMotionArgs motionArgs;
9830
9831 // Two fingers down at once.
9832 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9833 // Pointer's initial position is used the [0,0] coordinate.
9834 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9835
9836 processId(mapper, FIRST_TRACKING_ID);
9837 processPosition(mapper, x1, y1);
9838 processMTSync(mapper);
9839 processId(mapper, SECOND_TRACKING_ID);
9840 processPosition(mapper, x2, y2);
9841 processMTSync(mapper);
9842 processSync(mapper);
9843
9844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009845 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009846 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009847 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009848 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009849 ASSERT_NO_FATAL_FAILURE(
9850 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9851
9852 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9853 // that there should be 1 pointer.
9854 int32_t movingDistance = 200;
9855 y1 += movingDistance;
9856 y2 += movingDistance;
9857
9858 processId(mapper, FIRST_TRACKING_ID);
9859 processPosition(mapper, x1, y1);
9860 processMTSync(mapper);
9861 processId(mapper, SECOND_TRACKING_ID);
9862 processPosition(mapper, x2, y2);
9863 processMTSync(mapper);
9864 processSync(mapper);
9865
9866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009867 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009869 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009870 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9872 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9873 0, 0, 0, 0));
9874}
9875
9876/**
9877 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9878 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9879 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9880 * value to be freeform gesture, so that after two fingers start to move downwards,
9881 * the gesture should be swipe.
9882 */
9883TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9884 // The min freeform gesture width is 5units/mm x 30mm = 150
9885 // which is greater than fraction of the diagnal length of the touchpad (349).
9886 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +00009887 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +00009888 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009889 NotifyMotionArgs motionArgs;
9890
9891 // Two fingers down at once.
9892 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9893 // Pointer's initial position is used the [0,0] coordinate.
9894 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9895
9896 processId(mapper, FIRST_TRACKING_ID);
9897 processPosition(mapper, x1, y1);
9898 processMTSync(mapper);
9899 processId(mapper, SECOND_TRACKING_ID);
9900 processPosition(mapper, x2, y2);
9901 processMTSync(mapper);
9902 processSync(mapper);
9903
9904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009905 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009906 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009907 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009908 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009909 ASSERT_NO_FATAL_FAILURE(
9910 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9911
9912 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9913 // and there should be 1 pointer.
9914 int32_t movingDistance = 200;
9915 y1 += movingDistance;
9916 y2 += movingDistance;
9917
9918 processId(mapper, FIRST_TRACKING_ID);
9919 processPosition(mapper, x1, y1);
9920 processMTSync(mapper);
9921 processId(mapper, SECOND_TRACKING_ID);
9922 processPosition(mapper, x2, y2);
9923 processMTSync(mapper);
9924 processSync(mapper);
9925
9926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009927 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009929 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009930 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009931 // New coordinate is the scaled relative coordinate from the initial coordinate.
9932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9933 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9934 0, 0, 0, 0));
9935}
9936
9937/**
9938 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9939 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9940 * freeform gestures after two fingers start to move downwards.
9941 */
9942TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +00009943 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009944 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009945
9946 NotifyMotionArgs motionArgs;
9947
9948 // Two fingers down at once. Wider than the max swipe width.
9949 // The gesture is expected to be PRESS, then transformed to FREEFORM
9950 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9951
9952 processId(mapper, FIRST_TRACKING_ID);
9953 processPosition(mapper, x1, y1);
9954 processMTSync(mapper);
9955 processId(mapper, SECOND_TRACKING_ID);
9956 processPosition(mapper, x2, y2);
9957 processMTSync(mapper);
9958 processSync(mapper);
9959
9960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009961 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009962 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009963 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009964 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009965 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9966 ASSERT_NO_FATAL_FAILURE(
9967 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9968
9969 int32_t movingDistance = 200;
9970
9971 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9972 // then two down events for two pointers.
9973 y1 += movingDistance;
9974 y2 += movingDistance;
9975
9976 processId(mapper, FIRST_TRACKING_ID);
9977 processPosition(mapper, x1, y1);
9978 processMTSync(mapper);
9979 processId(mapper, SECOND_TRACKING_ID);
9980 processPosition(mapper, x2, y2);
9981 processMTSync(mapper);
9982 processSync(mapper);
9983
9984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9985 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009986 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009987 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009989 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009990 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009991 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009993 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009994 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009995 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009996 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009997 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009998 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009999 // Two pointers' scaled relative coordinates from their initial centroid.
10000 // Initial y coordinates are 0 as y1 and y2 have the same value.
10001 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10002 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10003 // When pointers move, the new coordinates equal to the initial coordinates plus
10004 // scaled moving distance.
10005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10006 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10007 0, 0, 0, 0));
10008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10009 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10010 0, 0, 0, 0));
10011
10012 // Move two fingers down again, expect one MOVE motion event.
10013 y1 += movingDistance;
10014 y2 += movingDistance;
10015
10016 processId(mapper, FIRST_TRACKING_ID);
10017 processPosition(mapper, x1, y1);
10018 processMTSync(mapper);
10019 processId(mapper, SECOND_TRACKING_ID);
10020 processPosition(mapper, x2, y2);
10021 processMTSync(mapper);
10022 processSync(mapper);
10023
10024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010025 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010027 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010028 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010029 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10030 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10031 0, 0, 0, 0, 0));
10032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10033 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10034 0, 0, 0, 0, 0));
10035}
10036
Harry Cutts39b7ca22022-10-05 15:55:48 +000010037TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010038 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010039 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010040 NotifyMotionArgs motionArgs;
10041
10042 // Place two fingers down.
10043 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10044
10045 processId(mapper, FIRST_TRACKING_ID);
10046 processPosition(mapper, x1, y1);
10047 processMTSync(mapper);
10048 processId(mapper, SECOND_TRACKING_ID);
10049 processPosition(mapper, x2, y2);
10050 processMTSync(mapper);
10051 processSync(mapper);
10052
10053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010054 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010055 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10056 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10057 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10058 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10059
10060 // Move the two fingers down and to the left.
10061 int32_t movingDistance = 200;
10062 x1 -= movingDistance;
10063 y1 += movingDistance;
10064 x2 -= movingDistance;
10065 y2 += movingDistance;
10066
10067 processId(mapper, FIRST_TRACKING_ID);
10068 processPosition(mapper, x1, y1);
10069 processMTSync(mapper);
10070 processId(mapper, SECOND_TRACKING_ID);
10071 processPosition(mapper, x2, y2);
10072 processMTSync(mapper);
10073 processSync(mapper);
10074
10075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010076 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10078 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10079 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10080 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10081}
10082
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010083TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010084 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010085 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010086 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10088
10089 // Start a stylus gesture.
10090 processKey(mapper, BTN_TOOL_PEN, 1);
10091 processId(mapper, FIRST_TRACKING_ID);
10092 processPosition(mapper, 100, 200);
10093 processSync(mapper);
10094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10095 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10096 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010097 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010098 // TODO(b/257078296): Pointer mode generates extra event.
10099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10100 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10101 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010102 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10104
10105 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10106 // gesture should be disabled.
10107 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10108 viewport->isActive = false;
10109 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010110 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10112 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10113 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010114 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010115 // TODO(b/257078296): Pointer mode generates extra event.
10116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10117 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10118 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010119 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10121}
10122
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010123// --- JoystickInputMapperTest ---
10124
10125class JoystickInputMapperTest : public InputMapperTest {
10126protected:
10127 static const int32_t RAW_X_MIN;
10128 static const int32_t RAW_X_MAX;
10129 static const int32_t RAW_Y_MIN;
10130 static const int32_t RAW_Y_MAX;
10131
10132 void SetUp() override {
10133 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10134 }
10135 void prepareAxes() {
10136 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10137 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10138 }
10139
10140 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10142 }
10143
10144 void processSync(JoystickInputMapper& mapper) {
10145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10146 }
10147
Michael Wrighta9cf4192022-12-01 23:46:39 +000010148 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010149 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10150 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10151 NO_PORT, ViewportType::VIRTUAL);
10152 }
10153};
10154
10155const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10156const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10157const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10158const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10159
10160TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10161 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000010162 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010163
10164 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10165
Michael Wrighta9cf4192022-12-01 23:46:39 +000010166 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010167
10168 // Send an axis event
10169 processAxis(mapper, ABS_X, 100);
10170 processSync(mapper);
10171
10172 NotifyMotionArgs args;
10173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10174 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10175
10176 // Send another axis event
10177 processAxis(mapper, ABS_Y, 100);
10178 processSync(mapper);
10179
10180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10181 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10182}
10183
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010184// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010185
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010186class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010187protected:
10188 static const char* DEVICE_NAME;
10189 static const char* DEVICE_LOCATION;
10190 static const int32_t DEVICE_ID;
10191 static const int32_t DEVICE_GENERATION;
10192 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010193 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010194 static const int32_t EVENTHUB_ID;
10195
10196 std::shared_ptr<FakeEventHub> mFakeEventHub;
10197 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010198 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010199 std::unique_ptr<InstrumentedInputReader> mReader;
10200 std::shared_ptr<InputDevice> mDevice;
10201
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010202 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010203 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010204 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010205 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010206 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010207 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010208 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10209 }
10210
10211 void SetUp() override { SetUp(DEVICE_CLASSES); }
10212
10213 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010214 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010215 mFakePolicy.clear();
10216 }
10217
Chris Yee2b1e5c2021-03-10 22:45:12 -080010218 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10219 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010220 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010221 InputDeviceIdentifier identifier;
10222 identifier.name = name;
10223 identifier.location = location;
10224 std::shared_ptr<InputDevice> device =
10225 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10226 identifier);
10227 mReader->pushNextDevice(device);
10228 mFakeEventHub->addDevice(eventHubId, name, classes);
10229 mReader->loopOnce();
10230 return device;
10231 }
10232
10233 template <class T, typename... Args>
10234 T& addControllerAndConfigure(Args... args) {
10235 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10236
10237 return controller;
10238 }
10239};
10240
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010241const char* PeripheralControllerTest::DEVICE_NAME = "device";
10242const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10243const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10244const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10245const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010246const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10247 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010248const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010249
10250// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010251class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010252protected:
10253 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010254 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010255 }
10256};
10257
10258TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010259 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010260
Harry Cuttsa5b71292022-11-28 12:56:17 +000010261 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10262 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10263 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010264}
10265
10266TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010267 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010268
Harry Cuttsa5b71292022-11-28 12:56:17 +000010269 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10270 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10271 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010272}
10273
10274// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010275class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010276protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010277 void SetUp() override {
10278 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10279 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010280};
10281
Chris Ye85758332021-05-16 23:05:17 -070010282TEST_F(LightControllerTest, MonoLight) {
10283 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010284 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010285 .maxBrightness = 255,
10286 .flags = InputLightClass::BRIGHTNESS,
10287 .path = ""};
10288 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010289
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010290 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010291 InputDeviceInfo info;
10292 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010293 std::vector<InputDeviceLightInfo> lights = info.getLights();
10294 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010295 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10296 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10297
10298 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10299 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10300}
10301
DingYong99f2c3c2023-12-20 15:46:06 +080010302TEST_F(LightControllerTest, MonoKeyboardMuteLight) {
10303 RawLightInfo infoMono = {.id = 1,
10304 .name = "mono_keyboard_mute",
10305 .maxBrightness = 255,
10306 .flags = InputLightClass::BRIGHTNESS |
10307 InputLightClass::KEYBOARD_MIC_MUTE,
10308 .path = ""};
10309 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10310
10311 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10312 std::list<NotifyArgs> unused =
10313 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10314 /*changes=*/{});
10315
10316 InputDeviceInfo info;
10317 controller.populateDeviceInfo(&info);
10318 std::vector<InputDeviceLightInfo> lights = info.getLights();
10319 ASSERT_EQ(1U, lights.size());
10320 ASSERT_EQ(InputDeviceLightType::KEYBOARD_MIC_MUTE, lights[0].type);
10321 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10322}
10323
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010324TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10325 RawLightInfo infoMono = {.id = 1,
10326 .name = "mono_keyboard_backlight",
10327 .maxBrightness = 255,
10328 .flags = InputLightClass::BRIGHTNESS |
10329 InputLightClass::KEYBOARD_BACKLIGHT,
10330 .path = ""};
10331 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10332
10333 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10334 InputDeviceInfo info;
10335 controller.populateDeviceInfo(&info);
10336 std::vector<InputDeviceLightInfo> lights = info.getLights();
10337 ASSERT_EQ(1U, lights.size());
10338 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10339 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010340
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010341 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10342 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010343}
10344
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000010345TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
10346 RawLightInfo infoMono = {.id = 1,
10347 .name = "mono_light",
10348 .maxBrightness = 255,
10349 .flags = InputLightClass::BRIGHTNESS,
10350 .path = ""};
10351 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10352 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10353 "0,100,200");
10354
10355 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10356 std::list<NotifyArgs> unused =
10357 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10358 /*changes=*/{});
10359
10360 InputDeviceInfo info;
10361 controller.populateDeviceInfo(&info);
10362 std::vector<InputDeviceLightInfo> lights = info.getLights();
10363 ASSERT_EQ(1U, lights.size());
10364 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10365}
10366
10367TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
10368 RawLightInfo infoMono = {.id = 1,
10369 .name = "mono_keyboard_backlight",
10370 .maxBrightness = 255,
10371 .flags = InputLightClass::BRIGHTNESS |
10372 InputLightClass::KEYBOARD_BACKLIGHT,
10373 .path = ""};
10374 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10375
10376 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10377 std::list<NotifyArgs> unused =
10378 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10379 /*changes=*/{});
10380
10381 InputDeviceInfo info;
10382 controller.populateDeviceInfo(&info);
10383 std::vector<InputDeviceLightInfo> lights = info.getLights();
10384 ASSERT_EQ(1U, lights.size());
10385 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10386}
10387
10388TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
10389 RawLightInfo infoMono = {.id = 1,
10390 .name = "mono_keyboard_backlight",
10391 .maxBrightness = 255,
10392 .flags = InputLightClass::BRIGHTNESS |
10393 InputLightClass::KEYBOARD_BACKLIGHT,
10394 .path = ""};
10395 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10396 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10397 "0,100,200");
10398
10399 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10400 std::list<NotifyArgs> unused =
10401 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10402 /*changes=*/{});
10403
10404 InputDeviceInfo info;
10405 controller.populateDeviceInfo(&info);
10406 std::vector<InputDeviceLightInfo> lights = info.getLights();
10407 ASSERT_EQ(1U, lights.size());
10408 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
10409 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
10410 ASSERT_EQ(BrightnessLevel(0), *it);
10411 std::advance(it, 1);
10412 ASSERT_EQ(BrightnessLevel(100), *it);
10413 std::advance(it, 1);
10414 ASSERT_EQ(BrightnessLevel(200), *it);
10415}
10416
10417TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
10418 RawLightInfo infoMono = {.id = 1,
10419 .name = "mono_keyboard_backlight",
10420 .maxBrightness = 255,
10421 .flags = InputLightClass::BRIGHTNESS |
10422 InputLightClass::KEYBOARD_BACKLIGHT,
10423 .path = ""};
10424 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10425 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10426 "0,100,200,300,400,500");
10427
10428 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10429 std::list<NotifyArgs> unused =
10430 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10431 /*changes=*/{});
10432
10433 InputDeviceInfo info;
10434 controller.populateDeviceInfo(&info);
10435 std::vector<InputDeviceLightInfo> lights = info.getLights();
10436 ASSERT_EQ(1U, lights.size());
10437 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10438}
10439
Chris Yee2b1e5c2021-03-10 22:45:12 -080010440TEST_F(LightControllerTest, RGBLight) {
10441 RawLightInfo infoRed = {.id = 1,
10442 .name = "red",
10443 .maxBrightness = 255,
10444 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10445 .path = ""};
10446 RawLightInfo infoGreen = {.id = 2,
10447 .name = "green",
10448 .maxBrightness = 255,
10449 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10450 .path = ""};
10451 RawLightInfo infoBlue = {.id = 3,
10452 .name = "blue",
10453 .maxBrightness = 255,
10454 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10455 .path = ""};
10456 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10457 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10458 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10459
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010460 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010461 InputDeviceInfo info;
10462 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010463 std::vector<InputDeviceLightInfo> lights = info.getLights();
10464 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010465 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10466 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10467 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10468
10469 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10470 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10471}
10472
10473TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10474 RawLightInfo infoRed = {.id = 1,
10475 .name = "red_keyboard_backlight",
10476 .maxBrightness = 255,
10477 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10478 InputLightClass::KEYBOARD_BACKLIGHT,
10479 .path = ""};
10480 RawLightInfo infoGreen = {.id = 2,
10481 .name = "green_keyboard_backlight",
10482 .maxBrightness = 255,
10483 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10484 InputLightClass::KEYBOARD_BACKLIGHT,
10485 .path = ""};
10486 RawLightInfo infoBlue = {.id = 3,
10487 .name = "blue_keyboard_backlight",
10488 .maxBrightness = 255,
10489 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10490 InputLightClass::KEYBOARD_BACKLIGHT,
10491 .path = ""};
10492 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10493 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10494 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10495
10496 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10497 InputDeviceInfo info;
10498 controller.populateDeviceInfo(&info);
10499 std::vector<InputDeviceLightInfo> lights = info.getLights();
10500 ASSERT_EQ(1U, lights.size());
10501 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10502 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10503 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10504
10505 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10506 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10507}
10508
10509TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10510 RawLightInfo infoRed = {.id = 1,
10511 .name = "red",
10512 .maxBrightness = 255,
10513 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10514 .path = ""};
10515 RawLightInfo infoGreen = {.id = 2,
10516 .name = "green",
10517 .maxBrightness = 255,
10518 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10519 .path = ""};
10520 RawLightInfo infoBlue = {.id = 3,
10521 .name = "blue",
10522 .maxBrightness = 255,
10523 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10524 .path = ""};
10525 RawLightInfo infoGlobal = {.id = 3,
10526 .name = "global_keyboard_backlight",
10527 .maxBrightness = 255,
10528 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10529 InputLightClass::KEYBOARD_BACKLIGHT,
10530 .path = ""};
10531 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10532 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10533 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10534 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10535
10536 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10537 InputDeviceInfo info;
10538 controller.populateDeviceInfo(&info);
10539 std::vector<InputDeviceLightInfo> lights = info.getLights();
10540 ASSERT_EQ(1U, lights.size());
10541 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10542 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10543 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010544
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010545 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10546 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010547}
10548
10549TEST_F(LightControllerTest, MultiColorRGBLight) {
10550 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010551 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010552 .maxBrightness = 255,
10553 .flags = InputLightClass::BRIGHTNESS |
10554 InputLightClass::MULTI_INTENSITY |
10555 InputLightClass::MULTI_INDEX,
10556 .path = ""};
10557
10558 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10559
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010560 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010561 InputDeviceInfo info;
10562 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010563 std::vector<InputDeviceLightInfo> lights = info.getLights();
10564 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010565 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10566 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10567 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10568
10569 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10570 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10571}
10572
10573TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10574 RawLightInfo infoColor = {.id = 1,
10575 .name = "multi_color_keyboard_backlight",
10576 .maxBrightness = 255,
10577 .flags = InputLightClass::BRIGHTNESS |
10578 InputLightClass::MULTI_INTENSITY |
10579 InputLightClass::MULTI_INDEX |
10580 InputLightClass::KEYBOARD_BACKLIGHT,
10581 .path = ""};
10582
10583 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10584
10585 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10586 InputDeviceInfo info;
10587 controller.populateDeviceInfo(&info);
10588 std::vector<InputDeviceLightInfo> lights = info.getLights();
10589 ASSERT_EQ(1U, lights.size());
10590 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10591 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10592 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010593
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010594 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10595 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010596}
10597
10598TEST_F(LightControllerTest, PlayerIdLight) {
10599 RawLightInfo info1 = {.id = 1,
10600 .name = "player1",
10601 .maxBrightness = 255,
10602 .flags = InputLightClass::BRIGHTNESS,
10603 .path = ""};
10604 RawLightInfo info2 = {.id = 2,
10605 .name = "player2",
10606 .maxBrightness = 255,
10607 .flags = InputLightClass::BRIGHTNESS,
10608 .path = ""};
10609 RawLightInfo info3 = {.id = 3,
10610 .name = "player3",
10611 .maxBrightness = 255,
10612 .flags = InputLightClass::BRIGHTNESS,
10613 .path = ""};
10614 RawLightInfo info4 = {.id = 4,
10615 .name = "player4",
10616 .maxBrightness = 255,
10617 .flags = InputLightClass::BRIGHTNESS,
10618 .path = ""};
10619 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10620 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10621 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10622 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10623
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010624 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010625 InputDeviceInfo info;
10626 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010627 std::vector<InputDeviceLightInfo> lights = info.getLights();
10628 ASSERT_EQ(1U, lights.size());
10629 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010630 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10631 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010632
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010633 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10634 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10635 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010636}
10637
Michael Wrightd02c5b62014-02-10 15:10:22 -080010638} // namespace android