blob: 18469e0b0282301d8f7689cde756630df0d93cdf [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>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Prabir Pradhan018faea2024-05-08 21:52:54 +000029#include <NotifyArgsBuilders.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070031#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070032#include <SingleTouchInputMapper.h>
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000033#include <TestEventMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070034#include <TestInputListener.h>
35#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080036#include <UinputDevice.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070037#include <android-base/thread_annotations.h>
Byoungho Jungda10dd32023-10-06 17:03:45 +090038#include <com_android_input_flags.h>
Henry Barnora7ed6372024-10-21 14:33:43 -070039#include <flag_macros.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000040#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041#include <gtest/gtest.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000042#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080043
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070044#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000045#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000046#include "FakeInputReaderPolicy.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000047#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000048#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000049#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000050#include "input/DisplayViewport.h"
51#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010052
Michael Wrightd02c5b62014-02-10 15:10:22 -080053namespace android {
54
Dominik Laskowski2f01d772022-03-23 16:01:29 -070055using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000056using testing::AllOf;
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +000057using testing::VariantWith;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070058using std::chrono_literals::operator""ms;
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -080059using std::chrono_literals::operator""s;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070060
Michael Wrightd02c5b62014-02-10 15:10:22 -080061// Arbitrary display properties.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070062static constexpr ui::LogicalDisplayId DISPLAY_ID = ui::LogicalDisplayId::DEFAULT;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000063static const std::string DISPLAY_UNIQUE_ID = "local:1";
Linnan Li13bf76a2024-05-05 19:18:02 +080064static constexpr ui::LogicalDisplayId SECONDARY_DISPLAY_ID =
65 ui::LogicalDisplayId{DISPLAY_ID.val() + 1};
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t DISPLAY_WIDTH = 480;
67static constexpr int32_t DISPLAY_HEIGHT = 800;
Linnan Li13bf76a2024-05-05 19:18:02 +080068static constexpr ui::LogicalDisplayId VIRTUAL_DISPLAY_ID = ui::LogicalDisplayId{1};
arthurhungcc7f9802020-04-30 17:55:40 +080069static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
70static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070071static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070072static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080073
arthurhungcc7f9802020-04-30 17:55:40 +080074static constexpr int32_t FIRST_SLOT = 0;
75static constexpr int32_t SECOND_SLOT = 1;
76static constexpr int32_t THIRD_SLOT = 2;
77static constexpr int32_t INVALID_TRACKING_ID = -1;
78static constexpr int32_t FIRST_TRACKING_ID = 0;
79static constexpr int32_t SECOND_TRACKING_ID = 1;
80static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080081static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
82static constexpr int32_t LIGHT_COLOR = 0x7F448866;
83static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080084
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080085static constexpr int32_t ACTION_POINTER_0_DOWN =
86 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
87static constexpr int32_t ACTION_POINTER_0_UP =
88 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
89static constexpr int32_t ACTION_POINTER_1_DOWN =
90 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_1_UP =
92 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93
Prabir Pradhanb08a0e82023-09-14 22:28:32 +000094static constexpr uint32_t STYLUS_FUSION_SOURCE =
95 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
96
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000097// Minimum timestamp separation between subsequent input events from a Bluetooth device.
98static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000099
Byoungho Jungda10dd32023-10-06 17:03:45 +0900100namespace input_flags = com::android::input::flags;
101
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102template<typename T>
103static inline T min(T a, T b) {
104 return a < b ? a : b;
105}
106
107static inline float avg(float x, float y) {
108 return (x + y) / 2;
109}
110
Chris Ye3fdbfef2021-01-06 18:45:18 -0800111// Mapping for light color name and the light color
112const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
113 {"green", LightColor::GREEN},
114 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115
Michael Wrighta9cf4192022-12-01 23:46:39 +0000116static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700117 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000118 case ui::ROTATION_90:
119 return ui::ROTATION_270;
120 case ui::ROTATION_270:
121 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700122 default:
123 return orientation;
124 }
125}
126
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800127static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
128 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000129 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800130
131 const InputDeviceInfo::MotionRange* motionRange =
132 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
133 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
134}
135
136static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
137 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000138 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800139
140 const InputDeviceInfo::MotionRange* motionRange =
141 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
142 ASSERT_EQ(nullptr, motionRange);
143}
144
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700145[[maybe_unused]] static void dumpReader(InputReader& reader) {
146 std::string dump;
147 reader.dump(dump);
148 std::istringstream iss(dump);
149 for (std::string line; std::getline(iss, line);) {
150 ALOGE("%s", line.c_str());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -0800151 std::this_thread::sleep_for(1ms);
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700152 }
153}
154
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155// --- FakeInputMapper ---
156
157class FakeInputMapper : public InputMapper {
158 uint32_t mSources;
159 int32_t mKeyboardType;
160 int32_t mMetaState;
161 KeyedVector<int32_t, int32_t> mKeyCodeStates;
162 KeyedVector<int32_t, int32_t> mScanCodeStates;
163 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100164 // fake mapping which would normally come from keyCharacterMap
165 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800166 std::vector<int32_t> mSupportedKeyCodes;
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700167 std::list<NotifyArgs> mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700169 std::mutex mLock;
170 std::condition_variable mStateChangedCondition;
171 bool mConfigureWasCalled GUARDED_BY(mLock);
172 bool mResetWasCalled GUARDED_BY(mLock);
173 bool mProcessWasCalled GUARDED_BY(mLock);
174 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175
Arthur Hungc23540e2018-11-29 20:42:11 +0800176 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000178 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
179 uint32_t sources)
180 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800181 mSources(sources),
182 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800184 mConfigureWasCalled(false),
185 mResetWasCalled(false),
186 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187
Chris Yea52ade12020-08-27 16:49:20 -0700188 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800189
190 void setKeyboardType(int32_t keyboardType) {
191 mKeyboardType = keyboardType;
192 }
193
194 void setMetaState(int32_t metaState) {
195 mMetaState = metaState;
196 }
197
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700198 // Sets the return value for the `process` call.
199 void setProcessResult(std::list<NotifyArgs> notifyArgs) {
200 mProcessResult.clear();
201 for (auto notifyArg : notifyArgs) {
202 mProcessResult.push_back(notifyArg);
203 }
204 }
205
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700207 std::unique_lock<std::mutex> lock(mLock);
208 base::ScopedLockAssertion assumeLocked(mLock);
209 const bool configureCalled =
210 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
211 return mConfigureWasCalled;
212 });
213 if (!configureCalled) {
214 FAIL() << "Expected configure() to have been called.";
215 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216 mConfigureWasCalled = false;
217 }
218
219 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700220 std::unique_lock<std::mutex> lock(mLock);
221 base::ScopedLockAssertion assumeLocked(mLock);
222 const bool resetCalled =
223 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
224 return mResetWasCalled;
225 });
226 if (!resetCalled) {
227 FAIL() << "Expected reset() to have been called.";
228 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800229 mResetWasCalled = false;
230 }
231
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000232 void assertResetWasNotCalled() {
233 std::scoped_lock lock(mLock);
234 ASSERT_FALSE(mResetWasCalled) << "Expected reset to not have been called.";
235 }
236
Yi Kong9b14ac62018-07-17 13:48:38 -0700237 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700238 std::unique_lock<std::mutex> lock(mLock);
239 base::ScopedLockAssertion assumeLocked(mLock);
240 const bool processCalled =
241 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
242 return mProcessWasCalled;
243 });
244 if (!processCalled) {
245 FAIL() << "Expected process() to have been called.";
246 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800247 if (outLastEvent) {
248 *outLastEvent = mLastEvent;
249 }
250 mProcessWasCalled = false;
251 }
252
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000253 void assertProcessWasNotCalled() {
254 std::scoped_lock lock(mLock);
255 ASSERT_FALSE(mProcessWasCalled) << "Expected process to not have been called.";
256 }
257
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258 void setKeyCodeState(int32_t keyCode, int32_t state) {
259 mKeyCodeStates.replaceValueFor(keyCode, state);
260 }
261
262 void setScanCodeState(int32_t scanCode, int32_t state) {
263 mScanCodeStates.replaceValueFor(scanCode, state);
264 }
265
266 void setSwitchState(int32_t switchCode, int32_t state) {
267 mSwitchStates.replaceValueFor(switchCode, state);
268 }
269
270 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800271 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800272 }
273
Philip Junker4af3b3d2021-12-14 10:36:55 +0100274 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
275 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
276 }
277
Michael Wrightd02c5b62014-02-10 15:10:22 -0800278private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100279 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280
Harry Cuttsd02ea102023-03-17 18:21:30 +0000281 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800282 InputMapper::populateDeviceInfo(deviceInfo);
283
284 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000285 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800286 }
287 }
288
Arpit Singhed6c3de2023-04-05 19:24:37 +0000289 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000290 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700291 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800293
294 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800295 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000296 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000297 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800298 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700299
300 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700301 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800302 }
303
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700304 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700305 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800306 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700307 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700308 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 }
310
Harry Cuttsa32a1192024-06-04 15:10:31 +0000311 std::list<NotifyArgs> process(const RawEvent& rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700312 std::scoped_lock<std::mutex> lock(mLock);
Harry Cuttsa32a1192024-06-04 15:10:31 +0000313 mLastEvent = rawEvent;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700315 mStateChangedCondition.notify_all();
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700316 return mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800317 }
318
Chris Yea52ade12020-08-27 16:49:20 -0700319 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
321 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
322 }
323
Philip Junker4af3b3d2021-12-14 10:36:55 +0100324 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
325 auto it = mKeyCodeMapping.find(locationKeyCode);
326 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
327 }
328
Chris Yea52ade12020-08-27 16:49:20 -0700329 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800330 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
331 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
332 }
333
Chris Yea52ade12020-08-27 16:49:20 -0700334 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800335 ssize_t index = mSwitchStates.indexOfKey(switchCode);
336 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
337 }
338
Chris Yea52ade12020-08-27 16:49:20 -0700339 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700340 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700341 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700342 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800343 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
344 if (keyCodes[i] == mSupportedKeyCodes[j]) {
345 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800346 }
347 }
348 }
Chris Yea52ade12020-08-27 16:49:20 -0700349 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800350 return result;
351 }
352
353 virtual int32_t getMetaState() {
354 return mMetaState;
355 }
356
357 virtual void fadePointer() {
358 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800359
Linnan Li13bf76a2024-05-05 19:18:02 +0800360 virtual std::optional<ui::LogicalDisplayId> getAssociatedDisplay() {
Arthur Hungc23540e2018-11-29 20:42:11 +0800361 if (mViewport) {
362 return std::make_optional(mViewport->displayId);
363 }
364 return std::nullopt;
365 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800366};
367
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700368// --- InputReaderPolicyTest ---
369class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700370protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700371 sp<FakeInputReaderPolicy> mFakePolicy;
372
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700373 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700374 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700375};
376
377/**
378 * Check that empty set of viewports is an acceptable configuration.
379 * Also try to get internal viewport two different ways - by type and by uniqueId.
380 *
381 * There will be confusion if two viewports with empty uniqueId and identical type are present.
382 * Such configuration is not currently allowed.
383 */
384TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700385 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700386
387 // We didn't add any viewports yet, so there shouldn't be any.
388 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100389 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700390 ASSERT_FALSE(internalViewport);
391
392 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000393 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000394 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700395
396 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700397 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700398 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100399 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700400
401 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100402 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700403 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700404 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700405
406 mFakePolicy->clearViewports();
407 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700408 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700409 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100410 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700411 ASSERT_FALSE(internalViewport);
412}
413
414TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
415 const std::string internalUniqueId = "local:0";
416 const std::string externalUniqueId = "local:1";
417 const std::string virtualUniqueId1 = "virtual:2";
418 const std::string virtualUniqueId2 = "virtual:3";
Linnan Li13bf76a2024-05-05 19:18:02 +0800419 constexpr ui::LogicalDisplayId virtualDisplayId1 = ui::LogicalDisplayId{2};
420 constexpr ui::LogicalDisplayId virtualDisplayId2 = ui::LogicalDisplayId{3};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700421
422 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000423 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000424 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000425 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700426 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000427 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000428 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000429 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700430 // Add an virtual viewport
431 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000432 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000433 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700434 // Add another virtual viewport
435 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000436 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000437 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700438
439 // Check matching by type for internal
440 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100441 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700442 ASSERT_TRUE(internalViewport);
443 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
444
445 // Check matching by type for external
446 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100447 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700448 ASSERT_TRUE(externalViewport);
449 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
450
451 // Check matching by uniqueId for virtual viewport #1
452 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700453 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700454 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100455 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700456 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
457 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
458
459 // Check matching by uniqueId for virtual viewport #2
460 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700461 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700462 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100463 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700464 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
465 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
466}
467
468
469/**
470 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
471 * that lookup works by checking display id.
472 * Check that 2 viewports of each kind is possible, for all existing viewport types.
473 */
474TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
475 const std::string uniqueId1 = "uniqueId1";
476 const std::string uniqueId2 = "uniqueId2";
Linnan Li13bf76a2024-05-05 19:18:02 +0800477 constexpr ui::LogicalDisplayId displayId1 = ui::LogicalDisplayId{2};
478 constexpr ui::LogicalDisplayId displayId2 = ui::LogicalDisplayId{3};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700479
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100480 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
481 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700482 for (const ViewportType& type : types) {
483 mFakePolicy->clearViewports();
484 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000485 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000486 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700487 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000488 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000489 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700490
491 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700492 std::optional<DisplayViewport> viewport1 =
493 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700494 ASSERT_TRUE(viewport1);
495 ASSERT_EQ(displayId1, viewport1->displayId);
496 ASSERT_EQ(type, viewport1->type);
497
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700498 std::optional<DisplayViewport> viewport2 =
499 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700500 ASSERT_TRUE(viewport2);
501 ASSERT_EQ(displayId2, viewport2->displayId);
502 ASSERT_EQ(type, viewport2->type);
503
504 // When there are multiple viewports of the same kind, and uniqueId is not specified
505 // in the call to getDisplayViewport, then that situation is not supported.
506 // The viewports can be stored in any order, so we cannot rely on the order, since that
507 // is just implementation detail.
508 // However, we can check that it still returns *a* viewport, we just cannot assert
509 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700510 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700511 ASSERT_TRUE(someViewport);
512 }
513}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800514
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700515/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000516 * When we have multiple internal displays make sure we always return the default display when
517 * querying by type.
518 */
519TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
520 const std::string uniqueId1 = "uniqueId1";
521 const std::string uniqueId2 = "uniqueId2";
Linnan Li13bf76a2024-05-05 19:18:02 +0800522 constexpr ui::LogicalDisplayId nonDefaultDisplayId = ui::LogicalDisplayId{2};
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700523 ASSERT_NE(nonDefaultDisplayId, ui::LogicalDisplayId::DEFAULT)
524 << "Test display ID should not be ui::LogicalDisplayId::DEFAULT ";
Michael Wrightdde67b82020-10-27 16:09:22 +0000525
526 // Add the default display first and ensure it gets returned.
527 mFakePolicy->clearViewports();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700528 mFakePolicy->addDisplayViewport(ui::LogicalDisplayId::DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000529 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000530 ViewportType::INTERNAL);
531 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000532 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000533 ViewportType::INTERNAL);
534
535 std::optional<DisplayViewport> viewport =
536 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
537 ASSERT_TRUE(viewport);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700538 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, viewport->displayId);
Michael Wrightdde67b82020-10-27 16:09:22 +0000539 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
540
541 // Add the default display second to make sure order doesn't matter.
542 mFakePolicy->clearViewports();
543 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000544 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000545 ViewportType::INTERNAL);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700546 mFakePolicy->addDisplayViewport(ui::LogicalDisplayId::DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000547 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000548 ViewportType::INTERNAL);
549
550 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
551 ASSERT_TRUE(viewport);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700552 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, viewport->displayId);
Michael Wrightdde67b82020-10-27 16:09:22 +0000553 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
554}
555
556/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700557 * Check getDisplayViewportByPort
558 */
559TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100560 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700561 const std::string uniqueId1 = "uniqueId1";
562 const std::string uniqueId2 = "uniqueId2";
Linnan Li13bf76a2024-05-05 19:18:02 +0800563 constexpr ui::LogicalDisplayId displayId1 = ui::LogicalDisplayId{1};
564 constexpr ui::LogicalDisplayId displayId2 = ui::LogicalDisplayId{2};
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700565 const uint8_t hdmi1 = 0;
566 const uint8_t hdmi2 = 1;
567 const uint8_t hdmi3 = 2;
568
569 mFakePolicy->clearViewports();
570 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000571 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000572 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700573 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000574 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000575 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700576
577 // Check that correct display viewport was returned by comparing the display ports.
578 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
579 ASSERT_TRUE(hdmi1Viewport);
580 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
581 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
582
583 // Check that we can still get the same viewport using the uniqueId
584 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
585 ASSERT_TRUE(hdmi1Viewport);
586 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
587 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
588 ASSERT_EQ(type, hdmi1Viewport->type);
589
590 // Check that we cannot find a port with "HDMI2", because we never added one
591 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
592 ASSERT_FALSE(hdmi2Viewport);
593}
594
Michael Wrightd02c5b62014-02-10 15:10:22 -0800595// --- InputReaderTest ---
596
597class InputReaderTest : public testing::Test {
598protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700599 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700601 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000602 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800603
Chris Yea52ade12020-08-27 16:49:20 -0700604 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700605 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700606 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700607 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800608
Prabir Pradhan28efc192019-11-05 01:10:04 +0000609 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700610 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611 }
612
Chris Yea52ade12020-08-27 16:49:20 -0700613 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700614 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800615 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616 }
617
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700618 void addDevice(int32_t eventHubId, const std::string& name,
619 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800620 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621
622 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800623 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800624 }
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000625 mReader->loopOnce();
626 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700627 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700629 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630 }
631
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800632 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700633 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000634 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700635 }
636
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800637 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700638 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000639 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700640 }
641
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800642 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700643 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700644 ftl::Flags<InputDeviceClass> classes,
645 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800646 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800647 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000648 FakeInputMapper& mapper =
649 device->addMapper<FakeInputMapper>(eventHubId,
650 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800651 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800652 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800653 return mapper;
654 }
655};
656
Chris Ye98d3f532020-10-01 21:48:59 -0700657TEST_F(InputReaderTest, PolicyGetInputDevices) {
658 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700659 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700660 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661
662 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700663 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800665 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100666 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
668 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000669 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670}
671
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000672TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
673 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
674 mFakeEventHub->setSysfsRootPath(1, "xyz");
675
676 // Should also have received a notification describing the new input device.
677 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
678 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
679 ASSERT_EQ(0U, inputDevice.getLights().size());
680
681 RawLightInfo infoMonolight = {.id = 123,
682 .name = "mono_keyboard_backlight",
683 .maxBrightness = 255,
684 .flags = InputLightClass::BRIGHTNESS,
685 .path = ""};
686 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
687 mReader->sysfsNodeChanged("xyz");
688 mReader->loopOnce();
689
690 // Should also have received a notification describing the new recreated input device.
691 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
692 inputDevice = mFakePolicy->getInputDevices()[0];
693 ASSERT_EQ(1U, inputDevice.getLights().size());
694}
695
Chris Yee7310032020-09-22 15:36:28 -0700696TEST_F(InputReaderTest, GetMergedInputDevices) {
697 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
698 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
699 // Add two subdevices to device
700 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
701 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000702 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
703 AINPUT_SOURCE_KEYBOARD);
704 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
705 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700706
707 // Push same device instance for next device to be added, so they'll have same identifier.
708 mReader->pushNextDevice(device);
709 mReader->pushNextDevice(device);
710 ASSERT_NO_FATAL_FAILURE(
711 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
712 ASSERT_NO_FATAL_FAILURE(
713 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
714
715 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000716 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700717}
718
Chris Yee14523a2020-12-19 13:46:00 -0800719TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
720 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
721 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
722 // Add two subdevices to device
723 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
724 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000725 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
726 AINPUT_SOURCE_KEYBOARD);
727 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
728 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800729
730 // Push same device instance for next device to be added, so they'll have same identifier.
731 mReader->pushNextDevice(device);
732 mReader->pushNextDevice(device);
733 // Sensor device is initially disabled
734 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
735 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
736 nullptr));
737 // Device is disabled because the only sub device is a sensor device and disabled initially.
738 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
739 ASSERT_FALSE(device->isEnabled());
740 ASSERT_NO_FATAL_FAILURE(
741 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
742 // The merged device is enabled if any sub device is enabled
743 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
744 ASSERT_TRUE(device->isEnabled());
745}
746
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700747TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800748 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700749 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800750 constexpr int32_t eventHubId = 1;
751 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700752 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000753 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
754 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800755 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800756 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700757
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700758 NotifyDeviceResetArgs resetArgs;
759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700760 ASSERT_EQ(deviceId, resetArgs.deviceId);
761
762 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800763 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000764 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700765
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700767 ASSERT_EQ(deviceId, resetArgs.deviceId);
768 ASSERT_EQ(device->isEnabled(), false);
769
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800770 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000771 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700773 ASSERT_EQ(device->isEnabled(), false);
774
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800775 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000776 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700778 ASSERT_EQ(deviceId, resetArgs.deviceId);
779 ASSERT_EQ(device->isEnabled(), true);
780}
781
Michael Wrightd02c5b62014-02-10 15:10:22 -0800782TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800783 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700784 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800785 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800786 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800787 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800788 AINPUT_SOURCE_KEYBOARD, nullptr);
789 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790
791 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
792 AINPUT_SOURCE_ANY, AKEYCODE_A))
793 << "Should return unknown when the device id is >= 0 but unknown.";
794
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800795 ASSERT_EQ(AKEY_STATE_UNKNOWN,
796 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
797 << "Should return unknown when the device id is valid but the sources are not "
798 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800799
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800800 ASSERT_EQ(AKEY_STATE_DOWN,
801 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
802 AKEYCODE_A))
803 << "Should return value provided by mapper when device id is valid and the device "
804 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805
806 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
807 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
808 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
809
810 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
811 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
812 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
813}
814
Philip Junker4af3b3d2021-12-14 10:36:55 +0100815TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
816 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
817 constexpr int32_t eventHubId = 1;
818 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
819 InputDeviceClass::KEYBOARD,
820 AINPUT_SOURCE_KEYBOARD, nullptr);
821 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
822
823 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
824 << "Should return unknown when the device with the specified id is not found.";
825
826 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
827 << "Should return correct mapping when device id is valid and mapping exists.";
828
829 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
830 << "Should return the location key code when device id is valid and there's no "
831 "mapping.";
832}
833
834TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
835 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
836 constexpr int32_t eventHubId = 1;
837 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
838 InputDeviceClass::JOYSTICK,
839 AINPUT_SOURCE_GAMEPAD, nullptr);
840 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
841
842 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
843 << "Should return unknown when the device id is valid but there is no keyboard mapper";
844}
845
Michael Wrightd02c5b62014-02-10 15:10:22 -0800846TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800847 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700848 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800849 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800850 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800851 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800852 AINPUT_SOURCE_KEYBOARD, nullptr);
853 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854
855 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
856 AINPUT_SOURCE_ANY, KEY_A))
857 << "Should return unknown when the device id is >= 0 but unknown.";
858
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800859 ASSERT_EQ(AKEY_STATE_UNKNOWN,
860 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
861 << "Should return unknown when the device id is valid but the sources are not "
862 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800863
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 ASSERT_EQ(AKEY_STATE_DOWN,
865 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
866 KEY_A))
867 << "Should return value provided by mapper when device id is valid and the device "
868 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869
870 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
871 AINPUT_SOURCE_TRACKBALL, KEY_A))
872 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
873
874 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
875 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
876 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
877}
878
879TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800880 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700881 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800882 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800883 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800884 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800885 AINPUT_SOURCE_KEYBOARD, nullptr);
886 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887
888 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
889 AINPUT_SOURCE_ANY, SW_LID))
890 << "Should return unknown when the device id is >= 0 but unknown.";
891
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800892 ASSERT_EQ(AKEY_STATE_UNKNOWN,
893 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
894 << "Should return unknown when the device id is valid but the sources are not "
895 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800897 ASSERT_EQ(AKEY_STATE_DOWN,
898 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
899 SW_LID))
900 << "Should return value provided by mapper when device id is valid and the device "
901 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800902
903 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
904 AINPUT_SOURCE_TRACKBALL, SW_LID))
905 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
906
907 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
908 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
909 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
910}
911
912TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800913 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700914 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800915 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800916 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800917 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800918 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100919
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800920 mapper.addSupportedKeyCode(AKEYCODE_A);
921 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700923 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800924 uint8_t flags[4] = { 0, 0, 0, 1 };
925
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700926 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927 << "Should return false when device id is >= 0 but unknown.";
928 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
929
930 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700931 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800932 << "Should return false when device id is valid but the sources are not supported by "
933 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
935
936 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700937 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800938 keyCodes, flags))
939 << "Should return value provided by mapper when device id is valid and the device "
940 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800941 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
942
943 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700944 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
945 << "Should return false when the device id is < 0 but the sources are not supported by "
946 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
948
949 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700950 ASSERT_TRUE(
951 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
952 << "Should return value provided by mapper when device id is < 0 and one of the "
953 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800954 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
955}
956
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000957TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800958 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700959 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000960 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800961 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000962 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800963 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800964 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800965 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000967 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000968 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
970
971 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800972 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000973 ASSERT_EQ(when, event.when);
974 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800975 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800976 ASSERT_EQ(EV_KEY, event.type);
977 ASSERT_EQ(KEY_A, event.code);
978 ASSERT_EQ(1, event.value);
979}
980
Garfield Tan1c7bc862020-01-28 13:24:04 -0800981TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800982 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700983 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800984 constexpr int32_t eventHubId = 1;
985 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800986 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000987 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
988 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800989 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800990 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800991
992 NotifyDeviceResetArgs resetArgs;
993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800994 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800995
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800996 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000997 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800999 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001000 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001001
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001002 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001003 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001005 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001006 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001007
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001008 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001009 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001011 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001012 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001013}
1014
Garfield Tan1c7bc862020-01-28 13:24:04 -08001015TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1016 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001017 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001018 constexpr int32_t eventHubId = 1;
1019 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1020 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001021 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1022 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001023 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001024 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1025
1026 NotifyDeviceResetArgs resetArgs;
1027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1028 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1029}
1030
Arthur Hungc23540e2018-11-29 20:42:11 +08001031TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001032 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001033 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001034 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001035 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001036 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1037 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001038 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1039 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001040 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001041
1042 const uint8_t hdmi1 = 1;
1043
1044 // Associated touch screen with second display.
1045 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1046
1047 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001048 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001049 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001050 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001051 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001052 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001053 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001054 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001055 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001056
1057 // Add the device, and make sure all of the callbacks are triggered.
1058 // The device is added after the input port associations are processed since
1059 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001060 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan28efc192019-11-05 01:10:04 +00001061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001062 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001063
Arthur Hung2c9a3342019-07-23 14:18:59 +08001064 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001065 ASSERT_EQ(deviceId, device->getId());
1066 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1067 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001068
1069 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001070 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001071 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001072 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001073}
1074
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001075TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1076 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001077 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001078 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1079 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1080 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001081 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1082 AINPUT_SOURCE_KEYBOARD);
1083 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1084 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001085 mReader->pushNextDevice(device);
1086 mReader->pushNextDevice(device);
1087 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1088 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1089
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001090 NotifyDeviceResetArgs resetArgs;
1091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1092 ASSERT_EQ(deviceId, resetArgs.deviceId);
1093 ASSERT_TRUE(device->isEnabled());
1094 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1095 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1096
1097 disableDevice(deviceId);
1098 mReader->loopOnce();
1099
1100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1101 ASSERT_EQ(deviceId, resetArgs.deviceId);
1102 ASSERT_FALSE(device->isEnabled());
1103 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1104 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1105
1106 enableDevice(deviceId);
1107 mReader->loopOnce();
1108
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
1116TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1117 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001118 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001119 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1120 // Add two subdevices to device
1121 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1122 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001123 device->addMapper<FakeInputMapper>(eventHubIds[0],
1124 mFakePolicy->getReaderConfiguration(),
1125 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001126 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001127 device->addMapper<FakeInputMapper>(eventHubIds[1],
1128 mFakePolicy->getReaderConfiguration(),
1129 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001130 mReader->pushNextDevice(device);
1131 mReader->pushNextDevice(device);
1132 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1133 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1134
1135 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1136 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1137
1138 ASSERT_EQ(AKEY_STATE_DOWN,
1139 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1140 ASSERT_EQ(AKEY_STATE_DOWN,
1141 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1142 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1143 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1144}
1145
Prabir Pradhan7e186182020-11-10 13:56:45 -08001146TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1147 NotifyPointerCaptureChangedArgs args;
1148
Hiroki Sato25040232024-02-22 17:21:22 +09001149 auto request = mFakePolicy->setPointerCapture(/*window=*/sp<BBinder>::make());
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001150 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001151 mReader->loopOnce();
1152 mFakeListener->assertNotifyCaptureWasCalled(&args);
Hiroki Sato25040232024-02-22 17:21:22 +09001153 ASSERT_TRUE(args.request.isEnable()) << "Pointer Capture should be enabled.";
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001154 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001155
Hiroki Sato25040232024-02-22 17:21:22 +09001156 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001157 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001158 mReader->loopOnce();
1159 mFakeListener->assertNotifyCaptureWasCalled(&args);
Hiroki Sato25040232024-02-22 17:21:22 +09001160 ASSERT_FALSE(args.request.isEnable()) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001161
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001162 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001163 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001164 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001165 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001166 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001167}
1168
Prabir Pradhan018faea2024-05-08 21:52:54 +00001169TEST_F(InputReaderTest, GetLastUsedInputDeviceId) {
1170 constexpr int32_t FIRST_DEVICE_ID = END_RESERVED_ID + 1000;
1171 constexpr int32_t SECOND_DEVICE_ID = FIRST_DEVICE_ID + 1;
1172 FakeInputMapper& firstMapper =
1173 addDeviceWithFakeInputMapper(FIRST_DEVICE_ID, FIRST_DEVICE_ID, "first",
1174 InputDeviceClass::KEYBOARD, AINPUT_SOURCE_KEYBOARD,
1175 /*configuration=*/nullptr);
1176 FakeInputMapper& secondMapper =
1177 addDeviceWithFakeInputMapper(SECOND_DEVICE_ID, SECOND_DEVICE_ID, "second",
1178 InputDeviceClass::TOUCH_MT, AINPUT_SOURCE_STYLUS,
1179 /*configuration=*/nullptr);
1180
1181 ASSERT_EQ(ReservedInputDeviceId::INVALID_INPUT_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1182
1183 // Start a new key gesture from the first device
1184 firstMapper.setProcessResult({KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
1185 .deviceId(FIRST_DEVICE_ID)
1186 .build()});
1187 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, FIRST_DEVICE_ID, 0, 0, 0);
1188 mReader->loopOnce();
1189 ASSERT_EQ(firstMapper.getDeviceId(), mReader->getLastUsedInputDeviceId());
1190
1191 // Start a new touch gesture from the second device
1192 secondMapper.setProcessResult(
1193 {MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS)
1194 .deviceId(SECOND_DEVICE_ID)
1195 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER))
1196 .build()});
1197 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1198 mReader->loopOnce();
1199 ASSERT_EQ(SECOND_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1200
1201 // Releasing the key is not a new gesture, so it does not update the last used device
1202 firstMapper.setProcessResult({KeyArgsBuilder(AKEY_EVENT_ACTION_UP, 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(SECOND_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1208
1209 // But pressing a new key does start a new gesture
1210 firstMapper.setProcessResult({KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
1211 .deviceId(FIRST_DEVICE_ID)
1212 .build()});
1213 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, FIRST_DEVICE_ID, 0, 0, 0);
1214 mReader->loopOnce();
1215 ASSERT_EQ(FIRST_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1216
1217 // Moving or ending a touch gesture does not update the last used device
1218 secondMapper.setProcessResult(
1219 {MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
1220 .deviceId(SECOND_DEVICE_ID)
1221 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS))
1222 .build()});
1223 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1224 mReader->loopOnce();
1225 ASSERT_EQ(FIRST_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1226 secondMapper.setProcessResult({MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
1227 .deviceId(SECOND_DEVICE_ID)
1228 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS))
1229 .build()});
1230 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1231 mReader->loopOnce();
1232 ASSERT_EQ(FIRST_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1233
1234 // Starting a new hover gesture updates the last used device
1235 secondMapper.setProcessResult(
1236 {MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
1237 .deviceId(SECOND_DEVICE_ID)
1238 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS))
1239 .build()});
1240 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1241 mReader->loopOnce();
1242 ASSERT_EQ(SECOND_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1243}
1244
Chris Ye87143712020-11-10 05:05:58 +00001245class FakeVibratorInputMapper : public FakeInputMapper {
1246public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001247 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1248 const InputReaderConfiguration& readerConfig, uint32_t sources)
1249 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001250
1251 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1252};
1253
1254TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1255 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001256 ftl::Flags<InputDeviceClass> deviceClass =
1257 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001258 constexpr int32_t eventHubId = 1;
1259 const char* DEVICE_LOCATION = "BLUETOOTH";
1260 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1261 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001262 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1263 mFakePolicy->getReaderConfiguration(),
1264 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001265 mReader->pushNextDevice(device);
1266
1267 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1268 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1269
1270 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1271 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1272}
1273
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001274// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001275
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001276class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001277public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001278 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001279
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001280 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001281
Andy Chenf9f1a022022-08-29 20:07:10 -04001282 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1283
Chris Yee2b1e5c2021-03-10 22:45:12 -08001284 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1285
1286 void dump(std::string& dump) override {}
1287
1288 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1289 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001290 }
1291
Chris Yee2b1e5c2021-03-10 22:45:12 -08001292 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1293 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001294 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001295
1296 bool setLightColor(int32_t lightId, int32_t color) override {
1297 getDeviceContext().setLightBrightness(lightId, color >> 24);
1298 return true;
1299 }
1300
1301 std::optional<int32_t> getLightColor(int32_t lightId) override {
1302 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1303 if (!result.has_value()) {
1304 return std::nullopt;
1305 }
1306 return result.value() << 24;
1307 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001308
1309 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1310
1311 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1312
1313private:
1314 InputDeviceContext& mDeviceContext;
1315 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1316 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001317 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001318};
1319
Chris Yee2b1e5c2021-03-10 22:45:12 -08001320TEST_F(InputReaderTest, BatteryGetCapacity) {
1321 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001322 ftl::Flags<InputDeviceClass> deviceClass =
1323 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001324 constexpr int32_t eventHubId = 1;
1325 const char* DEVICE_LOCATION = "BLUETOOTH";
1326 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001327 FakePeripheralController& controller =
1328 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001329 mReader->pushNextDevice(device);
1330
1331 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1332
Harry Cuttsa5b71292022-11-28 12:56:17 +00001333 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1334 FakeEventHub::BATTERY_CAPACITY);
1335 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001336}
1337
1338TEST_F(InputReaderTest, BatteryGetStatus) {
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.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1352 FakeEventHub::BATTERY_STATUS);
1353 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001354}
1355
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001356TEST_F(InputReaderTest, BatteryGetDevicePath) {
1357 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1358 ftl::Flags<InputDeviceClass> deviceClass =
1359 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1360 constexpr int32_t eventHubId = 1;
1361 const char* DEVICE_LOCATION = "BLUETOOTH";
1362 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1363 device->addController<FakePeripheralController>(eventHubId);
1364 mReader->pushNextDevice(device);
1365
1366 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1367
Harry Cuttsa5b71292022-11-28 12:56:17 +00001368 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001369}
1370
Chris Ye3fdbfef2021-01-06 18:45:18 -08001371TEST_F(InputReaderTest, LightGetColor) {
1372 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001373 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001374 constexpr int32_t eventHubId = 1;
1375 const char* DEVICE_LOCATION = "BLUETOOTH";
1376 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001377 FakePeripheralController& controller =
1378 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001379 mReader->pushNextDevice(device);
1380 RawLightInfo info = {.id = 1,
1381 .name = "Mono",
1382 .maxBrightness = 255,
1383 .flags = InputLightClass::BRIGHTNESS,
1384 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001385 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1386 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001387
1388 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001389
Harry Cutts33476232023-01-30 19:57:29 +00001390 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1391 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1392 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1393 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001394}
1395
Yanye Li81a590c2024-10-22 19:25:54 +00001396TEST_F(InputReaderTest, SetPowerWakeUp) {
1397 ASSERT_NO_FATAL_FAILURE(addDevice(1, "1st", InputDeviceClass::KEYBOARD, nullptr));
1398 ASSERT_NO_FATAL_FAILURE(addDevice(2, "2nd", InputDeviceClass::KEYBOARD, nullptr));
1399 ASSERT_NO_FATAL_FAILURE(addDevice(3, "3rd", InputDeviceClass::KEYBOARD, nullptr));
1400
1401 ASSERT_EQ(mFakeEventHub->fakeReadKernelWakeup(1), false);
1402
1403 ASSERT_TRUE(mFakeEventHub->setKernelWakeEnabled(2, true));
1404 ASSERT_EQ(mFakeEventHub->fakeReadKernelWakeup(2), true);
1405
1406 ASSERT_TRUE(mFakeEventHub->setKernelWakeEnabled(3, false));
1407 ASSERT_EQ(mFakeEventHub->fakeReadKernelWakeup(3), false);
1408}
1409
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001410// --- InputReaderIntegrationTest ---
1411
1412// These tests create and interact with the InputReader only through its interface.
1413// The InputReader is started during SetUp(), which starts its processing in its own
1414// thread. The tests use linux uinput to emulate input devices.
1415// NOTE: Interacting with the physical device while these tests are running may cause
1416// the tests to fail.
1417class InputReaderIntegrationTest : public testing::Test {
1418protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001419 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001420 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001421 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001422
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001423 constexpr static auto EVENT_HAPPENED_TIMEOUT = 2000ms;
1424 constexpr static auto EVENT_DID_NOT_HAPPEN_TIMEOUT = 30ms;
1425
Chris Yea52ade12020-08-27 16:49:20 -07001426 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001427#if !defined(__ANDROID__)
1428 GTEST_SKIP();
1429#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001430 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001431
Arpit Singh440bf652023-08-09 09:23:43 +00001432 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001433 }
1434
Chris Yea52ade12020-08-27 16:49:20 -07001435 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001436#if !defined(__ANDROID__)
1437 return;
1438#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001439 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001440 mReader.reset();
1441 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001442 mFakePolicy.clear();
1443 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001444
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001445 std::optional<InputDeviceInfo> waitForDevice(const std::string& deviceName) {
1446 std::chrono::time_point start = std::chrono::steady_clock::now();
1447 while (true) {
1448 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1449 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1450 [&deviceName](const InputDeviceInfo& info) {
1451 return info.getIdentifier().name == deviceName;
1452 });
1453 if (it != inputDevices.end()) {
1454 return std::make_optional(*it);
1455 }
1456 std::this_thread::sleep_for(1ms);
1457 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
1458 if (elapsed > 5s) {
1459 return {};
1460 }
1461 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001462 }
Arpit Singh440bf652023-08-09 09:23:43 +00001463
1464 void setupInputReader() {
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001465 mTestListener = std::make_unique<TestInputListener>(EVENT_HAPPENED_TIMEOUT,
1466 EVENT_DID_NOT_HAPPEN_TIMEOUT);
Arpit Singh440bf652023-08-09 09:23:43 +00001467
1468 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1469 *mTestListener);
1470 ASSERT_EQ(mReader->start(), OK);
1471
1472 // Since this test is run on a real device, all the input devices connected
1473 // to the test device will show up in mReader. We wait for those input devices to
1474 // show up before beginning the tests.
Arpit Singh440bf652023-08-09 09:23:43 +00001475 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
Vaibhav Devmuraric109d812024-07-10 14:21:27 +00001476 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Arpit Singh440bf652023-08-09 09:23:43 +00001477 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001478};
1479
1480TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1481 // An invalid input device that is only used for this test.
1482 class InvalidUinputDevice : public UinputDevice {
1483 public:
Harry Cutts33476232023-01-30 19:57:29 +00001484 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001485
1486 private:
1487 void configureDevice(int fd, uinput_user_dev* device) override {}
1488 };
1489
1490 const size_t numDevices = mFakePolicy->getInputDevices().size();
1491
1492 // UinputDevice does not set any event or key bits, so InputReader should not
1493 // consider it as a valid device.
1494 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1495 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001496 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1497
1498 invalidDevice.reset();
1499 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001500 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1501}
1502
1503TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1504 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1505
1506 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1507 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001508 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1509
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001510 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001511 ASSERT_TRUE(device.has_value());
1512 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1513 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1514 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001515
1516 keyboard.reset();
1517 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001518 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1519}
1520
1521TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1522 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1523 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1524
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001525 NotifyKeyArgs keyArgs;
1526 keyboard->pressAndReleaseHomeKey();
1527 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1528 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001529 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Liana Kazanova5b8217b2024-07-18 17:44:51 +00001530
1531 int32_t prevId = keyArgs.id;
1532 nsecs_t prevTimestamp = keyArgs.eventTime;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001533
1534 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1535 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001536 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001537 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001538 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001539}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001540
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001541TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1542 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1543 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1544
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001545 const auto device = waitForDevice(stylus->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001546 ASSERT_TRUE(device.has_value());
1547
Prabir Pradhana3621852022-10-14 18:57:23 +00001548 // An external stylus with buttons should also be recognized as a keyboard.
1549 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001550 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1551 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1552
1553 const auto DOWN =
1554 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1555 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1556
1557 stylus->pressAndReleaseKey(BTN_STYLUS);
1558 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1559 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1560 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1561 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1562
1563 stylus->pressAndReleaseKey(BTN_STYLUS2);
1564 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1565 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1566 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1567 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1568
1569 stylus->pressAndReleaseKey(BTN_STYLUS3);
1570 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1571 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1572 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1573 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1574}
1575
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001576TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1577 std::unique_ptr<UinputKeyboard> keyboard =
1578 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1579 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1580 KEY_R, KEY_T, KEY_Y,
1581 BTN_STYLUS, BTN_STYLUS2,
1582 BTN_STYLUS3});
1583 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1584
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001585 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001586 ASSERT_TRUE(device.has_value());
1587
1588 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1589 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1590 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1591 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1592}
1593
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001594TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1595 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1596 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1597 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1598 createUinputDevice<UinputKeyboardWithHidUsage>(
1599 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1600 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1601
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001602 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001603 ASSERT_TRUE(device.has_value());
1604
1605 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1606 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1607
1608 // If a device supports reporting HID usage codes, it shouldn't automatically support
1609 // stylus keys.
1610 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1611 uint8_t outFlags[] = {0};
1612 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1613 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1614}
1615
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001616/**
1617 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1618 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1619 * are passed to the listener.
1620 */
1621static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1622TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1623 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1624 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1625 NotifyKeyArgs keyArgs;
1626
1627 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1628 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1629 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1630 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1631
1632 controller->pressAndReleaseKey(BTN_GEAR_UP);
1633 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1634 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1635 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1636}
1637
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001638// --- TouchIntegrationTest ---
1639
Arpit Singh440bf652023-08-09 09:23:43 +00001640class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001641protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001642 const std::string UNIQUE_ID = "local:0";
1643
Chris Yea52ade12020-08-27 16:49:20 -07001644 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001645#if !defined(__ANDROID__)
1646 GTEST_SKIP();
1647#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001648 InputReaderIntegrationTest::SetUp();
1649 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001650 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1651 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001652
1653 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1654 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001655 const auto info = waitForDevice(mDevice->getName());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001656 ASSERT_TRUE(info);
1657 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001658 }
1659
Linnan Li13bf76a2024-05-05 19:18:02 +08001660 void setDisplayInfoAndReconfigure(ui::LogicalDisplayId displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001661 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001662 std::optional<uint8_t> physicalPort,
1663 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001664 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001665 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001666 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001667 }
1668
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001669 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1670 NotifyMotionArgs args;
1671 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1672 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001673 ASSERT_EQ(points.size(), args.getPointerCount());
1674 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001675 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1676 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1677 }
1678 }
1679
Arthur Hungaab25622020-01-16 11:22:11 +08001680 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001681 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001682};
1683
Arpit Singh440bf652023-08-09 09:23:43 +00001684enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1685
1686class TouchIntegrationTest : public BaseTouchIntegrationTest,
1687 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1688protected:
1689 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1690 const std::string INPUT_PORT = "uinput_touch/input0";
1691
1692 void SetUp() override {
1693#if !defined(__ANDROID__)
1694 GTEST_SKIP();
1695#endif
1696 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1697 BaseTouchIntegrationTest::SetUp();
1698 return;
1699 }
1700
1701 // setup policy with a input-port or UniqueId association to the display
1702 bool isInputPortAssociation =
1703 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1704
1705 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1706 if (isInputPortAssociation) {
1707 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1708 } else {
1709 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1710 }
Arpit Singh440bf652023-08-09 09:23:43 +00001711
1712 InputReaderIntegrationTest::setupInputReader();
1713
1714 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1715 INPUT_PORT);
1716 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1717
1718 // Add a display linked to a physical port or UniqueId.
1719 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1720 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1721 ViewportType::INTERNAL);
1722 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001723 const auto info = waitForDevice(mDevice->getName());
Arpit Singh440bf652023-08-09 09:23:43 +00001724 ASSERT_TRUE(info);
1725 mDeviceInfo = *info;
1726 }
1727};
1728
1729TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001730 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1731 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1732 // presses).
1733 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1734 mDeviceInfo.getSources());
1735}
1736
Arpit Singh440bf652023-08-09 09:23:43 +00001737TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001738 NotifyMotionArgs args;
1739 const Point centerPoint = mDevice->getCenterPoint();
1740
1741 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001742 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001743 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001744 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001745 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1746 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1747
1748 // ACTION_MOVE
1749 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001750 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001751 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1752 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1753
1754 // ACTION_UP
1755 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001756 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001757 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1758 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1759}
1760
Arpit Singh440bf652023-08-09 09:23:43 +00001761TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001762 NotifyMotionArgs args;
1763 const Point centerPoint = mDevice->getCenterPoint();
1764
1765 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001766 mDevice->sendSlot(FIRST_SLOT);
1767 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001768 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001769 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001770 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1771 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1772
1773 // ACTION_POINTER_DOWN (Second slot)
1774 const Point secondPoint = centerPoint + Point(100, 100);
1775 mDevice->sendSlot(SECOND_SLOT);
1776 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001777 mDevice->sendDown(secondPoint);
1778 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001779 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001780 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001781
1782 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001783 mDevice->sendMove(secondPoint + Point(1, 1));
1784 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001785 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1787
1788 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001789 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001790 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001791 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001792 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001793
1794 // ACTION_UP
1795 mDevice->sendSlot(FIRST_SLOT);
1796 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001797 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001798 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1799 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1800}
1801
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001802/**
1803 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1804 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1805 * data?
1806 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1807 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1808 * for Pointer 0 only is generated after.
1809 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1810 * events, we will not miss any information.
1811 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1812 * event generated afterwards that contains the newest movement of pointer 0.
1813 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1814 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1815 * losing information about non-palm pointers.
1816 */
Arpit Singh440bf652023-08-09 09:23:43 +00001817TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001818 NotifyMotionArgs args;
1819 const Point centerPoint = mDevice->getCenterPoint();
1820
1821 // ACTION_DOWN
1822 mDevice->sendSlot(FIRST_SLOT);
1823 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1824 mDevice->sendDown(centerPoint);
1825 mDevice->sendSync();
1826 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1827
1828 // ACTION_POINTER_DOWN (Second slot)
1829 const Point secondPoint = centerPoint + Point(100, 100);
1830 mDevice->sendSlot(SECOND_SLOT);
1831 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1832 mDevice->sendDown(secondPoint);
1833 mDevice->sendSync();
1834 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1835
1836 // ACTION_MOVE (First slot)
1837 mDevice->sendSlot(FIRST_SLOT);
1838 mDevice->sendMove(centerPoint + Point(5, 5));
1839 // ACTION_POINTER_UP (Second slot)
1840 mDevice->sendSlot(SECOND_SLOT);
1841 mDevice->sendPointerUp();
1842 // Send a single sync for the above 2 pointer updates
1843 mDevice->sendSync();
1844
1845 // First, we should get POINTER_UP for the second pointer
1846 assertReceivedMotion(ACTION_POINTER_1_UP,
1847 {/*first pointer */ centerPoint + Point(5, 5),
1848 /*second pointer*/ secondPoint});
1849
1850 // Next, the MOVE event for the first pointer
1851 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1852}
1853
1854/**
1855 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1856 * move, and then it will go up, all in the same frame.
1857 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1858 * gets sent to the listener.
1859 */
Arpit Singh440bf652023-08-09 09:23:43 +00001860TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001861 NotifyMotionArgs args;
1862 const Point centerPoint = mDevice->getCenterPoint();
1863
1864 // ACTION_DOWN
1865 mDevice->sendSlot(FIRST_SLOT);
1866 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1867 mDevice->sendDown(centerPoint);
1868 mDevice->sendSync();
1869 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1870
1871 // ACTION_POINTER_DOWN (Second slot)
1872 const Point secondPoint = centerPoint + Point(100, 100);
1873 mDevice->sendSlot(SECOND_SLOT);
1874 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1875 mDevice->sendDown(secondPoint);
1876 mDevice->sendSync();
1877 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1878
1879 // ACTION_MOVE (First slot)
1880 mDevice->sendSlot(FIRST_SLOT);
1881 mDevice->sendMove(centerPoint + Point(5, 5));
1882 // ACTION_POINTER_UP (Second slot)
1883 mDevice->sendSlot(SECOND_SLOT);
1884 mDevice->sendMove(secondPoint + Point(6, 6));
1885 mDevice->sendPointerUp();
1886 // Send a single sync for the above 2 pointer updates
1887 mDevice->sendSync();
1888
1889 // First, we should get POINTER_UP for the second pointer
1890 // The movement of the second pointer during the liftoff frame is ignored.
1891 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1892 assertReceivedMotion(ACTION_POINTER_1_UP,
1893 {/*first pointer */ centerPoint + Point(5, 5),
1894 /*second pointer*/ secondPoint});
1895
1896 // Next, the MOVE event for the first pointer
1897 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1898}
1899
Arpit Singh440bf652023-08-09 09:23:43 +00001900TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001901 NotifyMotionArgs args;
1902 const Point centerPoint = mDevice->getCenterPoint();
1903
1904 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001905 mDevice->sendSlot(FIRST_SLOT);
1906 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001907 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001908 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001909 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1910 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1911
arthurhungcc7f9802020-04-30 17:55:40 +08001912 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001913 const Point secondPoint = centerPoint + Point(100, 100);
1914 mDevice->sendSlot(SECOND_SLOT);
1915 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1916 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001917 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001918 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001919 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001920
arthurhungcc7f9802020-04-30 17:55:40 +08001921 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001922 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001923 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001924 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1926
arthurhungcc7f9802020-04-30 17:55:40 +08001927 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1928 // a palm event.
1929 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001930 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001931 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001932 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001933 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001934 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001935
arthurhungcc7f9802020-04-30 17:55:40 +08001936 // Send up to second slot, expect first slot send moving.
1937 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001938 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001939 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001941
arthurhungcc7f9802020-04-30 17:55:40 +08001942 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001943 mDevice->sendSlot(FIRST_SLOT);
1944 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001945 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001946
arthurhungcc7f9802020-04-30 17:55:40 +08001947 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1948 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001949}
1950
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001951/**
1952 * Some drivers historically have reported axis values outside of the range specified in the
1953 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1954 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1955 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1956 * and no units for pressure (resolution) is specified by the evdev documentation.
1957 */
1958TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1959 const Point centerPoint = mDevice->getCenterPoint();
1960
1961 // Down with pressure outside the reported range
1962 mDevice->sendSlot(FIRST_SLOT);
1963 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1964 mDevice->sendDown(centerPoint);
1965 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1966 mDevice->sendSync();
1967 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1968 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1969
1970 // Move to a point outside the reported range
1971 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1972 mDevice->sendSync();
1973 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1974 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1975
1976 // Up
1977 mDevice->sendUp();
1978 mDevice->sendSync();
1979 ASSERT_NO_FATAL_FAILURE(
1980 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1981}
1982
Arpit Singh440bf652023-08-09 09:23:43 +00001983TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001984 const Point centerPoint = mDevice->getCenterPoint();
1985
1986 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1987 mDevice->sendSlot(FIRST_SLOT);
1988 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1989 mDevice->sendToolType(MT_TOOL_PEN);
1990 mDevice->sendDown(centerPoint);
1991 mDevice->sendSync();
1992 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1993 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001994 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001995
1996 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1997
1998 // Release the stylus touch.
1999 mDevice->sendUp();
2000 mDevice->sendSync();
2001 ASSERT_NO_FATAL_FAILURE(
2002 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2003
2004 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2005
2006 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2007 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2008 mDevice->sendToolType(MT_TOOL_FINGER);
2009 mDevice->sendDown(centerPoint);
2010 mDevice->sendSync();
2011 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2012 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002013 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00002014
2015 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2016
2017 mDevice->sendUp();
2018 mDevice->sendSync();
2019 ASSERT_NO_FATAL_FAILURE(
2020 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2021
2022 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2023 // The policy should be notified of the stylus presence.
2024 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2025 mDevice->sendToolType(MT_TOOL_PEN);
2026 mDevice->sendMove(centerPoint);
2027 mDevice->sendSync();
2028 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2029 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002030 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00002031
2032 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2033}
2034
Arpit Singh440bf652023-08-09 09:23:43 +00002035TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002036 const Point centerPoint = mDevice->getCenterPoint();
2037
2038 // Down
2039 mDevice->sendSlot(FIRST_SLOT);
2040 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2041 mDevice->sendDown(centerPoint);
2042 mDevice->sendSync();
2043 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2044 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
2045
2046 // Move
2047 mDevice->sendMove(centerPoint + Point(1, 1));
2048 mDevice->sendSync();
2049 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2050 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2051
2052 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2053 auto externalStylus = createUinputDevice<UinputExternalStylus>();
2054 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002055 const auto stylusInfo = waitForDevice(externalStylus->getName());
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002056 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002057
2058 // Move
2059 mDevice->sendMove(centerPoint + Point(2, 2));
2060 mDevice->sendSync();
2061 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2062 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2063
2064 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2065 externalStylus.reset();
2066 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002067 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2068
2069 // Up
2070 mDevice->sendUp();
2071 mDevice->sendSync();
2072 ASSERT_NO_FATAL_FAILURE(
2073 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2074
2075 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2076}
2077
Arpit Singh440bf652023-08-09 09:23:43 +00002078INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
2079 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
2080 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
2081 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
2082
Prabir Pradhan124ea442022-10-28 20:27:44 +00002083// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002084
Prabir Pradhan124ea442022-10-28 20:27:44 +00002085// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2086// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2087// stylus.
2088template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00002089class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002090protected:
2091 void SetUp() override {
2092#if !defined(__ANDROID__)
2093 GTEST_SKIP();
2094#endif
Arpit Singh440bf652023-08-09 09:23:43 +00002095 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00002096 mTouchscreen = mDevice.get();
2097 mTouchscreenInfo = mDeviceInfo;
2098
2099 setUpStylusDevice();
2100 }
2101
2102 UinputStylusDevice* mStylus{nullptr};
2103 InputDeviceInfo mStylusInfo{};
2104
2105 UinputTouchScreen* mTouchscreen{nullptr};
2106 InputDeviceInfo mTouchscreenInfo{};
2107
2108private:
2109 // When we are attempting to test stylus button events that are sent from the touchscreen,
2110 // use the same Uinput device for the touchscreen and the stylus.
2111 template <typename T = UinputStylusDevice>
2112 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2113 mStylus = mDevice.get();
2114 mStylusInfo = mDeviceInfo;
2115 }
2116
2117 // When we are attempting to stylus buttons from an external stylus being merged with touches
2118 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2119 template <typename T = UinputStylusDevice>
2120 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2121 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2122 mStylus = mStylusDeviceLifecycleTracker.get();
2123 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002124 const auto info = waitForDevice(mStylus->getName());
Prabir Pradhan124ea442022-10-28 20:27:44 +00002125 ASSERT_TRUE(info);
2126 mStylusInfo = *info;
2127 }
2128
2129 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2130
2131 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002132 using BaseTouchIntegrationTest::mDevice;
2133 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002134};
2135
2136using StylusButtonIntegrationTestTypes =
2137 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2138TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2139
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002140TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002141 const auto stylusId = TestFixture::mStylusInfo.getId();
2142
2143 TestFixture::mStylus->pressKey(BTN_STYLUS);
2144 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2145 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2146 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2147
2148 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2149 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002150 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002151 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002152}
2153
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002154TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002155 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2156 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2157 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002158
2159 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002160 TestFixture::mStylus->pressKey(BTN_STYLUS);
2161 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002162 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002163 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002164
2165 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002166 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2167 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2168 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2169 TestFixture::mTouchscreen->sendDown(centerPoint);
2170 TestFixture::mTouchscreen->sendSync();
2171 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002172 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002173 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002174 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2175 WithDeviceId(touchscreenId))));
2176 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002177 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002178 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002179 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2180 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002181
Prabir Pradhan124ea442022-10-28 20:27:44 +00002182 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2183 TestFixture::mTouchscreen->sendSync();
2184 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002185 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002186 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002187 WithDeviceId(touchscreenId))));
2188 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002189 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002190 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002191 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002192
2193 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002194 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2195 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002196 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002197 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002198}
2199
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002200TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002201 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2202 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2203 const auto stylusId = TestFixture::mStylusInfo.getId();
2204 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002205 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002206
2207 // Press the stylus button.
2208 TestFixture::mStylus->pressKey(BTN_STYLUS);
2209 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2210 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2211 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2212
2213 // Start hovering with the stylus.
2214 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2215 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2216 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2217 TestFixture::mTouchscreen->sendMove(centerPoint);
2218 TestFixture::mTouchscreen->sendSync();
2219 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2220 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2221 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2222 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2223 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2224 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2225 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2226 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2227 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2228
2229 // Touch down with the stylus.
2230 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2231 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2232 TestFixture::mTouchscreen->sendDown(centerPoint);
2233 TestFixture::mTouchscreen->sendSync();
2234 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2235 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2236 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2237
2238 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2239 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2240 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2241
2242 // Stop touching with the stylus, and start hovering.
2243 TestFixture::mTouchscreen->sendUp();
2244 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2245 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2246 TestFixture::mTouchscreen->sendMove(centerPoint);
2247 TestFixture::mTouchscreen->sendSync();
2248 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2249 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2250 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2251 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2252 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2253 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2254 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2255 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2256 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2257
2258 // Stop hovering.
2259 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2260 TestFixture::mTouchscreen->sendSync();
2261 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2262 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2263 WithButtonState(0))));
2264 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2265 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2266 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2267 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2268
2269 // Release the stylus button.
2270 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2271 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2272 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2273 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2274}
2275
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002276TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002277 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2278 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2279 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002280
2281 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002282 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2283 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2284 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2285 TestFixture::mTouchscreen->sendDown(centerPoint);
2286 TestFixture::mTouchscreen->sendSync();
2287 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002288 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002289 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002290 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002291
2292 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002293 TestFixture::mStylus->pressKey(BTN_STYLUS);
2294 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002295 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002296 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2297 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002298 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002299 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002300 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2301 WithDeviceId(touchscreenId))));
2302 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002303 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002304 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002305 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2306 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002307
Prabir Pradhan124ea442022-10-28 20:27:44 +00002308 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2309 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002310 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002311 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2312 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002313 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002314 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002315 WithDeviceId(touchscreenId))));
2316 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002317 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002318 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002319 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002320
2321 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002322 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2323 TestFixture::mTouchscreen->sendSync();
2324 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002325 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002326 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002327 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002328}
2329
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002330TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002331 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2332 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002333 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002334
2335 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2336 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2337 const auto stylusId = TestFixture::mStylusInfo.getId();
2338
2339 // Start a stylus gesture. By the time this event is processed, the configuration change that
2340 // was requested is guaranteed to be completed.
2341 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2342 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2343 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2344 TestFixture::mTouchscreen->sendDown(centerPoint);
2345 TestFixture::mTouchscreen->sendSync();
2346 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2347 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002348 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002349 WithDeviceId(touchscreenId))));
2350
2351 // Press and release a stylus button. Each change only generates a MOVE motion event.
2352 // Key events are unaffected.
2353 TestFixture::mStylus->pressKey(BTN_STYLUS);
2354 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2355 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2356 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2357 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2358 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002359 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002360 WithDeviceId(touchscreenId))));
2361
2362 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2363 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2364 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2365 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2366 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2367 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002368 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002369 WithDeviceId(touchscreenId))));
2370
2371 // Finish the stylus gesture.
2372 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2373 TestFixture::mTouchscreen->sendSync();
2374 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2375 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002376 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002377 WithDeviceId(touchscreenId))));
2378}
2379
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002380// --- ExternalStylusIntegrationTest ---
2381
2382// Verify the behavior of an external stylus. An external stylus can report pressure or button
2383// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2384// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002385using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002386
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002387TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreenSource) {
2388 // Create an external stylus capable of reporting pressure data that
2389 // should be fused with a touch pointer.
2390 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2391 createUinputDevice<UinputExternalStylusWithPressure>();
2392 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002393 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002394 ASSERT_TRUE(stylusInfo);
2395
2396 // Connecting an external stylus changes the source of the touchscreen.
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002397 const auto deviceInfo = waitForDevice(mDevice->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002398 ASSERT_TRUE(deviceInfo);
2399 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
2400}
2401
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002402TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002403 const Point centerPoint = mDevice->getCenterPoint();
2404
2405 // Create an external stylus capable of reporting pressure data that
2406 // should be fused with a touch pointer.
2407 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2408 createUinputDevice<UinputExternalStylusWithPressure>();
2409 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002410 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002411 ASSERT_TRUE(stylusInfo);
2412
2413 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2414
2415 const auto touchscreenId = mDeviceInfo.getId();
2416
2417 // Set a pressure value on the stylus. It doesn't generate any events.
2418 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2419 stylus->setPressure(100);
2420 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2421
2422 // Start a finger gesture, and ensure it shows up as stylus gesture
2423 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002424 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002425 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002426 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002427 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002428 mDevice->sendSync();
2429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002430 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithToolType(ToolType::STYLUS),
2431 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2432 WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002433
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002434 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2435 // event with the updated pressure.
2436 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002437 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002438 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithToolType(ToolType::STYLUS),
2439 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2440 WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002441
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002442 // The external stylus did not generate any events.
2443 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2444 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2445}
2446
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002447TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002448 const Point centerPoint = mDevice->getCenterPoint();
2449
2450 // Create an external stylus capable of reporting pressure data that
2451 // should be fused with a touch pointer.
2452 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2453 createUinputDevice<UinputExternalStylusWithPressure>();
2454 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002455 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002456 ASSERT_TRUE(stylusInfo);
2457
2458 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2459
2460 const auto touchscreenId = mDeviceInfo.getId();
2461
2462 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2463 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002464 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2465 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002466 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002468
2469 // Start a finger gesture. The touch device will withhold generating any touches for
2470 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2471 mDevice->sendSlot(FIRST_SLOT);
2472 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2473 mDevice->sendToolType(MT_TOOL_FINGER);
2474 mDevice->sendDown(centerPoint);
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002475 const auto syncTime = std::chrono::system_clock::now();
2476 // After 72 ms, the event *will* be generated. If we wait the full 72 ms to check that NO event
2477 // is generated in that period, there will be a race condition between the event being generated
2478 // and the test's wait timeout expiring. Thus, we wait for a shorter duration in the test, which
2479 // will reduce the liklihood of the race condition occurring.
2480 const auto waitUntilTimeForNoEvent =
2481 syncTime + std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT / 2));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002482 mDevice->sendSync();
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002483 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntilTimeForNoEvent));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002484
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002485 // Since the external stylus did not report a pressure value within the timeout,
2486 // it shows up as a finger pointer.
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002487 const auto waitUntilTimeForEvent = syncTime +
2488 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT)) + EVENT_HAPPENED_TIMEOUT;
2489 ASSERT_NO_FATAL_FAILURE(
2490 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2491 AMOTION_EVENT_ACTION_DOWN),
2492 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2493 AINPUT_SOURCE_STYLUS),
2494 WithToolType(ToolType::FINGER),
2495 WithDeviceId(touchscreenId),
2496 WithPressure(1.f)),
2497 waitUntilTimeForEvent));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002498
2499 // Change the pressure on the external stylus. Since the pressure was not present at the start
2500 // of the gesture, it is ignored for now.
2501 stylus->setPressure(200);
2502 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2503
2504 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002505 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2506 mDevice->sendSync();
2507 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2508 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002509 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002510 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002511
2512 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2513 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2514 mDevice->sendToolType(MT_TOOL_FINGER);
2515 mDevice->sendDown(centerPoint);
2516 mDevice->sendSync();
2517 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002518 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithSource(STYLUS_FUSION_SOURCE),
2519 WithToolType(ToolType::STYLUS), WithButtonState(0), WithDeviceId(touchscreenId),
2520 WithPressure(200.f / RAW_PRESSURE_MAX))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002521
2522 // The external stylus did not generate any events.
2523 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2524 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002525}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002526
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002527TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002528 const Point centerPoint = mDevice->getCenterPoint();
2529
2530 // Create an external stylus device that does not support pressure. It should not affect any
2531 // touch pointers.
2532 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2533 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002534 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002535 ASSERT_TRUE(stylusInfo);
2536
2537 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2538
2539 const auto touchscreenId = mDeviceInfo.getId();
2540
2541 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2542 // pressure data from the external stylus.
2543 mDevice->sendSlot(FIRST_SLOT);
2544 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2545 mDevice->sendToolType(MT_TOOL_FINGER);
2546 mDevice->sendDown(centerPoint);
2547 auto waitUntil = std::chrono::system_clock::now() +
2548 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2549 mDevice->sendSync();
2550 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002551 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2552 AMOTION_EVENT_ACTION_DOWN),
2553 WithToolType(ToolType::FINGER),
2554 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2555 AINPUT_SOURCE_STYLUS),
2556 WithButtonState(0),
2557 WithDeviceId(touchscreenId),
2558 WithPressure(1.f)),
2559 waitUntil));
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002560
2561 // The external stylus did not generate any events.
2562 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2563 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2564}
2565
Michael Wrightd02c5b62014-02-10 15:10:22 -08002566// --- InputDeviceTest ---
2567class InputDeviceTest : public testing::Test {
2568protected:
2569 static const char* DEVICE_NAME;
2570 static const char* DEVICE_LOCATION;
2571 static const int32_t DEVICE_ID;
2572 static const int32_t DEVICE_GENERATION;
2573 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002574 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002575 static const int32_t EVENTHUB_ID;
2576 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2577
2578 std::shared_ptr<FakeEventHub> mFakeEventHub;
2579 sp<FakeInputReaderPolicy> mFakePolicy;
2580 std::unique_ptr<TestInputListener> mFakeListener;
2581 std::unique_ptr<InstrumentedInputReader> mReader;
2582 std::shared_ptr<InputDevice> mDevice;
2583
2584 void SetUp() override {
2585 mFakeEventHub = std::make_unique<FakeEventHub>();
2586 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2587 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002588 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002589 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002590 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002591 identifier.name = DEVICE_NAME;
2592 identifier.location = DEVICE_LOCATION;
2593 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2594 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2595 identifier);
2596 mReader->pushNextDevice(mDevice);
2597 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002598 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002599 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002600
2601 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002602 mFakeListener.reset();
2603 mFakePolicy.clear();
2604 }
2605};
2606
2607const char* InputDeviceTest::DEVICE_NAME = "device";
2608const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2609const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2610const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002611const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002612const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2613 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002614const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002615const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2616
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002617TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002618 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002619 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2620 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002621}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002622
Michael Wrightd02c5b62014-02-10 15:10:22 -08002623TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2624 ASSERT_EQ(mDevice->isEnabled(), false);
2625}
2626
2627TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2628 // Configuration.
2629 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002630 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002631
2632 // Reset.
2633 unused += mDevice->reset(ARBITRARY_TIME);
2634
2635 NotifyDeviceResetArgs resetArgs;
2636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2637 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2638 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2639
2640 // Metadata.
2641 ASSERT_TRUE(mDevice->isIgnored());
2642 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2643
2644 InputDeviceInfo info = mDevice->getDeviceInfo();
2645 ASSERT_EQ(DEVICE_ID, info.getId());
2646 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2647 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2648 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2649
2650 // State queries.
2651 ASSERT_EQ(0, mDevice->getMetaState());
2652
2653 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2654 << "Ignored device should return unknown key code state.";
2655 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2656 << "Ignored device should return unknown scan code state.";
2657 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2658 << "Ignored device should return unknown switch state.";
2659
2660 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2661 uint8_t flags[2] = { 0, 1 };
2662 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2663 << "Ignored device should never mark any key codes.";
2664 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2665 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2666}
2667
2668TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2669 // Configuration.
2670 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2671
2672 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002673 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2674 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002675 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2676 mapper1.setMetaState(AMETA_ALT_ON);
2677 mapper1.addSupportedKeyCode(AKEYCODE_A);
2678 mapper1.addSupportedKeyCode(AKEYCODE_B);
2679 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2680 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2681 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2682 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2683 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2684
2685 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002686 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2687 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002688 mapper2.setMetaState(AMETA_SHIFT_ON);
2689
2690 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002691 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002692
Harry Cuttsf13161a2023-03-08 14:15:49 +00002693 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2694 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002696 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002698 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2699 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700
2701 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002702 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002703 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2704 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705
2706 NotifyDeviceResetArgs resetArgs;
2707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2708 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2709 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2710
2711 // Metadata.
2712 ASSERT_FALSE(mDevice->isIgnored());
2713 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2714
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002715 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002716 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002717 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002718 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2719 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2720
2721 // State queries.
2722 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2723 << "Should query mappers and combine meta states.";
2724
2725 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2726 << "Should return unknown key code state when source not supported.";
2727 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2728 << "Should return unknown scan code state when source not supported.";
2729 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2730 << "Should return unknown switch state when source not supported.";
2731
2732 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2733 << "Should query mapper when source is supported.";
2734 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2735 << "Should query mapper when source is supported.";
2736 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2737 << "Should query mapper when source is supported.";
2738
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002739 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002740 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002741 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002742 << "Should do nothing when source is unsupported.";
2743 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2744 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2745 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2746 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2747
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002748 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002749 << "Should query mapper when source is supported.";
2750 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2751 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2752 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2753 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2754
2755 // Event handling.
2756 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002757 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002758 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002760 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2761 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002762}
2763
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -08002764TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorNotSet) {
2765 // Set some behavior to force the configuration to be update.
2766 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2767 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2768 AINPUT_SOURCE_KEYBOARD);
2769
2770 std::list<NotifyArgs> unused =
2771 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2772 /*changes=*/{});
2773
2774 ASSERT_FALSE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.has_value());
2775}
2776
2777TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorEnabled) {
2778 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.viewBehavior_smoothScroll", "1");
2779 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2780 AINPUT_SOURCE_KEYBOARD);
2781
2782 std::list<NotifyArgs> unused =
2783 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2784 /*changes=*/{});
2785
2786 ASSERT_TRUE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.value_or(false));
2787}
2788
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002789TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2790 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2791 FakeInputMapper& mapper =
2792 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2793 AINPUT_SOURCE_KEYBOARD);
2794 NotifyMotionArgs args1;
2795 NotifySwitchArgs args2;
2796 NotifyKeyArgs args3;
2797 mapper.setProcessResult({args1, args2, args3});
2798
2799 InputReaderConfiguration config;
2800 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2801
2802 RawEvent event;
2803 event.deviceId = EVENTHUB_ID;
2804 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2805
2806 for (auto& arg : notifyArgs) {
2807 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2808 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2809 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2810 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2811 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2812 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2813 }
2814 }
2815}
2816
2817TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2818 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2819 FakeInputMapper& mapper =
2820 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2821 AINPUT_SOURCE_KEYBOARD);
2822 NotifyMotionArgs args;
2823 mapper.setProcessResult({args});
2824
2825 InputReaderConfiguration config;
2826 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2827
2828 RawEvent event;
2829 event.deviceId = EVENTHUB_ID;
2830 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2831
2832 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2833 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2834}
2835
2836TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2837 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2838 FakeInputMapper& mapper =
2839 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2840 AINPUT_SOURCE_KEYBOARD);
2841 NotifyMotionArgs args;
2842 args.policyFlags = POLICY_FLAG_WAKE;
2843 mapper.setProcessResult({args});
2844
2845 InputReaderConfiguration config;
2846 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2847
2848 RawEvent event;
2849 event.deviceId = EVENTHUB_ID;
2850 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2851
2852 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2853 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2854}
2855
Arthur Hung2c9a3342019-07-23 14:18:59 +08002856// A single input device is associated with a specific display. Check that:
2857// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002858// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002859TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002860 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2861 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002862
2863 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002864 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002865 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2866 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002867
2868 // Device should be enabled by default.
2869 ASSERT_TRUE(mDevice->isEnabled());
2870
2871 // Prepare associated info.
2872 constexpr uint8_t hdmi = 1;
2873 const std::string UNIQUE_ID = "local:1";
2874
2875 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002876 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002877 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002878 // Device should be disabled because it is associated with a specific display via
2879 // input port <-> display port association, but the corresponding display is not found
2880 ASSERT_FALSE(mDevice->isEnabled());
2881
2882 // Prepare displays.
2883 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002884 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002885 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002886 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002887 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002888 ASSERT_TRUE(mDevice->isEnabled());
2889
2890 // Device should be disabled after set disable.
2891 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002892 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002893 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002894 ASSERT_FALSE(mDevice->isEnabled());
2895
2896 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002897 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002898 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002899 ASSERT_FALSE(mDevice->isEnabled());
2900}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002901
Christine Franks1ba71cc2021-04-07 14:37:42 -07002902TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2903 // Device should be enabled by default.
2904 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002905 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2906 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002907 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002908 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2909 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002910 ASSERT_TRUE(mDevice->isEnabled());
2911
2912 // Device should be disabled because it is associated with a specific display, but the
2913 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002914 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002915 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002916 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002917 ASSERT_FALSE(mDevice->isEnabled());
2918
2919 // Device should be enabled when a display is found.
2920 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002921 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002922 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002923 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002924 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002925 ASSERT_TRUE(mDevice->isEnabled());
2926
2927 // Device should be disabled after set disable.
2928 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002929 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002930 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002931 ASSERT_FALSE(mDevice->isEnabled());
2932
2933 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002934 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002935 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002936 ASSERT_FALSE(mDevice->isEnabled());
2937}
2938
Christine Franks2a2293c2022-01-18 11:51:16 -08002939TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2940 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002941 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2942 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002943 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002944 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2945 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002946
Christine Franks2a2293c2022-01-18 11:51:16 -08002947 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2948 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002949 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002950 NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002951 const auto initialGeneration = mDevice->getGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002952 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002953 InputReaderConfiguration::Change::DISPLAY_INFO);
Antonio Kantek0ac5e092024-04-22 17:10:27 +00002954 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueIdByPort());
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002955 ASSERT_GT(mDevice->getGeneration(), initialGeneration);
2956 ASSERT_EQ(mDevice->getDeviceInfo().getAssociatedDisplayId(), SECONDARY_DISPLAY_ID);
Christine Franks2a2293c2022-01-18 11:51:16 -08002957}
2958
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002959/**
2960 * This test reproduces a crash caused by a dangling reference that remains after device is added
2961 * and removed. The reference is accessed in InputDevice::dump(..);
2962 */
2963TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2964 constexpr int32_t TEST_EVENTHUB_ID = 10;
2965 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2966
Harry Cutts33476232023-01-30 19:57:29 +00002967 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh82f29a12023-06-13 15:05:53 +00002968 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2969 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002970 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2971 std::string dumpStr, eventHubDevStr;
2972 device.dump(dumpStr, eventHubDevStr);
2973}
2974
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002975TEST_F(InputDeviceTest, GetBluetoothAddress) {
2976 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2977 ASSERT_TRUE(address);
2978 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2979}
2980
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002981TEST_F(InputDeviceTest, KernelBufferOverflowResetsMappers) {
2982 mFakePolicy->clearViewports();
2983 FakeInputMapper& mapper =
2984 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2985 AINPUT_SOURCE_KEYBOARD);
2986 std::list<NotifyArgs> unused =
2987 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2988 /*changes=*/{});
2989
2990 mapper.assertConfigureWasCalled();
2991 mapper.assertResetWasNotCalled();
2992
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002993 RawEvent event{.when = ARBITRARY_TIME,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002994 .readTime = ARBITRARY_TIME,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002995 .deviceId = EVENTHUB_ID,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002996 .type = EV_SYN,
2997 .code = SYN_REPORT,
2998 .value = 0};
2999
3000 // Events are processed normally.
3001 unused = mDevice->process(&event, /*count=*/1);
3002 mapper.assertProcessWasCalled();
3003
3004 // Simulate a kernel buffer overflow, which generates a SYN_DROPPED event.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003005 event.type = EV_SYN;
3006 event.code = SYN_DROPPED;
3007 event.value = 0;
3008 unused = mDevice->process(&event, /*count=*/1);
3009 mapper.assertProcessWasNotCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003010
3011 // All events until the next SYN_REPORT should be dropped.
3012 event.type = EV_KEY;
3013 event.code = KEY_A;
3014 event.value = 1;
3015 unused = mDevice->process(&event, /*count=*/1);
3016 mapper.assertProcessWasNotCalled();
3017
3018 // We get the SYN_REPORT event now, which is not forwarded to mappers.
Arpit Singh4b4a4572023-11-24 18:19:56 +00003019 // This should reset the mapper.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003020 event.type = EV_SYN;
3021 event.code = SYN_REPORT;
3022 event.value = 0;
3023 unused = mDevice->process(&event, /*count=*/1);
3024 mapper.assertProcessWasNotCalled();
Arpit Singh4b4a4572023-11-24 18:19:56 +00003025 mapper.assertResetWasCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003026
3027 // The mapper receives events normally now.
3028 event.type = EV_KEY;
3029 event.code = KEY_B;
3030 event.value = 1;
3031 unused = mDevice->process(&event, /*count=*/1);
3032 mapper.assertProcessWasCalled();
3033}
3034
Chris Yef59a2f42020-10-16 12:55:26 -07003035// --- SensorInputMapperTest ---
3036
3037class SensorInputMapperTest : public InputMapperTest {
3038protected:
3039 static const int32_t ACCEL_RAW_MIN;
3040 static const int32_t ACCEL_RAW_MAX;
3041 static const int32_t ACCEL_RAW_FUZZ;
3042 static const int32_t ACCEL_RAW_FLAT;
3043 static const int32_t ACCEL_RAW_RESOLUTION;
3044
3045 static const int32_t GYRO_RAW_MIN;
3046 static const int32_t GYRO_RAW_MAX;
3047 static const int32_t GYRO_RAW_FUZZ;
3048 static const int32_t GYRO_RAW_FLAT;
3049 static const int32_t GYRO_RAW_RESOLUTION;
3050
3051 static const float GRAVITY_MS2_UNIT;
3052 static const float DEGREE_RADIAN_UNIT;
3053
3054 void prepareAccelAxes();
3055 void prepareGyroAxes();
3056 void setAccelProperties();
3057 void setGyroProperties();
3058 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3059};
3060
3061const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3062const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3063const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3064const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3065const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3066
3067const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3068const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3069const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3070const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3071const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3072
3073const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3074const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3075
3076void SensorInputMapperTest::prepareAccelAxes() {
3077 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3078 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3079 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3080 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3081 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3082 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3083}
3084
3085void SensorInputMapperTest::prepareGyroAxes() {
3086 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3087 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3088 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3089 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3090 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3091 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3092}
3093
3094void SensorInputMapperTest::setAccelProperties() {
3095 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3096 /* sensorDataIndex */ 0);
3097 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3098 /* sensorDataIndex */ 1);
3099 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3100 /* sensorDataIndex */ 2);
3101 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3102 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3103 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3104 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3105 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3106}
3107
3108void SensorInputMapperTest::setGyroProperties() {
3109 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3110 /* sensorDataIndex */ 0);
3111 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3112 /* sensorDataIndex */ 1);
3113 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3114 /* sensorDataIndex */ 2);
3115 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3116 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3117 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3118 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3119 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3120}
3121
3122TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003123 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003124
3125 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3126}
3127
3128TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3129 setAccelProperties();
3130 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003131 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003132
3133 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3134 std::chrono::microseconds(10000),
3135 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003136 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003142
3143 NotifySensorArgs args;
3144 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3145 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3146 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3147
3148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3149 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3150 ASSERT_EQ(args.deviceId, DEVICE_ID);
3151 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3152 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3153 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3154 ASSERT_EQ(args.values, values);
3155 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3156}
3157
3158TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3159 setGyroProperties();
3160 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003161 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003162
3163 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3164 std::chrono::microseconds(10000),
3165 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003166 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003172
3173 NotifySensorArgs args;
3174 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3175 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3176 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3177
3178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3179 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3180 ASSERT_EQ(args.deviceId, DEVICE_ID);
3181 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3182 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3183 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3184 ASSERT_EQ(args.values, values);
3185 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3186}
3187
Michael Wrightd02c5b62014-02-10 15:10:22 -08003188// --- KeyboardInputMapperTest ---
3189
3190class KeyboardInputMapperTest : public InputMapperTest {
3191protected:
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003192 void SetUp() override {
3193 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::KEYBOARD |
3194 InputDeviceClass::ALPHAKEY);
3195 }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003196 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003197 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003198 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003199
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003200 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003201 int32_t originalKeyCode, int32_t rotatedKeyCode,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003202 ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203};
3204
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003205/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3206 * orientation.
3207 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003208void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003209 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3210 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003211}
3212
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003213void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003214 int32_t originalScanCode, int32_t originalKeyCode,
Linnan Li13bf76a2024-05-05 19:18:02 +08003215 int32_t rotatedKeyCode,
3216 ui::LogicalDisplayId displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003217 NotifyKeyArgs args;
3218
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3221 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3222 ASSERT_EQ(originalScanCode, args.scanCode);
3223 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003224 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3228 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3229 ASSERT_EQ(originalScanCode, args.scanCode);
3230 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003231 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003232}
3233
Michael Wrightd02c5b62014-02-10 15:10:22 -08003234TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003235 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003236 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003238 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003239}
3240
3241TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3242 const int32_t USAGE_A = 0x070004;
3243 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003244 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3245 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003246 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3247 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3248 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003250 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003251 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003252 // Initial metastate is AMETA_NONE.
3253 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254
3255 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257 NotifyKeyArgs args;
3258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3259 ASSERT_EQ(DEVICE_ID, args.deviceId);
3260 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3261 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3262 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3263 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3264 ASSERT_EQ(KEY_HOME, args.scanCode);
3265 ASSERT_EQ(AMETA_NONE, args.metaState);
3266 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3267 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3268 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3269
3270 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003271 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3273 ASSERT_EQ(DEVICE_ID, args.deviceId);
3274 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3275 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3276 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3277 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3278 ASSERT_EQ(KEY_HOME, args.scanCode);
3279 ASSERT_EQ(AMETA_NONE, args.metaState);
3280 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3281 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3282 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3283
3284 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3286 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3288 ASSERT_EQ(DEVICE_ID, args.deviceId);
3289 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3290 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3291 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3292 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3293 ASSERT_EQ(0, args.scanCode);
3294 ASSERT_EQ(AMETA_NONE, args.metaState);
3295 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3296 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3297 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3298
3299 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3301 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3303 ASSERT_EQ(DEVICE_ID, args.deviceId);
3304 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3305 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3306 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3307 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3308 ASSERT_EQ(0, args.scanCode);
3309 ASSERT_EQ(AMETA_NONE, args.metaState);
3310 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3311 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3312 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3313
3314 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3318 ASSERT_EQ(DEVICE_ID, args.deviceId);
3319 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3320 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3321 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3322 ASSERT_EQ(0, args.keyCode);
3323 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3324 ASSERT_EQ(AMETA_NONE, args.metaState);
3325 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3326 ASSERT_EQ(0U, args.policyFlags);
3327 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3328
3329 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3331 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3333 ASSERT_EQ(DEVICE_ID, args.deviceId);
3334 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3335 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3336 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3337 ASSERT_EQ(0, args.keyCode);
3338 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3339 ASSERT_EQ(AMETA_NONE, args.metaState);
3340 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3341 ASSERT_EQ(0U, args.policyFlags);
3342 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3343}
3344
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003345TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3346 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3347 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003348
3349 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003350 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003351
Linnan Lie5657f22024-09-13 21:54:37 +08003352 mFakeEventHub->setKeyRemapping(EVENTHUB_ID, {{AKEYCODE_A, AKEYCODE_B}});
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003353 // Key down by scan code.
3354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3355 NotifyKeyArgs args;
3356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3357 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3358
3359 // Key up by scan code.
3360 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3362 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3363}
3364
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003365/**
3366 * Ensure that the readTime is set to the time when the EV_KEY is received.
3367 */
3368TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3369 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3370
3371 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003372 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003373 NotifyKeyArgs args;
3374
3375 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003376 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3378 ASSERT_EQ(12, args.readTime);
3379
3380 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003381 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3383 ASSERT_EQ(15, args.readTime);
3384}
3385
Michael Wrightd02c5b62014-02-10 15:10:22 -08003386TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003387 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3388 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003389 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3390 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3391 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003392
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003393 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003394 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003395
Arthur Hung95f68612022-04-07 14:08:22 +08003396 // Initial metastate is AMETA_NONE.
3397 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398
3399 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003401 NotifyKeyArgs args;
3402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3403 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003404 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003405 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003406
3407 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003408 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3410 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003411 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412
3413 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003414 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3416 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003417 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418
3419 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003420 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3422 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003423 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003424 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425}
3426
3427TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003428 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3429 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3430 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3431 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003432
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003433 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003434 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435
Michael Wrighta9cf4192022-12-01 23:46:39 +00003436 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3438 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3439 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3440 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3441 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3442 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3443 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3444 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3445}
3446
3447TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003448 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3449 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3450 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3451 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003452
Michael Wrightd02c5b62014-02-10 15:10:22 -08003453 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003454 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003455 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456
Michael Wrighta9cf4192022-12-01 23:46:39 +00003457 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003458 ASSERT_NO_FATAL_FAILURE(
3459 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3460 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3461 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3462 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3463 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3464 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3465 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003466
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003467 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003468 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003469 ASSERT_NO_FATAL_FAILURE(
3470 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3471 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3472 AKEYCODE_DPAD_UP, DISPLAY_ID));
3473 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3474 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3475 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3476 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003477
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003478 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003479 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003480 ASSERT_NO_FATAL_FAILURE(
3481 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3482 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3483 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3484 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3485 AKEYCODE_DPAD_UP, DISPLAY_ID));
3486 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3487 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003488
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003489 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003490 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003491 ASSERT_NO_FATAL_FAILURE(
3492 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3493 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3494 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3495 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3496 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3497 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3498 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499
3500 // Special case: if orientation changes while key is down, we still emit the same keycode
3501 // in the key up as we did in the key down.
3502 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003503 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003504 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3507 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3508 ASSERT_EQ(KEY_UP, args.scanCode);
3509 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3510
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003511 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003512 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003513 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3515 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3516 ASSERT_EQ(KEY_UP, args.scanCode);
3517 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3518}
3519
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003520TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3521 // If the keyboard is not orientation aware,
3522 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003523 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003524
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003525 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003526 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003527 NotifyKeyArgs args;
3528
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003529 // Display id should be LogicalDisplayId::INVALID without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003530 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003534 ASSERT_EQ(ui::LogicalDisplayId::INVALID, args.displayId);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003535
Michael Wrighta9cf4192022-12-01 23:46:39 +00003536 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003539 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003541 ASSERT_EQ(ui::LogicalDisplayId::INVALID, args.displayId);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003542}
3543
3544TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3545 // If the keyboard is orientation aware,
3546 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003547 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003548
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003549 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003550 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003551 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003552 NotifyKeyArgs args;
3553
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003554 // Display id should be LogicalDisplayId::INVALID without any display configuration.
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003555 // ^--- already checked by the previous test
3556
Michael Wrighta9cf4192022-12-01 23:46:39 +00003557 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003558 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003561 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3563 ASSERT_EQ(DISPLAY_ID, args.displayId);
3564
Linnan Li13bf76a2024-05-05 19:18:02 +08003565 constexpr ui::LogicalDisplayId newDisplayId = ui::LogicalDisplayId{2};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003566 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003567 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003568 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3573 ASSERT_EQ(newDisplayId, args.displayId);
3574}
3575
Michael Wrightd02c5b62014-02-10 15:10:22 -08003576TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003577 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003578 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003580 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003581 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003582
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003583 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003584 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003585}
3586
Philip Junker4af3b3d2021-12-14 10:36:55 +01003587TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3588 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003589 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Philip Junker4af3b3d2021-12-14 10:36:55 +01003590
3591 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3592 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3593 << "If a mapping is available, the result is equal to the mapping";
3594
3595 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3596 << "If no mapping is available, the result is the key location";
3597}
3598
Michael Wrightd02c5b62014-02-10 15:10:22 -08003599TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003600 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003601 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003603 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003604 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003605
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003606 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003607 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003608}
3609
3610TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003611 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003612 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003614 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003615
Michael Wrightd02c5b62014-02-10 15:10:22 -08003616 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003617 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003618 ASSERT_TRUE(flags[0]);
3619 ASSERT_FALSE(flags[1]);
3620}
3621
3622TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003623 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3624 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3625 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3626 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3627 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3628 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003630 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003631 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003632 // Initial metastate is AMETA_NONE.
3633 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003634
3635 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003636 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3637 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3638 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003639
3640 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003643 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3644 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3645 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003646 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003647
3648 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003651 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3652 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3653 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003654 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003655
3656 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003659 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3660 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3661 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003662 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003663
3664 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003667 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3668 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3669 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003670 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671
3672 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003675 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3676 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3677 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003678 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679
3680 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3682 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003683 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3684 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3685 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003686 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687}
3688
Arthur Hung2c9a3342019-07-23 14:18:59 +08003689TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3690 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003691 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3692 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3693 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3694 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003695
3696 // keyboard 2.
3697 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003698 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003699 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003700 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003701 std::shared_ptr<InputDevice> device2 =
3702 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003703 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003704
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003705 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3706 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3707 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3708 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003709
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003710 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003711 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003712
Arpit Singh67ca6842023-04-26 14:43:16 +00003713 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003714 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003715 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3716 mFakePolicy
3717 ->getReaderConfiguration(),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003718 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003719 std::list<NotifyArgs> unused =
3720 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003721 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003722 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003723
3724 // Prepared displays and associated info.
3725 constexpr uint8_t hdmi1 = 0;
3726 constexpr uint8_t hdmi2 = 1;
3727 const std::string SECONDARY_UNIQUE_ID = "local:1";
3728
3729 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3730 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3731
3732 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003733 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003734 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003735 ASSERT_FALSE(device2->isEnabled());
3736
3737 // Prepare second display.
Linnan Li13bf76a2024-05-05 19:18:02 +08003738 constexpr ui::LogicalDisplayId newDisplayId = ui::LogicalDisplayId{2};
Michael Wrighta9cf4192022-12-01 23:46:39 +00003739 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003740 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003741 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003742 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003743 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003744 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003745 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003746
3747 // Device should be enabled after the associated display is found.
3748 ASSERT_TRUE(mDevice->isEnabled());
3749 ASSERT_TRUE(device2->isEnabled());
3750
3751 // Test pad key events
3752 ASSERT_NO_FATAL_FAILURE(
3753 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3754 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3755 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3756 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3757 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3758 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3759 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3760
3761 ASSERT_NO_FATAL_FAILURE(
3762 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3763 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3764 AKEYCODE_DPAD_RIGHT, newDisplayId));
3765 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3766 AKEYCODE_DPAD_DOWN, newDisplayId));
3767 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3768 AKEYCODE_DPAD_LEFT, newDisplayId));
3769}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003770
arthurhungc903df12020-08-11 15:08:42 +08003771TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3772 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3773 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3774 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3775 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3776 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3777 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3778
3779 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003780 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003781 // Initial metastate is AMETA_NONE.
3782 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003783
3784 // Initialization should have turned all of the lights off.
3785 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3786 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3787 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3788
3789 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003792 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3793 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3794
3795 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003798 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3799 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3800
3801 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003804 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3805 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3806
3807 mFakeEventHub->removeDevice(EVENTHUB_ID);
3808 mReader->loopOnce();
3809
3810 // keyboard 2 should default toggle keys.
3811 const std::string USB2 = "USB2";
3812 const std::string DEVICE_NAME2 = "KEYBOARD2";
3813 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3814 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3815 std::shared_ptr<InputDevice> device2 =
3816 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003817 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003818 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3819 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3820 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3821 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3822 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3823 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3824
Arpit Singh67ca6842023-04-26 14:43:16 +00003825 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003826 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003827 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3828 mFakePolicy
3829 ->getReaderConfiguration(),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003830 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003831 std::list<NotifyArgs> unused =
3832 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003833 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003834 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003835
3836 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3837 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3838 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003839 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3840 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003841}
3842
Arthur Hungcb40a002021-08-03 14:31:01 +00003843TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3844 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3845 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3846 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3847
3848 // Suppose we have two mappers. (DPAD + KEYBOARD)
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003849 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD);
Arthur Hungcb40a002021-08-03 14:31:01 +00003850 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003851 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003852 // Initial metastate is AMETA_NONE.
3853 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003854
3855 mReader->toggleCapsLockState(DEVICE_ID);
3856 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3857}
3858
Arthur Hungfb3cc112022-04-13 07:39:50 +00003859TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3860 // keyboard 1.
3861 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3862 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3863 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3864 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3865 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3866 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3867
3868 KeyboardInputMapper& mapper1 =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003869 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003870
3871 // keyboard 2.
3872 const std::string USB2 = "USB2";
3873 const std::string DEVICE_NAME2 = "KEYBOARD2";
3874 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3875 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3876 std::shared_ptr<InputDevice> device2 =
3877 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3878 ftl::Flags<InputDeviceClass>(0));
3879 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3880 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3881 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3882 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3883 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3884 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3885
Arpit Singh67ca6842023-04-26 14:43:16 +00003886 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003887 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003888 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3889 mFakePolicy
3890 ->getReaderConfiguration(),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003891 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003892 std::list<NotifyArgs> unused =
3893 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003894 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003895 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003896
Arthur Hung95f68612022-04-07 14:08:22 +08003897 // Initial metastate is AMETA_NONE.
3898 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3899 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3900
3901 // Toggle num lock on and off.
3902 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3903 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003904 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3905 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3906 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3907
3908 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3909 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3910 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3911 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3912 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3913
3914 // Toggle caps lock on and off.
3915 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3916 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3917 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3918 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3919 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3920
3921 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3922 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3923 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3924 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3925 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3926
3927 // Toggle scroll lock on and off.
3928 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3929 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3930 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3931 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3932 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3933
3934 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3935 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3936 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3937 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3938 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3939}
3940
Arthur Hung2141d542022-08-23 07:45:21 +00003941TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3942 const int32_t USAGE_A = 0x070004;
3943 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3944 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3945
3946 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003947 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung2141d542022-08-23 07:45:21 +00003948 // Key down by scan code.
3949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3950 NotifyKeyArgs args;
3951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3952 ASSERT_EQ(DEVICE_ID, args.deviceId);
3953 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3954 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3955 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3956 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3957 ASSERT_EQ(KEY_HOME, args.scanCode);
3958 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3959
3960 // Disable device, it should synthesize cancellation events for down events.
3961 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003962 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003963
3964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3965 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3966 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3967 ASSERT_EQ(KEY_HOME, args.scanCode);
3968 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3969}
3970
Zixuan Qufecb6062022-11-12 04:44:31 +00003971TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003972 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Zixuan Qufecb6062022-11-12 04:44:31 +00003973 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003974 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3975 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003976
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003977 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003978 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3979
3980 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003981 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003982
3983 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3984 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3985 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3986 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3987 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003988 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3989
3990 // Call change layout association with the same values: Generation shouldn't change
3991 generation = mReader->getContext()->getGeneration();
3992 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3993 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3994 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3995 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003996}
3997
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003998TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3999 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
4000 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
4001
4002 // Configuration
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004003 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004004 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004005 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004006
4007 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4008 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4009}
4010
Justin Chung71ddb432023-03-27 04:29:07 +00004011TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4012 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4013 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004014 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Justin Chung71ddb432023-03-27 04:29:07 +00004015 NotifyKeyArgs args;
4016
4017 // Key down
4018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4020 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4021}
4022
Henry Barnora7ed6372024-10-21 14:33:43 -07004023TEST_F_WITH_FLAGS(KeyboardInputMapperTest, WakeBehavior_AlphabeticKeyboard,
4024 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
4025 enable_alphabetic_keyboard_wake))) {
4026 // For internal alphabetic devices, keys will trigger wake on key down.
4027
4028 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
4029 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4030 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE, 0);
4031
4032 KeyboardInputMapper& mapper =
4033 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
4034
4035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
4036 NotifyKeyArgs args;
4037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4038 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4039
4040 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
4041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4042 ASSERT_EQ(uint32_t(0), args.policyFlags);
4043
4044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4046 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4047
4048 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
4049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4050 ASSERT_EQ(uint32_t(0), args.policyFlags);
4051
4052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
4053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4054 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4055
4056 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
4057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4058 ASSERT_EQ(uint32_t(0), args.policyFlags);
4059}
4060
Prabir Pradhan38636652024-07-23 21:59:36 +00004061/**
4062 * When there is more than one KeyboardInputMapper for an InputDevice, each mapper should produce
4063 * events that use the shared keyboard source across all mappers. This is to ensure that each
4064 * input device generates key events in a consistent manner, regardless of which mapper produces
4065 * the event.
4066 */
4067TEST_F(KeyboardInputMapperTest, UsesSharedKeyboardSource) {
4068 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4069
4070 // Add a mapper with SOURCE_KEYBOARD
4071 KeyboardInputMapper& keyboardMapper =
4072 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
4073
4074 process(keyboardMapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 1);
4075 ASSERT_NO_FATAL_FAILURE(
4076 mFakeListener->assertNotifyKeyWasCalled(WithSource(AINPUT_SOURCE_KEYBOARD)));
4077 process(keyboardMapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 0);
4078 ASSERT_NO_FATAL_FAILURE(
4079 mFakeListener->assertNotifyKeyWasCalled(WithSource(AINPUT_SOURCE_KEYBOARD)));
4080
4081 // Add a mapper with SOURCE_DPAD
4082 KeyboardInputMapper& dpadMapper =
4083 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD);
4084 for (auto* mapper : {&keyboardMapper, &dpadMapper}) {
4085 process(*mapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 1);
4086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(
4087 WithSource(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_DPAD)));
4088 process(*mapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 0);
4089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(
4090 WithSource(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_DPAD)));
4091 }
4092
4093 // Add a mapper with SOURCE_GAMEPAD
4094 KeyboardInputMapper& gamepadMapper =
4095 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_GAMEPAD);
4096 for (auto* mapper : {&keyboardMapper, &dpadMapper, &gamepadMapper}) {
4097 process(*mapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 1);
4098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(
4099 WithSource(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_DPAD | AINPUT_SOURCE_GAMEPAD)));
4100 process(*mapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 0);
4101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(
4102 WithSource(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_DPAD | AINPUT_SOURCE_GAMEPAD)));
4103 }
4104}
4105
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004106// --- KeyboardInputMapperTest_ExternalAlphabeticDevice ---
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004107
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004108class KeyboardInputMapperTest_ExternalAlphabeticDevice : public InputMapperTest {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004109protected:
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004110 void SetUp() override {
4111 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::KEYBOARD |
4112 InputDeviceClass::ALPHAKEY | InputDeviceClass::EXTERNAL);
4113 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004114};
4115
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004116// --- KeyboardInputMapperTest_ExternalNonAlphabeticDevice ---
4117
4118class KeyboardInputMapperTest_ExternalNonAlphabeticDevice : public InputMapperTest {
4119protected:
4120 void SetUp() override {
4121 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::KEYBOARD |
4122 InputDeviceClass::EXTERNAL);
4123 }
4124};
4125
4126TEST_F(KeyboardInputMapperTest_ExternalAlphabeticDevice, WakeBehavior_AlphabeticKeyboard) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004127 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4128 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004129
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004130 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4131 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4132 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4133 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004134
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004135 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004136 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Powei Fengd041c5d2019-05-03 17:11:33 -07004137
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004139 NotifyKeyArgs args;
4140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4141 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4142
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004143 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4145 ASSERT_EQ(uint32_t(0), args.policyFlags);
4146
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004149 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004150
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004151 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4153 ASSERT_EQ(uint32_t(0), args.policyFlags);
4154
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4157 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4158
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004159 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4161 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4162}
4163
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004164TEST_F(KeyboardInputMapperTest_ExternalNonAlphabeticDevice, WakeBehavior_NonAlphabeticKeyboard) {
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004165 // For external devices, keys will trigger wake on key down. Media keys should not trigger
4166 // wake if triggered from external non-alphaebtic keyboard (e.g. headsets).
4167
4168 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4169 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4170 POLICY_FLAG_WAKE);
4171
4172 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004173 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004174
4175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
4176 NotifyKeyArgs args;
4177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4178 ASSERT_EQ(uint32_t(0), args.policyFlags);
4179
4180 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
4181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4182 ASSERT_EQ(uint32_t(0), args.policyFlags);
4183
4184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
4185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4186 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4187
4188 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
4189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4190 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4191}
4192
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004193TEST_F(KeyboardInputMapperTest_ExternalAlphabeticDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004194 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004195
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004196 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4197 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4198 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004199
Powei Fengd041c5d2019-05-03 17:11:33 -07004200 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004201 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004202 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Powei Fengd041c5d2019-05-03 17:11:33 -07004203
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004205 NotifyKeyArgs args;
4206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4207 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4208
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004209 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4211 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4212
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4215 ASSERT_EQ(uint32_t(0), args.policyFlags);
4216
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004217 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4219 ASSERT_EQ(uint32_t(0), args.policyFlags);
4220
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004221 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4223 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4224
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004225 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4227 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4228}
4229
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230// --- TouchInputMapperTest ---
4231
4232class TouchInputMapperTest : public InputMapperTest {
4233protected:
4234 static const int32_t RAW_X_MIN;
4235 static const int32_t RAW_X_MAX;
4236 static const int32_t RAW_Y_MIN;
4237 static const int32_t RAW_Y_MAX;
4238 static const int32_t RAW_TOUCH_MIN;
4239 static const int32_t RAW_TOUCH_MAX;
4240 static const int32_t RAW_TOOL_MIN;
4241 static const int32_t RAW_TOOL_MAX;
4242 static const int32_t RAW_PRESSURE_MIN;
4243 static const int32_t RAW_PRESSURE_MAX;
4244 static const int32_t RAW_ORIENTATION_MIN;
4245 static const int32_t RAW_ORIENTATION_MAX;
4246 static const int32_t RAW_DISTANCE_MIN;
4247 static const int32_t RAW_DISTANCE_MAX;
4248 static const int32_t RAW_TILT_MIN;
4249 static const int32_t RAW_TILT_MAX;
4250 static const int32_t RAW_ID_MIN;
4251 static const int32_t RAW_ID_MAX;
4252 static const int32_t RAW_SLOT_MIN;
4253 static const int32_t RAW_SLOT_MAX;
4254 static const float X_PRECISION;
4255 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004256 static const float X_PRECISION_VIRTUAL;
4257 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258
4259 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004260 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261
4262 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4263
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004264 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004265 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004266
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 enum Axes {
4268 POSITION = 1 << 0,
4269 TOUCH = 1 << 1,
4270 TOOL = 1 << 2,
4271 PRESSURE = 1 << 3,
4272 ORIENTATION = 1 << 4,
4273 MINOR = 1 << 5,
4274 ID = 1 << 6,
4275 DISTANCE = 1 << 7,
4276 TILT = 1 << 8,
4277 SLOT = 1 << 9,
4278 TOOL_TYPE = 1 << 10,
4279 };
4280
Michael Wrighta9cf4192022-12-01 23:46:39 +00004281 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004282 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004283 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004285 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 int32_t toRawX(float displayX);
4287 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004288 int32_t toRotatedRawX(float displayX);
4289 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004290 float toCookedX(float rawX, float rawY);
4291 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004292 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004293 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004295 float toDisplayY(int32_t rawY, int32_t displayHeight);
4296
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297};
4298
4299const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4300const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4301const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4302const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4303const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4304const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4305const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4306const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004307const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4308const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4310const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4311const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4312const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4313const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4314const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4315const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4316const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4317const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4318const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4319const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4320const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004321const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4322 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4323const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4324 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004325const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4326 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327
4328const float TouchInputMapperTest::GEOMETRIC_SCALE =
4329 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4330 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4331
4332const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4333 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4334 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4335};
4336
Michael Wrighta9cf4192022-12-01 23:46:39 +00004337void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004338 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4339 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004340}
4341
4342void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4343 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004344 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345}
4346
Michael Wrighta9cf4192022-12-01 23:46:39 +00004347void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004348 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4349 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4350 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004351}
4352
Michael Wrightd02c5b62014-02-10 15:10:22 -08004353void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004354 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4355 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4356 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4357 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358}
4359
Jason Gerecke489fda82012-09-07 17:19:40 -07004360void TouchInputMapperTest::prepareLocationCalibration() {
4361 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4362}
4363
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364int32_t TouchInputMapperTest::toRawX(float displayX) {
4365 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4366}
4367
4368int32_t TouchInputMapperTest::toRawY(float displayY) {
4369 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4370}
4371
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004372int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4373 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4374}
4375
4376int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4377 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4378}
4379
Jason Gerecke489fda82012-09-07 17:19:40 -07004380float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4381 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4382 return rawX;
4383}
4384
4385float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4386 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4387 return rawY;
4388}
4389
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004391 return toDisplayX(rawX, DISPLAY_WIDTH);
4392}
4393
4394float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4395 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004396}
4397
4398float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004399 return toDisplayY(rawY, DISPLAY_HEIGHT);
4400}
4401
4402float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4403 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404}
4405
4406
4407// --- SingleTouchInputMapperTest ---
4408
4409class SingleTouchInputMapperTest : public TouchInputMapperTest {
4410protected:
4411 void prepareButtons();
4412 void prepareAxes(int axes);
4413
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004414 std::list<NotifyArgs> processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4415 std::list<NotifyArgs> processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4416 std::list<NotifyArgs> processUp(SingleTouchInputMapper& mappery);
4417 std::list<NotifyArgs> processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4418 std::list<NotifyArgs> processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4419 std::list<NotifyArgs> processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4420 std::list<NotifyArgs> processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4421 std::list<NotifyArgs> processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4422 std::list<NotifyArgs> processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423};
4424
4425void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004426 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427}
4428
4429void SingleTouchInputMapperTest::prepareAxes(int axes) {
4430 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004431 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4432 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433 }
4434 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004435 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4436 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437 }
4438 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004439 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4440 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441 }
4442 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004443 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4444 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445 }
4446 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004447 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4448 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004449 }
4450}
4451
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004452std::list<NotifyArgs> SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper,
4453 int32_t x, int32_t y) {
4454 std::list<NotifyArgs> args;
4455 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4456 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4457 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
4458 return args;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004459}
4460
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004461std::list<NotifyArgs> SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper,
4462 int32_t x, int32_t y) {
4463 std::list<NotifyArgs> args;
4464 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4465 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
4466 return args;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467}
4468
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004469std::list<NotifyArgs> SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
4470 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471}
4472
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004473std::list<NotifyArgs> SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper,
4474 int32_t pressure) {
4475 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476}
4477
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004478std::list<NotifyArgs> SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4479 int32_t toolMajor) {
4480 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481}
4482
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004483std::list<NotifyArgs> SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper,
4484 int32_t distance) {
4485 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486}
4487
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004488std::list<NotifyArgs> SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper,
4489 int32_t tiltX, int32_t tiltY) {
4490 std::list<NotifyArgs> args;
4491 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4492 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
4493 return args;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494}
4495
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004496std::list<NotifyArgs> SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper,
4497 int32_t code, int32_t value) {
4498 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499}
4500
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004501std::list<NotifyArgs> SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
4502 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503}
4504
Michael Wrightd02c5b62014-02-10 15:10:22 -08004505TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506 prepareButtons();
4507 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00004508 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509
Josep del Río2d8c79a2023-01-23 19:33:50 +00004510 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511}
4512
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514 prepareButtons();
4515 prepareAxes(POSITION);
4516 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00004517 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004519 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520}
4521
4522TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004524 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004525 prepareButtons();
4526 prepareAxes(POSITION);
4527 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004528 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529
4530 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004531 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532
4533 // Virtual key is down.
4534 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4535 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4536 processDown(mapper, x, y);
4537 processSync(mapper);
4538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4539
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004540 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541
4542 // Virtual key is up.
4543 processUp(mapper);
4544 processSync(mapper);
4545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4546
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004547 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548}
4549
4550TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004551 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004552 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553 prepareButtons();
4554 prepareAxes(POSITION);
4555 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004556 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004557
4558 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004559 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560
4561 // Virtual key is down.
4562 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4563 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4564 processDown(mapper, x, y);
4565 processSync(mapper);
4566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4567
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004568 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569
4570 // Virtual key is up.
4571 processUp(mapper);
4572 processSync(mapper);
4573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4574
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004575 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004576}
4577
4578TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004580 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581 prepareButtons();
4582 prepareAxes(POSITION);
4583 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004584 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004587 ASSERT_TRUE(
4588 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004589 ASSERT_TRUE(flags[0]);
4590 ASSERT_FALSE(flags[1]);
4591}
4592
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004593TEST_F(SingleTouchInputMapperTest, DeviceTypeChange_RecalculatesRawToDisplayTransform) {
4594 prepareDisplay(ui::ROTATION_0);
4595 prepareAxes(POSITION);
4596 addConfigurationProperty("touch.deviceType", "touchScreen");
4597 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
4598
4599 const int32_t x = 900;
4600 const int32_t y = 75;
4601 std::list<NotifyArgs> args;
4602 args += processDown(mapper, x, y);
4603 args += processSync(mapper);
4604
4605 // Assert that motion event is received in display coordinate space for deviceType touchScreen.
4606 ASSERT_THAT(args,
4607 ElementsAre(VariantWith<NotifyMotionArgs>(
4608 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4609 WithCoords(toDisplayX(x), toDisplayY(y))))));
4610
4611 // Add device type association after the device was created.
4612 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
4613 // Send update to the mapper.
4614 std::list<NotifyArgs> unused =
4615 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4616 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
4617
4618 args.clear();
4619 args += processDown(mapper, x, y);
4620 args += processSync(mapper);
4621
4622 // Assert that motion event is received in raw coordinate space for deviceType touchNavigation.
4623 ASSERT_THAT(args,
4624 ElementsAre(VariantWith<NotifyMotionArgs>(
4625 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4626 WithCoords(x - RAW_X_MIN, y - RAW_Y_MIN)))));
4627}
4628
Michael Wrightd02c5b62014-02-10 15:10:22 -08004629TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
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
arthurhungdcef2dc2020-08-11 14:47:50 +08004637 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638
4639 NotifyKeyArgs args;
4640
4641 // Press virtual key.
4642 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4643 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4644 processDown(mapper, x, y);
4645 processSync(mapper);
4646
4647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4648 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4649 ASSERT_EQ(DEVICE_ID, args.deviceId);
4650 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4651 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4652 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4653 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4654 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4655 ASSERT_EQ(KEY_HOME, args.scanCode);
4656 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4657 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4658
4659 // Release virtual key.
4660 processUp(mapper);
4661 processSync(mapper);
4662
4663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4664 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4665 ASSERT_EQ(DEVICE_ID, args.deviceId);
4666 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4667 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4668 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4669 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4670 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4671 ASSERT_EQ(KEY_HOME, args.scanCode);
4672 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4673 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4674
4675 // Should not have sent any motions.
4676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4677}
4678
4679TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004680 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004681 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004682 prepareButtons();
4683 prepareAxes(POSITION);
4684 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004685 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686
arthurhungdcef2dc2020-08-11 14:47:50 +08004687 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004688
4689 NotifyKeyArgs keyArgs;
4690
4691 // Press virtual key.
4692 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4693 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4694 processDown(mapper, x, y);
4695 processSync(mapper);
4696
4697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4698 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4699 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4700 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4701 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4702 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4703 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4704 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4705 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4706 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4707 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4708
4709 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4710 // into the display area.
4711 y -= 100;
4712 processMove(mapper, x, y);
4713 processSync(mapper);
4714
4715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4716 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4717 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4718 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4719 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4720 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4721 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4722 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4723 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4724 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4725 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4726 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4727
4728 NotifyMotionArgs motionArgs;
4729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4730 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4731 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4732 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4733 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4734 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4735 ASSERT_EQ(0, motionArgs.flags);
4736 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4737 ASSERT_EQ(0, motionArgs.buttonState);
4738 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004739 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004740 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004741 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4743 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4744 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4745 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4746 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4747
4748 // Keep moving out of bounds. Should generate a pointer move.
4749 y -= 50;
4750 processMove(mapper, x, y);
4751 processSync(mapper);
4752
4753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4754 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4755 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4756 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4757 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4758 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4759 ASSERT_EQ(0, motionArgs.flags);
4760 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4761 ASSERT_EQ(0, motionArgs.buttonState);
4762 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004763 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004764 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004765 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4767 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4768 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4769 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4770 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4771
4772 // Release out of bounds. Should generate a pointer up.
4773 processUp(mapper);
4774 processSync(mapper);
4775
4776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4777 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4778 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4779 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4780 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4781 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4782 ASSERT_EQ(0, motionArgs.flags);
4783 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4784 ASSERT_EQ(0, motionArgs.buttonState);
4785 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004786 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004788 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4790 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4791 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4792 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4793 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4794
4795 // Should not have sent any more keys or motions.
4796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4798}
4799
4800TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004802 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803 prepareButtons();
4804 prepareAxes(POSITION);
4805 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004806 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807
arthurhungdcef2dc2020-08-11 14:47:50 +08004808 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004809
4810 NotifyMotionArgs motionArgs;
4811
4812 // Initially go down out of bounds.
4813 int32_t x = -10;
4814 int32_t y = -10;
4815 processDown(mapper, x, y);
4816 processSync(mapper);
4817
4818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4819
4820 // Move into the display area. Should generate a pointer down.
4821 x = 50;
4822 y = 75;
4823 processMove(mapper, x, y);
4824 processSync(mapper);
4825
4826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4827 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4828 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4829 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4830 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4831 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4832 ASSERT_EQ(0, motionArgs.flags);
4833 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4834 ASSERT_EQ(0, motionArgs.buttonState);
4835 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004836 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004838 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4840 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4841 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4842 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4843 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4844
4845 // Release. Should generate a pointer up.
4846 processUp(mapper);
4847 processSync(mapper);
4848
4849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4850 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4851 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4852 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4853 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4854 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4855 ASSERT_EQ(0, motionArgs.flags);
4856 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4857 ASSERT_EQ(0, motionArgs.buttonState);
4858 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004859 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004860 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004861 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4863 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4864 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4865 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4866 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4867
4868 // Should not have sent any more keys or motions.
4869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4871}
4872
Santos Cordonfa5cf462017-04-05 10:37:00 -07004873TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004874 addConfigurationProperty("touch.deviceType", "touchScreen");
4875 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4876
Michael Wrighta9cf4192022-12-01 23:46:39 +00004877 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004878 prepareButtons();
4879 prepareAxes(POSITION);
4880 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004881 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004882
arthurhungdcef2dc2020-08-11 14:47:50 +08004883 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004884
4885 NotifyMotionArgs motionArgs;
4886
4887 // Down.
4888 int32_t x = 100;
4889 int32_t y = 125;
4890 processDown(mapper, x, y);
4891 processSync(mapper);
4892
4893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4894 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4895 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4896 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4897 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4898 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4899 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4900 ASSERT_EQ(0, motionArgs.flags);
4901 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4902 ASSERT_EQ(0, motionArgs.buttonState);
4903 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004904 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004905 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004906 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4908 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4909 1, 0, 0, 0, 0, 0, 0, 0));
4910 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4911 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4912 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4913
4914 // Move.
4915 x += 50;
4916 y += 75;
4917 processMove(mapper, x, y);
4918 processSync(mapper);
4919
4920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4921 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4922 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4923 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4924 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4925 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4927 ASSERT_EQ(0, motionArgs.flags);
4928 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4929 ASSERT_EQ(0, motionArgs.buttonState);
4930 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004931 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004932 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004933 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4935 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4936 1, 0, 0, 0, 0, 0, 0, 0));
4937 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4938 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4939 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4940
4941 // Up.
4942 processUp(mapper);
4943 processSync(mapper);
4944
4945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4946 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4947 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4948 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4949 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4950 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4951 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4952 ASSERT_EQ(0, motionArgs.flags);
4953 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4954 ASSERT_EQ(0, motionArgs.buttonState);
4955 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004956 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004957 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004958 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4960 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4961 1, 0, 0, 0, 0, 0, 0, 0));
4962 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4963 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4964 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4965
4966 // Should not have sent any more keys or motions.
4967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4969}
4970
Michael Wrightd02c5b62014-02-10 15:10:22 -08004971TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004973 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974 prepareButtons();
4975 prepareAxes(POSITION);
4976 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004977 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978
arthurhungdcef2dc2020-08-11 14:47:50 +08004979 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980
4981 NotifyMotionArgs motionArgs;
4982
4983 // Down.
4984 int32_t x = 100;
4985 int32_t y = 125;
4986 processDown(mapper, x, y);
4987 processSync(mapper);
4988
4989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4990 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4991 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4992 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4993 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4994 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, 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());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005001 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5003 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5004 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5005 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5006 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5007
5008 // Move.
5009 x += 50;
5010 y += 75;
5011 processMove(mapper, x, y);
5012 processSync(mapper);
5013
5014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5015 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5016 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5017 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5018 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5020 ASSERT_EQ(0, motionArgs.flags);
5021 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5022 ASSERT_EQ(0, motionArgs.buttonState);
5023 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005024 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005025 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005026 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5028 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5029 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5030 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5031 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5032
5033 // Up.
5034 processUp(mapper);
5035 processSync(mapper);
5036
5037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5038 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5039 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5040 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5041 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5042 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5043 ASSERT_EQ(0, motionArgs.flags);
5044 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5045 ASSERT_EQ(0, motionArgs.buttonState);
5046 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005047 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005048 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005049 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5051 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5052 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5053 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5054 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5055
5056 // Should not have sent any more keys or motions.
5057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5059}
5060
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005061TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062 addConfigurationProperty("touch.deviceType", "touchScreen");
5063 prepareButtons();
5064 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005065 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5066 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005067 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068
5069 NotifyMotionArgs args;
5070
5071 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005072 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073 processDown(mapper, toRawX(50), toRawY(75));
5074 processSync(mapper);
5075
5076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5077 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5078 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5079
5080 processUp(mapper);
5081 processSync(mapper);
5082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5083}
5084
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005085TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086 addConfigurationProperty("touch.deviceType", "touchScreen");
5087 prepareButtons();
5088 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005089 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5090 // orientation-aware are affected by display rotation.
5091 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005092 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005093
5094 NotifyMotionArgs args;
5095
5096 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005097 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005098 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005099 processDown(mapper, toRawX(50), toRawY(75));
5100 processSync(mapper);
5101
5102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5103 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5104 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5105
5106 processUp(mapper);
5107 processSync(mapper);
5108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5109
5110 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005111 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005112 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005113 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005114 processSync(mapper);
5115
5116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5117 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5118 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5119
5120 processUp(mapper);
5121 processSync(mapper);
5122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5123
5124 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005125 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005126 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005127 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5128 processSync(mapper);
5129
5130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5131 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5132 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5133
5134 processUp(mapper);
5135 processSync(mapper);
5136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5137
5138 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005139 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005140 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005141 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005142 processSync(mapper);
5143
5144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5145 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5146 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5147
5148 processUp(mapper);
5149 processSync(mapper);
5150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5151}
5152
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005153TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5154 addConfigurationProperty("touch.deviceType", "touchScreen");
5155 prepareButtons();
5156 prepareAxes(POSITION);
5157 addConfigurationProperty("touch.orientationAware", "1");
5158 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5159 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005160 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005161 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005162 NotifyMotionArgs args;
5163
5164 // Orientation 0.
5165 processDown(mapper, toRawX(50), toRawY(75));
5166 processSync(mapper);
5167
5168 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5169 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5170 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5171
5172 processUp(mapper);
5173 processSync(mapper);
5174 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5175}
5176
5177TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5178 addConfigurationProperty("touch.deviceType", "touchScreen");
5179 prepareButtons();
5180 prepareAxes(POSITION);
5181 addConfigurationProperty("touch.orientationAware", "1");
5182 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5183 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005184 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005185 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005186 NotifyMotionArgs args;
5187
5188 // Orientation 90.
5189 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5190 processSync(mapper);
5191
5192 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5193 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5194 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5195
5196 processUp(mapper);
5197 processSync(mapper);
5198 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5199}
5200
5201TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5202 addConfigurationProperty("touch.deviceType", "touchScreen");
5203 prepareButtons();
5204 prepareAxes(POSITION);
5205 addConfigurationProperty("touch.orientationAware", "1");
5206 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5207 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005208 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005209 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005210 NotifyMotionArgs args;
5211
5212 // Orientation 180.
5213 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5214 processSync(mapper);
5215
5216 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5217 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5218 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5219
5220 processUp(mapper);
5221 processSync(mapper);
5222 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5223}
5224
5225TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5226 addConfigurationProperty("touch.deviceType", "touchScreen");
5227 prepareButtons();
5228 prepareAxes(POSITION);
5229 addConfigurationProperty("touch.orientationAware", "1");
5230 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5231 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005232 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005233 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005234 NotifyMotionArgs args;
5235
5236 // Orientation 270.
5237 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5238 processSync(mapper);
5239
5240 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5241 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5242 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5243
5244 processUp(mapper);
5245 processSync(mapper);
5246 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5247}
5248
5249TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5250 addConfigurationProperty("touch.deviceType", "touchScreen");
5251 prepareButtons();
5252 prepareAxes(POSITION);
5253 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5254 // orientation-aware are affected by display rotation.
5255 addConfigurationProperty("touch.orientationAware", "0");
5256 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005257 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005258
5259 NotifyMotionArgs args;
5260
5261 // Orientation 90, Rotation 0.
5262 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005263 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005264 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5265 processSync(mapper);
5266
5267 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5268 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5269 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5270
5271 processUp(mapper);
5272 processSync(mapper);
5273 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5274
5275 // Orientation 90, Rotation 90.
5276 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005277 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005278 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005279 processSync(mapper);
5280
5281 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5282 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5283 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5284
5285 processUp(mapper);
5286 processSync(mapper);
5287 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5288
5289 // Orientation 90, Rotation 180.
5290 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005291 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005292 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5293 processSync(mapper);
5294
5295 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5296 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5297 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5298
5299 processUp(mapper);
5300 processSync(mapper);
5301 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5302
5303 // Orientation 90, Rotation 270.
5304 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005305 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005306 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 -07005307 processSync(mapper);
5308
5309 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5310 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5311 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5312
5313 processUp(mapper);
5314 processSync(mapper);
5315 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5316}
5317
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005318TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5319 addConfigurationProperty("touch.deviceType", "touchScreen");
5320 prepareButtons();
5321 prepareAxes(POSITION);
5322 addConfigurationProperty("touch.orientationAware", "1");
5323 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005324 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005325
5326 // Set a physical frame in the display viewport.
5327 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5328 viewport->physicalLeft = 20;
5329 viewport->physicalTop = 600;
5330 viewport->physicalRight = 30;
5331 viewport->physicalBottom = 610;
5332 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005333 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005334
5335 // Start the touch.
5336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5337 processSync(mapper);
5338
5339 // Expect all input starting outside the physical frame to be ignored.
5340 const std::array<Point, 6> outsidePoints = {
5341 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5342 for (const auto& p : outsidePoints) {
5343 processMove(mapper, toRawX(p.x), toRawY(p.y));
5344 processSync(mapper);
5345 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5346 }
5347
5348 // Move the touch into the physical frame.
5349 processMove(mapper, toRawX(25), toRawY(605));
5350 processSync(mapper);
5351 NotifyMotionArgs args;
5352 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5353 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5354 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5355 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5356
5357 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5358 for (const auto& p : outsidePoints) {
5359 processMove(mapper, toRawX(p.x), toRawY(p.y));
5360 processSync(mapper);
5361 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5362 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5363 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5364 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5365 }
5366
5367 processUp(mapper);
5368 processSync(mapper);
5369 EXPECT_NO_FATAL_FAILURE(
5370 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5371}
5372
Harry Cutts1db43992023-06-19 17:05:07 +00005373TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
Harry Cutts1db43992023-06-19 17:05:07 +00005374 addConfigurationProperty("touch.deviceType", "pointer");
5375 prepareAxes(POSITION);
5376 prepareDisplay(ui::ROTATION_0);
5377 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5378
5379 // Set a physical frame in the display viewport.
5380 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5381 viewport->physicalLeft = 20;
5382 viewport->physicalTop = 600;
5383 viewport->physicalRight = 30;
5384 viewport->physicalBottom = 610;
5385 mFakePolicy->updateViewport(*viewport);
5386 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5387
5388 // Start the touch.
5389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5390 processSync(mapper);
5391
5392 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5393 // produced.
5394 const std::array<Point, 6> outsidePoints = {
5395 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5396 for (const auto& p : outsidePoints) {
5397 processMove(mapper, toRawX(p.x), toRawY(p.y));
5398 processSync(mapper);
5399 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5400 }
5401}
5402
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005405 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406 prepareButtons();
5407 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00005408 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005409
5410 // These calculations are based on the input device calibration documentation.
5411 int32_t rawX = 100;
5412 int32_t rawY = 200;
5413 int32_t rawPressure = 10;
5414 int32_t rawToolMajor = 12;
5415 int32_t rawDistance = 2;
5416 int32_t rawTiltX = 30;
5417 int32_t rawTiltY = 110;
5418
5419 float x = toDisplayX(rawX);
5420 float y = toDisplayY(rawY);
5421 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5422 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5423 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5424 float distance = float(rawDistance);
5425
5426 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5427 float tiltScale = M_PI / 180;
5428 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5429 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5430 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5431 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5432
5433 processDown(mapper, rawX, rawY);
5434 processPressure(mapper, rawPressure);
5435 processToolMajor(mapper, rawToolMajor);
5436 processDistance(mapper, rawDistance);
5437 processTilt(mapper, rawTiltX, rawTiltY);
5438 processSync(mapper);
5439
5440 NotifyMotionArgs args;
5441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5443 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5444 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
Prabir Pradhan9a53b552024-06-04 02:59:40 +00005445 ASSERT_EQ(args.flags,
5446 AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_ORIENTATION |
5447 AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_DIRECTIONAL_ORIENTATION);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005448}
5449
Jason Gerecke489fda82012-09-07 17:19:40 -07005450TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005451 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005452 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005453 prepareLocationCalibration();
5454 prepareButtons();
5455 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005456 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005457
5458 int32_t rawX = 100;
5459 int32_t rawY = 200;
5460
5461 float x = toDisplayX(toCookedX(rawX, rawY));
5462 float y = toDisplayY(toCookedY(rawX, rawY));
5463
5464 processDown(mapper, rawX, rawY);
5465 processSync(mapper);
5466
5467 NotifyMotionArgs args;
5468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5470 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5471}
5472
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005474 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005475 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005476 prepareButtons();
5477 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005478 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005479
5480 NotifyMotionArgs motionArgs;
5481 NotifyKeyArgs keyArgs;
5482
5483 processDown(mapper, 100, 200);
5484 processSync(mapper);
5485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5486 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5487 ASSERT_EQ(0, motionArgs.buttonState);
5488
5489 // press BTN_LEFT, release BTN_LEFT
5490 processKey(mapper, BTN_LEFT, 1);
5491 processSync(mapper);
5492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5494 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5495
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5498 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5499
Michael Wrightd02c5b62014-02-10 15:10:22 -08005500 processKey(mapper, BTN_LEFT, 0);
5501 processSync(mapper);
5502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005503 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005504 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005505
5506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005508 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005509
5510 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5511 processKey(mapper, BTN_RIGHT, 1);
5512 processKey(mapper, BTN_MIDDLE, 1);
5513 processSync(mapper);
5514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5516 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5517 motionArgs.buttonState);
5518
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5520 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5521 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5522
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5525 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5526 motionArgs.buttonState);
5527
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528 processKey(mapper, BTN_RIGHT, 0);
5529 processSync(mapper);
5530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005531 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005532 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005533
5534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005536 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005537
5538 processKey(mapper, BTN_MIDDLE, 0);
5539 processSync(mapper);
5540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005541 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005542 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005543
5544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005546 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005547
5548 // press BTN_BACK, release BTN_BACK
5549 processKey(mapper, BTN_BACK, 1);
5550 processSync(mapper);
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5552 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5553 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005554
Michael Wrightd02c5b62014-02-10 15:10:22 -08005555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005557 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5558
5559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5560 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5561 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562
5563 processKey(mapper, BTN_BACK, 0);
5564 processSync(mapper);
5565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005566 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005567 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005568
5569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005571 ASSERT_EQ(0, motionArgs.buttonState);
5572
Michael Wrightd02c5b62014-02-10 15:10:22 -08005573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5574 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5575 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5576
5577 // press BTN_SIDE, release BTN_SIDE
5578 processKey(mapper, BTN_SIDE, 1);
5579 processSync(mapper);
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5581 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5582 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005583
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584 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(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5587
5588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5589 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5590 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005591
5592 processKey(mapper, BTN_SIDE, 0);
5593 processSync(mapper);
5594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005595 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005596 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005597
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005600 ASSERT_EQ(0, motionArgs.buttonState);
5601
Michael Wrightd02c5b62014-02-10 15:10:22 -08005602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5603 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5604 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5605
5606 // press BTN_FORWARD, release BTN_FORWARD
5607 processKey(mapper, BTN_FORWARD, 1);
5608 processSync(mapper);
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5610 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5611 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005612
Michael Wrightd02c5b62014-02-10 15:10:22 -08005613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005615 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5616
5617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5618 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5619 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620
5621 processKey(mapper, BTN_FORWARD, 0);
5622 processSync(mapper);
5623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005624 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005625 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005626
5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005629 ASSERT_EQ(0, motionArgs.buttonState);
5630
Michael Wrightd02c5b62014-02-10 15:10:22 -08005631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5632 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5633 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5634
5635 // press BTN_EXTRA, release BTN_EXTRA
5636 processKey(mapper, BTN_EXTRA, 1);
5637 processSync(mapper);
5638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5639 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5640 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005641
Michael Wrightd02c5b62014-02-10 15:10:22 -08005642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005643 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005644 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5645
5646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5647 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5648 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649
5650 processKey(mapper, BTN_EXTRA, 0);
5651 processSync(mapper);
5652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005653 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005654 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005655
5656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005658 ASSERT_EQ(0, motionArgs.buttonState);
5659
Michael Wrightd02c5b62014-02-10 15:10:22 -08005660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5661 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5662 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5663
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5665
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666 // press BTN_STYLUS, release BTN_STYLUS
5667 processKey(mapper, BTN_STYLUS, 1);
5668 processSync(mapper);
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005671 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5672
5673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5674 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5675 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005676
5677 processKey(mapper, BTN_STYLUS, 0);
5678 processSync(mapper);
5679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005680 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005681 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005682
5683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005684 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005685 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005686
5687 // press BTN_STYLUS2, release BTN_STYLUS2
5688 processKey(mapper, BTN_STYLUS2, 1);
5689 processSync(mapper);
5690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005692 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5693
5694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5695 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5696 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697
5698 processKey(mapper, BTN_STYLUS2, 0);
5699 processSync(mapper);
5700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005701 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005702 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005703
5704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005706 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005707
5708 // release touch
5709 processUp(mapper);
5710 processSync(mapper);
5711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5712 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5713 ASSERT_EQ(0, motionArgs.buttonState);
5714}
5715
5716TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005717 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005718 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005719 prepareButtons();
5720 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005721 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005722
5723 NotifyMotionArgs motionArgs;
5724
5725 // default tool type is finger
5726 processDown(mapper, 100, 200);
5727 processSync(mapper);
5728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5729 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005730 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005731
5732 // eraser
5733 processKey(mapper, BTN_TOOL_RUBBER, 1);
5734 processSync(mapper);
5735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5736 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005737 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005738
5739 // stylus
5740 processKey(mapper, BTN_TOOL_RUBBER, 0);
5741 processKey(mapper, BTN_TOOL_PEN, 1);
5742 processSync(mapper);
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005745 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005746
5747 // brush
5748 processKey(mapper, BTN_TOOL_PEN, 0);
5749 processKey(mapper, BTN_TOOL_BRUSH, 1);
5750 processSync(mapper);
5751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5752 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005753 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005754
5755 // pencil
5756 processKey(mapper, BTN_TOOL_BRUSH, 0);
5757 processKey(mapper, BTN_TOOL_PENCIL, 1);
5758 processSync(mapper);
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005761 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005762
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005763 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005764 processKey(mapper, BTN_TOOL_PENCIL, 0);
5765 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5766 processSync(mapper);
5767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5768 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005769 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770
5771 // mouse
5772 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5773 processKey(mapper, BTN_TOOL_MOUSE, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005778
5779 // lens
5780 processKey(mapper, BTN_TOOL_MOUSE, 0);
5781 processKey(mapper, BTN_TOOL_LENS, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005786
5787 // double-tap
5788 processKey(mapper, BTN_TOOL_LENS, 0);
5789 processKey(mapper, BTN_TOOL_DOUBLETAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005794
5795 // triple-tap
5796 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5797 processKey(mapper, BTN_TOOL_TRIPLETAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005802
5803 // quad-tap
5804 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5805 processKey(mapper, BTN_TOOL_QUADTAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005810
5811 // finger
5812 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5813 processKey(mapper, BTN_TOOL_FINGER, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005818
5819 // stylus trumps finger
5820 processKey(mapper, BTN_TOOL_PEN, 1);
5821 processSync(mapper);
5822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5823 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005824 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005825
5826 // eraser trumps stylus
5827 processKey(mapper, BTN_TOOL_RUBBER, 1);
5828 processSync(mapper);
5829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5830 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005831 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005832
5833 // mouse trumps eraser
5834 processKey(mapper, BTN_TOOL_MOUSE, 1);
5835 processSync(mapper);
5836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005838 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839
5840 // back to default tool type
5841 processKey(mapper, BTN_TOOL_MOUSE, 0);
5842 processKey(mapper, BTN_TOOL_RUBBER, 0);
5843 processKey(mapper, BTN_TOOL_PEN, 0);
5844 processKey(mapper, BTN_TOOL_FINGER, 0);
5845 processSync(mapper);
5846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005848 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005849}
5850
5851TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005852 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005853 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005854 prepareButtons();
5855 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005856 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005857 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005858
5859 NotifyMotionArgs motionArgs;
5860
5861 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5862 processKey(mapper, BTN_TOOL_FINGER, 1);
5863 processMove(mapper, 100, 200);
5864 processSync(mapper);
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5866 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5868 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5869
5870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5871 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5873 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5874
5875 // move a little
5876 processMove(mapper, 150, 250);
5877 processSync(mapper);
5878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5879 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5881 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5882
5883 // down when BTN_TOUCH is pressed, pressure defaults to 1
5884 processKey(mapper, BTN_TOUCH, 1);
5885 processSync(mapper);
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5887 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5889 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5890
5891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5892 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5894 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5895
5896 // up when BTN_TOUCH is released, hover restored
5897 processKey(mapper, BTN_TOUCH, 0);
5898 processSync(mapper);
5899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5900 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5902 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5903
5904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5905 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5907 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5908
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5910 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5912 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5913
5914 // exit hover when pointer goes away
5915 processKey(mapper, BTN_TOOL_FINGER, 0);
5916 processSync(mapper);
5917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5918 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5920 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5921}
5922
5923TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005924 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005925 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005926 prepareButtons();
5927 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005928 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005929
5930 NotifyMotionArgs motionArgs;
5931
5932 // initially hovering because pressure is 0
5933 processDown(mapper, 100, 200);
5934 processPressure(mapper, 0);
5935 processSync(mapper);
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5937 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5939 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5940
5941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5942 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5944 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5945
5946 // move a little
5947 processMove(mapper, 150, 250);
5948 processSync(mapper);
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 // down when pressure is non-zero
5955 processPressure(mapper, RAW_PRESSURE_MAX);
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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5963 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5965 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5966
5967 // up when pressure becomes 0, hover restored
5968 processPressure(mapper, 0);
5969 processSync(mapper);
5970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5971 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5973 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5974
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5976 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5978 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5979
5980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5981 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5983 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5984
5985 // exit hover when pointer goes away
5986 processUp(mapper);
5987 processSync(mapper);
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5989 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5991 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5992}
5993
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005994TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
5995 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005996 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005997 prepareButtons();
5998 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005999 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006000
6001 // Touch down.
6002 processDown(mapper, 100, 200);
6003 processPressure(mapper, 1);
6004 processSync(mapper);
6005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6006 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6007
6008 // Reset the mapper. This should cancel the ongoing gesture.
6009 resetMapper(mapper, ARBITRARY_TIME);
6010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6011 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6012
6013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6014}
6015
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006016TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6017 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006018 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006019 prepareButtons();
6020 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006021 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006022
6023 // Set the initial state for the touch pointer.
6024 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6025 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6026 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6027 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6028
6029 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006030 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6031 // does not generate any events.
6032 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006033
6034 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6035 // the recreated touch state to generate a down event.
6036 processSync(mapper);
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6038 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6039
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6041}
6042
lilinnan687e58f2022-07-19 16:00:50 +08006043TEST_F(SingleTouchInputMapperTest,
6044 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6045 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006046 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006047 prepareButtons();
6048 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006049 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006050 NotifyMotionArgs motionArgs;
6051
6052 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006053 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006054 processSync(mapper);
6055
6056 // We should receive a down event
6057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6058 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6059
6060 // Change display id
6061 clearViewports();
6062 prepareSecondaryDisplay(ViewportType::INTERNAL);
6063
6064 // We should receive a cancel event
6065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6066 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6067 // Then receive reset called
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6069}
6070
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006071TEST_F(SingleTouchInputMapperTest,
6072 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6073 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006074 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006075 prepareButtons();
6076 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006077 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6079 NotifyMotionArgs motionArgs;
6080
6081 // Start a new gesture.
6082 processDown(mapper, 100, 200);
6083 processSync(mapper);
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6085 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6086
6087 // Make the viewport inactive. This will put the device in disabled mode.
6088 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6089 viewport->isActive = false;
6090 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006091 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006092
6093 // We should receive a cancel event for the ongoing gesture.
6094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6095 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6096 // Then we should be notified that the device was reset.
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6098
6099 // No events are generated while the viewport is inactive.
6100 processMove(mapper, 101, 201);
6101 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006102 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006103 processSync(mapper);
6104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6105
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006106 // Start a new gesture while the viewport is still inactive.
6107 processDown(mapper, 300, 400);
6108 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6109 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6110 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6111 processSync(mapper);
6112
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006113 // Make the viewport active again. The device should resume processing events.
6114 viewport->isActive = true;
6115 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006116 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006117
6118 // The device is reset because it changes back to direct mode, without generating any events.
6119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6121
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006122 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006123 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6125 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006126
6127 // No more events.
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6130}
6131
Prabir Pradhan211ba622022-10-31 21:09:21 +00006132TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6133 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006134 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006135 prepareButtons();
6136 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006137 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6139
6140 // Press a stylus button.
6141 processKey(mapper, BTN_STYLUS, 1);
6142 processSync(mapper);
6143
6144 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6145 processDown(mapper, 100, 200);
6146 processSync(mapper);
6147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6148 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6149 WithCoords(toDisplayX(100), toDisplayY(200)),
6150 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6152 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6153 WithCoords(toDisplayX(100), toDisplayY(200)),
6154 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6155
6156 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6157 // the button has not actually been released, since there will be no pointers through which the
6158 // button state can be reported. The event is generated at the location of the pointer before
6159 // it went up.
6160 processUp(mapper);
6161 processSync(mapper);
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6163 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6164 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6166 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6167 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6168}
6169
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006170TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6171 addConfigurationProperty("touch.deviceType", "touchScreen");
6172 prepareDisplay(ui::ROTATION_0);
6173 prepareButtons();
6174 prepareAxes(POSITION);
6175
6176 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6177
Arpit Singha8c236b2023-04-25 13:56:05 +00006178 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6180
6181 // Press a stylus button.
6182 processKey(mapper, BTN_STYLUS, 1);
6183 processSync(mapper);
6184
6185 // Start a touch gesture and ensure that the stylus button is not reported.
6186 processDown(mapper, 100, 200);
6187 processSync(mapper);
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6189 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6190
6191 // Release and press the stylus button again.
6192 processKey(mapper, BTN_STYLUS, 0);
6193 processSync(mapper);
6194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6195 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6196 processKey(mapper, BTN_STYLUS, 1);
6197 processSync(mapper);
6198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6199 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6200
6201 // Release the touch gesture.
6202 processUp(mapper);
6203 processSync(mapper);
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6205 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6206
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6208}
6209
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006210TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6211 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6212 prepareDisplay(ui::ROTATION_0);
6213 prepareButtons();
6214 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006215 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6217
Prabir Pradhan9cf8f322024-06-18 20:22:42 +00006218 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006219}
6220
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006221TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6222 // Initialize the device without setting device source to touch navigation.
6223 addConfigurationProperty("touch.deviceType", "touchScreen");
6224 prepareDisplay(ui::ROTATION_0);
6225 prepareButtons();
6226 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006227 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006228
6229 // Ensure that the device is created as a touchscreen, not touch navigation.
6230 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6231
6232 // Add device type association after the device was created.
6233 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6234
6235 // Send update to the mapper.
6236 std::list<NotifyArgs> unused2 =
6237 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006238 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006239
6240 // Check whether device type update was successful.
Prabir Pradhan9cf8f322024-06-18 20:22:42 +00006241 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION | AINPUT_SOURCE_TOUCHPAD, mDevice->getSources());
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006242}
6243
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006244TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6245 // Initialize the device without setting device source to touch navigation.
6246 addConfigurationProperty("touch.deviceType", "touchScreen");
6247 prepareDisplay(ui::ROTATION_0);
6248 prepareButtons();
6249 prepareAxes(POSITION);
6250 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6251
6252 // Set a physical frame in the display viewport.
6253 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6254 viewport->physicalLeft = 0;
6255 viewport->physicalTop = 0;
6256 viewport->physicalRight = DISPLAY_WIDTH / 2;
6257 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6258 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006259 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006260
Arpit Singha8c236b2023-04-25 13:56:05 +00006261 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006262
6263 // Hovering inside the physical frame produces events.
6264 processKey(mapper, BTN_TOOL_PEN, 1);
6265 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6266 processSync(mapper);
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6268 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6270 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6271
6272 // Leaving the physical frame ends the hovering gesture.
6273 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6274 processSync(mapper);
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6276 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6277
6278 // Moving outside the physical frame does not produce events.
6279 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6280 processSync(mapper);
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6282
6283 // Re-entering the physical frame produces events.
6284 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6285 processSync(mapper);
6286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6287 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6289 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6290}
6291
Prabir Pradhan5632d622021-09-06 07:57:20 -07006292// --- TouchDisplayProjectionTest ---
6293
6294class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6295public:
6296 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6297 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6298 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006299 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6300 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6301 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006302 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006303 auto rotatedWidth = naturalDisplayWidth;
6304 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006305 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006306 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006307 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006308 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006309 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006310 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006311 inverseRotationFlags = ui::Transform::ROT_180;
6312 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006313 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006314 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006315 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006316 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006317 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006318 inverseRotationFlags = ui::Transform::ROT_0;
6319 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006320 }
6321
Prabir Pradhana9df3162022-12-05 23:57:27 +00006322 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006323 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6324
6325 std::optional<DisplayViewport> internalViewport =
6326 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6327 DisplayViewport& v = *internalViewport;
6328 v.displayId = DISPLAY_ID;
6329 v.orientation = orientation;
6330
6331 v.logicalLeft = 0;
6332 v.logicalTop = 0;
6333 v.logicalRight = 100;
6334 v.logicalBottom = 100;
6335
6336 v.physicalLeft = rotatedPhysicalDisplay.left;
6337 v.physicalTop = rotatedPhysicalDisplay.top;
6338 v.physicalRight = rotatedPhysicalDisplay.right;
6339 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6340
Prabir Pradhana9df3162022-12-05 23:57:27 +00006341 v.deviceWidth = rotatedWidth;
6342 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006343
6344 v.isActive = true;
6345 v.uniqueId = UNIQUE_ID;
6346 v.type = ViewportType::INTERNAL;
6347 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006348 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006349 }
6350
6351 void assertReceivedMove(const Point& point) {
6352 NotifyMotionArgs motionArgs;
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006355 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6357 1, 0, 0, 0, 0, 0, 0, 0));
6358 }
6359};
6360
6361TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6362 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006363 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006364
6365 prepareButtons();
6366 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006367 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006368
6369 NotifyMotionArgs motionArgs;
6370
6371 // Configure the DisplayViewport such that the logical display maps to a subsection of
6372 // the display panel called the physical display. Here, the physical display is bounded by the
6373 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6374 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6375 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6376 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6377
Michael Wrighta9cf4192022-12-01 23:46:39 +00006378 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006379 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6380
6381 // Touches outside the physical display should be ignored, and should not generate any
6382 // events. Ensure touches at the following points that lie outside of the physical display
6383 // area do not generate any events.
6384 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6385 processDown(mapper, toRawX(point.x), toRawY(point.y));
6386 processSync(mapper);
6387 processUp(mapper);
6388 processSync(mapper);
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6390 << "Unexpected event generated for touch outside physical display at point: "
6391 << point.x << ", " << point.y;
6392 }
6393 }
6394}
6395
6396TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6397 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006398 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006399
6400 prepareButtons();
6401 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006402 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006403
6404 NotifyMotionArgs motionArgs;
6405
6406 // Configure the DisplayViewport such that the logical display maps to a subsection of
6407 // the display panel called the physical display. Here, the physical display is bounded by the
6408 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6409 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6410
Michael Wrighta9cf4192022-12-01 23:46:39 +00006411 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006412 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6413
6414 // Touches that start outside the physical display should be ignored until it enters the
6415 // physical display bounds, at which point it should generate a down event. Start a touch at
6416 // the point (5, 100), which is outside the physical display bounds.
6417 static const Point kOutsidePoint{5, 100};
6418 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6419 processSync(mapper);
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6421
6422 // Move the touch into the physical display area. This should generate a pointer down.
6423 processMove(mapper, toRawX(11), toRawY(21));
6424 processSync(mapper);
6425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6426 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006427 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006428 ASSERT_NO_FATAL_FAILURE(
6429 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6430
6431 // Move the touch inside the physical display area. This should generate a pointer move.
6432 processMove(mapper, toRawX(69), toRawY(159));
6433 processSync(mapper);
6434 assertReceivedMove({69, 159});
6435
6436 // Move outside the physical display area. Since the pointer is already down, this should
6437 // now continue generating events.
6438 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6439 processSync(mapper);
6440 assertReceivedMove(kOutsidePoint);
6441
6442 // Release. This should generate a pointer up.
6443 processUp(mapper);
6444 processSync(mapper);
6445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6446 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6448 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6449
6450 // Ensure no more events were generated.
6451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6453 }
6454}
6455
Prabir Pradhana9df3162022-12-05 23:57:27 +00006456// --- TouchscreenPrecisionTests ---
6457
6458// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6459// in various orientations and with different display rotations. We configure the touchscreen to
6460// have a higher resolution than that of the display by an integer scale factor in each axis so that
6461// we can enforce that coordinates match precisely as expected.
6462class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6463 public ::testing::WithParamInterface<ui::Rotation> {
6464public:
6465 void SetUp() override {
6466 SingleTouchInputMapperTest::SetUp();
6467
6468 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6469 // four times the resolution of the display in the Y axis.
6470 prepareButtons();
6471 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006472 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6473 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006474 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006475 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6476 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006477 }
6478
6479 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6480 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6481 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6482 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6483
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006484 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6485 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6486
6487 static const int32_t PRECISION_RAW_X_FLAT = 16;
6488 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6489
6490 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6491 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6492
Prabir Pradhana9df3162022-12-05 23:57:27 +00006493 static const std::array<Point, 4> kRawCorners;
6494};
6495
6496const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6497 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6498 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6499 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6500 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6501}};
6502
6503// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6504// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6505// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6506TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6507 enum class Orientation {
6508 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6509 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6510 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6511 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6512 ftl_last = ORIENTATION_270,
6513 };
6514 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6515 Orientation::ORIENTATION_270;
6516 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6517 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6518 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6519 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6520 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6521 };
6522
6523 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6524
6525 // Configure the touchscreen as being installed in the one of the four different orientations
6526 // relative to the display.
6527 addConfigurationProperty("touch.deviceType", "touchScreen");
6528 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6529 prepareDisplay(ui::ROTATION_0);
6530
Arpit Singha8c236b2023-04-25 13:56:05 +00006531 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00006532
6533 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6534 // orientations of either 90 or 270) this means the display's natural resolution will be
6535 // flipped.
6536 const bool displayRotated =
6537 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6538 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6539 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6540 const Rect physicalFrame{0, 0, width, height};
6541 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6542
6543 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6544 const float expectedPrecisionX = displayRotated ? 4 : 2;
6545 const float expectedPrecisionY = displayRotated ? 2 : 4;
6546
6547 // Test all four corners.
6548 for (int i = 0; i < 4; i++) {
6549 const auto& raw = kRawCorners[i];
6550 processDown(mapper, raw.x, raw.y);
6551 processSync(mapper);
6552 const auto& expected = expectedPoints[i];
6553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6554 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6555 WithCoords(expected.x, expected.y),
6556 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6557 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6558 << "with touchscreen orientation "
6559 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6560 << expected.x << ", " << expected.y << ").";
6561 processUp(mapper);
6562 processSync(mapper);
6563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6564 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6565 WithCoords(expected.x, expected.y))));
6566 }
6567}
6568
Prabir Pradhan82687402022-12-06 01:32:53 +00006569TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6570 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6571 kMappedCorners = {
6572 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6573 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6574 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6575 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
6576 };
6577
6578 const ui::Rotation displayRotation = GetParam();
6579
6580 addConfigurationProperty("touch.deviceType", "touchScreen");
6581 prepareDisplay(displayRotation);
6582
Arpit Singha8c236b2023-04-25 13:56:05 +00006583 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00006584
6585 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6586
6587 // Test all four corners.
6588 for (int i = 0; i < 4; i++) {
6589 const auto& expected = expectedPoints[i];
6590 const auto& raw = kRawCorners[i];
6591 processDown(mapper, raw.x, raw.y);
6592 processSync(mapper);
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6594 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6595 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
6596 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6597 << "with display rotation " << ui::toCString(displayRotation)
6598 << ", expected point (" << expected.x << ", " << expected.y << ").";
6599 processUp(mapper);
6600 processSync(mapper);
6601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6602 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6603 WithCoords(expected.x, expected.y))));
6604 }
6605}
6606
Prabir Pradhan3e798762022-12-02 21:02:11 +00006607TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
6608 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
6609 kMappedCorners = {
6610 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6611 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
6612 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
6613 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
6614 };
6615
6616 const ui::Rotation displayRotation = GetParam();
6617
6618 addConfigurationProperty("touch.deviceType", "touchScreen");
6619 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6620
Arpit Singha8c236b2023-04-25 13:56:05 +00006621 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00006622
6623 // Ori 270, so width and height swapped
6624 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
6625 prepareDisplay(displayRotation);
6626 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
6627
6628 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6629
6630 // Test all four corners.
6631 for (int i = 0; i < 4; i++) {
6632 const auto& expected = expectedPoints[i];
6633 const auto& raw = kRawCorners[i];
6634 processDown(mapper, raw.x, raw.y);
6635 processSync(mapper);
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6637 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6638 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
6639 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6640 << "with display rotation " << ui::toCString(displayRotation)
6641 << ", expected point (" << expected.x << ", " << expected.y << ").";
6642 processUp(mapper);
6643 processSync(mapper);
6644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6645 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6646 WithCoords(expected.x, expected.y))));
6647 }
6648}
6649
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006650TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
6651 const ui::Rotation displayRotation = GetParam();
6652
6653 addConfigurationProperty("touch.deviceType", "touchScreen");
6654 prepareDisplay(displayRotation);
6655
6656 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00006657 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006658
6659 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
6660 // MotionRanges use display pixels as their units
6661 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
6662 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
6663
6664 // The MotionRanges should be oriented in the rotated display's coordinate space
6665 const bool displayRotated =
6666 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
6667
6668 constexpr float MAX_X = 479.5;
6669 constexpr float MAX_Y = 799.75;
6670 EXPECT_EQ(xRange->min, 0.f);
6671 EXPECT_EQ(yRange->min, 0.f);
6672 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
6673 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
6674
6675 EXPECT_EQ(xRange->flat, 8.f);
6676 EXPECT_EQ(yRange->flat, 8.f);
6677
6678 EXPECT_EQ(xRange->fuzz, 2.f);
6679 EXPECT_EQ(yRange->fuzz, 2.f);
6680
6681 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
6682 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
6683}
6684
Prabir Pradhana9df3162022-12-05 23:57:27 +00006685// Run the precision tests for all rotations.
6686INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
6687 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6688 ui::ROTATION_270),
6689 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6690 return ftl::enum_string(testParamInfo.param);
6691 });
6692
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006693// --- ExternalStylusFusionTest ---
6694
6695class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6696public:
Prabir Pradhan7ff19a12024-07-29 22:40:31 +00006697 void SetUp() override {
6698 SingleTouchInputMapperTest::SetUp();
6699 mExternalStylusDeviceInfo = {};
6700 mStylusState = {};
6701 }
6702
6703 SingleTouchInputMapper& initializeInputMapperWithExternalStylus(bool supportsPressure = true) {
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006704 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006705 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006706 prepareButtons();
6707 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006708 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006709
Prabir Pradhan7ff19a12024-07-29 22:40:31 +00006710 if (supportsPressure) {
6711 mExternalStylusDeviceInfo.addMotionRange(AMOTION_EVENT_AXIS_PRESSURE,
6712 AINPUT_SOURCE_STYLUS, 0.0f, 1.0f, 0.0f, 0.0f,
6713 0.0f);
6714 mStylusState.pressure = 0.f;
6715 }
6716
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006717 mStylusState.when = ARBITRARY_TIME;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006718 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006719 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006720 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006721 processExternalStylusState(mapper);
6722 return mapper;
6723 }
6724
6725 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6726 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6727 for (const NotifyArgs& args : generatedArgs) {
6728 mFakeListener->notify(args);
6729 }
6730 // Loop the reader to flush the input listener queue.
6731 mReader->loopOnce();
6732 return generatedArgs;
6733 }
6734
6735protected:
6736 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006737
6738 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6739 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006740 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006741
6742 // The first pointer is withheld.
6743 processDown(mapper, 100, 200);
6744 processSync(mapper);
6745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6746 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6747 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6748
6749 // The external stylus reports pressure. The withheld finger pointer is released as a
6750 // stylus.
6751 mStylusState.pressure = 1.f;
6752 processExternalStylusState(mapper);
6753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6754 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6755 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6756
6757 // Subsequent pointer events are not withheld.
6758 processMove(mapper, 101, 201);
6759 processSync(mapper);
6760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6761 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6762
6763 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6765 }
6766
6767 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6768 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6769
6770 // Releasing the touch pointer ends the gesture.
6771 processUp(mapper);
6772 processSync(mapper);
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006774 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006775 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006776
6777 mStylusState.pressure = 0.f;
6778 processExternalStylusState(mapper);
6779 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6781 }
6782
6783 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006784 // When stylus fusion is not successful, events should be reported with the original source.
6785 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006786 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006787 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006788
6789 // The first pointer is withheld when an external stylus is connected,
6790 // and a timeout is requested.
6791 processDown(mapper, 100, 200);
6792 processSync(mapper);
6793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6794 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6795 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6796
6797 // If the timeout expires early, it is requested again.
6798 handleTimeout(mapper, ARBITRARY_TIME + 1);
6799 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6800 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6801
6802 // When the timeout expires, the withheld touch is released as a finger pointer.
6803 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6805 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6806
6807 // Subsequent pointer events are not withheld.
6808 processMove(mapper, 101, 201);
6809 processSync(mapper);
6810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6811 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6812 processUp(mapper);
6813 processSync(mapper);
6814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6815 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6816
6817 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6819 }
6820
6821private:
6822 InputDeviceInfo mExternalStylusDeviceInfo{};
6823};
6824
Prabir Pradhan7ff19a12024-07-29 22:40:31 +00006825TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSourceWithPressure) {
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006826 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006827 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006828}
6829
Prabir Pradhan7ff19a12024-07-29 22:40:31 +00006830TEST_F(ExternalStylusFusionTest, DoesNotUseBluetoothStylusSourceWithoutPressure) {
6831 SingleTouchInputMapper& mapper =
6832 initializeInputMapperWithExternalStylus(/*supportsPressure=*/false);
6833 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6834}
6835
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006836TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6837 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6838 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6839}
6840
6841TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6842 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6843 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6844}
6845
6846// Test a successful stylus fusion gesture where the pressure is reported by the external
6847// before the touch is reported by the touchscreen.
6848TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6849 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006850 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006851
6852 // The external stylus reports pressure first. It is ignored for now.
6853 mStylusState.pressure = 1.f;
6854 processExternalStylusState(mapper);
6855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6856 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6857
6858 // When the touch goes down afterwards, it is reported as a stylus pointer.
6859 processDown(mapper, 100, 200);
6860 processSync(mapper);
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6862 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6863 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6864
6865 processMove(mapper, 101, 201);
6866 processSync(mapper);
6867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6868 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6869 processUp(mapper);
6870 processSync(mapper);
6871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6872 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6873
6874 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6876}
6877
6878TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6879 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6880
6881 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6882 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6883
6884 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6885 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6886 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6887 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6888}
6889
6890TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6891 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006892 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006893
6894 mStylusState.pressure = 0.8f;
6895 processExternalStylusState(mapper);
6896 processDown(mapper, 100, 200);
6897 processSync(mapper);
6898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6899 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6900 WithPressure(0.8f))));
6901 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6902
6903 // The external stylus reports a pressure change. We wait for some time for a touch event.
6904 mStylusState.pressure = 0.6f;
6905 processExternalStylusState(mapper);
6906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6907 ASSERT_NO_FATAL_FAILURE(
6908 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6909
6910 // If a touch is reported within the timeout, it reports the updated pressure.
6911 processMove(mapper, 101, 201);
6912 processSync(mapper);
6913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6914 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6915 WithPressure(0.6f))));
6916 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6917
6918 // There is another pressure change.
6919 mStylusState.pressure = 0.5f;
6920 processExternalStylusState(mapper);
6921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6922 ASSERT_NO_FATAL_FAILURE(
6923 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6924
6925 // If a touch is not reported within the timeout, a move event is generated to report
6926 // the new pressure.
6927 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6929 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6930 WithPressure(0.5f))));
6931
6932 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6933 // repeated indefinitely.
6934 mStylusState.pressure = 0.0f;
6935 processExternalStylusState(mapper);
6936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6937 ASSERT_NO_FATAL_FAILURE(
6938 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6939 processMove(mapper, 102, 202);
6940 processSync(mapper);
6941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6942 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6943 WithPressure(0.5f))));
6944 processMove(mapper, 103, 203);
6945 processSync(mapper);
6946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6947 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6948 WithPressure(0.5f))));
6949
6950 processUp(mapper);
6951 processSync(mapper);
6952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006953 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006954 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006955
6956 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6958}
6959
6960TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6961 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006962 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006963
6964 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006965 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006966 processExternalStylusState(mapper);
6967 processDown(mapper, 100, 200);
6968 processSync(mapper);
6969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6970 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006971 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006972 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6973
6974 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006975 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006976 processExternalStylusState(mapper);
6977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6978 ASSERT_NO_FATAL_FAILURE(
6979 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6980
6981 // If a touch is reported within the timeout, it reports the updated pressure.
6982 processMove(mapper, 101, 201);
6983 processSync(mapper);
6984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6985 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006986 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006987 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6988
6989 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006990 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006991 processExternalStylusState(mapper);
6992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6993 ASSERT_NO_FATAL_FAILURE(
6994 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6995
6996 // If a touch is not reported within the timeout, a move event is generated to report
6997 // the new tool type.
6998 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7000 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007001 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007002
7003 processUp(mapper);
7004 processSync(mapper);
7005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7006 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007007 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007008
7009 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7011}
7012
7013TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7014 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007015 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007016
7017 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7018
7019 // The external stylus reports a button change. We wait for some time for a touch event.
7020 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7021 processExternalStylusState(mapper);
7022 ASSERT_NO_FATAL_FAILURE(
7023 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7024
7025 // If a touch is reported within the timeout, it reports the updated button state.
7026 processMove(mapper, 101, 201);
7027 processSync(mapper);
7028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7029 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7030 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7032 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7033 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7034 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7035
7036 // The button is now released.
7037 mStylusState.buttons = 0;
7038 processExternalStylusState(mapper);
7039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7040 ASSERT_NO_FATAL_FAILURE(
7041 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7042
7043 // If a touch is not reported within the timeout, a move event is generated to report
7044 // the new button state.
7045 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7047 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7048 WithButtonState(0))));
7049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007050 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7051 WithButtonState(0))));
7052
7053 processUp(mapper);
7054 processSync(mapper);
7055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007056 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7057
7058 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7060}
7061
Michael Wrightd02c5b62014-02-10 15:10:22 -08007062// --- MultiTouchInputMapperTest ---
7063
7064class MultiTouchInputMapperTest : public TouchInputMapperTest {
7065protected:
7066 void prepareAxes(int axes);
7067
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007068 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7069 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7070 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7071 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7072 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7073 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7074 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7075 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7076 void processId(MultiTouchInputMapper& mapper, int32_t id);
7077 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7078 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7079 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007080 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007081 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007082 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7083 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007084};
7085
7086void MultiTouchInputMapperTest::prepareAxes(int axes) {
7087 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007088 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7089 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007090 }
7091 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007092 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7093 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007094 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007095 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7096 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007097 }
7098 }
7099 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007100 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7101 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007102 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007103 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007104 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007105 }
7106 }
7107 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007108 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7109 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007110 }
7111 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007112 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7113 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007114 }
7115 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007116 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7117 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007118 }
7119 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007120 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7121 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007122 }
7123 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007124 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7125 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126 }
7127 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007128 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007129 }
7130}
7131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007132void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7133 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007136}
7137
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007138void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7139 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007141}
7142
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007143void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7144 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007146}
7147
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007148void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150}
7151
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007152void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007154}
7155
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007156void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7157 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007159}
7160
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007161void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007163}
7164
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007165void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007167}
7168
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007169void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007171}
7172
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007173void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007175}
7176
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007177void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007179}
7180
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007181void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7182 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007184}
7185
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007186void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7187 int32_t value) {
7188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7190}
7191
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007192void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007194}
7195
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007196void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7197 nsecs_t readTime) {
7198 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007199}
7200
Michael Wrightd02c5b62014-02-10 15:10:22 -08007201TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007202 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007203 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007204 prepareAxes(POSITION);
7205 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007206 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007207
arthurhungdcef2dc2020-08-11 14:47:50 +08007208 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007209
7210 NotifyMotionArgs motionArgs;
7211
7212 // Two fingers down at once.
7213 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7214 processPosition(mapper, x1, y1);
7215 processMTSync(mapper);
7216 processPosition(mapper, x2, y2);
7217 processMTSync(mapper);
7218 processSync(mapper);
7219
7220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7221 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7222 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7223 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7224 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7225 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7226 ASSERT_EQ(0, motionArgs.flags);
7227 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7228 ASSERT_EQ(0, motionArgs.buttonState);
7229 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007230 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007231 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007232 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7234 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7235 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7236 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7237 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7238
7239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7240 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7241 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7242 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7243 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007244 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007245 ASSERT_EQ(0, motionArgs.flags);
7246 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7247 ASSERT_EQ(0, motionArgs.buttonState);
7248 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007249 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007250 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007251 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007252 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007253 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7255 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7257 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7258 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7259 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7260 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7261
7262 // Move.
7263 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7264 processPosition(mapper, x1, y1);
7265 processMTSync(mapper);
7266 processPosition(mapper, x2, y2);
7267 processMTSync(mapper);
7268 processSync(mapper);
7269
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7271 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7272 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7273 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7274 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7276 ASSERT_EQ(0, motionArgs.flags);
7277 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7278 ASSERT_EQ(0, motionArgs.buttonState);
7279 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007280 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007281 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007282 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007283 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007284 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7286 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7288 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7289 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7290 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7291 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7292
7293 // First finger up.
7294 x2 += 15; y2 -= 20;
7295 processPosition(mapper, x2, y2);
7296 processMTSync(mapper);
7297 processSync(mapper);
7298
7299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7300 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7301 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7302 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7303 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007304 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007305 ASSERT_EQ(0, motionArgs.flags);
7306 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7307 ASSERT_EQ(0, motionArgs.buttonState);
7308 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007309 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007310 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007311 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007312 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007313 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7315 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7317 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7318 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7319 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7320 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7321
7322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7323 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7324 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7325 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7326 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7328 ASSERT_EQ(0, motionArgs.flags);
7329 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7330 ASSERT_EQ(0, motionArgs.buttonState);
7331 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007332 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007333 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007334 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7336 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7337 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7338 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7339 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7340
7341 // Move.
7342 x2 += 20; y2 -= 25;
7343 processPosition(mapper, x2, y2);
7344 processMTSync(mapper);
7345 processSync(mapper);
7346
7347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7348 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7349 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7350 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7351 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7353 ASSERT_EQ(0, motionArgs.flags);
7354 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7355 ASSERT_EQ(0, motionArgs.buttonState);
7356 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007357 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007358 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007359 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7361 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7362 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7363 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7364 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7365
7366 // New finger down.
7367 int32_t x3 = 700, y3 = 300;
7368 processPosition(mapper, x2, y2);
7369 processMTSync(mapper);
7370 processPosition(mapper, x3, y3);
7371 processMTSync(mapper);
7372 processSync(mapper);
7373
7374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7375 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7376 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7377 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7378 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007379 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007380 ASSERT_EQ(0, motionArgs.flags);
7381 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7382 ASSERT_EQ(0, motionArgs.buttonState);
7383 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007384 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007385 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007386 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007387 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007388 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7390 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7392 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7393 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7394 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7395 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7396
7397 // Second finger up.
7398 x3 += 30; y3 -= 20;
7399 processPosition(mapper, x3, y3);
7400 processMTSync(mapper);
7401 processSync(mapper);
7402
7403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7404 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7405 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7406 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7407 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007408 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007409 ASSERT_EQ(0, motionArgs.flags);
7410 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7411 ASSERT_EQ(0, motionArgs.buttonState);
7412 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007413 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007414 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007415 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007416 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007417 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7419 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7421 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7422 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7423 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7424 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7425
7426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7427 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7428 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7429 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7430 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7432 ASSERT_EQ(0, motionArgs.flags);
7433 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7434 ASSERT_EQ(0, motionArgs.buttonState);
7435 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007436 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007437 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007438 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7440 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7441 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7442 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7443 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7444
7445 // Last finger up.
7446 processMTSync(mapper);
7447 processSync(mapper);
7448
7449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7450 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7451 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7452 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7453 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7454 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7455 ASSERT_EQ(0, motionArgs.flags);
7456 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7457 ASSERT_EQ(0, motionArgs.buttonState);
7458 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007459 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007460 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007461 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7463 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7464 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7465 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7466 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7467
7468 // Should not have sent any more keys or motions.
7469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7471}
7472
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007473TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7474 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007475 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007476
7477 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7478 /*fuzz*/ 0, /*resolution*/ 10);
7479 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7480 /*fuzz*/ 0, /*resolution*/ 11);
7481 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7482 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7483 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7484 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7485 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7486 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7487 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7488 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7489
Arpit Singha8c236b2023-04-25 13:56:05 +00007490 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007491
7492 // X and Y axes
7493 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7494 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7495 // Touch major and minor
7496 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7497 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7498 // Tool major and minor
7499 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7500 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7501}
7502
7503TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7504 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007505 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007506
7507 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7508 /*fuzz*/ 0, /*resolution*/ 10);
7509 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7510 /*fuzz*/ 0, /*resolution*/ 11);
7511
7512 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7513
Arpit Singha8c236b2023-04-25 13:56:05 +00007514 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007515
7516 // Touch major and minor
7517 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7518 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7519 // Tool major and minor
7520 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7521 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7522}
7523
Michael Wrightd02c5b62014-02-10 15:10:22 -08007524TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007525 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007526 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007527 prepareAxes(POSITION | ID);
7528 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007529 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007530
arthurhungdcef2dc2020-08-11 14:47:50 +08007531 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007532
7533 NotifyMotionArgs motionArgs;
7534
7535 // Two fingers down at once.
7536 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7537 processPosition(mapper, x1, y1);
7538 processId(mapper, 1);
7539 processMTSync(mapper);
7540 processPosition(mapper, x2, y2);
7541 processId(mapper, 2);
7542 processMTSync(mapper);
7543 processSync(mapper);
7544
7545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7546 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007547 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007548 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007549 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7551 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7552
7553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007554 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007555 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007556 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007557 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007558 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007559 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7561 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7563 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7564
7565 // Move.
7566 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7567 processPosition(mapper, x1, y1);
7568 processId(mapper, 1);
7569 processMTSync(mapper);
7570 processPosition(mapper, x2, y2);
7571 processId(mapper, 2);
7572 processMTSync(mapper);
7573 processSync(mapper);
7574
7575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7576 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, 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 // First finger up.
7588 x2 += 15; y2 -= 20;
7589 processPosition(mapper, x2, y2);
7590 processId(mapper, 2);
7591 processMTSync(mapper);
7592 processSync(mapper);
7593
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007595 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007596 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007597 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007598 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007599 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007600 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7602 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7604 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7605
7606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007608 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007609 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007610 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7612 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7613
7614 // Move.
7615 x2 += 20; y2 -= 25;
7616 processPosition(mapper, x2, y2);
7617 processId(mapper, 2);
7618 processMTSync(mapper);
7619 processSync(mapper);
7620
7621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007623 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007624 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007625 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7627 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7628
7629 // New finger down.
7630 int32_t x3 = 700, y3 = 300;
7631 processPosition(mapper, x2, y2);
7632 processId(mapper, 2);
7633 processMTSync(mapper);
7634 processPosition(mapper, x3, y3);
7635 processId(mapper, 3);
7636 processMTSync(mapper);
7637 processSync(mapper);
7638
7639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007640 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007641 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007642 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007643 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007644 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007645 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7647 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7649 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7650
7651 // Second finger up.
7652 x3 += 30; y3 -= 20;
7653 processPosition(mapper, x3, y3);
7654 processId(mapper, 3);
7655 processMTSync(mapper);
7656 processSync(mapper);
7657
7658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007659 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007660 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007661 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007662 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007663 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007664 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7666 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7668 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7669
7670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7671 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007672 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007673 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007674 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7676 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7677
7678 // Last finger up.
7679 processMTSync(mapper);
7680 processSync(mapper);
7681
7682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7683 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007684 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007685 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007686 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].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
7690 // Should not have sent any more keys or motions.
7691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7693}
7694
7695TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007696 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007697 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007698 prepareAxes(POSITION | ID | SLOT);
7699 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007700 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007701
arthurhungdcef2dc2020-08-11 14:47:50 +08007702 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007703
7704 NotifyMotionArgs motionArgs;
7705
7706 // Two fingers down at once.
7707 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7708 processPosition(mapper, x1, y1);
7709 processId(mapper, 1);
7710 processSlot(mapper, 1);
7711 processPosition(mapper, x2, y2);
7712 processId(mapper, 2);
7713 processSync(mapper);
7714
7715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7716 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007717 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007718 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007719 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7721 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7722
7723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007724 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007725 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007726 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007727 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007728 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007729 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007730 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7731 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7733 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7734
7735 // Move.
7736 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7737 processSlot(mapper, 0);
7738 processPosition(mapper, x1, y1);
7739 processSlot(mapper, 1);
7740 processPosition(mapper, x2, y2);
7741 processSync(mapper);
7742
7743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007745 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007746 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007747 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007748 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007749 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7751 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7753 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7754
7755 // First finger up.
7756 x2 += 15; y2 -= 20;
7757 processSlot(mapper, 0);
7758 processId(mapper, -1);
7759 processSlot(mapper, 1);
7760 processPosition(mapper, x2, y2);
7761 processSync(mapper);
7762
7763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007764 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007765 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007766 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007767 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007768 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007769 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7771 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7773 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7774
7775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007777 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007778 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007779 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7781 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7782
7783 // Move.
7784 x2 += 20; y2 -= 25;
7785 processPosition(mapper, x2, y2);
7786 processSync(mapper);
7787
7788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007790 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007791 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007792 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7794 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7795
7796 // New finger down.
7797 int32_t x3 = 700, y3 = 300;
7798 processPosition(mapper, x2, y2);
7799 processSlot(mapper, 0);
7800 processId(mapper, 3);
7801 processPosition(mapper, x3, y3);
7802 processSync(mapper);
7803
7804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007805 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007806 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007807 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007808 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007809 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007810 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007811 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7812 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7814 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7815
7816 // Second finger up.
7817 x3 += 30; y3 -= 20;
7818 processSlot(mapper, 1);
7819 processId(mapper, -1);
7820 processSlot(mapper, 0);
7821 processPosition(mapper, x3, y3);
7822 processSync(mapper);
7823
7824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007825 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007826 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007827 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007828 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007829 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007830 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7832 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7834 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7835
7836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007838 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007839 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007840 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7842 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7843
7844 // Last finger up.
7845 processId(mapper, -1);
7846 processSync(mapper);
7847
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7849 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007850 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007851 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007852 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].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
7856 // Should not have sent any more keys or motions.
7857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7859}
7860
7861TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007862 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007863 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007864 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007865 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007866
7867 // These calculations are based on the input device calibration documentation.
7868 int32_t rawX = 100;
7869 int32_t rawY = 200;
7870 int32_t rawTouchMajor = 7;
7871 int32_t rawTouchMinor = 6;
7872 int32_t rawToolMajor = 9;
7873 int32_t rawToolMinor = 8;
7874 int32_t rawPressure = 11;
7875 int32_t rawDistance = 0;
7876 int32_t rawOrientation = 3;
7877 int32_t id = 5;
7878
7879 float x = toDisplayX(rawX);
7880 float y = toDisplayY(rawY);
7881 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7882 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7883 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7884 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7885 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7886 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7887 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7888 float distance = float(rawDistance);
7889
7890 processPosition(mapper, rawX, rawY);
7891 processTouchMajor(mapper, rawTouchMajor);
7892 processTouchMinor(mapper, rawTouchMinor);
7893 processToolMajor(mapper, rawToolMajor);
7894 processToolMinor(mapper, rawToolMinor);
7895 processPressure(mapper, rawPressure);
7896 processOrientation(mapper, rawOrientation);
7897 processDistance(mapper, rawDistance);
7898 processId(mapper, id);
7899 processMTSync(mapper);
7900 processSync(mapper);
7901
7902 NotifyMotionArgs args;
7903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7904 ASSERT_EQ(0, args.pointerProperties[0].id);
7905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7906 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7907 orientation, distance));
Prabir Pradhan9a53b552024-06-04 02:59:40 +00007908 ASSERT_EQ(args.flags, AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_ORIENTATION);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007909}
7910
7911TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007912 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007913 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007914 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7915 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00007916 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007917
7918 // These calculations are based on the input device calibration documentation.
7919 int32_t rawX = 100;
7920 int32_t rawY = 200;
7921 int32_t rawTouchMajor = 140;
7922 int32_t rawTouchMinor = 120;
7923 int32_t rawToolMajor = 180;
7924 int32_t rawToolMinor = 160;
7925
7926 float x = toDisplayX(rawX);
7927 float y = toDisplayY(rawY);
7928 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7929 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7930 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7931 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7932 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7933
7934 processPosition(mapper, rawX, rawY);
7935 processTouchMajor(mapper, rawTouchMajor);
7936 processTouchMinor(mapper, rawTouchMinor);
7937 processToolMajor(mapper, rawToolMajor);
7938 processToolMinor(mapper, rawToolMinor);
7939 processMTSync(mapper);
7940 processSync(mapper);
7941
7942 NotifyMotionArgs args;
7943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7945 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7946}
7947
7948TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007949 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007950 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007951 prepareAxes(POSITION | TOUCH | TOOL);
7952 addConfigurationProperty("touch.size.calibration", "diameter");
7953 addConfigurationProperty("touch.size.scale", "10");
7954 addConfigurationProperty("touch.size.bias", "160");
7955 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00007956 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007957
7958 // These calculations are based on the input device calibration documentation.
7959 // Note: We only provide a single common touch/tool value because the device is assumed
7960 // not to emit separate values for each pointer (isSummed = 1).
7961 int32_t rawX = 100;
7962 int32_t rawY = 200;
7963 int32_t rawX2 = 150;
7964 int32_t rawY2 = 250;
7965 int32_t rawTouchMajor = 5;
7966 int32_t rawToolMajor = 8;
7967
7968 float x = toDisplayX(rawX);
7969 float y = toDisplayY(rawY);
7970 float x2 = toDisplayX(rawX2);
7971 float y2 = toDisplayY(rawY2);
7972 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7973 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7974 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7975
7976 processPosition(mapper, rawX, rawY);
7977 processTouchMajor(mapper, rawTouchMajor);
7978 processToolMajor(mapper, rawToolMajor);
7979 processMTSync(mapper);
7980 processPosition(mapper, rawX2, rawY2);
7981 processTouchMajor(mapper, rawTouchMajor);
7982 processToolMajor(mapper, rawToolMajor);
7983 processMTSync(mapper);
7984 processSync(mapper);
7985
7986 NotifyMotionArgs args;
7987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7988 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7989
7990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007991 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007992 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7994 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7996 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7997}
7998
7999TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008000 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008001 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008002 prepareAxes(POSITION | TOUCH | TOOL);
8003 addConfigurationProperty("touch.size.calibration", "area");
8004 addConfigurationProperty("touch.size.scale", "43");
8005 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008006 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008007
8008 // These calculations are based on the input device calibration documentation.
8009 int32_t rawX = 100;
8010 int32_t rawY = 200;
8011 int32_t rawTouchMajor = 5;
8012 int32_t rawToolMajor = 8;
8013
8014 float x = toDisplayX(rawX);
8015 float y = toDisplayY(rawY);
8016 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8017 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8018 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8019
8020 processPosition(mapper, rawX, rawY);
8021 processTouchMajor(mapper, rawTouchMajor);
8022 processToolMajor(mapper, rawToolMajor);
8023 processMTSync(mapper);
8024 processSync(mapper);
8025
8026 NotifyMotionArgs args;
8027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8029 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8030}
8031
8032TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008033 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008034 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008035 prepareAxes(POSITION | PRESSURE);
8036 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8037 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008038 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039
Michael Wrightaa449c92017-12-13 21:21:43 +00008040 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008041 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008042 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8043 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8044 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8045
Michael Wrightd02c5b62014-02-10 15:10:22 -08008046 // These calculations are based on the input device calibration documentation.
8047 int32_t rawX = 100;
8048 int32_t rawY = 200;
8049 int32_t rawPressure = 60;
8050
8051 float x = toDisplayX(rawX);
8052 float y = toDisplayY(rawY);
8053 float pressure = float(rawPressure) * 0.01f;
8054
8055 processPosition(mapper, rawX, rawY);
8056 processPressure(mapper, rawPressure);
8057 processMTSync(mapper);
8058 processSync(mapper);
8059
8060 NotifyMotionArgs args;
8061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8063 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8064}
8065
8066TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008067 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008068 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008069 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008070 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008071
8072 NotifyMotionArgs motionArgs;
8073 NotifyKeyArgs keyArgs;
8074
8075 processId(mapper, 1);
8076 processPosition(mapper, 100, 200);
8077 processSync(mapper);
8078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8079 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8080 ASSERT_EQ(0, motionArgs.buttonState);
8081
8082 // press BTN_LEFT, release BTN_LEFT
8083 processKey(mapper, BTN_LEFT, 1);
8084 processSync(mapper);
8085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8087 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8088
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8091 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8092
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 processKey(mapper, BTN_LEFT, 0);
8094 processSync(mapper);
8095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008096 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008098
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008101 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008102
8103 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8104 processKey(mapper, BTN_RIGHT, 1);
8105 processKey(mapper, BTN_MIDDLE, 1);
8106 processSync(mapper);
8107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8109 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8110 motionArgs.buttonState);
8111
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8113 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8114 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8115
8116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8117 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8118 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8119 motionArgs.buttonState);
8120
Michael Wrightd02c5b62014-02-10 15:10:22 -08008121 processKey(mapper, BTN_RIGHT, 0);
8122 processSync(mapper);
8123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008125 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008126
8127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008129 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008130
8131 processKey(mapper, BTN_MIDDLE, 0);
8132 processSync(mapper);
8133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008134 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008135 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008136
8137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008139 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008140
8141 // press BTN_BACK, release BTN_BACK
8142 processKey(mapper, BTN_BACK, 1);
8143 processSync(mapper);
8144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8145 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8146 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008147
Michael Wrightd02c5b62014-02-10 15:10:22 -08008148 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_BACK, motionArgs.buttonState);
8151
8152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8153 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8154 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008155
8156 processKey(mapper, BTN_BACK, 0);
8157 processSync(mapper);
8158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008160 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008161
8162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008164 ASSERT_EQ(0, motionArgs.buttonState);
8165
Michael Wrightd02c5b62014-02-10 15:10:22 -08008166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8167 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8168 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8169
8170 // press BTN_SIDE, release BTN_SIDE
8171 processKey(mapper, BTN_SIDE, 1);
8172 processSync(mapper);
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8174 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8175 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008176
Michael Wrightd02c5b62014-02-10 15:10:22 -08008177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008179 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8180
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8182 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8183 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008184
8185 processKey(mapper, BTN_SIDE, 0);
8186 processSync(mapper);
8187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008189 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008190
8191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008193 ASSERT_EQ(0, motionArgs.buttonState);
8194
Michael Wrightd02c5b62014-02-10 15:10:22 -08008195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8196 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8197 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8198
8199 // press BTN_FORWARD, release BTN_FORWARD
8200 processKey(mapper, BTN_FORWARD, 1);
8201 processSync(mapper);
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8203 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8204 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008205
Michael Wrightd02c5b62014-02-10 15:10:22 -08008206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008208 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8209
8210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8211 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8212 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008213
8214 processKey(mapper, BTN_FORWARD, 0);
8215 processSync(mapper);
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008217 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008218 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008219
8220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008222 ASSERT_EQ(0, motionArgs.buttonState);
8223
Michael Wrightd02c5b62014-02-10 15:10:22 -08008224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8225 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8226 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8227
8228 // press BTN_EXTRA, release BTN_EXTRA
8229 processKey(mapper, BTN_EXTRA, 1);
8230 processSync(mapper);
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8232 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8233 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008234
Michael Wrightd02c5b62014-02-10 15:10:22 -08008235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008237 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8238
8239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8240 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8241 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242
8243 processKey(mapper, BTN_EXTRA, 0);
8244 processSync(mapper);
8245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008247 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008248
8249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008251 ASSERT_EQ(0, motionArgs.buttonState);
8252
Michael Wrightd02c5b62014-02-10 15:10:22 -08008253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8254 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8255 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8256
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8258
Michael Wrightd02c5b62014-02-10 15:10:22 -08008259 // press BTN_STYLUS, release BTN_STYLUS
8260 processKey(mapper, BTN_STYLUS, 1);
8261 processSync(mapper);
8262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008264 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8265
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8268 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008269
8270 processKey(mapper, BTN_STYLUS, 0);
8271 processSync(mapper);
8272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008273 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008274 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008275
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008278 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008279
8280 // press BTN_STYLUS2, release BTN_STYLUS2
8281 processKey(mapper, BTN_STYLUS2, 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_SECONDARY, 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_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008290
8291 processKey(mapper, BTN_STYLUS2, 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 // release touch
8302 processId(mapper, -1);
8303 processSync(mapper);
8304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8305 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8306 ASSERT_EQ(0, motionArgs.buttonState);
8307}
8308
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008309TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8310 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008311 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008312 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008313 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008314
8315 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8316 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8317
8318 // Touch down.
8319 processId(mapper, 1);
8320 processPosition(mapper, 100, 200);
8321 processSync(mapper);
8322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8323 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8324
8325 // Press and release button mapped to the primary stylus button.
8326 processKey(mapper, BTN_A, 1);
8327 processSync(mapper);
8328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8329 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8330 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8332 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8333 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8334
8335 processKey(mapper, BTN_A, 0);
8336 processSync(mapper);
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8338 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8340 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8341
8342 // Press and release the HID usage mapped to the secondary stylus button.
8343 processHidUsage(mapper, 0xabcd, 1);
8344 processSync(mapper);
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8346 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8347 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8349 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8350 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8351
8352 processHidUsage(mapper, 0xabcd, 0);
8353 processSync(mapper);
8354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8355 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8357 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8358
8359 // Release touch.
8360 processId(mapper, -1);
8361 processSync(mapper);
8362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8363 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8364}
8365
Michael Wrightd02c5b62014-02-10 15:10:22 -08008366TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008367 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008368 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008369 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008370 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008371
8372 NotifyMotionArgs motionArgs;
8373
8374 // default tool type is finger
8375 processId(mapper, 1);
8376 processPosition(mapper, 100, 200);
8377 processSync(mapper);
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8379 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008380 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008381
8382 // eraser
8383 processKey(mapper, BTN_TOOL_RUBBER, 1);
8384 processSync(mapper);
8385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008387 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008388
8389 // stylus
8390 processKey(mapper, BTN_TOOL_RUBBER, 0);
8391 processKey(mapper, BTN_TOOL_PEN, 1);
8392 processSync(mapper);
8393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008395 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008396
8397 // brush
8398 processKey(mapper, BTN_TOOL_PEN, 0);
8399 processKey(mapper, BTN_TOOL_BRUSH, 1);
8400 processSync(mapper);
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008403 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008404
8405 // pencil
8406 processKey(mapper, BTN_TOOL_BRUSH, 0);
8407 processKey(mapper, BTN_TOOL_PENCIL, 1);
8408 processSync(mapper);
8409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008411 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008412
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008413 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008414 processKey(mapper, BTN_TOOL_PENCIL, 0);
8415 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8416 processSync(mapper);
8417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008419 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008420
8421 // mouse
8422 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8423 processKey(mapper, BTN_TOOL_MOUSE, 1);
8424 processSync(mapper);
8425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008427 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008428
8429 // lens
8430 processKey(mapper, BTN_TOOL_MOUSE, 0);
8431 processKey(mapper, BTN_TOOL_LENS, 1);
8432 processSync(mapper);
8433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008435 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008436
8437 // double-tap
8438 processKey(mapper, BTN_TOOL_LENS, 0);
8439 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8440 processSync(mapper);
8441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008443 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008444
8445 // triple-tap
8446 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8447 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8448 processSync(mapper);
8449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8450 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008451 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008452
8453 // quad-tap
8454 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8455 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8456 processSync(mapper);
8457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008459 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008460
8461 // finger
8462 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8463 processKey(mapper, BTN_TOOL_FINGER, 1);
8464 processSync(mapper);
8465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8466 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008467 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008468
8469 // stylus trumps finger
8470 processKey(mapper, BTN_TOOL_PEN, 1);
8471 processSync(mapper);
8472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008474 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008475
8476 // eraser trumps stylus
8477 processKey(mapper, BTN_TOOL_RUBBER, 1);
8478 processSync(mapper);
8479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008481 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008482
8483 // mouse trumps eraser
8484 processKey(mapper, BTN_TOOL_MOUSE, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008489
8490 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8491 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008496
8497 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8498 processToolType(mapper, MT_TOOL_PEN);
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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008503
8504 // back to default tool type
8505 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8506 processKey(mapper, BTN_TOOL_MOUSE, 0);
8507 processKey(mapper, BTN_TOOL_RUBBER, 0);
8508 processKey(mapper, BTN_TOOL_PEN, 0);
8509 processKey(mapper, BTN_TOOL_FINGER, 0);
8510 processSync(mapper);
8511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008513 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008514}
8515
8516TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008517 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008518 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008519 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008520 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008521 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008522
8523 NotifyMotionArgs motionArgs;
8524
8525 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8526 processId(mapper, 1);
8527 processPosition(mapper, 100, 200);
8528 processSync(mapper);
8529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8530 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8532 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8533
8534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8535 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8537 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8538
8539 // move a little
8540 processPosition(mapper, 150, 250);
8541 processSync(mapper);
8542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8543 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8545 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8546
8547 // down when BTN_TOUCH is pressed, pressure defaults to 1
8548 processKey(mapper, BTN_TOUCH, 1);
8549 processSync(mapper);
8550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8551 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8553 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8554
8555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8556 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8558 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8559
8560 // up when BTN_TOUCH is released, hover restored
8561 processKey(mapper, BTN_TOUCH, 0);
8562 processSync(mapper);
8563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8564 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8566 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8567
8568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8569 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8571 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8572
8573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8574 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8576 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8577
8578 // exit hover when pointer goes away
8579 processId(mapper, -1);
8580 processSync(mapper);
8581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8582 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8584 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8585}
8586
8587TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008588 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008589 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008590 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008591 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008592
8593 NotifyMotionArgs motionArgs;
8594
8595 // initially hovering because pressure is 0
8596 processId(mapper, 1);
8597 processPosition(mapper, 100, 200);
8598 processPressure(mapper, 0);
8599 processSync(mapper);
8600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8601 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8603 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8604
8605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8606 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8608 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8609
8610 // move a little
8611 processPosition(mapper, 150, 250);
8612 processSync(mapper);
8613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8614 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8616 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8617
8618 // down when pressure becomes non-zero
8619 processPressure(mapper, RAW_PRESSURE_MAX);
8620 processSync(mapper);
8621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8622 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8624 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8625
8626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8627 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8629 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8630
8631 // up when pressure becomes 0, hover restored
8632 processPressure(mapper, 0);
8633 processSync(mapper);
8634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8635 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8637 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8638
8639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8640 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8642 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8643
8644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8645 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8647 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8648
8649 // exit hover when pointer goes away
8650 processId(mapper, -1);
8651 processSync(mapper);
8652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8653 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8655 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8656}
8657
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008658/**
8659 * Set the input device port <--> display port associations, and check that the
8660 * events are routed to the display that matches the display port.
8661 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8662 */
8663TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008664 const std::string usb2 = "USB2";
8665 const uint8_t hdmi1 = 0;
8666 const uint8_t hdmi2 = 1;
8667 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008668 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008669
8670 addConfigurationProperty("touch.deviceType", "touchScreen");
8671 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008672 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008673
8674 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8675 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8676
8677 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8678 // for this input device is specified, and the matching viewport is not present,
8679 // the input device should be disabled (at the mapper level).
8680
8681 // Add viewport for display 2 on hdmi2
8682 prepareSecondaryDisplay(type, hdmi2);
8683 // Send a touch event
8684 processPosition(mapper, 100, 100);
8685 processSync(mapper);
8686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8687
8688 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008689 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008690 // Send a touch event again
8691 processPosition(mapper, 100, 100);
8692 processSync(mapper);
8693
8694 NotifyMotionArgs args;
8695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8696 ASSERT_EQ(DISPLAY_ID, args.displayId);
8697}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008698
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008699TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8700 addConfigurationProperty("touch.deviceType", "touchScreen");
8701 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008702 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008703
8704 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8705
Michael Wrighta9cf4192022-12-01 23:46:39 +00008706 prepareDisplay(ui::ROTATION_0);
8707 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008708
8709 // Send a touch event
8710 processPosition(mapper, 100, 100);
8711 processSync(mapper);
8712
8713 NotifyMotionArgs args;
8714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8715 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8716}
8717
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008718TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008719 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008720
Michael Wrighta9cf4192022-12-01 23:46:39 +00008721 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008722 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008723 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008724
Josep del Río2d8c79a2023-01-23 19:33:50 +00008725 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008726
8727 NotifyMotionArgs motionArgs;
8728 processPosition(mapper, 100, 100);
8729 processSync(mapper);
8730
8731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8732 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008733 ASSERT_EQ(ui::LogicalDisplayId::INVALID, motionArgs.displayId);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008734}
8735
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008736/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008737 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8738 */
8739TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8740 addConfigurationProperty("touch.deviceType", "touchScreen");
8741 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008742 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008743
Michael Wrighta9cf4192022-12-01 23:46:39 +00008744 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00008745 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
8746 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
8747 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
8748 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008749
8750 NotifyMotionArgs args;
8751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8752 ASSERT_EQ(26, args.readTime);
8753
Harry Cutts33476232023-01-30 19:57:29 +00008754 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
8755 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
8756 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008757
8758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8759 ASSERT_EQ(33, args.readTime);
8760}
8761
8762/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008763 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8764 * events should not be delivered to the listener.
8765 */
8766TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8767 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008768 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008769 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008770 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008771 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008772 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008773 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008774
8775 NotifyMotionArgs motionArgs;
8776 processPosition(mapper, 100, 100);
8777 processSync(mapper);
8778
8779 mFakeListener->assertNotifyMotionWasNotCalled();
8780}
8781
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008782/**
8783 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8784 * the touch mapper can process the events and the events can be delivered to the listener.
8785 */
8786TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8787 addConfigurationProperty("touch.deviceType", "touchScreen");
8788 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008789 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008790 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008791 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008792 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008793 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008794
8795 NotifyMotionArgs motionArgs;
8796 processPosition(mapper, 100, 100);
8797 processSync(mapper);
8798
8799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8800 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8801}
8802
Josh Thielene986aed2023-06-01 14:17:30 +00008803/**
8804 * When the viewport is deactivated (isActive transitions from true to false),
8805 * and touch.enableForInactiveViewport is false, touches prior to the transition
8806 * should be cancelled.
8807 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08008808TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8809 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008810 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008811 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008812 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008813 std::optional<DisplayViewport> optionalDisplayViewport =
8814 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8815 ASSERT_TRUE(optionalDisplayViewport.has_value());
8816 DisplayViewport displayViewport = *optionalDisplayViewport;
8817
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008818 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008819 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008820 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08008821
8822 // Finger down
8823 int32_t x = 100, y = 100;
8824 processPosition(mapper, x, y);
8825 processSync(mapper);
8826
8827 NotifyMotionArgs motionArgs;
8828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8829 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8830
8831 // Deactivate display viewport
8832 displayViewport.isActive = false;
8833 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008834 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008835
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008836 // The ongoing touch should be canceled immediately
8837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8838 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8839
8840 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008841 x += 10, y += 10;
8842 processPosition(mapper, x, y);
8843 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008845
8846 // Reactivate display viewport
8847 displayViewport.isActive = true;
8848 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008849 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008850
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008851 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008852 x += 10, y += 10;
8853 processPosition(mapper, x, y);
8854 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8856 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008857}
8858
Josh Thielene986aed2023-06-01 14:17:30 +00008859/**
8860 * When the viewport is deactivated (isActive transitions from true to false),
8861 * and touch.enableForInactiveViewport is true, touches prior to the transition
8862 * should not be cancelled.
8863 */
8864TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
8865 addConfigurationProperty("touch.deviceType", "touchScreen");
8866 addConfigurationProperty("touch.enableForInactiveViewport", "1");
8867 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8868 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
8869 std::optional<DisplayViewport> optionalDisplayViewport =
8870 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8871 ASSERT_TRUE(optionalDisplayViewport.has_value());
8872 DisplayViewport displayViewport = *optionalDisplayViewport;
8873
8874 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8875 prepareAxes(POSITION);
8876 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
8877
8878 // Finger down
8879 int32_t x = 100, y = 100;
8880 processPosition(mapper, x, y);
8881 processSync(mapper);
8882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8883 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
8884
8885 // Deactivate display viewport
8886 displayViewport.isActive = false;
8887 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8888 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8889
8890 // The ongoing touch should not be canceled
8891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8892
8893 // Finger move is not ignored
8894 x += 10, y += 10;
8895 processPosition(mapper, x, y);
8896 processSync(mapper);
8897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8898 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8899
8900 // Reactivate display viewport
8901 displayViewport.isActive = true;
8902 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8903 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8904
8905 // Finger move continues and does not start new gesture
8906 x += 10, y += 10;
8907 processPosition(mapper, x, y);
8908 processSync(mapper);
8909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8910 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8911}
8912
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008913TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008914 prepareAxes(POSITION);
8915 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008916 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008917 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008918
8919 NotifyMotionArgs motionArgs;
8920 // Unrotated video frame
8921 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8922 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008923 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008924 processPosition(mapper, 100, 200);
8925 processSync(mapper);
8926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8927 ASSERT_EQ(frames, motionArgs.videoFrames);
8928
8929 // Subsequent touch events should not have any videoframes
8930 // This is implemented separately in FakeEventHub,
8931 // but that should match the behaviour of TouchVideoDevice.
8932 processPosition(mapper, 200, 200);
8933 processSync(mapper);
8934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8935 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8936}
8937
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008938TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008939 prepareAxes(POSITION);
8940 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008941 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008942 // Unrotated video frame
8943 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8944 NotifyMotionArgs motionArgs;
8945
8946 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008947 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008948 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008949 clearViewports();
8950 prepareDisplay(orientation);
8951 std::vector<TouchVideoFrame> frames{frame};
8952 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8953 processPosition(mapper, 100, 200);
8954 processSync(mapper);
8955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8956 ASSERT_EQ(frames, motionArgs.videoFrames);
8957 }
8958}
8959
8960TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8961 prepareAxes(POSITION);
8962 addConfigurationProperty("touch.deviceType", "touchScreen");
8963 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8964 // orientation-aware are affected by display rotation.
8965 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00008966 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008967 // Unrotated video frame
8968 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8969 NotifyMotionArgs motionArgs;
8970
8971 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008972 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008973 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008974 clearViewports();
8975 prepareDisplay(orientation);
8976 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008977 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008978 processPosition(mapper, 100, 200);
8979 processSync(mapper);
8980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008981 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8982 // compared to the display. This is so that when the window transform (which contains the
8983 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8984 // window's coordinate space.
8985 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008986 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008987
8988 // Release finger.
8989 processSync(mapper);
8990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008991 }
8992}
8993
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008994TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008995 prepareAxes(POSITION);
8996 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008997 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008998 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8999 // so mix these.
9000 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9001 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9002 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9003 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9004 NotifyMotionArgs motionArgs;
9005
Michael Wrighta9cf4192022-12-01 23:46:39 +00009006 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009007 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009008 processPosition(mapper, 100, 200);
9009 processSync(mapper);
9010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009011 ASSERT_EQ(frames, motionArgs.videoFrames);
9012}
9013
9014TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9015 prepareAxes(POSITION);
9016 addConfigurationProperty("touch.deviceType", "touchScreen");
9017 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9018 // orientation-aware are affected by display rotation.
9019 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009020 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009021 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9022 // so mix these.
9023 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9024 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9025 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9026 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9027 NotifyMotionArgs motionArgs;
9028
Michael Wrighta9cf4192022-12-01 23:46:39 +00009029 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009030 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9031 processPosition(mapper, 100, 200);
9032 processSync(mapper);
9033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9034 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9035 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9036 // compared to the display. This is so that when the window transform (which contains the
9037 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9038 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009039 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009040 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009041 ASSERT_EQ(frames, motionArgs.videoFrames);
9042}
9043
Arthur Hung9da14732019-09-02 16:16:58 +08009044/**
9045 * If we had defined port associations, but the viewport is not ready, the touch device would be
9046 * expected to be disabled, and it should be enabled after the viewport has found.
9047 */
9048TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009049 constexpr uint8_t hdmi2 = 1;
9050 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009051 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009052
9053 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9054
9055 addConfigurationProperty("touch.deviceType", "touchScreen");
9056 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009057 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009058
9059 ASSERT_EQ(mDevice->isEnabled(), false);
9060
9061 // Add display on hdmi2, the device should be enabled and can receive touch event.
9062 prepareSecondaryDisplay(type, hdmi2);
9063 ASSERT_EQ(mDevice->isEnabled(), true);
9064
9065 // Send a touch event.
9066 processPosition(mapper, 100, 100);
9067 processSync(mapper);
9068
9069 NotifyMotionArgs args;
9070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9071 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9072}
9073
Arthur Hung421eb1c2020-01-16 00:09:42 +08009074TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009075 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009076 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009077 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009078 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009079
9080 NotifyMotionArgs motionArgs;
9081
9082 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9083 // finger down
9084 processId(mapper, 1);
9085 processPosition(mapper, x1, y1);
9086 processSync(mapper);
9087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9088 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009089 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009090
9091 // finger move
9092 processId(mapper, 1);
9093 processPosition(mapper, x2, y2);
9094 processSync(mapper);
9095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009097 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009098
9099 // finger up.
9100 processId(mapper, -1);
9101 processSync(mapper);
9102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9103 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009104 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009105
9106 // new finger down
9107 processId(mapper, 1);
9108 processPosition(mapper, x3, y3);
9109 processSync(mapper);
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9111 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009112 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009113}
9114
9115/**
arthurhungcc7f9802020-04-30 17:55:40 +08009116 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9117 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009118 */
arthurhungcc7f9802020-04-30 17:55:40 +08009119TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009120 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009121 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009122 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009123 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009124
9125 NotifyMotionArgs motionArgs;
9126
9127 // default tool type is finger
9128 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009129 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009130 processPosition(mapper, x1, y1);
9131 processSync(mapper);
9132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9133 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009134 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009135
9136 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9137 processToolType(mapper, MT_TOOL_PALM);
9138 processSync(mapper);
9139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9140 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9141
9142 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009143 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009144 processPosition(mapper, x2, y2);
9145 processSync(mapper);
9146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9147
9148 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009149 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009150 processSync(mapper);
9151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9152
9153 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009154 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009155 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009156 processPosition(mapper, x3, y3);
9157 processSync(mapper);
9158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9159 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009160 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009161}
9162
arthurhungbf89a482020-04-17 17:37:55 +08009163/**
arthurhungcc7f9802020-04-30 17:55:40 +08009164 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9165 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009166 */
arthurhungcc7f9802020-04-30 17:55:40 +08009167TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009168 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009169 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009170 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009171 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009172
9173 NotifyMotionArgs motionArgs;
9174
9175 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009176 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9177 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009178 processPosition(mapper, x1, y1);
9179 processSync(mapper);
9180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9181 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009182 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009183
9184 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009185 processSlot(mapper, SECOND_SLOT);
9186 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009187 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009188 processSync(mapper);
9189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009190 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009191 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009192
9193 // If the tool type of the first finger changes to MT_TOOL_PALM,
9194 // we expect to receive ACTION_POINTER_UP with cancel flag.
9195 processSlot(mapper, FIRST_SLOT);
9196 processId(mapper, FIRST_TRACKING_ID);
9197 processToolType(mapper, MT_TOOL_PALM);
9198 processSync(mapper);
9199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009200 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009201 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9202
9203 // The following MOVE events of second finger should be processed.
9204 processSlot(mapper, SECOND_SLOT);
9205 processId(mapper, SECOND_TRACKING_ID);
9206 processPosition(mapper, x2 + 1, y2 + 1);
9207 processSync(mapper);
9208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009210 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009211
9212 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9213 // it. Second finger receive move.
9214 processSlot(mapper, FIRST_SLOT);
9215 processId(mapper, INVALID_TRACKING_ID);
9216 processSync(mapper);
9217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009219 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009220
9221 // Second finger keeps moving.
9222 processSlot(mapper, SECOND_SLOT);
9223 processId(mapper, SECOND_TRACKING_ID);
9224 processPosition(mapper, x2 + 2, y2 + 2);
9225 processSync(mapper);
9226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009228 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009229
9230 // Second finger up.
9231 processId(mapper, INVALID_TRACKING_ID);
9232 processSync(mapper);
9233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9234 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9235 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9236}
9237
9238/**
9239 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9240 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9241 */
9242TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9243 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009244 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009245 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009246 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009247
9248 NotifyMotionArgs motionArgs;
9249
9250 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9251 // First finger down.
9252 processId(mapper, FIRST_TRACKING_ID);
9253 processPosition(mapper, x1, y1);
9254 processSync(mapper);
9255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9256 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009257 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009258
9259 // Second finger down.
9260 processSlot(mapper, SECOND_SLOT);
9261 processId(mapper, SECOND_TRACKING_ID);
9262 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009263 processSync(mapper);
9264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009265 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009266 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009267
arthurhungcc7f9802020-04-30 17:55:40 +08009268 // If the tool type of the first finger changes to MT_TOOL_PALM,
9269 // we expect to receive ACTION_POINTER_UP with cancel flag.
9270 processSlot(mapper, FIRST_SLOT);
9271 processId(mapper, FIRST_TRACKING_ID);
9272 processToolType(mapper, MT_TOOL_PALM);
9273 processSync(mapper);
9274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009275 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009276 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9277
9278 // Second finger keeps moving.
9279 processSlot(mapper, SECOND_SLOT);
9280 processId(mapper, SECOND_TRACKING_ID);
9281 processPosition(mapper, x2 + 1, y2 + 1);
9282 processSync(mapper);
9283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9285
9286 // second finger becomes palm, receive cancel due to only 1 finger is active.
9287 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009288 processToolType(mapper, MT_TOOL_PALM);
9289 processSync(mapper);
9290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9291 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9292
arthurhungcc7f9802020-04-30 17:55:40 +08009293 // third finger down.
9294 processSlot(mapper, THIRD_SLOT);
9295 processId(mapper, THIRD_TRACKING_ID);
9296 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009297 processPosition(mapper, x3, y3);
9298 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9300 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009301 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009302 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009303
9304 // third finger move
9305 processId(mapper, THIRD_TRACKING_ID);
9306 processPosition(mapper, x3 + 1, y3 + 1);
9307 processSync(mapper);
9308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9310
9311 // first finger up, third finger receive move.
9312 processSlot(mapper, FIRST_SLOT);
9313 processId(mapper, INVALID_TRACKING_ID);
9314 processSync(mapper);
9315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009317 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009318
9319 // second finger up, third finger receive move.
9320 processSlot(mapper, SECOND_SLOT);
9321 processId(mapper, INVALID_TRACKING_ID);
9322 processSync(mapper);
9323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009325 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009326
9327 // third finger up.
9328 processSlot(mapper, THIRD_SLOT);
9329 processId(mapper, INVALID_TRACKING_ID);
9330 processSync(mapper);
9331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9332 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9333 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9334}
9335
9336/**
9337 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9338 * and the active finger could still be allowed to receive the events
9339 */
9340TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9341 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009342 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009343 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009344 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009345
9346 NotifyMotionArgs motionArgs;
9347
9348 // default tool type is finger
9349 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9350 processId(mapper, FIRST_TRACKING_ID);
9351 processPosition(mapper, x1, y1);
9352 processSync(mapper);
9353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9354 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009355 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009356
9357 // Second finger down.
9358 processSlot(mapper, SECOND_SLOT);
9359 processId(mapper, SECOND_TRACKING_ID);
9360 processPosition(mapper, x2, y2);
9361 processSync(mapper);
9362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009363 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009364 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009365
9366 // If the tool type of the second finger changes to MT_TOOL_PALM,
9367 // we expect to receive ACTION_POINTER_UP with cancel flag.
9368 processId(mapper, SECOND_TRACKING_ID);
9369 processToolType(mapper, MT_TOOL_PALM);
9370 processSync(mapper);
9371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009372 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009373 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9374
9375 // The following MOVE event should be processed.
9376 processSlot(mapper, FIRST_SLOT);
9377 processId(mapper, FIRST_TRACKING_ID);
9378 processPosition(mapper, x1 + 1, y1 + 1);
9379 processSync(mapper);
9380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009382 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009383
9384 // second finger up.
9385 processSlot(mapper, SECOND_SLOT);
9386 processId(mapper, INVALID_TRACKING_ID);
9387 processSync(mapper);
9388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9390
9391 // first finger keep moving
9392 processSlot(mapper, FIRST_SLOT);
9393 processId(mapper, FIRST_TRACKING_ID);
9394 processPosition(mapper, x1 + 2, y1 + 2);
9395 processSync(mapper);
9396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9398
9399 // first finger up.
9400 processId(mapper, INVALID_TRACKING_ID);
9401 processSync(mapper);
9402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9403 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9404 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009405}
9406
Arthur Hung9ad18942021-06-19 02:04:46 +00009407/**
9408 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9409 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9410 * cause slot be valid again.
9411 */
9412TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9413 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009414 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009415 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009416 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +00009417
9418 NotifyMotionArgs motionArgs;
9419
9420 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9421 // First finger down.
9422 processId(mapper, FIRST_TRACKING_ID);
9423 processPosition(mapper, x1, y1);
9424 processPressure(mapper, RAW_PRESSURE_MAX);
9425 processSync(mapper);
9426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9427 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009428 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009429
9430 // First finger move.
9431 processId(mapper, FIRST_TRACKING_ID);
9432 processPosition(mapper, x1 + 1, y1 + 1);
9433 processPressure(mapper, RAW_PRESSURE_MAX);
9434 processSync(mapper);
9435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009437 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009438
9439 // Second finger down.
9440 processSlot(mapper, SECOND_SLOT);
9441 processId(mapper, SECOND_TRACKING_ID);
9442 processPosition(mapper, x2, y2);
9443 processPressure(mapper, RAW_PRESSURE_MAX);
9444 processSync(mapper);
9445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009446 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009447 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009448
9449 // second finger up with some unexpected data.
9450 processSlot(mapper, SECOND_SLOT);
9451 processId(mapper, INVALID_TRACKING_ID);
9452 processPosition(mapper, x2, y2);
9453 processSync(mapper);
9454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009455 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009456 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009457
9458 // first finger up with some unexpected data.
9459 processSlot(mapper, FIRST_SLOT);
9460 processId(mapper, INVALID_TRACKING_ID);
9461 processPosition(mapper, x2, y2);
9462 processPressure(mapper, RAW_PRESSURE_MAX);
9463 processSync(mapper);
9464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9465 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009466 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009467}
9468
Arpit Singh4b4a4572023-11-24 18:19:56 +00009469TEST_F(MultiTouchInputMapperTest, Reset_RepopulatesMultiTouchState) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009470 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009471 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009472 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009473 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009474
9475 // First finger down.
Arpit Singh4b4a4572023-11-24 18:19:56 +00009476 constexpr int32_t x1 = 100, y1 = 200, x2 = 300, y2 = 400;
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009477 processId(mapper, FIRST_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009478 processPosition(mapper, x1, y1);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009479 processPressure(mapper, RAW_PRESSURE_MAX);
9480 processSync(mapper);
9481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9482 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9483
9484 // Second finger down.
9485 processSlot(mapper, SECOND_SLOT);
9486 processId(mapper, SECOND_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009487 processPosition(mapper, x2, y2);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009488 processPressure(mapper, RAW_PRESSURE_MAX);
9489 processSync(mapper);
9490 ASSERT_NO_FATAL_FAILURE(
9491 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9492
Arpit Singh4b4a4572023-11-24 18:19:56 +00009493 // Set MT Slot state to be repopulated for the required slots
9494 std::vector<int32_t> mtSlotValues(RAW_SLOT_MAX + 1, -1);
9495 mtSlotValues[0] = FIRST_TRACKING_ID;
9496 mtSlotValues[1] = SECOND_TRACKING_ID;
9497 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_TRACKING_ID, mtSlotValues);
9498
9499 mtSlotValues[0] = x1;
9500 mtSlotValues[1] = x2;
9501 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_X, mtSlotValues);
9502
9503 mtSlotValues[0] = y1;
9504 mtSlotValues[1] = y2;
9505 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_Y, mtSlotValues);
9506
9507 mtSlotValues[0] = RAW_PRESSURE_MAX;
9508 mtSlotValues[1] = RAW_PRESSURE_MAX;
9509 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_PRESSURE, mtSlotValues);
9510
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009511 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Arpit Singh4b4a4572023-11-24 18:19:56 +00009512 // repopulated. Resetting should cancel the ongoing gesture.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009513 resetMapper(mapper, ARBITRARY_TIME);
9514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9515 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009516
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009517 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9518 // the existing touch state to generate a down event.
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009519 processPosition(mapper, 301, 302);
9520 processSync(mapper);
9521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9522 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9524 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009525
9526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9527}
9528
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009529TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009530 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009531 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009532 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009533 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009534
9535 // First finger touches down and releases.
9536 processId(mapper, FIRST_TRACKING_ID);
9537 processPosition(mapper, 100, 200);
9538 processPressure(mapper, RAW_PRESSURE_MAX);
9539 processSync(mapper);
9540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9541 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9542 processId(mapper, INVALID_TRACKING_ID);
9543 processSync(mapper);
9544 ASSERT_NO_FATAL_FAILURE(
9545 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9546
9547 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9548 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009549 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9551
9552 // Send an empty sync frame. Since there are no pointers, no events are generated.
9553 processSync(mapper);
9554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9555}
9556
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009557TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009558 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009559 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009560 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009561 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009563
9564 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9565 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9566 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9567 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9568 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9569
9570 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009571 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009572 processId(mapper, FIRST_TRACKING_ID);
9573 processToolType(mapper, MT_TOOL_PEN);
9574 processPosition(mapper, 100, 200);
9575 processPressure(mapper, RAW_PRESSURE_MAX);
9576 processSync(mapper);
9577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9578 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9579 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009580 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009581
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009582 // Now that we know the device supports styluses, ensure that the device is re-configured with
9583 // the stylus source.
9584 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9585 {
9586 const auto& devices = mReader->getInputDevices();
9587 auto deviceInfo =
9588 std::find_if(devices.begin(), devices.end(),
9589 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9590 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9591 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9592 }
9593
9594 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9596
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009597 processId(mapper, INVALID_TRACKING_ID);
9598 processSync(mapper);
9599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9600 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9601 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009602 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009603}
9604
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009605// --- MultiTouchInputMapperTest_ExternalDevice ---
9606
9607class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9608protected:
Chris Yea52ade12020-08-27 16:49:20 -07009609 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009610};
9611
9612/**
9613 * Expect fallback to internal viewport if device is external and external viewport is not present.
9614 */
9615TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9616 prepareAxes(POSITION);
9617 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009618 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009619 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009620
9621 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9622
9623 NotifyMotionArgs motionArgs;
9624
9625 // Expect the event to be sent to the internal viewport,
9626 // because an external viewport is not present.
9627 processPosition(mapper, 100, 100);
9628 processSync(mapper);
9629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009630 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, motionArgs.displayId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009631
9632 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009633 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009634 processPosition(mapper, 100, 100);
9635 processSync(mapper);
9636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9637 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9638}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009639
Prabir Pradhan3ed7e352024-05-03 23:59:43 +00009640// TODO(b/281840344): Remove the test when the old touchpad stack is removed. It is currently
9641// unclear what the behavior of the touchpad logic in TouchInputMapper should do after the
9642// PointerChoreographer refactor.
9643TEST_F(MultiTouchInputMapperTest, DISABLED_Process_TouchpadPointer) {
Harry Cutts8722be92024-04-05 14:46:05 +00009644 // prepare device
Michael Wrighta9cf4192022-12-01 23:46:39 +00009645 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009646 prepareAxes(POSITION | ID | SLOT);
9647 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9648 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009649 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009650 // run uncaptured pointer tests - pushes out generic events
9651 // FINGER 0 DOWN
9652 processId(mapper, 3);
9653 processPosition(mapper, 100, 100);
9654 processKey(mapper, BTN_TOUCH, 1);
9655 processSync(mapper);
9656
9657 // start at (100,100), cursor should be at (0,0) * scale
9658 NotifyMotionArgs args;
9659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9660 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9661 ASSERT_NO_FATAL_FAILURE(
9662 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9663
9664 // FINGER 0 MOVE
9665 processPosition(mapper, 200, 200);
9666 processSync(mapper);
9667
9668 // compute scaling to help with touch position checking
9669 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9670 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9671 float scale =
9672 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9673
9674 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9676 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9678 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +08009679
9680 // BUTTON DOWN
9681 processKey(mapper, BTN_LEFT, 1);
9682 processSync(mapper);
9683
9684 // touchinputmapper design sends a move before button press
9685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9686 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9688 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9689
9690 // BUTTON UP
9691 processKey(mapper, BTN_LEFT, 0);
9692 processSync(mapper);
9693
9694 // touchinputmapper design sends a move after button release
9695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9696 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9698 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009699}
9700
Harry Cutts8722be92024-04-05 14:46:05 +00009701TEST_F(MultiTouchInputMapperTest, Touchpad_GetSources) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00009702 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009703 prepareAxes(POSITION | ID | SLOT);
9704 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Hiroki Sato25040232024-02-22 17:21:22 +09009705 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Arpit Singha8c236b2023-04-25 13:56:05 +00009706 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009707
Josep del Río2d8c79a2023-01-23 19:33:50 +00009708 // uncaptured touchpad should be a pointer device
9709 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009710}
9711
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009712// --- BluetoothMultiTouchInputMapperTest ---
9713
9714class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9715protected:
9716 void SetUp() override {
9717 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9718 }
9719};
9720
9721TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9722 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009723 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009724 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009725 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009726
9727 nsecs_t kernelEventTime = ARBITRARY_TIME;
9728 nsecs_t expectedEventTime = ARBITRARY_TIME;
9729 // Touch down.
9730 processId(mapper, FIRST_TRACKING_ID);
9731 processPosition(mapper, 100, 200);
9732 processPressure(mapper, RAW_PRESSURE_MAX);
9733 processSync(mapper, ARBITRARY_TIME);
9734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9735 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9736
9737 // Process several events that come in quick succession, according to their timestamps.
9738 for (int i = 0; i < 3; i++) {
9739 constexpr static nsecs_t delta = ms2ns(1);
9740 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9741 kernelEventTime += delta;
9742 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
9743
9744 processPosition(mapper, 101 + i, 201 + i);
9745 processSync(mapper, kernelEventTime);
9746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9747 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9748 WithEventTime(expectedEventTime))));
9749 }
9750
9751 // Release the touch.
9752 processId(mapper, INVALID_TRACKING_ID);
9753 processPressure(mapper, RAW_PRESSURE_MIN);
9754 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
9755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9756 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9757 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
9758}
9759
9760// --- MultiTouchPointerModeTest ---
9761
HQ Liue6983c72022-04-19 22:14:56 +00009762class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9763protected:
9764 float mPointerMovementScale;
9765 float mPointerXZoomScale;
9766 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9767 addConfigurationProperty("touch.deviceType", "pointer");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009768 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +00009769
9770 prepareAxes(POSITION);
9771 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9772 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9773 // needs to be disabled, and the pointer gesture needs to be enabled.
Hiroki Sato25040232024-02-22 17:21:22 +09009774 mFakePolicy->setPointerCapture(/*window=*/nullptr);
HQ Liue6983c72022-04-19 22:14:56 +00009775 mFakePolicy->setPointerGestureEnabled(true);
HQ Liue6983c72022-04-19 22:14:56 +00009776
9777 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9778 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9779 mPointerMovementScale =
9780 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9781 mPointerXZoomScale =
9782 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9783 }
9784
9785 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9786 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9787 /*flat*/ 0,
9788 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9789 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9790 /*flat*/ 0,
9791 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9792 }
9793};
9794
9795/**
9796 * Two fingers down on a pointer mode touch pad. The width
9797 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9798 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9799 * be greater than the both value to be freeform gesture, so that after two
9800 * fingers start to move downwards, the gesture should be swipe.
9801 */
9802TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9803 // The min freeform gesture width is 25units/mm x 30mm = 750
9804 // which is greater than fraction of the diagnal length of the touchpad (349).
9805 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +00009806 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009807 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009808 NotifyMotionArgs motionArgs;
9809
9810 // Two fingers down at once.
9811 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9812 // Pointer's initial position is used the [0,0] coordinate.
9813 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9814
9815 processId(mapper, FIRST_TRACKING_ID);
9816 processPosition(mapper, x1, y1);
9817 processMTSync(mapper);
9818 processId(mapper, SECOND_TRACKING_ID);
9819 processPosition(mapper, x2, y2);
9820 processMTSync(mapper);
9821 processSync(mapper);
9822
9823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009824 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009825 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009826 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009827 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009828 ASSERT_NO_FATAL_FAILURE(
9829 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9830
9831 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9832 // that there should be 1 pointer.
9833 int32_t movingDistance = 200;
9834 y1 += movingDistance;
9835 y2 += movingDistance;
9836
9837 processId(mapper, FIRST_TRACKING_ID);
9838 processPosition(mapper, x1, y1);
9839 processMTSync(mapper);
9840 processId(mapper, SECOND_TRACKING_ID);
9841 processPosition(mapper, x2, y2);
9842 processMTSync(mapper);
9843 processSync(mapper);
9844
9845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009846 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009848 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009849 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9851 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9852 0, 0, 0, 0));
9853}
9854
9855/**
9856 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9857 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9858 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9859 * value to be freeform gesture, so that after two fingers start to move downwards,
9860 * the gesture should be swipe.
9861 */
9862TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9863 // The min freeform gesture width is 5units/mm x 30mm = 150
9864 // which is greater than fraction of the diagnal length of the touchpad (349).
9865 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +00009866 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +00009867 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009868 NotifyMotionArgs motionArgs;
9869
9870 // Two fingers down at once.
9871 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9872 // Pointer's initial position is used the [0,0] coordinate.
9873 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9874
9875 processId(mapper, FIRST_TRACKING_ID);
9876 processPosition(mapper, x1, y1);
9877 processMTSync(mapper);
9878 processId(mapper, SECOND_TRACKING_ID);
9879 processPosition(mapper, x2, y2);
9880 processMTSync(mapper);
9881 processSync(mapper);
9882
9883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009884 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009885 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009886 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009887 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009888 ASSERT_NO_FATAL_FAILURE(
9889 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9890
9891 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9892 // and there should be 1 pointer.
9893 int32_t movingDistance = 200;
9894 y1 += movingDistance;
9895 y2 += movingDistance;
9896
9897 processId(mapper, FIRST_TRACKING_ID);
9898 processPosition(mapper, x1, y1);
9899 processMTSync(mapper);
9900 processId(mapper, SECOND_TRACKING_ID);
9901 processPosition(mapper, x2, y2);
9902 processMTSync(mapper);
9903 processSync(mapper);
9904
9905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009906 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009908 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009909 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009910 // New coordinate is the scaled relative coordinate from the initial coordinate.
9911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9912 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9913 0, 0, 0, 0));
9914}
9915
9916/**
9917 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9918 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9919 * freeform gestures after two fingers start to move downwards.
9920 */
9921TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +00009922 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009923 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009924
9925 NotifyMotionArgs motionArgs;
9926
9927 // Two fingers down at once. Wider than the max swipe width.
9928 // The gesture is expected to be PRESS, then transformed to FREEFORM
9929 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9930
9931 processId(mapper, FIRST_TRACKING_ID);
9932 processPosition(mapper, x1, y1);
9933 processMTSync(mapper);
9934 processId(mapper, SECOND_TRACKING_ID);
9935 processPosition(mapper, x2, y2);
9936 processMTSync(mapper);
9937 processSync(mapper);
9938
9939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009940 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009941 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009942 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009943 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009944 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9945 ASSERT_NO_FATAL_FAILURE(
9946 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9947
9948 int32_t movingDistance = 200;
9949
9950 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9951 // then two down events for two pointers.
9952 y1 += movingDistance;
9953 y2 += movingDistance;
9954
9955 processId(mapper, FIRST_TRACKING_ID);
9956 processPosition(mapper, x1, y1);
9957 processMTSync(mapper);
9958 processId(mapper, SECOND_TRACKING_ID);
9959 processPosition(mapper, x2, y2);
9960 processMTSync(mapper);
9961 processSync(mapper);
9962
9963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9964 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009965 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009966 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009968 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009969 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009970 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009972 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009973 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009974 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009975 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009976 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009977 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009978 // Two pointers' scaled relative coordinates from their initial centroid.
9979 // Initial y coordinates are 0 as y1 and y2 have the same value.
9980 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9981 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9982 // When pointers move, the new coordinates equal to the initial coordinates plus
9983 // scaled moving distance.
9984 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9985 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9986 0, 0, 0, 0));
9987 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9988 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9989 0, 0, 0, 0));
9990
9991 // Move two fingers down again, expect one MOVE motion event.
9992 y1 += movingDistance;
9993 y2 += movingDistance;
9994
9995 processId(mapper, FIRST_TRACKING_ID);
9996 processPosition(mapper, x1, y1);
9997 processMTSync(mapper);
9998 processId(mapper, SECOND_TRACKING_ID);
9999 processPosition(mapper, x2, y2);
10000 processMTSync(mapper);
10001 processSync(mapper);
10002
10003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010004 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010006 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010007 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10009 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10010 0, 0, 0, 0, 0));
10011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10012 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10013 0, 0, 0, 0, 0));
10014}
10015
Harry Cutts39b7ca22022-10-05 15:55:48 +000010016TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010017 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010018 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010019 NotifyMotionArgs motionArgs;
10020
10021 // Place two fingers down.
10022 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10023
10024 processId(mapper, FIRST_TRACKING_ID);
10025 processPosition(mapper, x1, y1);
10026 processMTSync(mapper);
10027 processId(mapper, SECOND_TRACKING_ID);
10028 processPosition(mapper, x2, y2);
10029 processMTSync(mapper);
10030 processSync(mapper);
10031
10032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010033 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010034 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10035 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10036 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10037 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10038
10039 // Move the two fingers down and to the left.
10040 int32_t movingDistance = 200;
10041 x1 -= movingDistance;
10042 y1 += movingDistance;
10043 x2 -= movingDistance;
10044 y2 += movingDistance;
10045
10046 processId(mapper, FIRST_TRACKING_ID);
10047 processPosition(mapper, x1, y1);
10048 processMTSync(mapper);
10049 processId(mapper, SECOND_TRACKING_ID);
10050 processPosition(mapper, x2, y2);
10051 processMTSync(mapper);
10052 processSync(mapper);
10053
10054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010055 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10057 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10058 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10059 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10060}
10061
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010062TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010063 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010064 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010065 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10067
10068 // Start a stylus gesture.
10069 processKey(mapper, BTN_TOOL_PEN, 1);
10070 processId(mapper, FIRST_TRACKING_ID);
10071 processPosition(mapper, 100, 200);
10072 processSync(mapper);
10073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10074 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10075 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010076 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010077 // TODO(b/257078296): Pointer mode generates extra event.
10078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10079 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10080 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010081 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10083
10084 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10085 // gesture should be disabled.
10086 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10087 viewport->isActive = false;
10088 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010089 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10091 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10092 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010093 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010094 // TODO(b/257078296): Pointer mode generates extra event.
10095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10096 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10097 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010098 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10100}
10101
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010102// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010103
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010104class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010105protected:
10106 static const char* DEVICE_NAME;
10107 static const char* DEVICE_LOCATION;
10108 static const int32_t DEVICE_ID;
10109 static const int32_t DEVICE_GENERATION;
10110 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010111 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010112 static const int32_t EVENTHUB_ID;
10113
10114 std::shared_ptr<FakeEventHub> mFakeEventHub;
10115 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010116 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010117 std::unique_ptr<InstrumentedInputReader> mReader;
10118 std::shared_ptr<InputDevice> mDevice;
10119
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010120 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010121 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010122 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010123 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010124 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010125 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010126 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10127 }
10128
10129 void SetUp() override { SetUp(DEVICE_CLASSES); }
10130
10131 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010132 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010133 mFakePolicy.clear();
10134 }
10135
Chris Yee2b1e5c2021-03-10 22:45:12 -080010136 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10137 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010138 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010139 InputDeviceIdentifier identifier;
10140 identifier.name = name;
10141 identifier.location = location;
10142 std::shared_ptr<InputDevice> device =
10143 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10144 identifier);
10145 mReader->pushNextDevice(device);
10146 mFakeEventHub->addDevice(eventHubId, name, classes);
10147 mReader->loopOnce();
10148 return device;
10149 }
10150
10151 template <class T, typename... Args>
10152 T& addControllerAndConfigure(Args... args) {
10153 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10154
10155 return controller;
10156 }
10157};
10158
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010159const char* PeripheralControllerTest::DEVICE_NAME = "device";
10160const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10161const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10162const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10163const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010164const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10165 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010166const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010167
10168// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010169class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010170protected:
10171 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010172 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010173 }
10174};
10175
10176TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010177 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010178
Harry Cuttsa5b71292022-11-28 12:56:17 +000010179 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10180 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10181 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010182}
10183
10184TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010185 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010186
Harry Cuttsa5b71292022-11-28 12:56:17 +000010187 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10188 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10189 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010190}
10191
10192// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010193class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010194protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010195 void SetUp() override {
10196 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10197 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010198};
10199
Chris Ye85758332021-05-16 23:05:17 -070010200TEST_F(LightControllerTest, MonoLight) {
10201 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010202 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010203 .maxBrightness = 255,
10204 .flags = InputLightClass::BRIGHTNESS,
10205 .path = ""};
10206 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010207
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010208 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010209 InputDeviceInfo info;
10210 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010211 std::vector<InputDeviceLightInfo> lights = info.getLights();
10212 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010213 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10214 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10215
10216 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10217 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10218}
10219
DingYong99f2c3c2023-12-20 15:46:06 +080010220TEST_F(LightControllerTest, MonoKeyboardMuteLight) {
10221 RawLightInfo infoMono = {.id = 1,
10222 .name = "mono_keyboard_mute",
10223 .maxBrightness = 255,
10224 .flags = InputLightClass::BRIGHTNESS |
10225 InputLightClass::KEYBOARD_MIC_MUTE,
10226 .path = ""};
10227 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10228
10229 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10230 std::list<NotifyArgs> unused =
10231 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10232 /*changes=*/{});
10233
10234 InputDeviceInfo info;
10235 controller.populateDeviceInfo(&info);
10236 std::vector<InputDeviceLightInfo> lights = info.getLights();
10237 ASSERT_EQ(1U, lights.size());
10238 ASSERT_EQ(InputDeviceLightType::KEYBOARD_MIC_MUTE, lights[0].type);
10239 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10240}
10241
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010242TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10243 RawLightInfo infoMono = {.id = 1,
10244 .name = "mono_keyboard_backlight",
10245 .maxBrightness = 255,
10246 .flags = InputLightClass::BRIGHTNESS |
10247 InputLightClass::KEYBOARD_BACKLIGHT,
10248 .path = ""};
10249 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10250
10251 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10252 InputDeviceInfo info;
10253 controller.populateDeviceInfo(&info);
10254 std::vector<InputDeviceLightInfo> lights = info.getLights();
10255 ASSERT_EQ(1U, lights.size());
10256 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10257 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010258
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010259 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10260 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010261}
10262
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000010263TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
10264 RawLightInfo infoMono = {.id = 1,
10265 .name = "mono_light",
10266 .maxBrightness = 255,
10267 .flags = InputLightClass::BRIGHTNESS,
10268 .path = ""};
10269 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10270 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10271 "0,100,200");
10272
10273 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10274 std::list<NotifyArgs> unused =
10275 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10276 /*changes=*/{});
10277
10278 InputDeviceInfo info;
10279 controller.populateDeviceInfo(&info);
10280 std::vector<InputDeviceLightInfo> lights = info.getLights();
10281 ASSERT_EQ(1U, lights.size());
10282 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10283}
10284
10285TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
10286 RawLightInfo infoMono = {.id = 1,
10287 .name = "mono_keyboard_backlight",
10288 .maxBrightness = 255,
10289 .flags = InputLightClass::BRIGHTNESS |
10290 InputLightClass::KEYBOARD_BACKLIGHT,
10291 .path = ""};
10292 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10293
10294 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10295 std::list<NotifyArgs> unused =
10296 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10297 /*changes=*/{});
10298
10299 InputDeviceInfo info;
10300 controller.populateDeviceInfo(&info);
10301 std::vector<InputDeviceLightInfo> lights = info.getLights();
10302 ASSERT_EQ(1U, lights.size());
10303 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10304}
10305
10306TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
10307 RawLightInfo infoMono = {.id = 1,
10308 .name = "mono_keyboard_backlight",
10309 .maxBrightness = 255,
10310 .flags = InputLightClass::BRIGHTNESS |
10311 InputLightClass::KEYBOARD_BACKLIGHT,
10312 .path = ""};
10313 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10314 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10315 "0,100,200");
10316
10317 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10318 std::list<NotifyArgs> unused =
10319 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10320 /*changes=*/{});
10321
10322 InputDeviceInfo info;
10323 controller.populateDeviceInfo(&info);
10324 std::vector<InputDeviceLightInfo> lights = info.getLights();
10325 ASSERT_EQ(1U, lights.size());
10326 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
10327 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
10328 ASSERT_EQ(BrightnessLevel(0), *it);
10329 std::advance(it, 1);
10330 ASSERT_EQ(BrightnessLevel(100), *it);
10331 std::advance(it, 1);
10332 ASSERT_EQ(BrightnessLevel(200), *it);
10333}
10334
10335TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
10336 RawLightInfo infoMono = {.id = 1,
10337 .name = "mono_keyboard_backlight",
10338 .maxBrightness = 255,
10339 .flags = InputLightClass::BRIGHTNESS |
10340 InputLightClass::KEYBOARD_BACKLIGHT,
10341 .path = ""};
10342 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10343 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10344 "0,100,200,300,400,500");
10345
10346 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10347 std::list<NotifyArgs> unused =
10348 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10349 /*changes=*/{});
10350
10351 InputDeviceInfo info;
10352 controller.populateDeviceInfo(&info);
10353 std::vector<InputDeviceLightInfo> lights = info.getLights();
10354 ASSERT_EQ(1U, lights.size());
10355 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10356}
10357
Chris Yee2b1e5c2021-03-10 22:45:12 -080010358TEST_F(LightControllerTest, RGBLight) {
10359 RawLightInfo infoRed = {.id = 1,
10360 .name = "red",
10361 .maxBrightness = 255,
10362 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10363 .path = ""};
10364 RawLightInfo infoGreen = {.id = 2,
10365 .name = "green",
10366 .maxBrightness = 255,
10367 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10368 .path = ""};
10369 RawLightInfo infoBlue = {.id = 3,
10370 .name = "blue",
10371 .maxBrightness = 255,
10372 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10373 .path = ""};
10374 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10375 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10376 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10377
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010378 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010379 InputDeviceInfo info;
10380 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010381 std::vector<InputDeviceLightInfo> lights = info.getLights();
10382 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010383 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10384 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10385 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10386
10387 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10388 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10389}
10390
10391TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10392 RawLightInfo infoRed = {.id = 1,
10393 .name = "red_keyboard_backlight",
10394 .maxBrightness = 255,
10395 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10396 InputLightClass::KEYBOARD_BACKLIGHT,
10397 .path = ""};
10398 RawLightInfo infoGreen = {.id = 2,
10399 .name = "green_keyboard_backlight",
10400 .maxBrightness = 255,
10401 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10402 InputLightClass::KEYBOARD_BACKLIGHT,
10403 .path = ""};
10404 RawLightInfo infoBlue = {.id = 3,
10405 .name = "blue_keyboard_backlight",
10406 .maxBrightness = 255,
10407 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10408 InputLightClass::KEYBOARD_BACKLIGHT,
10409 .path = ""};
10410 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10411 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10412 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10413
10414 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10415 InputDeviceInfo info;
10416 controller.populateDeviceInfo(&info);
10417 std::vector<InputDeviceLightInfo> lights = info.getLights();
10418 ASSERT_EQ(1U, lights.size());
10419 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10420 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10421 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10422
10423 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10424 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10425}
10426
10427TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10428 RawLightInfo infoRed = {.id = 1,
10429 .name = "red",
10430 .maxBrightness = 255,
10431 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10432 .path = ""};
10433 RawLightInfo infoGreen = {.id = 2,
10434 .name = "green",
10435 .maxBrightness = 255,
10436 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10437 .path = ""};
10438 RawLightInfo infoBlue = {.id = 3,
10439 .name = "blue",
10440 .maxBrightness = 255,
10441 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10442 .path = ""};
10443 RawLightInfo infoGlobal = {.id = 3,
10444 .name = "global_keyboard_backlight",
10445 .maxBrightness = 255,
10446 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10447 InputLightClass::KEYBOARD_BACKLIGHT,
10448 .path = ""};
10449 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10450 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10451 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10452 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10453
10454 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10455 InputDeviceInfo info;
10456 controller.populateDeviceInfo(&info);
10457 std::vector<InputDeviceLightInfo> lights = info.getLights();
10458 ASSERT_EQ(1U, lights.size());
10459 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10460 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10461 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010462
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010463 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10464 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010465}
10466
10467TEST_F(LightControllerTest, MultiColorRGBLight) {
10468 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010469 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010470 .maxBrightness = 255,
10471 .flags = InputLightClass::BRIGHTNESS |
10472 InputLightClass::MULTI_INTENSITY |
10473 InputLightClass::MULTI_INDEX,
10474 .path = ""};
10475
10476 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10477
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010478 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010479 InputDeviceInfo info;
10480 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010481 std::vector<InputDeviceLightInfo> lights = info.getLights();
10482 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010483 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10484 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10485 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10486
10487 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10488 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10489}
10490
10491TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10492 RawLightInfo infoColor = {.id = 1,
10493 .name = "multi_color_keyboard_backlight",
10494 .maxBrightness = 255,
10495 .flags = InputLightClass::BRIGHTNESS |
10496 InputLightClass::MULTI_INTENSITY |
10497 InputLightClass::MULTI_INDEX |
10498 InputLightClass::KEYBOARD_BACKLIGHT,
10499 .path = ""};
10500
10501 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10502
10503 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10504 InputDeviceInfo info;
10505 controller.populateDeviceInfo(&info);
10506 std::vector<InputDeviceLightInfo> lights = info.getLights();
10507 ASSERT_EQ(1U, lights.size());
10508 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10509 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10510 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010511
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010512 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10513 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010514}
10515
Josep del Rioa1046a82023-08-24 19:57:27 +000010516TEST_F(LightControllerTest, SonyPlayerIdLight) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010517 RawLightInfo info1 = {.id = 1,
Josep del Rioa1046a82023-08-24 19:57:27 +000010518 .name = "sony1",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010519 .maxBrightness = 255,
10520 .flags = InputLightClass::BRIGHTNESS,
10521 .path = ""};
10522 RawLightInfo info2 = {.id = 2,
Josep del Rioa1046a82023-08-24 19:57:27 +000010523 .name = "sony2",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010524 .maxBrightness = 255,
10525 .flags = InputLightClass::BRIGHTNESS,
10526 .path = ""};
10527 RawLightInfo info3 = {.id = 3,
Josep del Rioa1046a82023-08-24 19:57:27 +000010528 .name = "sony3",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010529 .maxBrightness = 255,
10530 .flags = InputLightClass::BRIGHTNESS,
10531 .path = ""};
10532 RawLightInfo info4 = {.id = 4,
Josep del Rioa1046a82023-08-24 19:57:27 +000010533 .name = "sony4",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010534 .maxBrightness = 255,
10535 .flags = InputLightClass::BRIGHTNESS,
10536 .path = ""};
10537 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10538 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10539 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10540 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10541
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010542 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010543 InputDeviceInfo info;
10544 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010545 std::vector<InputDeviceLightInfo> lights = info.getLights();
10546 ASSERT_EQ(1U, lights.size());
Josep del Rioa1046a82023-08-24 19:57:27 +000010547 ASSERT_STREQ("sony", lights[0].name.c_str());
10548 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
10549 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10550 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10551
10552 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10553 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10554 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
10555 ASSERT_STREQ("sony", lights[0].name.c_str());
10556}
10557
10558TEST_F(LightControllerTest, PlayerIdLight) {
10559 RawLightInfo info1 = {.id = 1,
10560 .name = "player-1",
10561 .maxBrightness = 255,
10562 .flags = InputLightClass::BRIGHTNESS,
10563 .path = ""};
10564 RawLightInfo info2 = {.id = 2,
10565 .name = "player-2",
10566 .maxBrightness = 255,
10567 .flags = InputLightClass::BRIGHTNESS,
10568 .path = ""};
10569 RawLightInfo info3 = {.id = 3,
10570 .name = "player-3",
10571 .maxBrightness = 255,
10572 .flags = InputLightClass::BRIGHTNESS,
10573 .path = ""};
10574 RawLightInfo info4 = {.id = 4,
10575 .name = "player-4",
10576 .maxBrightness = 255,
10577 .flags = InputLightClass::BRIGHTNESS,
10578 .path = ""};
10579 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10580 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10581 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10582 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10583
10584 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10585 InputDeviceInfo info;
10586 controller.populateDeviceInfo(&info);
10587 std::vector<InputDeviceLightInfo> lights = info.getLights();
10588 ASSERT_EQ(1U, lights.size());
10589 ASSERT_STREQ("player", lights[0].name.c_str());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010590 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010591 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10592 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010593
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010594 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10595 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10596 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010597}
10598
Michael Wrightd02c5b62014-02-10 15:10:22 -080010599} // namespace android