blob: 7d157f61a162ddebea93013a013da151c654cbc0 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
Harry Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020
Prabir Pradhan2770d242019-09-02 18:07:11 -070021#include <CursorInputMapper.h>
22#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080025#include <InputReaderBase.h>
26#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000027#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <KeyboardInputMapper.h>
29#include <MultiTouchInputMapper.h>
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>
33#include <SwitchInputMapper.h>
34#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000035#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070036#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080037#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000038#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039#include <android-base/thread_annotations.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>
chaviw3277faf2021-05-19 16:45:23 -050042#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000043#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080044
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070045#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000046#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000047#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000048#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000049#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000050#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000051#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000052#include "input/DisplayViewport.h"
53#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010054
Michael Wrightd02c5b62014-02-10 15:10:22 -080055namespace android {
56
Dominik Laskowski2f01d772022-03-23 16:01:29 -070057using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000058using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070059using std::chrono_literals::operator""ms;
60
Michael Wrightd02c5b62014-02-10 15:10:22 -080061// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000063static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t DISPLAY_WIDTH = 480;
67static constexpr int32_t DISPLAY_HEIGHT = 800;
68static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
69static 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 Pradhan2f37bcb2022-11-08 20:41:28 +000094// Minimum timestamp separation between subsequent input events from a Bluetooth device.
95static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
96// Maximum smoothing time delta so that we don't generate events too far into the future.
97constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
98
Michael Wrightd02c5b62014-02-10 15:10:22 -080099template<typename T>
100static inline T min(T a, T b) {
101 return a < b ? a : b;
102}
103
104static inline float avg(float x, float y) {
105 return (x + y) / 2;
106}
107
Chris Ye3fdbfef2021-01-06 18:45:18 -0800108// Mapping for light color name and the light color
109const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
110 {"green", LightColor::GREEN},
111 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112
Michael Wrighta9cf4192022-12-01 23:46:39 +0000113static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700114 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000115 case ui::ROTATION_90:
116 return ui::ROTATION_270;
117 case ui::ROTATION_270:
118 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700119 default:
120 return orientation;
121 }
122}
123
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800124static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
125 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000126 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800127
128 const InputDeviceInfo::MotionRange* motionRange =
129 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
130 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
131}
132
133static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
134 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000135 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800136
137 const InputDeviceInfo::MotionRange* motionRange =
138 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
139 ASSERT_EQ(nullptr, motionRange);
140}
141
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700142[[maybe_unused]] static void dumpReader(InputReader& reader) {
143 std::string dump;
144 reader.dump(dump);
145 std::istringstream iss(dump);
146 for (std::string line; std::getline(iss, line);) {
147 ALOGE("%s", line.c_str());
148 std::this_thread::sleep_for(std::chrono::milliseconds(1));
149 }
150}
151
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152// --- FakeInputMapper ---
153
154class FakeInputMapper : public InputMapper {
155 uint32_t mSources;
156 int32_t mKeyboardType;
157 int32_t mMetaState;
158 KeyedVector<int32_t, int32_t> mKeyCodeStates;
159 KeyedVector<int32_t, int32_t> mScanCodeStates;
160 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100161 // fake mapping which would normally come from keyCharacterMap
162 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800163 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700165 std::mutex mLock;
166 std::condition_variable mStateChangedCondition;
167 bool mConfigureWasCalled GUARDED_BY(mLock);
168 bool mResetWasCalled GUARDED_BY(mLock);
169 bool mProcessWasCalled GUARDED_BY(mLock);
170 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171
Arthur Hungc23540e2018-11-29 20:42:11 +0800172 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000174 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
175 uint32_t sources)
176 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800177 mSources(sources),
178 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800180 mConfigureWasCalled(false),
181 mResetWasCalled(false),
182 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
Chris Yea52ade12020-08-27 16:49:20 -0700184 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185
186 void setKeyboardType(int32_t keyboardType) {
187 mKeyboardType = keyboardType;
188 }
189
190 void setMetaState(int32_t metaState) {
191 mMetaState = metaState;
192 }
193
194 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700195 std::unique_lock<std::mutex> lock(mLock);
196 base::ScopedLockAssertion assumeLocked(mLock);
197 const bool configureCalled =
198 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
199 return mConfigureWasCalled;
200 });
201 if (!configureCalled) {
202 FAIL() << "Expected configure() to have been called.";
203 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204 mConfigureWasCalled = false;
205 }
206
207 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208 std::unique_lock<std::mutex> lock(mLock);
209 base::ScopedLockAssertion assumeLocked(mLock);
210 const bool resetCalled =
211 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
212 return mResetWasCalled;
213 });
214 if (!resetCalled) {
215 FAIL() << "Expected reset() to have been called.";
216 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 mResetWasCalled = false;
218 }
219
Yi Kong9b14ac62018-07-17 13:48:38 -0700220 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 std::unique_lock<std::mutex> lock(mLock);
222 base::ScopedLockAssertion assumeLocked(mLock);
223 const bool processCalled =
224 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
225 return mProcessWasCalled;
226 });
227 if (!processCalled) {
228 FAIL() << "Expected process() to have been called.";
229 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 if (outLastEvent) {
231 *outLastEvent = mLastEvent;
232 }
233 mProcessWasCalled = false;
234 }
235
236 void setKeyCodeState(int32_t keyCode, int32_t state) {
237 mKeyCodeStates.replaceValueFor(keyCode, state);
238 }
239
240 void setScanCodeState(int32_t scanCode, int32_t state) {
241 mScanCodeStates.replaceValueFor(scanCode, state);
242 }
243
244 void setSwitchState(int32_t switchCode, int32_t state) {
245 mSwitchStates.replaceValueFor(switchCode, state);
246 }
247
248 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800249 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250 }
251
Philip Junker4af3b3d2021-12-14 10:36:55 +0100252 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
253 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
254 }
255
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100257 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258
Harry Cuttsd02ea102023-03-17 18:21:30 +0000259 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 InputMapper::populateDeviceInfo(deviceInfo);
261
262 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000263 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264 }
265 }
266
Arpit Singhed6c3de2023-04-05 19:24:37 +0000267 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000268 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700269 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800271
272 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800273 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000274 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000275 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800276 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700277
278 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700279 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280 }
281
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700282 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700283 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700285 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700286 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 }
288
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700289 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700290 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291 mLastEvent = *rawEvent;
292 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700293 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700294 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295 }
296
Chris Yea52ade12020-08-27 16:49:20 -0700297 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
299 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
300 }
301
Philip Junker4af3b3d2021-12-14 10:36:55 +0100302 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
303 auto it = mKeyCodeMapping.find(locationKeyCode);
304 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
305 }
306
Chris Yea52ade12020-08-27 16:49:20 -0700307 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
309 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
310 }
311
Chris Yea52ade12020-08-27 16:49:20 -0700312 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 ssize_t index = mSwitchStates.indexOfKey(switchCode);
314 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
315 }
316
Chris Yea52ade12020-08-27 16:49:20 -0700317 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700318 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700319 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700320 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
322 if (keyCodes[i] == mSupportedKeyCodes[j]) {
323 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800324 }
325 }
326 }
Chris Yea52ade12020-08-27 16:49:20 -0700327 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328 return result;
329 }
330
331 virtual int32_t getMetaState() {
332 return mMetaState;
333 }
334
335 virtual void fadePointer() {
336 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800337
338 virtual std::optional<int32_t> getAssociatedDisplay() {
339 if (mViewport) {
340 return std::make_optional(mViewport->displayId);
341 }
342 return std::nullopt;
343 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344};
345
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700346// --- InputReaderPolicyTest ---
347class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700348protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700349 sp<FakeInputReaderPolicy> mFakePolicy;
350
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700351 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700352 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700353};
354
355/**
356 * Check that empty set of viewports is an acceptable configuration.
357 * Also try to get internal viewport two different ways - by type and by uniqueId.
358 *
359 * There will be confusion if two viewports with empty uniqueId and identical type are present.
360 * Such configuration is not currently allowed.
361 */
362TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700363 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700364
365 // We didn't add any viewports yet, so there shouldn't be any.
366 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100367 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700368 ASSERT_FALSE(internalViewport);
369
370 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000371 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000372 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700373
374 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700375 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700376 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100377 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700378
379 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100380 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700381 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700382 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700383
384 mFakePolicy->clearViewports();
385 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700386 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700387 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100388 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700389 ASSERT_FALSE(internalViewport);
390}
391
392TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
393 const std::string internalUniqueId = "local:0";
394 const std::string externalUniqueId = "local:1";
395 const std::string virtualUniqueId1 = "virtual:2";
396 const std::string virtualUniqueId2 = "virtual:3";
397 constexpr int32_t virtualDisplayId1 = 2;
398 constexpr int32_t virtualDisplayId2 = 3;
399
400 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000401 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000402 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000403 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700404 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000405 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000406 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000407 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700408 // Add an virtual viewport
409 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000410 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000411 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700412 // Add another virtual viewport
413 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000414 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000415 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700416
417 // Check matching by type for internal
418 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100419 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700420 ASSERT_TRUE(internalViewport);
421 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
422
423 // Check matching by type for external
424 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100425 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700426 ASSERT_TRUE(externalViewport);
427 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
428
429 // Check matching by uniqueId for virtual viewport #1
430 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700431 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700432 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100433 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700434 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
435 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
436
437 // Check matching by uniqueId for virtual viewport #2
438 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700439 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700440 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100441 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700442 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
443 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
444}
445
446
447/**
448 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
449 * that lookup works by checking display id.
450 * Check that 2 viewports of each kind is possible, for all existing viewport types.
451 */
452TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
453 const std::string uniqueId1 = "uniqueId1";
454 const std::string uniqueId2 = "uniqueId2";
455 constexpr int32_t displayId1 = 2;
456 constexpr int32_t displayId2 = 3;
457
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100458 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
459 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700460 for (const ViewportType& type : types) {
461 mFakePolicy->clearViewports();
462 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000463 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000464 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700465 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000466 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000467 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700468
469 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700470 std::optional<DisplayViewport> viewport1 =
471 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700472 ASSERT_TRUE(viewport1);
473 ASSERT_EQ(displayId1, viewport1->displayId);
474 ASSERT_EQ(type, viewport1->type);
475
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700476 std::optional<DisplayViewport> viewport2 =
477 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700478 ASSERT_TRUE(viewport2);
479 ASSERT_EQ(displayId2, viewport2->displayId);
480 ASSERT_EQ(type, viewport2->type);
481
482 // When there are multiple viewports of the same kind, and uniqueId is not specified
483 // in the call to getDisplayViewport, then that situation is not supported.
484 // The viewports can be stored in any order, so we cannot rely on the order, since that
485 // is just implementation detail.
486 // However, we can check that it still returns *a* viewport, we just cannot assert
487 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700488 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700489 ASSERT_TRUE(someViewport);
490 }
491}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700493/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000494 * When we have multiple internal displays make sure we always return the default display when
495 * querying by type.
496 */
497TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
498 const std::string uniqueId1 = "uniqueId1";
499 const std::string uniqueId2 = "uniqueId2";
500 constexpr int32_t nonDefaultDisplayId = 2;
501 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
502 "Test display ID should not be ADISPLAY_ID_DEFAULT");
503
504 // Add the default display first and ensure it gets returned.
505 mFakePolicy->clearViewports();
506 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000507 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000508 ViewportType::INTERNAL);
509 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000510 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000511 ViewportType::INTERNAL);
512
513 std::optional<DisplayViewport> viewport =
514 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
515 ASSERT_TRUE(viewport);
516 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
517 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
518
519 // Add the default display second to make sure order doesn't matter.
520 mFakePolicy->clearViewports();
521 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000522 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000523 ViewportType::INTERNAL);
524 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000525 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000526 ViewportType::INTERNAL);
527
528 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
529 ASSERT_TRUE(viewport);
530 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
531 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
532}
533
534/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700535 * Check getDisplayViewportByPort
536 */
537TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100538 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700539 const std::string uniqueId1 = "uniqueId1";
540 const std::string uniqueId2 = "uniqueId2";
541 constexpr int32_t displayId1 = 1;
542 constexpr int32_t displayId2 = 2;
543 const uint8_t hdmi1 = 0;
544 const uint8_t hdmi2 = 1;
545 const uint8_t hdmi3 = 2;
546
547 mFakePolicy->clearViewports();
548 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000549 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000550 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700551 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000552 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000553 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700554
555 // Check that correct display viewport was returned by comparing the display ports.
556 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
557 ASSERT_TRUE(hdmi1Viewport);
558 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
559 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
560
561 // Check that we can still get the same viewport using the uniqueId
562 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
563 ASSERT_TRUE(hdmi1Viewport);
564 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
565 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
566 ASSERT_EQ(type, hdmi1Viewport->type);
567
568 // Check that we cannot find a port with "HDMI2", because we never added one
569 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
570 ASSERT_FALSE(hdmi2Viewport);
571}
572
Michael Wrightd02c5b62014-02-10 15:10:22 -0800573// --- InputReaderTest ---
574
575class InputReaderTest : public testing::Test {
576protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700577 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700579 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000580 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581
Chris Yea52ade12020-08-27 16:49:20 -0700582 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700583 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700584 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700585 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586
Prabir Pradhan28efc192019-11-05 01:10:04 +0000587 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700588 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589 }
590
Chris Yea52ade12020-08-27 16:49:20 -0700591 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700592 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594 }
595
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700596 void addDevice(int32_t eventHubId, const std::string& name,
597 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800598 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599
600 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800601 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 }
603 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000604 mReader->loopOnce();
605 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700606 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700608 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 }
610
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800611 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700612 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000613 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700614 }
615
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800616 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700617 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000618 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700619 }
620
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700622 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700623 ftl::Flags<InputDeviceClass> classes,
624 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800625 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800626 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000627 FakeInputMapper& mapper =
628 device->addMapper<FakeInputMapper>(eventHubId,
629 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800630 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800631 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 return mapper;
633 }
634};
635
Chris Ye98d3f532020-10-01 21:48:59 -0700636TEST_F(InputReaderTest, PolicyGetInputDevices) {
637 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700638 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700639 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640
641 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700642 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800644 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100645 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800646 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
647 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000648 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649}
650
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000651TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
652 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
653 mFakeEventHub->setSysfsRootPath(1, "xyz");
654
655 // Should also have received a notification describing the new input device.
656 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
657 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
658 ASSERT_EQ(0U, inputDevice.getLights().size());
659
660 RawLightInfo infoMonolight = {.id = 123,
661 .name = "mono_keyboard_backlight",
662 .maxBrightness = 255,
663 .flags = InputLightClass::BRIGHTNESS,
664 .path = ""};
665 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
666 mReader->sysfsNodeChanged("xyz");
667 mReader->loopOnce();
668
669 // Should also have received a notification describing the new recreated input device.
670 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
671 inputDevice = mFakePolicy->getInputDevices()[0];
672 ASSERT_EQ(1U, inputDevice.getLights().size());
673}
674
Chris Yee7310032020-09-22 15:36:28 -0700675TEST_F(InputReaderTest, GetMergedInputDevices) {
676 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
677 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
678 // Add two subdevices to device
679 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
680 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000681 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
682 AINPUT_SOURCE_KEYBOARD);
683 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
684 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700685
686 // Push same device instance for next device to be added, so they'll have same identifier.
687 mReader->pushNextDevice(device);
688 mReader->pushNextDevice(device);
689 ASSERT_NO_FATAL_FAILURE(
690 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
691 ASSERT_NO_FATAL_FAILURE(
692 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
693
694 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000695 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700696}
697
Chris Yee14523a2020-12-19 13:46:00 -0800698TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
700 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
701 // Add two subdevices to device
702 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
703 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000704 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
705 AINPUT_SOURCE_KEYBOARD);
706 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
707 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800708
709 // Push same device instance for next device to be added, so they'll have same identifier.
710 mReader->pushNextDevice(device);
711 mReader->pushNextDevice(device);
712 // Sensor device is initially disabled
713 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
714 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
715 nullptr));
716 // Device is disabled because the only sub device is a sensor device and disabled initially.
717 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
718 ASSERT_FALSE(device->isEnabled());
719 ASSERT_NO_FATAL_FAILURE(
720 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
721 // The merged device is enabled if any sub device is enabled
722 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
723 ASSERT_TRUE(device->isEnabled());
724}
725
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700726TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800727 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700728 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800729 constexpr int32_t eventHubId = 1;
730 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700731 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000732 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
733 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800734 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800735 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700736
Yi Kong9b14ac62018-07-17 13:48:38 -0700737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700738
739 NotifyDeviceResetArgs resetArgs;
740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700741 ASSERT_EQ(deviceId, resetArgs.deviceId);
742
743 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800744 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000745 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700746
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700748 ASSERT_EQ(deviceId, resetArgs.deviceId);
749 ASSERT_EQ(device->isEnabled(), false);
750
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800751 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000752 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700755 ASSERT_EQ(device->isEnabled(), false);
756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800757 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000758 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700760 ASSERT_EQ(deviceId, resetArgs.deviceId);
761 ASSERT_EQ(device->isEnabled(), true);
762}
763
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800765 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700766 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800767 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800768 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800769 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800770 AINPUT_SOURCE_KEYBOARD, nullptr);
771 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800772
773 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
774 AINPUT_SOURCE_ANY, AKEYCODE_A))
775 << "Should return unknown when the device id is >= 0 but unknown.";
776
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800777 ASSERT_EQ(AKEY_STATE_UNKNOWN,
778 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
779 << "Should return unknown when the device id is valid but the sources are not "
780 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800781
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800782 ASSERT_EQ(AKEY_STATE_DOWN,
783 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
784 AKEYCODE_A))
785 << "Should return value provided by mapper when device id is valid and the device "
786 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787
788 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
789 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
790 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
791
792 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
793 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
794 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
795}
796
Philip Junker4af3b3d2021-12-14 10:36:55 +0100797TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
798 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
799 constexpr int32_t eventHubId = 1;
800 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
801 InputDeviceClass::KEYBOARD,
802 AINPUT_SOURCE_KEYBOARD, nullptr);
803 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
804
805 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
806 << "Should return unknown when the device with the specified id is not found.";
807
808 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
809 << "Should return correct mapping when device id is valid and mapping exists.";
810
811 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
812 << "Should return the location key code when device id is valid and there's no "
813 "mapping.";
814}
815
816TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
817 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
818 constexpr int32_t eventHubId = 1;
819 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
820 InputDeviceClass::JOYSTICK,
821 AINPUT_SOURCE_GAMEPAD, nullptr);
822 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
823
824 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
825 << "Should return unknown when the device id is valid but there is no keyboard mapper";
826}
827
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800829 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700830 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800831 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800832 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800833 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800834 AINPUT_SOURCE_KEYBOARD, nullptr);
835 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836
837 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
838 AINPUT_SOURCE_ANY, KEY_A))
839 << "Should return unknown when the device id is >= 0 but unknown.";
840
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800841 ASSERT_EQ(AKEY_STATE_UNKNOWN,
842 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
843 << "Should return unknown when the device id is valid but the sources are not "
844 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800846 ASSERT_EQ(AKEY_STATE_DOWN,
847 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
848 KEY_A))
849 << "Should return value provided by mapper when device id is valid and the device "
850 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851
852 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
853 AINPUT_SOURCE_TRACKBALL, KEY_A))
854 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
855
856 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
857 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
858 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
859}
860
861TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800862 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700863 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800865 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800866 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800867 AINPUT_SOURCE_KEYBOARD, nullptr);
868 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869
870 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
871 AINPUT_SOURCE_ANY, SW_LID))
872 << "Should return unknown when the device id is >= 0 but unknown.";
873
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800874 ASSERT_EQ(AKEY_STATE_UNKNOWN,
875 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
876 << "Should return unknown when the device id is valid but the sources are not "
877 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800879 ASSERT_EQ(AKEY_STATE_DOWN,
880 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
881 SW_LID))
882 << "Should return value provided by mapper when device id is valid and the device "
883 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800884
885 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
886 AINPUT_SOURCE_TRACKBALL, SW_LID))
887 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
888
889 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
890 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
891 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
892}
893
894TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800895 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700896 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800897 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800898 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800899 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800900 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100901
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800902 mapper.addSupportedKeyCode(AKEYCODE_A);
903 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700905 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800906 uint8_t flags[4] = { 0, 0, 0, 1 };
907
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700908 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 << "Should return false when device id is >= 0 but unknown.";
910 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
911
912 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700913 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800914 << "Should return false when device id is valid but the sources are not supported by "
915 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
917
918 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700919 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800920 keyCodes, flags))
921 << "Should return value provided by mapper when device id is valid and the device "
922 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
924
925 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700926 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
927 << "Should return false when the device id is < 0 but the sources are not supported by "
928 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
930
931 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700932 ASSERT_TRUE(
933 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
934 << "Should return value provided by mapper when device id is < 0 and one of the "
935 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800936 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
937}
938
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000939TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800940 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700941 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942
943 NotifyConfigurationChangedArgs args;
944
945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
946 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
947}
948
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000949TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800950 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700951 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000952 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800953 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000954 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800955 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800956 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800957 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000959 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000960 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
962
963 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800964 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000965 ASSERT_EQ(when, event.when);
966 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800967 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 ASSERT_EQ(EV_KEY, event.type);
969 ASSERT_EQ(KEY_A, event.code);
970 ASSERT_EQ(1, event.value);
971}
972
Garfield Tan1c7bc862020-01-28 13:24:04 -0800973TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800974 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700975 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800976 constexpr int32_t eventHubId = 1;
977 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800978 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000979 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
980 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800981 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800982 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800983
984 NotifyDeviceResetArgs resetArgs;
985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800986 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800987
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800988 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000989 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800991 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800992 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800994 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000995 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800997 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800998 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001000 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001001 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001003 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001004 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001005}
1006
Garfield Tan1c7bc862020-01-28 13:24:04 -08001007TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1008 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001009 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001010 constexpr int32_t eventHubId = 1;
1011 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1012 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001013 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1014 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001015 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001016 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1017
1018 NotifyDeviceResetArgs resetArgs;
1019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1020 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1021}
1022
Arthur Hungc23540e2018-11-29 20:42:11 +08001023TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001024 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001025 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001026 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001027 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001028 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1029 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001030 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1031 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001032 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001033
1034 const uint8_t hdmi1 = 1;
1035
1036 // Associated touch screen with second display.
1037 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1038
1039 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001040 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001041 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001042 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001043 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001044 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001045 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001046 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001047 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001048
1049 // Add the device, and make sure all of the callbacks are triggered.
1050 // The device is added after the input port associations are processed since
1051 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001052 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001055 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001056
Arthur Hung2c9a3342019-07-23 14:18:59 +08001057 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001058 ASSERT_EQ(deviceId, device->getId());
1059 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1060 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001061
1062 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001063 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001064 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001065 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001066}
1067
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001068TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1069 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001070 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001071 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1072 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1073 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001074 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1075 AINPUT_SOURCE_KEYBOARD);
1076 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1077 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001078 mReader->pushNextDevice(device);
1079 mReader->pushNextDevice(device);
1080 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1081 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1082
1083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1084
1085 NotifyDeviceResetArgs resetArgs;
1086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1087 ASSERT_EQ(deviceId, resetArgs.deviceId);
1088 ASSERT_TRUE(device->isEnabled());
1089 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1090 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1091
1092 disableDevice(deviceId);
1093 mReader->loopOnce();
1094
1095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1096 ASSERT_EQ(deviceId, resetArgs.deviceId);
1097 ASSERT_FALSE(device->isEnabled());
1098 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1099 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1100
1101 enableDevice(deviceId);
1102 mReader->loopOnce();
1103
1104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1105 ASSERT_EQ(deviceId, resetArgs.deviceId);
1106 ASSERT_TRUE(device->isEnabled());
1107 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1108 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1109}
1110
1111TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1112 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001113 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001114 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1115 // Add two subdevices to device
1116 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1117 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001118 device->addMapper<FakeInputMapper>(eventHubIds[0],
1119 mFakePolicy->getReaderConfiguration(),
1120 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001121 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001122 device->addMapper<FakeInputMapper>(eventHubIds[1],
1123 mFakePolicy->getReaderConfiguration(),
1124 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001125 mReader->pushNextDevice(device);
1126 mReader->pushNextDevice(device);
1127 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1128 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1129
1130 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1131 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1132
1133 ASSERT_EQ(AKEY_STATE_DOWN,
1134 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1135 ASSERT_EQ(AKEY_STATE_DOWN,
1136 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1137 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1138 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1139}
1140
Prabir Pradhan7e186182020-11-10 13:56:45 -08001141TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1142 NotifyPointerCaptureChangedArgs args;
1143
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001144 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001145 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001146 mReader->loopOnce();
1147 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001148 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1149 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001150
1151 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001152 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001153 mReader->loopOnce();
1154 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001155 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001156
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001157 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001158 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001159 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001160 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001161 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001162}
1163
Chris Ye87143712020-11-10 05:05:58 +00001164class FakeVibratorInputMapper : public FakeInputMapper {
1165public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001166 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1167 const InputReaderConfiguration& readerConfig, uint32_t sources)
1168 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001169
1170 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1171};
1172
1173TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1174 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001175 ftl::Flags<InputDeviceClass> deviceClass =
1176 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001177 constexpr int32_t eventHubId = 1;
1178 const char* DEVICE_LOCATION = "BLUETOOTH";
1179 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1180 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001181 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1182 mFakePolicy->getReaderConfiguration(),
1183 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001184 mReader->pushNextDevice(device);
1185
1186 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1187 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1188
1189 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1190 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1191}
1192
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001193// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001194
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001195class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001196public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001197 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001198
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001199 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001200
Andy Chenf9f1a022022-08-29 20:07:10 -04001201 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1202
Chris Yee2b1e5c2021-03-10 22:45:12 -08001203 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1204
1205 void dump(std::string& dump) override {}
1206
1207 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1208 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001209 }
1210
Chris Yee2b1e5c2021-03-10 22:45:12 -08001211 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1212 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001213 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001214
1215 bool setLightColor(int32_t lightId, int32_t color) override {
1216 getDeviceContext().setLightBrightness(lightId, color >> 24);
1217 return true;
1218 }
1219
1220 std::optional<int32_t> getLightColor(int32_t lightId) override {
1221 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1222 if (!result.has_value()) {
1223 return std::nullopt;
1224 }
1225 return result.value() << 24;
1226 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001227
1228 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1229
1230 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1231
1232private:
1233 InputDeviceContext& mDeviceContext;
1234 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1235 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001236 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001237};
1238
Chris Yee2b1e5c2021-03-10 22:45:12 -08001239TEST_F(InputReaderTest, BatteryGetCapacity) {
1240 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001241 ftl::Flags<InputDeviceClass> deviceClass =
1242 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001243 constexpr int32_t eventHubId = 1;
1244 const char* DEVICE_LOCATION = "BLUETOOTH";
1245 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001246 FakePeripheralController& controller =
1247 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001248 mReader->pushNextDevice(device);
1249
1250 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1251
Harry Cuttsa5b71292022-11-28 12:56:17 +00001252 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1253 FakeEventHub::BATTERY_CAPACITY);
1254 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001255}
1256
1257TEST_F(InputReaderTest, BatteryGetStatus) {
1258 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001259 ftl::Flags<InputDeviceClass> deviceClass =
1260 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001261 constexpr int32_t eventHubId = 1;
1262 const char* DEVICE_LOCATION = "BLUETOOTH";
1263 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001264 FakePeripheralController& controller =
1265 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001266 mReader->pushNextDevice(device);
1267
1268 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1269
Harry Cuttsa5b71292022-11-28 12:56:17 +00001270 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1271 FakeEventHub::BATTERY_STATUS);
1272 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001273}
1274
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001275TEST_F(InputReaderTest, BatteryGetDevicePath) {
1276 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1277 ftl::Flags<InputDeviceClass> deviceClass =
1278 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1279 constexpr int32_t eventHubId = 1;
1280 const char* DEVICE_LOCATION = "BLUETOOTH";
1281 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1282 device->addController<FakePeripheralController>(eventHubId);
1283 mReader->pushNextDevice(device);
1284
1285 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1286
Harry Cuttsa5b71292022-11-28 12:56:17 +00001287 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001288}
1289
Chris Ye3fdbfef2021-01-06 18:45:18 -08001290TEST_F(InputReaderTest, LightGetColor) {
1291 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001292 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001293 constexpr int32_t eventHubId = 1;
1294 const char* DEVICE_LOCATION = "BLUETOOTH";
1295 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001296 FakePeripheralController& controller =
1297 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001298 mReader->pushNextDevice(device);
1299 RawLightInfo info = {.id = 1,
1300 .name = "Mono",
1301 .maxBrightness = 255,
1302 .flags = InputLightClass::BRIGHTNESS,
1303 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001304 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1305 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001306
1307 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001308
Harry Cutts33476232023-01-30 19:57:29 +00001309 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1310 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1311 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1312 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001313}
1314
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001315// --- InputReaderIntegrationTest ---
1316
1317// These tests create and interact with the InputReader only through its interface.
1318// The InputReader is started during SetUp(), which starts its processing in its own
1319// thread. The tests use linux uinput to emulate input devices.
1320// NOTE: Interacting with the physical device while these tests are running may cause
1321// the tests to fail.
1322class InputReaderIntegrationTest : public testing::Test {
1323protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001324 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001325 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001326 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001327
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001328 std::shared_ptr<FakePointerController> mFakePointerController;
1329
Chris Yea52ade12020-08-27 16:49:20 -07001330 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001331#if !defined(__ANDROID__)
1332 GTEST_SKIP();
1333#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001334 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001335 mFakePointerController = std::make_shared<FakePointerController>();
1336 mFakePolicy->setPointerController(mFakePointerController);
Harry Cutts33476232023-01-30 19:57:29 +00001337 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1338 /*eventDidNotHappenTimeout=*/30ms);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001339
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001340 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1341 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001342 ASSERT_EQ(mReader->start(), OK);
1343
1344 // Since this test is run on a real device, all the input devices connected
1345 // to the test device will show up in mReader. We wait for those input devices to
1346 // show up before beginning the tests.
1347 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +00001348 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1350 }
1351
Chris Yea52ade12020-08-27 16:49:20 -07001352 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001353#if !defined(__ANDROID__)
1354 return;
1355#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001356 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001357 mReader.reset();
1358 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001359 mFakePolicy.clear();
1360 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001361
1362 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1363 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1364 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1365 [&name](const InputDeviceInfo& info) {
1366 return info.getIdentifier().name == name;
1367 });
1368 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1369 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001370};
1371
1372TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1373 // An invalid input device that is only used for this test.
1374 class InvalidUinputDevice : public UinputDevice {
1375 public:
Harry Cutts33476232023-01-30 19:57:29 +00001376 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001377
1378 private:
1379 void configureDevice(int fd, uinput_user_dev* device) override {}
1380 };
1381
1382 const size_t numDevices = mFakePolicy->getInputDevices().size();
1383
1384 // UinputDevice does not set any event or key bits, so InputReader should not
1385 // consider it as a valid device.
1386 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1387 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1388 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1389 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1390
1391 invalidDevice.reset();
1392 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1394 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1395}
1396
1397TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1398 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1399
1400 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1401 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1402 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1403 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1404
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001405 const auto device = findDeviceByName(keyboard->getName());
1406 ASSERT_TRUE(device.has_value());
1407 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1408 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1409 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001410
1411 keyboard.reset();
1412 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1414 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1415}
1416
1417TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1418 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1419 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1420
1421 NotifyConfigurationChangedArgs configChangedArgs;
1422 ASSERT_NO_FATAL_FAILURE(
1423 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001424 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001425 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1426
1427 NotifyKeyArgs keyArgs;
1428 keyboard->pressAndReleaseHomeKey();
1429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1430 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001431 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001432 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001433 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001434 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001435 prevTimestamp = keyArgs.eventTime;
1436
1437 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1438 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001439 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001440 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001441 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001442}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001443
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001444TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1445 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1446 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1447
1448 const auto device = findDeviceByName(stylus->getName());
1449 ASSERT_TRUE(device.has_value());
1450
Prabir Pradhana3621852022-10-14 18:57:23 +00001451 // An external stylus with buttons should also be recognized as a keyboard.
1452 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001453 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1454 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1455
1456 const auto DOWN =
1457 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1458 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1459
1460 stylus->pressAndReleaseKey(BTN_STYLUS);
1461 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1462 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1463 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1464 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1465
1466 stylus->pressAndReleaseKey(BTN_STYLUS2);
1467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1468 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1469 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1470 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1471
1472 stylus->pressAndReleaseKey(BTN_STYLUS3);
1473 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1474 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1475 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1476 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1477}
1478
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001479/**
1480 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1481 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1482 * are passed to the listener.
1483 */
1484static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1485TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1486 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1487 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1488 NotifyKeyArgs keyArgs;
1489
1490 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1491 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1492 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1493 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1494
1495 controller->pressAndReleaseKey(BTN_GEAR_UP);
1496 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1497 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1498 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1499}
1500
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001501// --- TouchIntegrationTest ---
1502
Arthur Hungaab25622020-01-16 11:22:11 +08001503class TouchIntegrationTest : public InputReaderIntegrationTest {
1504protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001505 const std::string UNIQUE_ID = "local:0";
1506
Chris Yea52ade12020-08-27 16:49:20 -07001507 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001508#if !defined(__ANDROID__)
1509 GTEST_SKIP();
1510#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001511 InputReaderIntegrationTest::SetUp();
1512 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001513 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1514 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001515
1516 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1517 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1518 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001519 const auto info = findDeviceByName(mDevice->getName());
1520 ASSERT_TRUE(info);
1521 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001522 }
1523
1524 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001525 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001526 std::optional<uint8_t> physicalPort,
1527 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001528 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001529 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001530 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001531 }
1532
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001533 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1534 NotifyMotionArgs args;
1535 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1536 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001537 ASSERT_EQ(points.size(), args.getPointerCount());
1538 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001539 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1540 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1541 }
1542 }
1543
Arthur Hungaab25622020-01-16 11:22:11 +08001544 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001545 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001546};
1547
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001548TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1549 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1550 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1551 // presses).
1552 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1553 mDeviceInfo.getSources());
1554}
1555
Arthur Hungaab25622020-01-16 11:22:11 +08001556TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1557 NotifyMotionArgs args;
1558 const Point centerPoint = mDevice->getCenterPoint();
1559
1560 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001561 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001562 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001563 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001564 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1565 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1566
1567 // ACTION_MOVE
1568 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001569 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001570 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1572
1573 // ACTION_UP
1574 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001575 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001576 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1577 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1578}
1579
1580TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1581 NotifyMotionArgs args;
1582 const Point centerPoint = mDevice->getCenterPoint();
1583
1584 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001585 mDevice->sendSlot(FIRST_SLOT);
1586 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001587 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001588 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001589 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1590 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1591
1592 // ACTION_POINTER_DOWN (Second slot)
1593 const Point secondPoint = centerPoint + Point(100, 100);
1594 mDevice->sendSlot(SECOND_SLOT);
1595 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001596 mDevice->sendDown(secondPoint);
1597 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001598 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001599 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001600
1601 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001602 mDevice->sendMove(secondPoint + Point(1, 1));
1603 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001604 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1606
1607 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001608 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001609 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001610 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001611 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001612
1613 // ACTION_UP
1614 mDevice->sendSlot(FIRST_SLOT);
1615 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001616 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001617 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1618 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1619}
1620
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001621/**
1622 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1623 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1624 * data?
1625 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1626 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1627 * for Pointer 0 only is generated after.
1628 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1629 * events, we will not miss any information.
1630 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1631 * event generated afterwards that contains the newest movement of pointer 0.
1632 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1633 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1634 * losing information about non-palm pointers.
1635 */
1636TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1637 NotifyMotionArgs args;
1638 const Point centerPoint = mDevice->getCenterPoint();
1639
1640 // ACTION_DOWN
1641 mDevice->sendSlot(FIRST_SLOT);
1642 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1643 mDevice->sendDown(centerPoint);
1644 mDevice->sendSync();
1645 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1646
1647 // ACTION_POINTER_DOWN (Second slot)
1648 const Point secondPoint = centerPoint + Point(100, 100);
1649 mDevice->sendSlot(SECOND_SLOT);
1650 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1651 mDevice->sendDown(secondPoint);
1652 mDevice->sendSync();
1653 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1654
1655 // ACTION_MOVE (First slot)
1656 mDevice->sendSlot(FIRST_SLOT);
1657 mDevice->sendMove(centerPoint + Point(5, 5));
1658 // ACTION_POINTER_UP (Second slot)
1659 mDevice->sendSlot(SECOND_SLOT);
1660 mDevice->sendPointerUp();
1661 // Send a single sync for the above 2 pointer updates
1662 mDevice->sendSync();
1663
1664 // First, we should get POINTER_UP for the second pointer
1665 assertReceivedMotion(ACTION_POINTER_1_UP,
1666 {/*first pointer */ centerPoint + Point(5, 5),
1667 /*second pointer*/ secondPoint});
1668
1669 // Next, the MOVE event for the first pointer
1670 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1671}
1672
1673/**
1674 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1675 * move, and then it will go up, all in the same frame.
1676 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1677 * gets sent to the listener.
1678 */
1679TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1680 NotifyMotionArgs args;
1681 const Point centerPoint = mDevice->getCenterPoint();
1682
1683 // ACTION_DOWN
1684 mDevice->sendSlot(FIRST_SLOT);
1685 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1686 mDevice->sendDown(centerPoint);
1687 mDevice->sendSync();
1688 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1689
1690 // ACTION_POINTER_DOWN (Second slot)
1691 const Point secondPoint = centerPoint + Point(100, 100);
1692 mDevice->sendSlot(SECOND_SLOT);
1693 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1694 mDevice->sendDown(secondPoint);
1695 mDevice->sendSync();
1696 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1697
1698 // ACTION_MOVE (First slot)
1699 mDevice->sendSlot(FIRST_SLOT);
1700 mDevice->sendMove(centerPoint + Point(5, 5));
1701 // ACTION_POINTER_UP (Second slot)
1702 mDevice->sendSlot(SECOND_SLOT);
1703 mDevice->sendMove(secondPoint + Point(6, 6));
1704 mDevice->sendPointerUp();
1705 // Send a single sync for the above 2 pointer updates
1706 mDevice->sendSync();
1707
1708 // First, we should get POINTER_UP for the second pointer
1709 // The movement of the second pointer during the liftoff frame is ignored.
1710 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1711 assertReceivedMotion(ACTION_POINTER_1_UP,
1712 {/*first pointer */ centerPoint + Point(5, 5),
1713 /*second pointer*/ secondPoint});
1714
1715 // Next, the MOVE event for the first pointer
1716 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1717}
1718
Arthur Hungaab25622020-01-16 11:22:11 +08001719TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1720 NotifyMotionArgs args;
1721 const Point centerPoint = mDevice->getCenterPoint();
1722
1723 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001724 mDevice->sendSlot(FIRST_SLOT);
1725 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001726 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001727 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001728 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1729 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1730
arthurhungcc7f9802020-04-30 17:55:40 +08001731 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001732 const Point secondPoint = centerPoint + Point(100, 100);
1733 mDevice->sendSlot(SECOND_SLOT);
1734 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1735 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001736 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001737 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001738 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001739
arthurhungcc7f9802020-04-30 17:55:40 +08001740 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001741 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001742 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001743 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1745
arthurhungcc7f9802020-04-30 17:55:40 +08001746 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1747 // a palm event.
1748 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001749 mDevice->sendToolType(MT_TOOL_PALM);
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));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001752 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001753 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001754
arthurhungcc7f9802020-04-30 17:55:40 +08001755 // Send up to second slot, expect first slot send moving.
1756 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001757 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001758 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001760
arthurhungcc7f9802020-04-30 17:55:40 +08001761 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001762 mDevice->sendSlot(FIRST_SLOT);
1763 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001764 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001765
arthurhungcc7f9802020-04-30 17:55:40 +08001766 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1767 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001768}
1769
Prabir Pradhanda20b172022-09-26 17:01:18 +00001770TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1771 const Point centerPoint = mDevice->getCenterPoint();
1772
1773 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1774 mDevice->sendSlot(FIRST_SLOT);
1775 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1776 mDevice->sendToolType(MT_TOOL_PEN);
1777 mDevice->sendDown(centerPoint);
1778 mDevice->sendSync();
1779 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1780 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001781 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001782
1783 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1784
1785 // Release the stylus touch.
1786 mDevice->sendUp();
1787 mDevice->sendSync();
1788 ASSERT_NO_FATAL_FAILURE(
1789 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1790
1791 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1792
1793 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1794 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1795 mDevice->sendToolType(MT_TOOL_FINGER);
1796 mDevice->sendDown(centerPoint);
1797 mDevice->sendSync();
1798 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1799 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001800 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001801
1802 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1803
1804 mDevice->sendUp();
1805 mDevice->sendSync();
1806 ASSERT_NO_FATAL_FAILURE(
1807 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1808
1809 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1810 // The policy should be notified of the stylus presence.
1811 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1812 mDevice->sendToolType(MT_TOOL_PEN);
1813 mDevice->sendMove(centerPoint);
1814 mDevice->sendSync();
1815 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1816 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001817 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001818
1819 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1820}
1821
Prabir Pradhan124ea442022-10-28 20:27:44 +00001822// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001823
Prabir Pradhan124ea442022-10-28 20:27:44 +00001824// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1825// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1826// stylus.
1827template <typename UinputStylusDevice>
1828class StylusButtonIntegrationTest : public TouchIntegrationTest {
1829protected:
1830 void SetUp() override {
1831#if !defined(__ANDROID__)
1832 GTEST_SKIP();
1833#endif
1834 TouchIntegrationTest::SetUp();
1835 mTouchscreen = mDevice.get();
1836 mTouchscreenInfo = mDeviceInfo;
1837
1838 setUpStylusDevice();
1839 }
1840
1841 UinputStylusDevice* mStylus{nullptr};
1842 InputDeviceInfo mStylusInfo{};
1843
1844 UinputTouchScreen* mTouchscreen{nullptr};
1845 InputDeviceInfo mTouchscreenInfo{};
1846
1847private:
1848 // When we are attempting to test stylus button events that are sent from the touchscreen,
1849 // use the same Uinput device for the touchscreen and the stylus.
1850 template <typename T = UinputStylusDevice>
1851 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1852 mStylus = mDevice.get();
1853 mStylusInfo = mDeviceInfo;
1854 }
1855
1856 // When we are attempting to stylus buttons from an external stylus being merged with touches
1857 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1858 template <typename T = UinputStylusDevice>
1859 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1860 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1861 mStylus = mStylusDeviceLifecycleTracker.get();
1862 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1863 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1864 const auto info = findDeviceByName(mStylus->getName());
1865 ASSERT_TRUE(info);
1866 mStylusInfo = *info;
1867 }
1868
1869 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1870
1871 // Hide the base class's device to expose it with a different name for readability.
1872 using TouchIntegrationTest::mDevice;
1873 using TouchIntegrationTest::mDeviceInfo;
1874};
1875
1876using StylusButtonIntegrationTestTypes =
1877 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1878TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1879
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001880TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001881 const auto stylusId = TestFixture::mStylusInfo.getId();
1882
1883 TestFixture::mStylus->pressKey(BTN_STYLUS);
1884 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1885 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1886 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1887
1888 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1889 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001890 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001891 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001892}
1893
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001894TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001895 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1896 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1897 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001898
1899 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001900 TestFixture::mStylus->pressKey(BTN_STYLUS);
1901 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001902 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001903 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001904
1905 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001906 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1907 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1908 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1909 TestFixture::mTouchscreen->sendDown(centerPoint);
1910 TestFixture::mTouchscreen->sendSync();
1911 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001912 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001913 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001914 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1915 WithDeviceId(touchscreenId))));
1916 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001917 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001918 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001919 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1920 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001921
Prabir Pradhan124ea442022-10-28 20:27:44 +00001922 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1923 TestFixture::mTouchscreen->sendSync();
1924 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001925 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001926 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001927 WithDeviceId(touchscreenId))));
1928 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001929 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001930 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001931 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001932
1933 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001934 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1935 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001936 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001937 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001938}
1939
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001940TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001941 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1942 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1943 const auto stylusId = TestFixture::mStylusInfo.getId();
1944 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001945 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001946
1947 // Press the stylus button.
1948 TestFixture::mStylus->pressKey(BTN_STYLUS);
1949 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1950 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1951 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1952
1953 // Start hovering with the stylus.
1954 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1955 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1956 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1957 TestFixture::mTouchscreen->sendMove(centerPoint);
1958 TestFixture::mTouchscreen->sendSync();
1959 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1960 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1961 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1962 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1963 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1964 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1965 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1966 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1967 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1968
1969 // Touch down with the stylus.
1970 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1971 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1972 TestFixture::mTouchscreen->sendDown(centerPoint);
1973 TestFixture::mTouchscreen->sendSync();
1974 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1975 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1976 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1977
1978 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1979 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1980 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1981
1982 // Stop touching with the stylus, and start hovering.
1983 TestFixture::mTouchscreen->sendUp();
1984 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1985 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1986 TestFixture::mTouchscreen->sendMove(centerPoint);
1987 TestFixture::mTouchscreen->sendSync();
1988 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1989 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
1990 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1991 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1992 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1993 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1994 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1995 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1996 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1997
1998 // Stop hovering.
1999 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2000 TestFixture::mTouchscreen->sendSync();
2001 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2002 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2003 WithButtonState(0))));
2004 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2005 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2006 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2007 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2008
2009 // Release the stylus button.
2010 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2011 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2012 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2013 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2014}
2015
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002016TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002017 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2018 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2019 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002020
2021 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002022 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2023 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2024 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2025 TestFixture::mTouchscreen->sendDown(centerPoint);
2026 TestFixture::mTouchscreen->sendSync();
2027 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002028 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002029 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002030 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002031
2032 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002033 TestFixture::mStylus->pressKey(BTN_STYLUS);
2034 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002035 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002036 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2037 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002038 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002039 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002040 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2041 WithDeviceId(touchscreenId))));
2042 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002043 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002044 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002045 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2046 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002047
Prabir Pradhan124ea442022-10-28 20:27:44 +00002048 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2049 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002050 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002051 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2052 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002053 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002054 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002055 WithDeviceId(touchscreenId))));
2056 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002057 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002058 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002059 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002060
2061 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002062 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2063 TestFixture::mTouchscreen->sendSync();
2064 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002065 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002066 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002067 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002068}
2069
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002070TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002071 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2072 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002073 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002074
2075 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2076 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2077 const auto stylusId = TestFixture::mStylusInfo.getId();
2078
2079 // Start a stylus gesture. By the time this event is processed, the configuration change that
2080 // was requested is guaranteed to be completed.
2081 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2082 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2083 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2084 TestFixture::mTouchscreen->sendDown(centerPoint);
2085 TestFixture::mTouchscreen->sendSync();
2086 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2087 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002088 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002089 WithDeviceId(touchscreenId))));
2090
2091 // Press and release a stylus button. Each change only generates a MOVE motion event.
2092 // Key events are unaffected.
2093 TestFixture::mStylus->pressKey(BTN_STYLUS);
2094 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2095 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2096 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2097 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2098 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002099 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002100 WithDeviceId(touchscreenId))));
2101
2102 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2103 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2104 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2105 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2106 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2107 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002108 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002109 WithDeviceId(touchscreenId))));
2110
2111 // Finish the stylus gesture.
2112 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2113 TestFixture::mTouchscreen->sendSync();
2114 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2115 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002116 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002117 WithDeviceId(touchscreenId))));
2118}
2119
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002120// --- ExternalStylusIntegrationTest ---
2121
2122// Verify the behavior of an external stylus. An external stylus can report pressure or button
2123// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2124// ongoing stylus gesture that is being emitted by the touchscreen.
2125using ExternalStylusIntegrationTest = TouchIntegrationTest;
2126
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002127TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002128 const Point centerPoint = mDevice->getCenterPoint();
2129
2130 // Create an external stylus capable of reporting pressure data that
2131 // should be fused with a touch pointer.
2132 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2133 createUinputDevice<UinputExternalStylusWithPressure>();
2134 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2135 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2136 const auto stylusInfo = findDeviceByName(stylus->getName());
2137 ASSERT_TRUE(stylusInfo);
2138
2139 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2140
2141 const auto touchscreenId = mDeviceInfo.getId();
2142
2143 // Set a pressure value on the stylus. It doesn't generate any events.
2144 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2145 stylus->setPressure(100);
2146 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2147
2148 // Start a finger gesture, and ensure it shows up as stylus gesture
2149 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002150 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002151 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002152 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002153 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002154 mDevice->sendSync();
2155 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2156 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002157 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002158 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002159
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002160 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2161 // event with the updated pressure.
2162 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002163 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2164 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002165 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002166 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002167
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002168 // The external stylus did not generate any events.
2169 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2170 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2171}
2172
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002173TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002174 const Point centerPoint = mDevice->getCenterPoint();
2175
2176 // Create an external stylus capable of reporting pressure data that
2177 // should be fused with a touch pointer.
2178 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2179 createUinputDevice<UinputExternalStylusWithPressure>();
2180 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2181 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2182 const auto stylusInfo = findDeviceByName(stylus->getName());
2183 ASSERT_TRUE(stylusInfo);
2184
2185 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2186
2187 const auto touchscreenId = mDeviceInfo.getId();
2188
2189 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2190 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002191 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2192 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002193 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002194 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002195
2196 // Start a finger gesture. The touch device will withhold generating any touches for
2197 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2198 mDevice->sendSlot(FIRST_SLOT);
2199 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2200 mDevice->sendToolType(MT_TOOL_FINGER);
2201 mDevice->sendDown(centerPoint);
2202 auto waitUntil = std::chrono::system_clock::now() +
2203 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002204 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002205 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002206
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002207 // Since the external stylus did not report a pressure value within the timeout,
2208 // it shows up as a finger pointer.
2209 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2210 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002211 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002212 WithPressure(1.f))));
2213
2214 // Change the pressure on the external stylus. Since the pressure was not present at the start
2215 // of the gesture, it is ignored for now.
2216 stylus->setPressure(200);
2217 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2218
2219 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002220 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2221 mDevice->sendSync();
2222 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2223 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002224 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002225
2226 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2227 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2228 mDevice->sendToolType(MT_TOOL_FINGER);
2229 mDevice->sendDown(centerPoint);
2230 mDevice->sendSync();
2231 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2232 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002233 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002234 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2235
2236 // The external stylus did not generate any events.
2237 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2238 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002239}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002240
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002241TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002242 const Point centerPoint = mDevice->getCenterPoint();
2243
2244 // Create an external stylus device that does not support pressure. It should not affect any
2245 // touch pointers.
2246 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2247 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2248 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2249 const auto stylusInfo = findDeviceByName(stylus->getName());
2250 ASSERT_TRUE(stylusInfo);
2251
2252 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2253
2254 const auto touchscreenId = mDeviceInfo.getId();
2255
2256 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2257 // pressure data from the external stylus.
2258 mDevice->sendSlot(FIRST_SLOT);
2259 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2260 mDevice->sendToolType(MT_TOOL_FINGER);
2261 mDevice->sendDown(centerPoint);
2262 auto waitUntil = std::chrono::system_clock::now() +
2263 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2264 mDevice->sendSync();
2265 ASSERT_NO_FATAL_FAILURE(
2266 mTestListener
2267 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2268 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002269 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002270 WithButtonState(0),
2271 WithDeviceId(touchscreenId),
2272 WithPressure(1.f)),
2273 waitUntil));
2274
2275 // The external stylus did not generate any events.
2276 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2277 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2278}
2279
Michael Wrightd02c5b62014-02-10 15:10:22 -08002280// --- InputDeviceTest ---
2281class InputDeviceTest : public testing::Test {
2282protected:
2283 static const char* DEVICE_NAME;
2284 static const char* DEVICE_LOCATION;
2285 static const int32_t DEVICE_ID;
2286 static const int32_t DEVICE_GENERATION;
2287 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002288 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002289 static const int32_t EVENTHUB_ID;
2290 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2291
2292 std::shared_ptr<FakeEventHub> mFakeEventHub;
2293 sp<FakeInputReaderPolicy> mFakePolicy;
2294 std::unique_ptr<TestInputListener> mFakeListener;
2295 std::unique_ptr<InstrumentedInputReader> mReader;
2296 std::shared_ptr<InputDevice> mDevice;
2297
2298 void SetUp() override {
2299 mFakeEventHub = std::make_unique<FakeEventHub>();
2300 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2301 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002302 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002303 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002304 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002305 identifier.name = DEVICE_NAME;
2306 identifier.location = DEVICE_LOCATION;
2307 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2308 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2309 identifier);
2310 mReader->pushNextDevice(mDevice);
2311 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002312 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002313 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002314
2315 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002316 mFakeListener.reset();
2317 mFakePolicy.clear();
2318 }
2319};
2320
2321const char* InputDeviceTest::DEVICE_NAME = "device";
2322const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2323const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2324const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002325const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002326const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2327 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002328const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002329const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2330
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002331TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002332 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002333 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2334 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002335}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002336
Michael Wrightd02c5b62014-02-10 15:10:22 -08002337TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2338 ASSERT_EQ(mDevice->isEnabled(), false);
2339}
2340
2341TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2342 // Configuration.
2343 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002344 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002345
2346 // Reset.
2347 unused += mDevice->reset(ARBITRARY_TIME);
2348
2349 NotifyDeviceResetArgs resetArgs;
2350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2351 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2352 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2353
2354 // Metadata.
2355 ASSERT_TRUE(mDevice->isIgnored());
2356 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2357
2358 InputDeviceInfo info = mDevice->getDeviceInfo();
2359 ASSERT_EQ(DEVICE_ID, info.getId());
2360 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2361 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2362 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2363
2364 // State queries.
2365 ASSERT_EQ(0, mDevice->getMetaState());
2366
2367 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2368 << "Ignored device should return unknown key code state.";
2369 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2370 << "Ignored device should return unknown scan code state.";
2371 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2372 << "Ignored device should return unknown switch state.";
2373
2374 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2375 uint8_t flags[2] = { 0, 1 };
2376 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2377 << "Ignored device should never mark any key codes.";
2378 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2379 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2380}
2381
2382TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2383 // Configuration.
2384 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2385
2386 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002387 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2388 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2390 mapper1.setMetaState(AMETA_ALT_ON);
2391 mapper1.addSupportedKeyCode(AKEYCODE_A);
2392 mapper1.addSupportedKeyCode(AKEYCODE_B);
2393 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2394 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2395 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2396 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2397 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2398
2399 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002400 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2401 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402 mapper2.setMetaState(AMETA_SHIFT_ON);
2403
2404 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002405 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002406
Harry Cuttsf13161a2023-03-08 14:15:49 +00002407 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2408 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002410 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002411
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002412 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2413 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414
2415 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002416 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002417 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2418 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419
2420 NotifyDeviceResetArgs resetArgs;
2421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2422 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2423 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2424
2425 // Metadata.
2426 ASSERT_FALSE(mDevice->isIgnored());
2427 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2428
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002429 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002431 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2433 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2434
2435 // State queries.
2436 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2437 << "Should query mappers and combine meta states.";
2438
2439 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2440 << "Should return unknown key code state when source not supported.";
2441 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2442 << "Should return unknown scan code state when source not supported.";
2443 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2444 << "Should return unknown switch state when source not supported.";
2445
2446 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2447 << "Should query mapper when source is supported.";
2448 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2449 << "Should query mapper when source is supported.";
2450 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2451 << "Should query mapper when source is supported.";
2452
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002453 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002455 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456 << "Should do nothing when source is unsupported.";
2457 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2458 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2459 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2460 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2461
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002462 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463 << "Should query mapper when source is supported.";
2464 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2465 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2466 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2467 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2468
2469 // Event handling.
2470 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002471 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002472 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002474 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2475 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002476}
2477
Arthur Hung2c9a3342019-07-23 14:18:59 +08002478// A single input device is associated with a specific display. Check that:
2479// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singhf4cdbef2023-05-30 14:12:49 +00002480// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002481TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002482 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2483 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002484
2485 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002486 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002487 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2488 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002489
2490 // Device should be enabled by default.
2491 ASSERT_TRUE(mDevice->isEnabled());
2492
2493 // Prepare associated info.
2494 constexpr uint8_t hdmi = 1;
2495 const std::string UNIQUE_ID = "local:1";
2496
2497 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002498 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002499 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002500 // Device should be disabled because it is associated with a specific display via
2501 // input port <-> display port association, but the corresponding display is not found
2502 ASSERT_FALSE(mDevice->isEnabled());
2503
2504 // Prepare displays.
2505 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002506 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002507 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002508 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002509 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002510 ASSERT_TRUE(mDevice->isEnabled());
2511
2512 // Device should be disabled after set disable.
2513 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002514 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002515 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002516 ASSERT_FALSE(mDevice->isEnabled());
2517
2518 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002519 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002520 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002521 ASSERT_FALSE(mDevice->isEnabled());
2522}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002523
Christine Franks1ba71cc2021-04-07 14:37:42 -07002524TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2525 // Device should be enabled by default.
2526 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002527 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2528 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002529 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002530 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2531 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002532 ASSERT_TRUE(mDevice->isEnabled());
2533
2534 // Device should be disabled because it is associated with a specific display, but the
2535 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002536 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002537 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002538 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002539 ASSERT_FALSE(mDevice->isEnabled());
2540
2541 // Device should be enabled when a display is found.
2542 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002543 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002544 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002545 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002546 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002547 ASSERT_TRUE(mDevice->isEnabled());
2548
2549 // Device should be disabled after set disable.
2550 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002551 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002552 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002553 ASSERT_FALSE(mDevice->isEnabled());
2554
2555 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002556 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002557 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002558 ASSERT_FALSE(mDevice->isEnabled());
2559}
2560
Christine Franks2a2293c2022-01-18 11:51:16 -08002561TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2562 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002563 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2564 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002565 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002566 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2567 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002568
Christine Franks2a2293c2022-01-18 11:51:16 -08002569 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2570 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002571 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002572 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002573 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002574 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002575 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2576}
2577
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002578/**
2579 * This test reproduces a crash caused by a dangling reference that remains after device is added
2580 * and removed. The reference is accessed in InputDevice::dump(..);
2581 */
2582TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2583 constexpr int32_t TEST_EVENTHUB_ID = 10;
2584 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2585
Harry Cutts33476232023-01-30 19:57:29 +00002586 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh4f61f3c2023-06-13 15:05:53 +00002587 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2588 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002589 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2590 std::string dumpStr, eventHubDevStr;
2591 device.dump(dumpStr, eventHubDevStr);
2592}
2593
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002594TEST_F(InputDeviceTest, GetBluetoothAddress) {
2595 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2596 ASSERT_TRUE(address);
2597 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2598}
2599
Michael Wrightd02c5b62014-02-10 15:10:22 -08002600// --- SwitchInputMapperTest ---
2601
2602class SwitchInputMapperTest : public InputMapperTest {
2603protected:
2604};
2605
2606TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002607 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002608
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002609 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002610}
2611
2612TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002613 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002614
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002615 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002616 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002617
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002618 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002619 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002620}
2621
2622TEST_F(SwitchInputMapperTest, Process) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002623 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002624 std::list<NotifyArgs> out;
2625 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2626 ASSERT_TRUE(out.empty());
2627 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2628 ASSERT_TRUE(out.empty());
2629 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2630 ASSERT_TRUE(out.empty());
2631 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002632
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002633 ASSERT_EQ(1u, out.size());
2634 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002636 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2637 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638 args.switchMask);
2639 ASSERT_EQ(uint32_t(0), args.policyFlags);
2640}
2641
Chris Ye87143712020-11-10 05:05:58 +00002642// --- VibratorInputMapperTest ---
2643class VibratorInputMapperTest : public InputMapperTest {
2644protected:
2645 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2646};
2647
2648TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singhc68f85b2023-04-26 16:23:13 +00002649 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002650
2651 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2652}
2653
2654TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singhc68f85b2023-04-26 16:23:13 +00002655 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002656
2657 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2658}
2659
2660TEST_F(VibratorInputMapperTest, Vibrate) {
2661 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002662 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singhc68f85b2023-04-26 16:23:13 +00002663 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002664
2665 VibrationElement pattern(2);
2666 VibrationSequence sequence(2);
2667 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002668 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2669 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002670 sequence.addElement(pattern);
2671 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002672 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2673 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002674 sequence.addElement(pattern);
2675
2676 std::vector<int64_t> timings = {0, 1};
2677 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2678
2679 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002680 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002681 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002682 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002683 // Verify vibrator state listener was notified.
2684 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002685 ASSERT_EQ(1u, out.size());
2686 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2687 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2688 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002689 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002690 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002691 ASSERT_FALSE(mapper.isVibrating());
2692 // Verify vibrator state listener was notified.
2693 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002694 ASSERT_EQ(1u, out.size());
2695 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2696 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2697 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002698}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002699
Chris Yef59a2f42020-10-16 12:55:26 -07002700// --- SensorInputMapperTest ---
2701
2702class SensorInputMapperTest : public InputMapperTest {
2703protected:
2704 static const int32_t ACCEL_RAW_MIN;
2705 static const int32_t ACCEL_RAW_MAX;
2706 static const int32_t ACCEL_RAW_FUZZ;
2707 static const int32_t ACCEL_RAW_FLAT;
2708 static const int32_t ACCEL_RAW_RESOLUTION;
2709
2710 static const int32_t GYRO_RAW_MIN;
2711 static const int32_t GYRO_RAW_MAX;
2712 static const int32_t GYRO_RAW_FUZZ;
2713 static const int32_t GYRO_RAW_FLAT;
2714 static const int32_t GYRO_RAW_RESOLUTION;
2715
2716 static const float GRAVITY_MS2_UNIT;
2717 static const float DEGREE_RADIAN_UNIT;
2718
2719 void prepareAccelAxes();
2720 void prepareGyroAxes();
2721 void setAccelProperties();
2722 void setGyroProperties();
2723 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2724};
2725
2726const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2727const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2728const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2729const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2730const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2731
2732const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2733const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2734const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2735const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2736const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2737
2738const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2739const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2740
2741void SensorInputMapperTest::prepareAccelAxes() {
2742 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2743 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2744 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2745 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2746 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2747 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2748}
2749
2750void SensorInputMapperTest::prepareGyroAxes() {
2751 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2752 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2753 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2754 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2755 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2756 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2757}
2758
2759void SensorInputMapperTest::setAccelProperties() {
2760 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2761 /* sensorDataIndex */ 0);
2762 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2763 /* sensorDataIndex */ 1);
2764 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2765 /* sensorDataIndex */ 2);
2766 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2767 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2768 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2769 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2770 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2771}
2772
2773void SensorInputMapperTest::setGyroProperties() {
2774 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2775 /* sensorDataIndex */ 0);
2776 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2777 /* sensorDataIndex */ 1);
2778 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2779 /* sensorDataIndex */ 2);
2780 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2781 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2782 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2783 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2784 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2785}
2786
2787TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singh61dd33e2023-04-26 15:07:55 +00002788 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002789
2790 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2791}
2792
2793TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2794 setAccelProperties();
2795 prepareAccelAxes();
Arpit Singh61dd33e2023-04-26 15:07:55 +00002796 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002797
2798 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2799 std::chrono::microseconds(10000),
2800 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002801 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2805 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002807
2808 NotifySensorArgs args;
2809 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2810 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2811 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2812
2813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2814 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2815 ASSERT_EQ(args.deviceId, DEVICE_ID);
2816 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2817 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2818 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2819 ASSERT_EQ(args.values, values);
2820 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2821}
2822
2823TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2824 setGyroProperties();
2825 prepareGyroAxes();
Arpit Singh61dd33e2023-04-26 15:07:55 +00002826 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002827
2828 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2829 std::chrono::microseconds(10000),
2830 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002831 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002837
2838 NotifySensorArgs args;
2839 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2840 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2841 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2842
2843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2844 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2845 ASSERT_EQ(args.deviceId, DEVICE_ID);
2846 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2847 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2848 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2849 ASSERT_EQ(args.values, values);
2850 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2851}
2852
Michael Wrightd02c5b62014-02-10 15:10:22 -08002853// --- KeyboardInputMapperTest ---
2854
2855class KeyboardInputMapperTest : public InputMapperTest {
2856protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002857 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002858 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002859 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002860
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002861 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002862 int32_t originalKeyCode, int32_t rotatedKeyCode,
2863 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002864};
2865
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002866/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2867 * orientation.
2868 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002869void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002870 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2871 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002872}
2873
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002874void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002875 int32_t originalScanCode, int32_t originalKeyCode,
2876 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002877 NotifyKeyArgs args;
2878
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2881 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2882 ASSERT_EQ(originalScanCode, args.scanCode);
2883 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002884 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002885
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2888 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2889 ASSERT_EQ(originalScanCode, args.scanCode);
2890 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002891 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002892}
2893
Michael Wrightd02c5b62014-02-10 15:10:22 -08002894TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002895 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00002896 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002897 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002898
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002899 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002900}
2901
2902TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2903 const int32_t USAGE_A = 0x070004;
2904 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002905 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2906 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002907 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2908 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2909 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002910
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002911 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00002912 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002913 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002914 // Initial metastate is AMETA_NONE.
2915 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002916
2917 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002919 NotifyKeyArgs args;
2920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2921 ASSERT_EQ(DEVICE_ID, args.deviceId);
2922 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2923 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2924 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2925 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2926 ASSERT_EQ(KEY_HOME, args.scanCode);
2927 ASSERT_EQ(AMETA_NONE, args.metaState);
2928 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2929 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2930 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2931
2932 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002933 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2935 ASSERT_EQ(DEVICE_ID, args.deviceId);
2936 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2937 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2938 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2939 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2940 ASSERT_EQ(KEY_HOME, args.scanCode);
2941 ASSERT_EQ(AMETA_NONE, args.metaState);
2942 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2943 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2944 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2945
2946 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2950 ASSERT_EQ(DEVICE_ID, args.deviceId);
2951 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2952 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2953 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2954 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2955 ASSERT_EQ(0, args.scanCode);
2956 ASSERT_EQ(AMETA_NONE, args.metaState);
2957 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2958 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2959 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2960
2961 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2963 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2965 ASSERT_EQ(DEVICE_ID, args.deviceId);
2966 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2967 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2968 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2969 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2970 ASSERT_EQ(0, args.scanCode);
2971 ASSERT_EQ(AMETA_NONE, args.metaState);
2972 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2973 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2974 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2975
2976 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2980 ASSERT_EQ(DEVICE_ID, args.deviceId);
2981 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2982 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2983 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2984 ASSERT_EQ(0, args.keyCode);
2985 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2986 ASSERT_EQ(AMETA_NONE, args.metaState);
2987 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2988 ASSERT_EQ(0U, args.policyFlags);
2989 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2990
2991 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2993 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2995 ASSERT_EQ(DEVICE_ID, args.deviceId);
2996 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2997 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2998 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2999 ASSERT_EQ(0, args.keyCode);
3000 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3001 ASSERT_EQ(AMETA_NONE, args.metaState);
3002 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3003 ASSERT_EQ(0U, args.policyFlags);
3004 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3005}
3006
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003007TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3008 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3009 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3010 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3011
3012 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003013 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003014 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3015
3016 // Key down by scan code.
3017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3018 NotifyKeyArgs args;
3019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3020 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3021
3022 // Key up by scan code.
3023 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3025 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3026}
3027
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003028/**
3029 * Ensure that the readTime is set to the time when the EV_KEY is received.
3030 */
3031TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3032 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3033
3034 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003035 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003036 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3037 NotifyKeyArgs args;
3038
3039 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003040 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3042 ASSERT_EQ(12, args.readTime);
3043
3044 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003045 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3047 ASSERT_EQ(15, args.readTime);
3048}
3049
Michael Wrightd02c5b62014-02-10 15:10:22 -08003050TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003051 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3052 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003053 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3054 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3055 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003057 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003058 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003059 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060
Arthur Hung95f68612022-04-07 14:08:22 +08003061 // Initial metastate is AMETA_NONE.
3062 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063
3064 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003066 NotifyKeyArgs args;
3067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3068 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003069 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003070 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071
3072 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003073 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3075 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003076 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077
3078 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003079 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3081 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003082 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083
3084 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003085 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3087 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003088 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003089 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090}
3091
3092TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003093 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3094 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3095 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3096 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003098 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003099 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003100 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003101
Michael Wrighta9cf4192022-12-01 23:46:39 +00003102 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3104 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3105 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3106 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3107 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3108 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3109 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3110 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3111}
3112
3113TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003114 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3115 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3116 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3117 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118
Michael Wrightd02c5b62014-02-10 15:10:22 -08003119 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003120 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003121 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003122 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123
Michael Wrighta9cf4192022-12-01 23:46:39 +00003124 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003125 ASSERT_NO_FATAL_FAILURE(
3126 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3127 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3128 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3129 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3130 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3131 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3132 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003133
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003134 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003135 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003136 ASSERT_NO_FATAL_FAILURE(
3137 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3138 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3139 AKEYCODE_DPAD_UP, DISPLAY_ID));
3140 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3141 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3142 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3143 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003145 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003146 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003147 ASSERT_NO_FATAL_FAILURE(
3148 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3149 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3150 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3151 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3152 AKEYCODE_DPAD_UP, DISPLAY_ID));
3153 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3154 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003156 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003157 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003158 ASSERT_NO_FATAL_FAILURE(
3159 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3160 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3161 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3162 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3163 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3164 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3165 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003166
3167 // Special case: if orientation changes while key is down, we still emit the same keycode
3168 // in the key up as we did in the key down.
3169 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003170 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003171 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3174 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3175 ASSERT_EQ(KEY_UP, args.scanCode);
3176 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3177
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003178 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003179 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3182 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3183 ASSERT_EQ(KEY_UP, args.scanCode);
3184 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3185}
3186
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003187TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3188 // If the keyboard is not orientation aware,
3189 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003190 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003191
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003192 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003193 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003194 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003195 NotifyKeyArgs args;
3196
3197 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003198 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3202 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3203
Michael Wrighta9cf4192022-12-01 23:46:39 +00003204 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3209 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3210}
3211
3212TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3213 // If the keyboard is orientation aware,
3214 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003215 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003216
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003217 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003218 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003219 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003220 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003221 NotifyKeyArgs args;
3222
3223 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3224 // ^--- already checked by the previous test
3225
Michael Wrighta9cf4192022-12-01 23:46:39 +00003226 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003227 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003230 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3232 ASSERT_EQ(DISPLAY_ID, args.displayId);
3233
3234 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003235 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003236 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003237 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3242 ASSERT_EQ(newDisplayId, args.displayId);
3243}
3244
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003246 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003247 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003248 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003250 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003251 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003253 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003254 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255}
3256
Philip Junker4af3b3d2021-12-14 10:36:55 +01003257TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3258 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003259 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003260 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3261
3262 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3263 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3264 << "If a mapping is available, the result is equal to the mapping";
3265
3266 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3267 << "If no mapping is available, the result is the key location";
3268}
3269
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003271 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003272 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003273 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003275 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003276 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003277
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003278 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003279 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003280}
3281
3282TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003283 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003284 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003285 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003287 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003290 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291 ASSERT_TRUE(flags[0]);
3292 ASSERT_FALSE(flags[1]);
3293}
3294
3295TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003296 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3297 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3298 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3299 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3300 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3301 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003303 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003304 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003305 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003306 // Initial metastate is AMETA_NONE.
3307 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308
3309 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003310 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3311 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3312 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313
3314 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003317 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3318 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3319 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003320 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003321
3322 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003325 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3326 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3327 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003328 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003329
3330 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003333 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3334 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3335 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003336 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003337
3338 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003341 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3342 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3343 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003344 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345
3346 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003349 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3350 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3351 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003352 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353
3354 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3356 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003357 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3358 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3359 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003360 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361}
3362
Chris Yea52ade12020-08-27 16:49:20 -07003363TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3364 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3365 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3366 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3367 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3368
3369 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003370 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003371 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3372
Chris Yea52ade12020-08-27 16:49:20 -07003373 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003374 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003375 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3376 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3377 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3378 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3379
3380 NotifyKeyArgs args;
3381 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3384 ASSERT_EQ(AMETA_NONE, args.metaState);
3385 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3386 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3387 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3388
3389 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003390 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3392 ASSERT_EQ(AMETA_NONE, args.metaState);
3393 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3394 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3395 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3396}
3397
Arthur Hung2c9a3342019-07-23 14:18:59 +08003398TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3399 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003400 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3401 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3402 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3403 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003404
3405 // keyboard 2.
3406 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003407 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003408 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003409 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003410 std::shared_ptr<InputDevice> device2 =
3411 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003412 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003413
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003414 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3415 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3416 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3417 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003418
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003419 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003420 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003421 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003422
Arpit Singh033e3ec2023-04-26 14:43:16 +00003423 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003424 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003425 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3426 mFakePolicy
3427 ->getReaderConfiguration(),
3428 AINPUT_SOURCE_KEYBOARD,
3429 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003430 std::list<NotifyArgs> unused =
3431 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003432 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003433 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003434
3435 // Prepared displays and associated info.
3436 constexpr uint8_t hdmi1 = 0;
3437 constexpr uint8_t hdmi2 = 1;
3438 const std::string SECONDARY_UNIQUE_ID = "local:1";
3439
3440 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3441 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3442
3443 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003444 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003445 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003446 ASSERT_FALSE(device2->isEnabled());
3447
3448 // Prepare second display.
3449 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003450 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003451 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003452 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003453 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003454 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003455 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003456 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003457
3458 // Device should be enabled after the associated display is found.
3459 ASSERT_TRUE(mDevice->isEnabled());
3460 ASSERT_TRUE(device2->isEnabled());
3461
3462 // Test pad key events
3463 ASSERT_NO_FATAL_FAILURE(
3464 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3465 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3466 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3467 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3468 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3469 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3470 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3471
3472 ASSERT_NO_FATAL_FAILURE(
3473 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3474 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3475 AKEYCODE_DPAD_RIGHT, newDisplayId));
3476 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3477 AKEYCODE_DPAD_DOWN, newDisplayId));
3478 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3479 AKEYCODE_DPAD_LEFT, newDisplayId));
3480}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481
arthurhungc903df12020-08-11 15:08:42 +08003482TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3483 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3484 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3485 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3486 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3487 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3488 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3489
3490 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003491 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003492 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003493 // Initial metastate is AMETA_NONE.
3494 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003495
3496 // Initialization should have turned all of the lights off.
3497 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3498 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3499 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3500
3501 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003504 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3505 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3506
3507 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003510 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3511 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3512
3513 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003514 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3515 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003516 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3517 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3518
3519 mFakeEventHub->removeDevice(EVENTHUB_ID);
3520 mReader->loopOnce();
3521
3522 // keyboard 2 should default toggle keys.
3523 const std::string USB2 = "USB2";
3524 const std::string DEVICE_NAME2 = "KEYBOARD2";
3525 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3526 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3527 std::shared_ptr<InputDevice> device2 =
3528 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003529 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003530 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3531 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3532 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3533 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3534 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3535 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3536
Arpit Singh033e3ec2023-04-26 14:43:16 +00003537 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003538 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003539 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3540 mFakePolicy
3541 ->getReaderConfiguration(),
3542 AINPUT_SOURCE_KEYBOARD,
3543 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003544 std::list<NotifyArgs> unused =
3545 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003546 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003547 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003548
3549 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3550 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3551 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003552 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3553 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003554}
3555
Arthur Hungcb40a002021-08-03 14:31:01 +00003556TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3557 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3558 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3559 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3560
3561 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh033e3ec2023-04-26 14:43:16 +00003562 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003563 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3564 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003565 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003566 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003567 // Initial metastate is AMETA_NONE.
3568 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003569
3570 mReader->toggleCapsLockState(DEVICE_ID);
3571 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3572}
3573
Arthur Hungfb3cc112022-04-13 07:39:50 +00003574TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3575 // keyboard 1.
3576 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3577 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3578 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3579 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3580 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3581 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3582
3583 KeyboardInputMapper& mapper1 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003584 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003585 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3586
3587 // keyboard 2.
3588 const std::string USB2 = "USB2";
3589 const std::string DEVICE_NAME2 = "KEYBOARD2";
3590 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3591 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3592 std::shared_ptr<InputDevice> device2 =
3593 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3594 ftl::Flags<InputDeviceClass>(0));
3595 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3596 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3597 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3598 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3599 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3600 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3601
Arpit Singh033e3ec2023-04-26 14:43:16 +00003602 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003603 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003604 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3605 mFakePolicy
3606 ->getReaderConfiguration(),
3607 AINPUT_SOURCE_KEYBOARD,
3608 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003609 std::list<NotifyArgs> unused =
3610 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003611 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003612 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003613
Arthur Hung95f68612022-04-07 14:08:22 +08003614 // Initial metastate is AMETA_NONE.
3615 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3616 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3617
3618 // Toggle num lock on and off.
3619 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3620 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003621 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3622 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3623 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3624
3625 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3626 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3627 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3628 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3629 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3630
3631 // Toggle caps lock on and off.
3632 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3633 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3634 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3635 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3636 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3637
3638 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3639 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3640 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3641 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3642 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3643
3644 // Toggle scroll lock on and off.
3645 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3646 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3647 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3648 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3649 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3650
3651 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3652 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3653 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3654 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3655 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3656}
3657
Arthur Hung2141d542022-08-23 07:45:21 +00003658TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3659 const int32_t USAGE_A = 0x070004;
3660 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3661 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3662
3663 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003664 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003665 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3666 // Key down by scan code.
3667 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3668 NotifyKeyArgs args;
3669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3670 ASSERT_EQ(DEVICE_ID, args.deviceId);
3671 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3672 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3673 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3674 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3675 ASSERT_EQ(KEY_HOME, args.scanCode);
3676 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3677
3678 // Disable device, it should synthesize cancellation events for down events.
3679 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003680 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003681
3682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3683 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3684 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3685 ASSERT_EQ(KEY_HOME, args.scanCode);
3686 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3687}
3688
Zixuan Qufecb6062022-11-12 04:44:31 +00003689TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh033e3ec2023-04-26 14:43:16 +00003690 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3691 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003692 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003693 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3694 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003695
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003696 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003697 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3698
3699 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003700 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003701
3702 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3703 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3704 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3705 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3706 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003707 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3708
3709 // Call change layout association with the same values: Generation shouldn't change
3710 generation = mReader->getContext()->getGeneration();
3711 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3712 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3713 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3714 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003715}
3716
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003717TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3718 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3719 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3720
3721 // Configuration
Arpit Singh033e3ec2023-04-26 14:43:16 +00003722 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003723 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3724 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003725 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003726
3727 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3728 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3729}
3730
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003731// --- KeyboardInputMapperTest_ExternalDevice ---
3732
3733class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3734protected:
Chris Yea52ade12020-08-27 16:49:20 -07003735 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003736};
3737
3738TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003739 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3740 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003741
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003742 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3743 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3744 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3745 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003746
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003747 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003748 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003749 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003750
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003751 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003752 NotifyKeyArgs args;
3753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3754 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3755
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003756 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3758 ASSERT_EQ(uint32_t(0), args.policyFlags);
3759
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003762 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003763
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003764 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3766 ASSERT_EQ(uint32_t(0), args.policyFlags);
3767
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3770 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3771
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003772 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3774 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3775}
3776
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003777TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003778 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003779
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003780 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3781 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3782 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003783
Powei Fengd041c5d2019-05-03 17:11:33 -07003784 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003785 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003786 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003787 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003788
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003790 NotifyKeyArgs args;
3791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3792 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3793
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003794 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3796 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3797
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3800 ASSERT_EQ(uint32_t(0), args.policyFlags);
3801
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003802 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3804 ASSERT_EQ(uint32_t(0), args.policyFlags);
3805
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3808 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3809
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003810 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3812 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3813}
3814
Michael Wrightd02c5b62014-02-10 15:10:22 -08003815// --- CursorInputMapperTest ---
3816
3817class CursorInputMapperTest : public InputMapperTest {
3818protected:
3819 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3820
Michael Wright17db18e2020-06-26 20:51:44 +01003821 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822
Chris Yea52ade12020-08-27 16:49:20 -07003823 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003824 InputMapperTest::SetUp();
3825
Michael Wright17db18e2020-06-26 20:51:44 +01003826 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003827 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003828 }
3829
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003830 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3831 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003832
Michael Wrighta9cf4192022-12-01 23:46:39 +00003833 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003834 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3835 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3836 }
3837
3838 void prepareSecondaryDisplay() {
3839 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003840 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003841 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003842 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003843
3844 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3845 float pressure) {
3846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3847 0.0f, 0.0f, 0.0f, EPSILON));
3848 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003849};
3850
3851const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3852
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003853void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3854 int32_t originalY, int32_t rotatedX,
3855 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003856 NotifyMotionArgs args;
3857
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003863 ASSERT_NO_FATAL_FAILURE(
3864 assertCursorPointerCoords(args.pointerCoords[0],
3865 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3866 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867}
3868
3869TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003870 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003871 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003873 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874}
3875
3876TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003877 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003878 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003880 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881}
3882
3883TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003884 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003885 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886
3887 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003888 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003889
3890 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003891 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3892 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3894 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3895
3896 // When the bounds are set, then there should be a valid motion range.
3897 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3898
3899 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003900 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901
3902 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3903 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3904 1, 800 - 1, 0.0f, 0.0f));
3905 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3906 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3907 2, 480 - 1, 0.0f, 0.0f));
3908 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3909 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3910 0.0f, 1.0f, 0.0f, 0.0f));
3911}
3912
3913TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003914 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003915 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916
3917 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003918 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003919
3920 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3921 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3922 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3923 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3924 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3925 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3926 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3927 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3928 0.0f, 1.0f, 0.0f, 0.0f));
3929}
3930
3931TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003932 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003933 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003934
arthurhungdcef2dc2020-08-11 14:47:50 +08003935 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936
3937 NotifyMotionArgs args;
3938
3939 // Button press.
3940 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3944 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3945 ASSERT_EQ(DEVICE_ID, args.deviceId);
3946 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3947 ASSERT_EQ(uint32_t(0), args.policyFlags);
3948 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3949 ASSERT_EQ(0, args.flags);
3950 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3951 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3952 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07003953 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003954 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003955 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003956 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003957 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3958 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3959 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3960
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3962 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3963 ASSERT_EQ(DEVICE_ID, args.deviceId);
3964 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3965 ASSERT_EQ(uint32_t(0), args.policyFlags);
3966 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3967 ASSERT_EQ(0, args.flags);
3968 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3969 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3970 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07003971 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003972 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003973 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003974 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003975 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3976 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3977 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3978
Michael Wrightd02c5b62014-02-10 15:10:22 -08003979 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003980 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3981 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3983 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3984 ASSERT_EQ(DEVICE_ID, args.deviceId);
3985 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3986 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3988 ASSERT_EQ(0, args.flags);
3989 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3990 ASSERT_EQ(0, args.buttonState);
3991 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07003992 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003993 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003994 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003995 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003996 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3997 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3998 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3999
4000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4001 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4002 ASSERT_EQ(DEVICE_ID, args.deviceId);
4003 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4004 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004005 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4006 ASSERT_EQ(0, args.flags);
4007 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4008 ASSERT_EQ(0, args.buttonState);
4009 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004010 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004012 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004013 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004014 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4015 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4016 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4017}
4018
4019TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004020 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004021 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022
4023 NotifyMotionArgs args;
4024
4025 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4029 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004030 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4031 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4032 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004033
4034 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004039 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4040 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041}
4042
4043TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004045 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046
4047 NotifyMotionArgs args;
4048
4049 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4053 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004054 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004055
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4057 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004058 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004059
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004065 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004066
4067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004069 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070}
4071
4072TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004074 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075
4076 NotifyMotionArgs args;
4077
4078 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4084 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004085 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4086 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4087 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004088
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004091 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4092 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4093 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004094
Michael Wrightd02c5b62014-02-10 15:10:22 -08004095 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004101 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4102 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4103 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004104
4105 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004109 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004110 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004111
4112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004114 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115}
4116
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004117TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004118 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004119 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004120 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4121 // need to be rotated.
4122 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004123 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124
Michael Wrighta9cf4192022-12-01 23:46:39 +00004125 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4130 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4133 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4134}
4135
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004136TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004137 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004139 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4140 // orientation-aware are affected by display rotation.
Arpit Singhd8510bd2023-04-27 12:48:15 +00004141 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004143 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004144 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4147 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4148 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4150 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4151 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4152 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4153
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004154 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004155 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004156 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4157 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4158 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4160 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4161 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4162 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4163 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004165 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004166 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4168 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4169 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4170 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4171 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4172 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4173 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4174 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4175
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004176 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004177 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004178 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4179 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4180 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4181 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4182 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4183 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004186}
4187
4188TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004190 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191
4192 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4193 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194
4195 NotifyMotionArgs motionArgs;
4196 NotifyKeyArgs keyArgs;
4197
4198 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4202 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4203 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004204 ASSERT_NO_FATAL_FAILURE(
4205 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4208 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4209 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004210 ASSERT_NO_FATAL_FAILURE(
4211 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004212
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004216 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004218 ASSERT_NO_FATAL_FAILURE(
4219 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220
4221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004222 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004224 ASSERT_NO_FATAL_FAILURE(
4225 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004226
4227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004229 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004230 ASSERT_NO_FATAL_FAILURE(
4231 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232
4233 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4238 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4239 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004240 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004241 ASSERT_NO_FATAL_FAILURE(
4242 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4245 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4246 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004247 ASSERT_NO_FATAL_FAILURE(
4248 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004249
4250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4251 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4252 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004253 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004254 ASSERT_NO_FATAL_FAILURE(
4255 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004256
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4258 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004260 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004262 ASSERT_NO_FATAL_FAILURE(
4263 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004264
4265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004267 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004268 ASSERT_NO_FATAL_FAILURE(
4269 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004274 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4275 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004276 ASSERT_NO_FATAL_FAILURE(
4277 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004280
4281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004284 ASSERT_NO_FATAL_FAILURE(
4285 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004286
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4288 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004290 ASSERT_NO_FATAL_FAILURE(
4291 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004292
4293 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4297 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4298 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004299
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004301 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004303 ASSERT_NO_FATAL_FAILURE(
4304 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004305
4306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4307 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4308 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004309 ASSERT_NO_FATAL_FAILURE(
4310 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004312 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004315 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004317 ASSERT_NO_FATAL_FAILURE(
4318 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004319
4320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004322 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004323
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004324 ASSERT_NO_FATAL_FAILURE(
4325 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4327 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4328 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4329
4330 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4334 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4335 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004336
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004338 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004340 ASSERT_NO_FATAL_FAILURE(
4341 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004342
4343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4344 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4345 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004346 ASSERT_NO_FATAL_FAILURE(
4347 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004348
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004352 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004353 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004354 ASSERT_NO_FATAL_FAILURE(
4355 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004356
4357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4358 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4359 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004360 ASSERT_NO_FATAL_FAILURE(
4361 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004362
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4364 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4365 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4366
4367 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4371 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4372 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004373
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004375 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004377 ASSERT_NO_FATAL_FAILURE(
4378 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004379
4380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4381 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4382 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004383 ASSERT_NO_FATAL_FAILURE(
4384 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004391 ASSERT_NO_FATAL_FAILURE(
4392 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004393
4394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4395 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4396 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004397 ASSERT_NO_FATAL_FAILURE(
4398 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004399
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4401 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4402 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4403
4404 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4408 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4409 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004414 ASSERT_NO_FATAL_FAILURE(
4415 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004416
4417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4418 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4419 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004420 ASSERT_NO_FATAL_FAILURE(
4421 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4424 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004426 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004428 ASSERT_NO_FATAL_FAILURE(
4429 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004430
4431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4432 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4433 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004434 ASSERT_NO_FATAL_FAILURE(
4435 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004436
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4438 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4439 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4440}
4441
4442TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004443 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004444 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445
4446 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4447 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448
4449 NotifyMotionArgs args;
4450
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004455 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4456 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4458 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004459 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004460}
4461
4462TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004463 addConfigurationProperty("cursor.mode", "pointer");
4464 mFakePolicy->setPointerCapture(true);
Arpit Singhd8510bd2023-04-27 12:48:15 +00004465 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004466
4467 NotifyDeviceResetArgs resetArgs;
4468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4469 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4470 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4471
4472 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4473 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004474
4475 NotifyMotionArgs args;
4476
4477 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4482 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4485 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004486 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004487
4488 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4492 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4493 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4495 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4497 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4498 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4500 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4501
4502 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004503 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4504 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4506 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4507 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4509 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4511 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4512 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4514 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4515
4516 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4521 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4524 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004525 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004526
4527 // Disable pointer capture and check that the device generation got bumped
4528 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004529 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004530 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004531 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004532 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004533
4534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004535 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4536
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4538 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4539 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4541 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4544 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004545 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004546}
4547
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004548/**
4549 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4550 * pointer acceleration or speed processing should not be applied.
4551 */
4552TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4553 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004554 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4555 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004556 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhd8510bd2023-04-27 12:48:15 +00004557 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004558
4559 NotifyDeviceResetArgs resetArgs;
4560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4561 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4562 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4563
4564 NotifyMotionArgs args;
4565
4566 // Move and verify scale is applied.
4567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4571 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4572 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4573 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4574 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4575 ASSERT_GT(relX, 10);
4576 ASSERT_GT(relY, 20);
4577
4578 // Enable Pointer Capture
4579 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004580 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004581 NotifyPointerCaptureChangedArgs captureArgs;
4582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4583 ASSERT_TRUE(captureArgs.request.enable);
4584
4585 // Move and verify scale is not applied.
4586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4588 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4590 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4591 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4592 ASSERT_EQ(10, args.pointerCoords[0].getX());
4593 ASSERT_EQ(20, args.pointerCoords[0].getY());
4594}
4595
Prabir Pradhan208360b2022-06-24 18:37:04 +00004596TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4597 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004598 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004599
4600 NotifyDeviceResetArgs resetArgs;
4601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4602 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4603 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4604
4605 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004606 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004607
4608 NotifyMotionArgs args;
4609
4610 // Verify that the coordinates are rotated.
4611 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4615 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4616 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4617 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4618 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4619
4620 // Enable Pointer Capture.
4621 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004622 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004623 NotifyPointerCaptureChangedArgs captureArgs;
4624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4625 ASSERT_TRUE(captureArgs.request.enable);
4626
4627 // Move and verify rotation is not applied.
4628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4632 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4634 ASSERT_EQ(10, args.pointerCoords[0].getX());
4635 ASSERT_EQ(20, args.pointerCoords[0].getY());
4636}
4637
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004638TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004639 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004640
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004641 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004642 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004643
4644 // Set up the secondary display as the display on which the pointer should be shown.
4645 // The InputDevice is not associated with any display.
4646 prepareSecondaryDisplay();
4647 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004648 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004649
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004650 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004651 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004652
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004653 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004658 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4659 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4660 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004661 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004662}
4663
4664TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004665 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004666
4667 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004668 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004669
4670 // Set up the secondary display as the display on which the pointer should be shown,
4671 // and associate the InputDevice with the secondary display.
4672 prepareSecondaryDisplay();
4673 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4674 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004675 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004676
4677 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4678 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004679
4680 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4682 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004684 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4685 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4686 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004687 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004688}
4689
4690TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004691 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004692
4693 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004694 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004695 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4696
4697 // Associate the InputDevice with the secondary display.
4698 prepareSecondaryDisplay();
4699 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004700 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004701
4702 // The mapper should not generate any events because it is associated with a display that is
4703 // different from the pointer display.
4704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004708}
4709
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004710// --- BluetoothCursorInputMapperTest ---
4711
4712class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4713protected:
4714 void SetUp() override {
4715 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4716
4717 mFakePointerController = std::make_shared<FakePointerController>();
4718 mFakePolicy->setPointerController(mFakePointerController);
4719 }
4720};
4721
4722TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4723 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004724 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004725
4726 nsecs_t kernelEventTime = ARBITRARY_TIME;
4727 nsecs_t expectedEventTime = ARBITRARY_TIME;
4728 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4729 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4731 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4732 WithEventTime(expectedEventTime))));
4733
4734 // Process several events that come in quick succession, according to their timestamps.
4735 for (int i = 0; i < 3; i++) {
4736 constexpr static nsecs_t delta = ms2ns(1);
4737 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4738 kernelEventTime += delta;
4739 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4740
4741 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4742 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4744 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4745 WithEventTime(expectedEventTime))));
4746 }
4747}
4748
4749TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4750 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004751 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004752
4753 nsecs_t expectedEventTime = ARBITRARY_TIME;
4754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4755 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4757 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4758 WithEventTime(expectedEventTime))));
4759
4760 // Process several events with the same timestamp from the kernel.
4761 // Ensure that we do not generate events too far into the future.
4762 constexpr static int32_t numEvents =
4763 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4764 for (int i = 0; i < numEvents; i++) {
4765 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4766
4767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4770 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4771 WithEventTime(expectedEventTime))));
4772 }
4773
4774 // By processing more events with the same timestamp, we should not generate events with a
4775 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4776 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4777 for (int i = 0; i < 3; i++) {
4778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4781 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4782 WithEventTime(cappedEventTime))));
4783 }
4784}
4785
4786TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4787 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004788 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004789
4790 nsecs_t kernelEventTime = ARBITRARY_TIME;
4791 nsecs_t expectedEventTime = ARBITRARY_TIME;
4792 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4793 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4795 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4796 WithEventTime(expectedEventTime))));
4797
4798 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4799 // smoothening is not needed, its timestamp is not affected.
4800 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4801 expectedEventTime = kernelEventTime;
4802
4803 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4804 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4806 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4807 WithEventTime(expectedEventTime))));
4808}
4809
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810// --- TouchInputMapperTest ---
4811
4812class TouchInputMapperTest : public InputMapperTest {
4813protected:
4814 static const int32_t RAW_X_MIN;
4815 static const int32_t RAW_X_MAX;
4816 static const int32_t RAW_Y_MIN;
4817 static const int32_t RAW_Y_MAX;
4818 static const int32_t RAW_TOUCH_MIN;
4819 static const int32_t RAW_TOUCH_MAX;
4820 static const int32_t RAW_TOOL_MIN;
4821 static const int32_t RAW_TOOL_MAX;
4822 static const int32_t RAW_PRESSURE_MIN;
4823 static const int32_t RAW_PRESSURE_MAX;
4824 static const int32_t RAW_ORIENTATION_MIN;
4825 static const int32_t RAW_ORIENTATION_MAX;
4826 static const int32_t RAW_DISTANCE_MIN;
4827 static const int32_t RAW_DISTANCE_MAX;
4828 static const int32_t RAW_TILT_MIN;
4829 static const int32_t RAW_TILT_MAX;
4830 static const int32_t RAW_ID_MIN;
4831 static const int32_t RAW_ID_MAX;
4832 static const int32_t RAW_SLOT_MIN;
4833 static const int32_t RAW_SLOT_MAX;
4834 static const float X_PRECISION;
4835 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004836 static const float X_PRECISION_VIRTUAL;
4837 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838
4839 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004840 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841
4842 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4843
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004844 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004845 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004846
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847 enum Axes {
4848 POSITION = 1 << 0,
4849 TOUCH = 1 << 1,
4850 TOOL = 1 << 2,
4851 PRESSURE = 1 << 3,
4852 ORIENTATION = 1 << 4,
4853 MINOR = 1 << 5,
4854 ID = 1 << 6,
4855 DISTANCE = 1 << 7,
4856 TILT = 1 << 8,
4857 SLOT = 1 << 9,
4858 TOOL_TYPE = 1 << 10,
4859 };
4860
Michael Wrighta9cf4192022-12-01 23:46:39 +00004861 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004862 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004863 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004864 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004865 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866 int32_t toRawX(float displayX);
4867 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004868 int32_t toRotatedRawX(float displayX);
4869 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004870 float toCookedX(float rawX, float rawY);
4871 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004873 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004875 float toDisplayY(int32_t rawY, int32_t displayHeight);
4876
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877};
4878
4879const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4880const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4881const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4882const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4883const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4884const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4885const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4886const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004887const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4888const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4890const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4891const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4892const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4893const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4894const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4895const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4896const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4897const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4898const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4899const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4900const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004901const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4902 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4903const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4904 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004905const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4906 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907
4908const float TouchInputMapperTest::GEOMETRIC_SCALE =
4909 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4910 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4911
4912const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4913 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4914 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4915};
4916
Michael Wrighta9cf4192022-12-01 23:46:39 +00004917void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004918 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4919 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004920}
4921
4922void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4923 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004924 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925}
4926
Michael Wrighta9cf4192022-12-01 23:46:39 +00004927void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004928 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4929 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4930 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004931}
4932
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004934 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4935 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4936 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4937 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938}
4939
Jason Gerecke489fda82012-09-07 17:19:40 -07004940void TouchInputMapperTest::prepareLocationCalibration() {
4941 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4942}
4943
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944int32_t TouchInputMapperTest::toRawX(float displayX) {
4945 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4946}
4947
4948int32_t TouchInputMapperTest::toRawY(float displayY) {
4949 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4950}
4951
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004952int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4953 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4954}
4955
4956int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4957 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4958}
4959
Jason Gerecke489fda82012-09-07 17:19:40 -07004960float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4961 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4962 return rawX;
4963}
4964
4965float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4966 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4967 return rawY;
4968}
4969
Michael Wrightd02c5b62014-02-10 15:10:22 -08004970float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004971 return toDisplayX(rawX, DISPLAY_WIDTH);
4972}
4973
4974float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4975 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976}
4977
4978float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004979 return toDisplayY(rawY, DISPLAY_HEIGHT);
4980}
4981
4982float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4983 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984}
4985
4986
4987// --- SingleTouchInputMapperTest ---
4988
4989class SingleTouchInputMapperTest : public TouchInputMapperTest {
4990protected:
4991 void prepareButtons();
4992 void prepareAxes(int axes);
4993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004994 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4995 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4996 void processUp(SingleTouchInputMapper& mappery);
4997 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4998 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4999 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5000 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5001 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5002 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005003};
5004
5005void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005006 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005007}
5008
5009void SingleTouchInputMapperTest::prepareAxes(int axes) {
5010 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005011 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5012 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013 }
5014 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005015 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5016 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017 }
5018 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005019 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5020 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021 }
5022 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005023 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5024 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005025 }
5026 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005027 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5028 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005029 }
5030}
5031
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005032void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005036}
5037
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005038void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5040 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041}
5042
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005043void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045}
5046
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005047void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049}
5050
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005051void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5052 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005054}
5055
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005056void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005057 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058}
5059
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005060void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5061 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064}
5065
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005066void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5067 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005069}
5070
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005071void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073}
5074
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005076 prepareButtons();
5077 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00005078 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079
Josep del Río2d8c79a2023-01-23 19:33:50 +00005080 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005081}
5082
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005084 prepareButtons();
5085 prepareAxes(POSITION);
5086 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00005087 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005089 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005090}
5091
5092TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005093 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005094 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095 prepareButtons();
5096 prepareAxes(POSITION);
5097 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005098 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005099
5100 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005101 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102
5103 // Virtual key is down.
5104 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5105 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5106 processDown(mapper, x, y);
5107 processSync(mapper);
5108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5109
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005110 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111
5112 // Virtual key is up.
5113 processUp(mapper);
5114 processSync(mapper);
5115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5116
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005117 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118}
5119
5120TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005121 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005122 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123 prepareButtons();
5124 prepareAxes(POSITION);
5125 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005126 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005127
5128 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005129 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130
5131 // Virtual key is down.
5132 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5133 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5134 processDown(mapper, x, y);
5135 processSync(mapper);
5136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5137
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005138 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005139
5140 // Virtual key is up.
5141 processUp(mapper);
5142 processSync(mapper);
5143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5144
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005145 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005146}
5147
5148TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005149 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005150 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151 prepareButtons();
5152 prepareAxes(POSITION);
5153 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005154 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005155
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005157 ASSERT_TRUE(
5158 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005159 ASSERT_TRUE(flags[0]);
5160 ASSERT_FALSE(flags[1]);
5161}
5162
5163TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005164 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005165 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166 prepareButtons();
5167 prepareAxes(POSITION);
5168 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005169 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170
arthurhungdcef2dc2020-08-11 14:47:50 +08005171 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172
5173 NotifyKeyArgs args;
5174
5175 // Press virtual key.
5176 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5177 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5178 processDown(mapper, x, y);
5179 processSync(mapper);
5180
5181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5182 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5183 ASSERT_EQ(DEVICE_ID, args.deviceId);
5184 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5185 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5186 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5187 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5188 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5189 ASSERT_EQ(KEY_HOME, args.scanCode);
5190 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5191 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5192
5193 // Release virtual key.
5194 processUp(mapper);
5195 processSync(mapper);
5196
5197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5198 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5199 ASSERT_EQ(DEVICE_ID, args.deviceId);
5200 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5201 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5202 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5203 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5204 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5205 ASSERT_EQ(KEY_HOME, args.scanCode);
5206 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5207 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5208
5209 // Should not have sent any motions.
5210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5211}
5212
5213TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005214 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005215 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216 prepareButtons();
5217 prepareAxes(POSITION);
5218 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005219 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220
arthurhungdcef2dc2020-08-11 14:47:50 +08005221 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005222
5223 NotifyKeyArgs keyArgs;
5224
5225 // Press virtual key.
5226 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5227 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5228 processDown(mapper, x, y);
5229 processSync(mapper);
5230
5231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5232 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5233 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5234 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5235 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5236 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5237 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5238 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5239 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5240 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5241 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5242
5243 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5244 // into the display area.
5245 y -= 100;
5246 processMove(mapper, x, y);
5247 processSync(mapper);
5248
5249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5250 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5251 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5252 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5253 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5254 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5255 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5256 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5257 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5258 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5259 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5260 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5261
5262 NotifyMotionArgs motionArgs;
5263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5264 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5265 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5266 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5267 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5268 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5269 ASSERT_EQ(0, motionArgs.flags);
5270 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5271 ASSERT_EQ(0, motionArgs.buttonState);
5272 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005273 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005274 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005275 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5277 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5278 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5279 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5280 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5281
5282 // Keep moving out of bounds. Should generate a pointer move.
5283 y -= 50;
5284 processMove(mapper, x, y);
5285 processSync(mapper);
5286
5287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5288 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5289 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5290 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5291 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5293 ASSERT_EQ(0, motionArgs.flags);
5294 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5295 ASSERT_EQ(0, motionArgs.buttonState);
5296 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005297 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005298 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005299 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5301 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5302 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5303 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5304 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5305
5306 // Release out of bounds. Should generate a pointer up.
5307 processUp(mapper);
5308 processSync(mapper);
5309
5310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5311 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5312 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5313 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5314 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5315 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5316 ASSERT_EQ(0, motionArgs.flags);
5317 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5318 ASSERT_EQ(0, motionArgs.buttonState);
5319 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005320 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005322 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5324 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5325 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5326 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5327 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5328
5329 // Should not have sent any more keys or motions.
5330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5332}
5333
5334TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005335 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005336 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337 prepareButtons();
5338 prepareAxes(POSITION);
5339 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005340 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005341
arthurhungdcef2dc2020-08-11 14:47:50 +08005342 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005343
5344 NotifyMotionArgs motionArgs;
5345
5346 // Initially go down out of bounds.
5347 int32_t x = -10;
5348 int32_t y = -10;
5349 processDown(mapper, x, y);
5350 processSync(mapper);
5351
5352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5353
5354 // Move into the display area. Should generate a pointer down.
5355 x = 50;
5356 y = 75;
5357 processMove(mapper, x, y);
5358 processSync(mapper);
5359
5360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5361 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5362 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5363 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5364 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5365 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5366 ASSERT_EQ(0, motionArgs.flags);
5367 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5368 ASSERT_EQ(0, motionArgs.buttonState);
5369 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005370 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005371 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005372 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5374 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5375 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5376 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5377 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5378
5379 // Release. Should generate a pointer up.
5380 processUp(mapper);
5381 processSync(mapper);
5382
5383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5384 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5385 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5386 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5387 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5388 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5389 ASSERT_EQ(0, motionArgs.flags);
5390 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5391 ASSERT_EQ(0, motionArgs.buttonState);
5392 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005393 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005395 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5397 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5398 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5399 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5400 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5401
5402 // Should not have sent any more keys or motions.
5403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5405}
5406
Santos Cordonfa5cf462017-04-05 10:37:00 -07005407TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005408 addConfigurationProperty("touch.deviceType", "touchScreen");
5409 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5410
Michael Wrighta9cf4192022-12-01 23:46:39 +00005411 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005412 prepareButtons();
5413 prepareAxes(POSITION);
5414 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005415 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005416
arthurhungdcef2dc2020-08-11 14:47:50 +08005417 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005418
5419 NotifyMotionArgs motionArgs;
5420
5421 // Down.
5422 int32_t x = 100;
5423 int32_t y = 125;
5424 processDown(mapper, x, y);
5425 processSync(mapper);
5426
5427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5428 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5429 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5430 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5431 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5432 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5433 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5434 ASSERT_EQ(0, motionArgs.flags);
5435 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5436 ASSERT_EQ(0, motionArgs.buttonState);
5437 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005438 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005439 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005440 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5442 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5443 1, 0, 0, 0, 0, 0, 0, 0));
5444 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5445 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5446 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5447
5448 // Move.
5449 x += 50;
5450 y += 75;
5451 processMove(mapper, x, y);
5452 processSync(mapper);
5453
5454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5455 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5456 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5457 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5458 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5459 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5461 ASSERT_EQ(0, motionArgs.flags);
5462 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5463 ASSERT_EQ(0, motionArgs.buttonState);
5464 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005465 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005466 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005467 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5469 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5470 1, 0, 0, 0, 0, 0, 0, 0));
5471 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5472 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5473 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5474
5475 // Up.
5476 processUp(mapper);
5477 processSync(mapper);
5478
5479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5480 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5481 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5482 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5483 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5484 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5485 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5486 ASSERT_EQ(0, motionArgs.flags);
5487 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5488 ASSERT_EQ(0, motionArgs.buttonState);
5489 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005490 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005491 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005492 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5494 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5495 1, 0, 0, 0, 0, 0, 0, 0));
5496 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5497 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5498 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5499
5500 // Should not have sent any more keys or motions.
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5503}
5504
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005506 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005507 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005508 prepareButtons();
5509 prepareAxes(POSITION);
5510 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005511 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512
arthurhungdcef2dc2020-08-11 14:47:50 +08005513 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005514
5515 NotifyMotionArgs motionArgs;
5516
5517 // Down.
5518 int32_t x = 100;
5519 int32_t y = 125;
5520 processDown(mapper, x, y);
5521 processSync(mapper);
5522
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5524 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5525 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5526 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5527 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5528 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5529 ASSERT_EQ(0, motionArgs.flags);
5530 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5531 ASSERT_EQ(0, motionArgs.buttonState);
5532 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005533 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005534 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005535 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5537 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5538 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5539 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5540 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5541
5542 // Move.
5543 x += 50;
5544 y += 75;
5545 processMove(mapper, x, y);
5546 processSync(mapper);
5547
5548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5549 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5550 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5551 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5552 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5554 ASSERT_EQ(0, motionArgs.flags);
5555 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5556 ASSERT_EQ(0, motionArgs.buttonState);
5557 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005558 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005559 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005560 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5562 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5563 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5564 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5565 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5566
5567 // Up.
5568 processUp(mapper);
5569 processSync(mapper);
5570
5571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5572 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5573 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5574 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5575 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5576 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5577 ASSERT_EQ(0, motionArgs.flags);
5578 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5579 ASSERT_EQ(0, motionArgs.buttonState);
5580 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005581 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005582 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005583 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5585 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5586 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5587 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5588 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5589
5590 // Should not have sent any more keys or motions.
5591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5593}
5594
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005595TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005596 addConfigurationProperty("touch.deviceType", "touchScreen");
5597 prepareButtons();
5598 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005599 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5600 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singh56adebc2023-04-25 13:56:05 +00005601 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005602
5603 NotifyMotionArgs args;
5604
5605 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005606 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005607 processDown(mapper, toRawX(50), toRawY(75));
5608 processSync(mapper);
5609
5610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5611 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5612 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5613
5614 processUp(mapper);
5615 processSync(mapper);
5616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5617}
5618
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005619TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 addConfigurationProperty("touch.deviceType", "touchScreen");
5621 prepareButtons();
5622 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005623 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5624 // orientation-aware are affected by display rotation.
5625 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00005626 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005627
5628 NotifyMotionArgs args;
5629
5630 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005631 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005632 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005633 processDown(mapper, toRawX(50), toRawY(75));
5634 processSync(mapper);
5635
5636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5637 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5638 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5639
5640 processUp(mapper);
5641 processSync(mapper);
5642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5643
5644 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005645 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005646 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005647 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005648 processSync(mapper);
5649
5650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5651 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5652 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5653
5654 processUp(mapper);
5655 processSync(mapper);
5656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5657
5658 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005659 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005660 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005661 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5662 processSync(mapper);
5663
5664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5665 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5666 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5667
5668 processUp(mapper);
5669 processSync(mapper);
5670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5671
5672 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005673 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005674 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005675 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005676 processSync(mapper);
5677
5678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5679 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5680 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5681
5682 processUp(mapper);
5683 processSync(mapper);
5684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5685}
5686
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005687TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5688 addConfigurationProperty("touch.deviceType", "touchScreen");
5689 prepareButtons();
5690 prepareAxes(POSITION);
5691 addConfigurationProperty("touch.orientationAware", "1");
5692 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5693 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005694 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005695 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005696 NotifyMotionArgs args;
5697
5698 // Orientation 0.
5699 processDown(mapper, toRawX(50), toRawY(75));
5700 processSync(mapper);
5701
5702 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5703 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5704 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5705
5706 processUp(mapper);
5707 processSync(mapper);
5708 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5709}
5710
5711TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5712 addConfigurationProperty("touch.deviceType", "touchScreen");
5713 prepareButtons();
5714 prepareAxes(POSITION);
5715 addConfigurationProperty("touch.orientationAware", "1");
5716 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5717 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005718 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005719 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005720 NotifyMotionArgs args;
5721
5722 // Orientation 90.
5723 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5724 processSync(mapper);
5725
5726 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5727 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5728 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5729
5730 processUp(mapper);
5731 processSync(mapper);
5732 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5733}
5734
5735TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5736 addConfigurationProperty("touch.deviceType", "touchScreen");
5737 prepareButtons();
5738 prepareAxes(POSITION);
5739 addConfigurationProperty("touch.orientationAware", "1");
5740 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5741 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005742 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005743 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005744 NotifyMotionArgs args;
5745
5746 // Orientation 180.
5747 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5748 processSync(mapper);
5749
5750 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5751 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5752 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5753
5754 processUp(mapper);
5755 processSync(mapper);
5756 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5757}
5758
5759TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5760 addConfigurationProperty("touch.deviceType", "touchScreen");
5761 prepareButtons();
5762 prepareAxes(POSITION);
5763 addConfigurationProperty("touch.orientationAware", "1");
5764 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5765 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005766 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005767 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005768 NotifyMotionArgs args;
5769
5770 // Orientation 270.
5771 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5772 processSync(mapper);
5773
5774 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5775 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5776 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5777
5778 processUp(mapper);
5779 processSync(mapper);
5780 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5781}
5782
5783TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5784 addConfigurationProperty("touch.deviceType", "touchScreen");
5785 prepareButtons();
5786 prepareAxes(POSITION);
5787 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5788 // orientation-aware are affected by display rotation.
5789 addConfigurationProperty("touch.orientationAware", "0");
5790 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singh56adebc2023-04-25 13:56:05 +00005791 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005792
5793 NotifyMotionArgs args;
5794
5795 // Orientation 90, Rotation 0.
5796 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005797 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005798 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5799 processSync(mapper);
5800
5801 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5802 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5803 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5804
5805 processUp(mapper);
5806 processSync(mapper);
5807 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5808
5809 // Orientation 90, Rotation 90.
5810 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005811 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005812 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005813 processSync(mapper);
5814
5815 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5816 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5817 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5818
5819 processUp(mapper);
5820 processSync(mapper);
5821 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5822
5823 // Orientation 90, Rotation 180.
5824 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005825 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005826 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5827 processSync(mapper);
5828
5829 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5830 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5831 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5832
5833 processUp(mapper);
5834 processSync(mapper);
5835 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5836
5837 // Orientation 90, Rotation 270.
5838 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005839 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005840 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 -07005841 processSync(mapper);
5842
5843 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5844 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5845 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5846
5847 processUp(mapper);
5848 processSync(mapper);
5849 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5850}
5851
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005852TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5853 addConfigurationProperty("touch.deviceType", "touchScreen");
5854 prepareButtons();
5855 prepareAxes(POSITION);
5856 addConfigurationProperty("touch.orientationAware", "1");
5857 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005858 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005859
5860 // Set a physical frame in the display viewport.
5861 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5862 viewport->physicalLeft = 20;
5863 viewport->physicalTop = 600;
5864 viewport->physicalRight = 30;
5865 viewport->physicalBottom = 610;
5866 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005867 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005868
5869 // Start the touch.
5870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5871 processSync(mapper);
5872
5873 // Expect all input starting outside the physical frame to be ignored.
5874 const std::array<Point, 6> outsidePoints = {
5875 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5876 for (const auto& p : outsidePoints) {
5877 processMove(mapper, toRawX(p.x), toRawY(p.y));
5878 processSync(mapper);
5879 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5880 }
5881
5882 // Move the touch into the physical frame.
5883 processMove(mapper, toRawX(25), toRawY(605));
5884 processSync(mapper);
5885 NotifyMotionArgs args;
5886 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5887 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5888 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5889 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5890
5891 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5892 for (const auto& p : outsidePoints) {
5893 processMove(mapper, toRawX(p.x), toRawY(p.y));
5894 processSync(mapper);
5895 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5896 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5897 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5898 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5899 }
5900
5901 processUp(mapper);
5902 processSync(mapper);
5903 EXPECT_NO_FATAL_FAILURE(
5904 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5905}
5906
Harry Cutts1db43992023-06-19 17:05:07 +00005907TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
5908 std::shared_ptr<FakePointerController> fakePointerController =
5909 std::make_shared<FakePointerController>();
5910 mFakePolicy->setPointerController(fakePointerController);
5911
5912 addConfigurationProperty("touch.deviceType", "pointer");
5913 prepareAxes(POSITION);
5914 prepareDisplay(ui::ROTATION_0);
5915 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5916
5917 // Set a physical frame in the display viewport.
5918 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5919 viewport->physicalLeft = 20;
5920 viewport->physicalTop = 600;
5921 viewport->physicalRight = 30;
5922 viewport->physicalBottom = 610;
5923 mFakePolicy->updateViewport(*viewport);
5924 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5925
5926 // Start the touch.
5927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5928 processSync(mapper);
5929
5930 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5931 // produced.
5932 const std::array<Point, 6> outsidePoints = {
5933 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5934 for (const auto& p : outsidePoints) {
5935 processMove(mapper, toRawX(p.x), toRawY(p.y));
5936 processSync(mapper);
5937 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5938 }
5939}
5940
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005942 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005943 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005944 prepareButtons();
5945 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singh56adebc2023-04-25 13:56:05 +00005946 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005947
5948 // These calculations are based on the input device calibration documentation.
5949 int32_t rawX = 100;
5950 int32_t rawY = 200;
5951 int32_t rawPressure = 10;
5952 int32_t rawToolMajor = 12;
5953 int32_t rawDistance = 2;
5954 int32_t rawTiltX = 30;
5955 int32_t rawTiltY = 110;
5956
5957 float x = toDisplayX(rawX);
5958 float y = toDisplayY(rawY);
5959 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5960 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5961 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5962 float distance = float(rawDistance);
5963
5964 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5965 float tiltScale = M_PI / 180;
5966 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5967 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5968 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5969 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5970
5971 processDown(mapper, rawX, rawY);
5972 processPressure(mapper, rawPressure);
5973 processToolMajor(mapper, rawToolMajor);
5974 processDistance(mapper, rawDistance);
5975 processTilt(mapper, rawTiltX, rawTiltY);
5976 processSync(mapper);
5977
5978 NotifyMotionArgs args;
5979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5981 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5982 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5983}
5984
Jason Gerecke489fda82012-09-07 17:19:40 -07005985TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005986 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005987 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005988 prepareLocationCalibration();
5989 prepareButtons();
5990 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00005991 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005992
5993 int32_t rawX = 100;
5994 int32_t rawY = 200;
5995
5996 float x = toDisplayX(toCookedX(rawX, rawY));
5997 float y = toDisplayY(toCookedY(rawX, rawY));
5998
5999 processDown(mapper, rawX, rawY);
6000 processSync(mapper);
6001
6002 NotifyMotionArgs args;
6003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6005 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6006}
6007
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006009 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006010 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006011 prepareButtons();
6012 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006013 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006014
6015 NotifyMotionArgs motionArgs;
6016 NotifyKeyArgs keyArgs;
6017
6018 processDown(mapper, 100, 200);
6019 processSync(mapper);
6020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6021 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6022 ASSERT_EQ(0, motionArgs.buttonState);
6023
6024 // press BTN_LEFT, release BTN_LEFT
6025 processKey(mapper, BTN_LEFT, 1);
6026 processSync(mapper);
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6029 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6030
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6032 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6033 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6034
Michael Wrightd02c5b62014-02-10 15:10:22 -08006035 processKey(mapper, BTN_LEFT, 0);
6036 processSync(mapper);
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006038 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006039 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006040
6041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006043 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044
6045 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6046 processKey(mapper, BTN_RIGHT, 1);
6047 processKey(mapper, BTN_MIDDLE, 1);
6048 processSync(mapper);
6049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6051 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6052 motionArgs.buttonState);
6053
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6056 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6057
6058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6059 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6060 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6061 motionArgs.buttonState);
6062
Michael Wrightd02c5b62014-02-10 15:10:22 -08006063 processKey(mapper, BTN_RIGHT, 0);
6064 processSync(mapper);
6065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006066 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006067 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006068
6069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006071 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006072
6073 processKey(mapper, BTN_MIDDLE, 0);
6074 processSync(mapper);
6075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006077 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006078
6079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006081 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006082
6083 // press BTN_BACK, release BTN_BACK
6084 processKey(mapper, BTN_BACK, 1);
6085 processSync(mapper);
6086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6087 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6088 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006089
Michael Wrightd02c5b62014-02-10 15:10:22 -08006090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006092 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6093
6094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6095 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6096 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097
6098 processKey(mapper, BTN_BACK, 0);
6099 processSync(mapper);
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006101 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006103
6104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006106 ASSERT_EQ(0, motionArgs.buttonState);
6107
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6109 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6110 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6111
6112 // press BTN_SIDE, release BTN_SIDE
6113 processKey(mapper, BTN_SIDE, 1);
6114 processSync(mapper);
6115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6116 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6117 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006118
Michael Wrightd02c5b62014-02-10 15:10:22 -08006119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006121 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6122
6123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6125 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126
6127 processKey(mapper, BTN_SIDE, 0);
6128 processSync(mapper);
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006130 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006131 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006132
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006135 ASSERT_EQ(0, motionArgs.buttonState);
6136
Michael Wrightd02c5b62014-02-10 15:10:22 -08006137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6138 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6139 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6140
6141 // press BTN_FORWARD, release BTN_FORWARD
6142 processKey(mapper, BTN_FORWARD, 1);
6143 processSync(mapper);
6144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6145 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6146 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006147
Michael Wrightd02c5b62014-02-10 15:10:22 -08006148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006150 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6151
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6153 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6154 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155
6156 processKey(mapper, BTN_FORWARD, 0);
6157 processSync(mapper);
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006161
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006164 ASSERT_EQ(0, motionArgs.buttonState);
6165
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6167 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6168 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6169
6170 // press BTN_EXTRA, release BTN_EXTRA
6171 processKey(mapper, BTN_EXTRA, 1);
6172 processSync(mapper);
6173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6174 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6175 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006176
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006179 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6180
6181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6182 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6183 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184
6185 processKey(mapper, BTN_EXTRA, 0);
6186 processSync(mapper);
6187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006190
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006193 ASSERT_EQ(0, motionArgs.buttonState);
6194
Michael Wrightd02c5b62014-02-10 15:10:22 -08006195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6196 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6197 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6198
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6200
Michael Wrightd02c5b62014-02-10 15:10:22 -08006201 // press BTN_STYLUS, release BTN_STYLUS
6202 processKey(mapper, BTN_STYLUS, 1);
6203 processSync(mapper);
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006206 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6207
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6209 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6210 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006211
6212 processKey(mapper, BTN_STYLUS, 0);
6213 processSync(mapper);
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006215 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006216 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006217
6218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006220 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006221
6222 // press BTN_STYLUS2, release BTN_STYLUS2
6223 processKey(mapper, BTN_STYLUS2, 1);
6224 processSync(mapper);
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006227 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6228
6229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6231 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006232
6233 processKey(mapper, BTN_STYLUS2, 0);
6234 processSync(mapper);
6235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006236 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006237 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006238
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006241 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006242
6243 // release touch
6244 processUp(mapper);
6245 processSync(mapper);
6246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6247 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6248 ASSERT_EQ(0, motionArgs.buttonState);
6249}
6250
6251TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006253 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006254 prepareButtons();
6255 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006256 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006257
6258 NotifyMotionArgs motionArgs;
6259
6260 // default tool type is finger
6261 processDown(mapper, 100, 200);
6262 processSync(mapper);
6263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6264 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006265 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006266
6267 // eraser
6268 processKey(mapper, BTN_TOOL_RUBBER, 1);
6269 processSync(mapper);
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006272 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273
6274 // stylus
6275 processKey(mapper, BTN_TOOL_RUBBER, 0);
6276 processKey(mapper, BTN_TOOL_PEN, 1);
6277 processSync(mapper);
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006280 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281
6282 // brush
6283 processKey(mapper, BTN_TOOL_PEN, 0);
6284 processKey(mapper, BTN_TOOL_BRUSH, 1);
6285 processSync(mapper);
6286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006288 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289
6290 // pencil
6291 processKey(mapper, BTN_TOOL_BRUSH, 0);
6292 processKey(mapper, BTN_TOOL_PENCIL, 1);
6293 processSync(mapper);
6294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6295 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006296 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006297
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006298 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006299 processKey(mapper, BTN_TOOL_PENCIL, 0);
6300 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6301 processSync(mapper);
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006304 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305
6306 // mouse
6307 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6308 processKey(mapper, BTN_TOOL_MOUSE, 1);
6309 processSync(mapper);
6310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006312 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313
6314 // lens
6315 processKey(mapper, BTN_TOOL_MOUSE, 0);
6316 processKey(mapper, BTN_TOOL_LENS, 1);
6317 processSync(mapper);
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006320 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006321
6322 // double-tap
6323 processKey(mapper, BTN_TOOL_LENS, 0);
6324 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6325 processSync(mapper);
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006328 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329
6330 // triple-tap
6331 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6332 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6333 processSync(mapper);
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006336 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006337
6338 // quad-tap
6339 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6340 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6341 processSync(mapper);
6342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006344 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006345
6346 // finger
6347 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6348 processKey(mapper, BTN_TOOL_FINGER, 1);
6349 processSync(mapper);
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006352 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006353
6354 // stylus trumps finger
6355 processKey(mapper, BTN_TOOL_PEN, 1);
6356 processSync(mapper);
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006359 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360
6361 // eraser trumps stylus
6362 processKey(mapper, BTN_TOOL_RUBBER, 1);
6363 processSync(mapper);
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006366 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367
6368 // mouse trumps eraser
6369 processKey(mapper, BTN_TOOL_MOUSE, 1);
6370 processSync(mapper);
6371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006373 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006374
6375 // back to default tool type
6376 processKey(mapper, BTN_TOOL_MOUSE, 0);
6377 processKey(mapper, BTN_TOOL_RUBBER, 0);
6378 processKey(mapper, BTN_TOOL_PEN, 0);
6379 processKey(mapper, BTN_TOOL_FINGER, 0);
6380 processSync(mapper);
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006383 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006384}
6385
6386TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006387 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006388 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389 prepareButtons();
6390 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006391 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +00006392 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006393
6394 NotifyMotionArgs motionArgs;
6395
6396 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6397 processKey(mapper, BTN_TOOL_FINGER, 1);
6398 processMove(mapper, 100, 200);
6399 processSync(mapper);
6400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6401 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6403 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6404
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6406 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6408 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6409
6410 // move a little
6411 processMove(mapper, 150, 250);
6412 processSync(mapper);
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6414 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6416 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6417
6418 // down when BTN_TOUCH is pressed, pressure defaults to 1
6419 processKey(mapper, BTN_TOUCH, 1);
6420 processSync(mapper);
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6424 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6425
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6427 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6429 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6430
6431 // up when BTN_TOUCH is released, hover restored
6432 processKey(mapper, BTN_TOUCH, 0);
6433 processSync(mapper);
6434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6435 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6437 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6438
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6440 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6442 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6443
6444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6445 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6447 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6448
6449 // exit hover when pointer goes away
6450 processKey(mapper, BTN_TOOL_FINGER, 0);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6455 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6456}
6457
6458TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006459 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006460 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006461 prepareButtons();
6462 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006463 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006464
6465 NotifyMotionArgs motionArgs;
6466
6467 // initially hovering because pressure is 0
6468 processDown(mapper, 100, 200);
6469 processPressure(mapper, 0);
6470 processSync(mapper);
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6472 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6474 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6475
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6477 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6479 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6480
6481 // move a little
6482 processMove(mapper, 150, 250);
6483 processSync(mapper);
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6485 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6487 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6488
6489 // down when pressure is non-zero
6490 processPressure(mapper, RAW_PRESSURE_MAX);
6491 processSync(mapper);
6492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6493 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6495 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6496
6497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6498 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6500 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6501
6502 // up when pressure becomes 0, hover restored
6503 processPressure(mapper, 0);
6504 processSync(mapper);
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6506 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6508 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6509
6510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6511 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6513 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6514
6515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6516 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6518 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6519
6520 // exit hover when pointer goes away
6521 processUp(mapper);
6522 processSync(mapper);
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6524 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6526 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6527}
6528
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006529TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6530 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006531 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006532 prepareButtons();
6533 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006534 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006535
6536 // Touch down.
6537 processDown(mapper, 100, 200);
6538 processPressure(mapper, 1);
6539 processSync(mapper);
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6541 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6542
6543 // Reset the mapper. This should cancel the ongoing gesture.
6544 resetMapper(mapper, ARBITRARY_TIME);
6545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6546 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6547
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6549}
6550
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006551TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6552 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006553 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006554 prepareButtons();
6555 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006556 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006557
6558 // Set the initial state for the touch pointer.
6559 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6560 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6561 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6562 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6563
6564 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006565 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6566 // does not generate any events.
6567 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006568
6569 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6570 // the recreated touch state to generate a down event.
6571 processSync(mapper);
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6573 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6574
6575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6576}
6577
lilinnan687e58f2022-07-19 16:00:50 +08006578TEST_F(SingleTouchInputMapperTest,
6579 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6580 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006581 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006582 prepareButtons();
6583 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006584 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006585 NotifyMotionArgs motionArgs;
6586
6587 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006588 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006589 processSync(mapper);
6590
6591 // We should receive a down event
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6593 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6594
6595 // Change display id
6596 clearViewports();
6597 prepareSecondaryDisplay(ViewportType::INTERNAL);
6598
6599 // We should receive a cancel event
6600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6601 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6602 // Then receive reset called
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6604}
6605
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006606TEST_F(SingleTouchInputMapperTest,
6607 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6608 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006609 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006610 prepareButtons();
6611 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006612 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6614 NotifyMotionArgs motionArgs;
6615
6616 // Start a new gesture.
6617 processDown(mapper, 100, 200);
6618 processSync(mapper);
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6621
6622 // Make the viewport inactive. This will put the device in disabled mode.
6623 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6624 viewport->isActive = false;
6625 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006626 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006627
6628 // We should receive a cancel event for the ongoing gesture.
6629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6630 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6631 // Then we should be notified that the device was reset.
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6633
6634 // No events are generated while the viewport is inactive.
6635 processMove(mapper, 101, 201);
6636 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006637 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006638 processSync(mapper);
6639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6640
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006641 // Start a new gesture while the viewport is still inactive.
6642 processDown(mapper, 300, 400);
6643 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6644 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6645 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6646 processSync(mapper);
6647
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006648 // Make the viewport active again. The device should resume processing events.
6649 viewport->isActive = true;
6650 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006651 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006652
6653 // The device is reset because it changes back to direct mode, without generating any events.
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6656
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006657 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006658 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6660 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006661
6662 // No more events.
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6665}
6666
Prabir Pradhan211ba622022-10-31 21:09:21 +00006667TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6668 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006669 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006670 prepareButtons();
6671 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006672 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6674
6675 // Press a stylus button.
6676 processKey(mapper, BTN_STYLUS, 1);
6677 processSync(mapper);
6678
6679 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6680 processDown(mapper, 100, 200);
6681 processSync(mapper);
6682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6683 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6684 WithCoords(toDisplayX(100), toDisplayY(200)),
6685 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6687 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6688 WithCoords(toDisplayX(100), toDisplayY(200)),
6689 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6690
6691 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6692 // the button has not actually been released, since there will be no pointers through which the
6693 // button state can be reported. The event is generated at the location of the pointer before
6694 // it went up.
6695 processUp(mapper);
6696 processSync(mapper);
6697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6698 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6699 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6701 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6702 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6703}
6704
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006705TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6706 addConfigurationProperty("touch.deviceType", "touchScreen");
6707 prepareDisplay(ui::ROTATION_0);
6708 prepareButtons();
6709 prepareAxes(POSITION);
6710
6711 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6712
Arpit Singh56adebc2023-04-25 13:56:05 +00006713 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6715
6716 // Press a stylus button.
6717 processKey(mapper, BTN_STYLUS, 1);
6718 processSync(mapper);
6719
6720 // Start a touch gesture and ensure that the stylus button is not reported.
6721 processDown(mapper, 100, 200);
6722 processSync(mapper);
6723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6724 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6725
6726 // Release and press the stylus button again.
6727 processKey(mapper, BTN_STYLUS, 0);
6728 processSync(mapper);
6729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6730 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6731 processKey(mapper, BTN_STYLUS, 1);
6732 processSync(mapper);
6733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6734 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6735
6736 // Release the touch gesture.
6737 processUp(mapper);
6738 processSync(mapper);
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6740 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6741
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6743}
6744
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006745TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6746 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6747 prepareDisplay(ui::ROTATION_0);
6748 prepareButtons();
6749 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006750 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6752
6753 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6754}
6755
Seunghwan Choi356026c2023-02-01 14:37:25 +09006756TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6757 std::shared_ptr<FakePointerController> fakePointerController =
6758 std::make_shared<FakePointerController>();
6759 addConfigurationProperty("touch.deviceType", "touchScreen");
6760 prepareDisplay(ui::ROTATION_0);
6761 prepareButtons();
6762 prepareAxes(POSITION);
6763 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6764 mFakePolicy->setPointerController(fakePointerController);
6765 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singh56adebc2023-04-25 13:56:05 +00006766 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006767
6768 processKey(mapper, BTN_TOOL_PEN, 1);
6769 processMove(mapper, 100, 200);
6770 processSync(mapper);
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6772 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006773 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006774 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6775 ASSERT_TRUE(fakePointerController->isPointerShown());
6776 ASSERT_NO_FATAL_FAILURE(
6777 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6778}
6779
6780TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6781 std::shared_ptr<FakePointerController> fakePointerController =
6782 std::make_shared<FakePointerController>();
6783 addConfigurationProperty("touch.deviceType", "touchScreen");
6784 prepareDisplay(ui::ROTATION_0);
6785 prepareButtons();
6786 prepareAxes(POSITION);
6787 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6788 mFakePolicy->setPointerController(fakePointerController);
6789 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singh56adebc2023-04-25 13:56:05 +00006790 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006791
6792 processKey(mapper, BTN_TOOL_PEN, 1);
6793 processMove(mapper, 100, 200);
6794 processSync(mapper);
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6796 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006797 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006798 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6799 ASSERT_FALSE(fakePointerController->isPointerShown());
6800}
6801
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006802TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6803 // Initialize the device without setting device source to touch navigation.
6804 addConfigurationProperty("touch.deviceType", "touchScreen");
6805 prepareDisplay(ui::ROTATION_0);
6806 prepareButtons();
6807 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006808 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006809
6810 // Ensure that the device is created as a touchscreen, not touch navigation.
6811 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6812
6813 // Add device type association after the device was created.
6814 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6815
6816 // Send update to the mapper.
6817 std::list<NotifyArgs> unused2 =
6818 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006819 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006820
6821 // Check whether device type update was successful.
6822 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6823}
6824
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006825TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6826 // Initialize the device without setting device source to touch navigation.
6827 addConfigurationProperty("touch.deviceType", "touchScreen");
6828 prepareDisplay(ui::ROTATION_0);
6829 prepareButtons();
6830 prepareAxes(POSITION);
6831 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6832
6833 // Set a physical frame in the display viewport.
6834 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6835 viewport->physicalLeft = 0;
6836 viewport->physicalTop = 0;
6837 viewport->physicalRight = DISPLAY_WIDTH / 2;
6838 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6839 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006840 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006841
Arpit Singh56adebc2023-04-25 13:56:05 +00006842 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006843
6844 // Hovering inside the physical frame produces events.
6845 processKey(mapper, BTN_TOOL_PEN, 1);
6846 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6847 processSync(mapper);
6848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6849 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6851 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6852
6853 // Leaving the physical frame ends the hovering gesture.
6854 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6855 processSync(mapper);
6856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6857 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6858
6859 // Moving outside the physical frame does not produce events.
6860 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6861 processSync(mapper);
6862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6863
6864 // Re-entering the physical frame produces events.
6865 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6866 processSync(mapper);
6867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6868 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6870 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6871}
6872
Prabir Pradhan5632d622021-09-06 07:57:20 -07006873// --- TouchDisplayProjectionTest ---
6874
6875class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6876public:
6877 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6878 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6879 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006880 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6881 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6882 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006883 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006884 auto rotatedWidth = naturalDisplayWidth;
6885 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006886 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006887 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006888 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006889 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006890 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006891 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006892 inverseRotationFlags = ui::Transform::ROT_180;
6893 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006894 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006895 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006896 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006897 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006898 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006899 inverseRotationFlags = ui::Transform::ROT_0;
6900 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006901 }
6902
Prabir Pradhana9df3162022-12-05 23:57:27 +00006903 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006904 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6905
6906 std::optional<DisplayViewport> internalViewport =
6907 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6908 DisplayViewport& v = *internalViewport;
6909 v.displayId = DISPLAY_ID;
6910 v.orientation = orientation;
6911
6912 v.logicalLeft = 0;
6913 v.logicalTop = 0;
6914 v.logicalRight = 100;
6915 v.logicalBottom = 100;
6916
6917 v.physicalLeft = rotatedPhysicalDisplay.left;
6918 v.physicalTop = rotatedPhysicalDisplay.top;
6919 v.physicalRight = rotatedPhysicalDisplay.right;
6920 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6921
Prabir Pradhana9df3162022-12-05 23:57:27 +00006922 v.deviceWidth = rotatedWidth;
6923 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006924
6925 v.isActive = true;
6926 v.uniqueId = UNIQUE_ID;
6927 v.type = ViewportType::INTERNAL;
6928 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006929 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006930 }
6931
6932 void assertReceivedMove(const Point& point) {
6933 NotifyMotionArgs motionArgs;
6934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006936 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6938 1, 0, 0, 0, 0, 0, 0, 0));
6939 }
6940};
6941
6942TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6943 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006944 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006945
6946 prepareButtons();
6947 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006948 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006949
6950 NotifyMotionArgs motionArgs;
6951
6952 // Configure the DisplayViewport such that the logical display maps to a subsection of
6953 // the display panel called the physical display. Here, the physical display is bounded by the
6954 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6955 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6956 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6957 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6958
Michael Wrighta9cf4192022-12-01 23:46:39 +00006959 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006960 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6961
6962 // Touches outside the physical display should be ignored, and should not generate any
6963 // events. Ensure touches at the following points that lie outside of the physical display
6964 // area do not generate any events.
6965 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6966 processDown(mapper, toRawX(point.x), toRawY(point.y));
6967 processSync(mapper);
6968 processUp(mapper);
6969 processSync(mapper);
6970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6971 << "Unexpected event generated for touch outside physical display at point: "
6972 << point.x << ", " << point.y;
6973 }
6974 }
6975}
6976
6977TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6978 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006979 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006980
6981 prepareButtons();
6982 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006983 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006984
6985 NotifyMotionArgs motionArgs;
6986
6987 // Configure the DisplayViewport such that the logical display maps to a subsection of
6988 // the display panel called the physical display. Here, the physical display is bounded by the
6989 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6990 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6991
Michael Wrighta9cf4192022-12-01 23:46:39 +00006992 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006993 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6994
6995 // Touches that start outside the physical display should be ignored until it enters the
6996 // physical display bounds, at which point it should generate a down event. Start a touch at
6997 // the point (5, 100), which is outside the physical display bounds.
6998 static const Point kOutsidePoint{5, 100};
6999 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7000 processSync(mapper);
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7002
7003 // Move the touch into the physical display area. This should generate a pointer down.
7004 processMove(mapper, toRawX(11), toRawY(21));
7005 processSync(mapper);
7006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7007 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007008 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007009 ASSERT_NO_FATAL_FAILURE(
7010 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7011
7012 // Move the touch inside the physical display area. This should generate a pointer move.
7013 processMove(mapper, toRawX(69), toRawY(159));
7014 processSync(mapper);
7015 assertReceivedMove({69, 159});
7016
7017 // Move outside the physical display area. Since the pointer is already down, this should
7018 // now continue generating events.
7019 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7020 processSync(mapper);
7021 assertReceivedMove(kOutsidePoint);
7022
7023 // Release. This should generate a pointer up.
7024 processUp(mapper);
7025 processSync(mapper);
7026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7027 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7029 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7030
7031 // Ensure no more events were generated.
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7034 }
7035}
7036
Prabir Pradhana9df3162022-12-05 23:57:27 +00007037// --- TouchscreenPrecisionTests ---
7038
7039// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7040// in various orientations and with different display rotations. We configure the touchscreen to
7041// have a higher resolution than that of the display by an integer scale factor in each axis so that
7042// we can enforce that coordinates match precisely as expected.
7043class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7044 public ::testing::WithParamInterface<ui::Rotation> {
7045public:
7046 void SetUp() override {
7047 SingleTouchInputMapperTest::SetUp();
7048
7049 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7050 // four times the resolution of the display in the Y axis.
7051 prepareButtons();
7052 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007053 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7054 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007055 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007056 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7057 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007058 }
7059
7060 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7061 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7062 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7063 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7064
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007065 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7066 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7067
7068 static const int32_t PRECISION_RAW_X_FLAT = 16;
7069 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7070
7071 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7072 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7073
Prabir Pradhana9df3162022-12-05 23:57:27 +00007074 static const std::array<Point, 4> kRawCorners;
7075};
7076
7077const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7078 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7079 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7080 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7081 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7082}};
7083
7084// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7085// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7086// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7087TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7088 enum class Orientation {
7089 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7090 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7091 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7092 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7093 ftl_last = ORIENTATION_270,
7094 };
7095 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7096 Orientation::ORIENTATION_270;
7097 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7098 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7099 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7100 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7101 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7102 };
7103
7104 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7105
7106 // Configure the touchscreen as being installed in the one of the four different orientations
7107 // relative to the display.
7108 addConfigurationProperty("touch.deviceType", "touchScreen");
7109 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7110 prepareDisplay(ui::ROTATION_0);
7111
Arpit Singh56adebc2023-04-25 13:56:05 +00007112 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007113
7114 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7115 // orientations of either 90 or 270) this means the display's natural resolution will be
7116 // flipped.
7117 const bool displayRotated =
7118 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7119 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7120 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7121 const Rect physicalFrame{0, 0, width, height};
7122 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7123
7124 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7125 const float expectedPrecisionX = displayRotated ? 4 : 2;
7126 const float expectedPrecisionY = displayRotated ? 2 : 4;
7127
7128 // Test all four corners.
7129 for (int i = 0; i < 4; i++) {
7130 const auto& raw = kRawCorners[i];
7131 processDown(mapper, raw.x, raw.y);
7132 processSync(mapper);
7133 const auto& expected = expectedPoints[i];
7134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7135 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7136 WithCoords(expected.x, expected.y),
7137 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7138 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7139 << "with touchscreen orientation "
7140 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7141 << expected.x << ", " << expected.y << ").";
7142 processUp(mapper);
7143 processSync(mapper);
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7145 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7146 WithCoords(expected.x, expected.y))));
7147 }
7148}
7149
Prabir Pradhan82687402022-12-06 01:32:53 +00007150TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7151 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7152 kMappedCorners = {
7153 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7154 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7155 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7156 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7157 };
7158
7159 const ui::Rotation displayRotation = GetParam();
7160
7161 addConfigurationProperty("touch.deviceType", "touchScreen");
7162 prepareDisplay(displayRotation);
7163
Arpit Singh56adebc2023-04-25 13:56:05 +00007164 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007165
7166 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7167
7168 // Test all four corners.
7169 for (int i = 0; i < 4; i++) {
7170 const auto& expected = expectedPoints[i];
7171 const auto& raw = kRawCorners[i];
7172 processDown(mapper, raw.x, raw.y);
7173 processSync(mapper);
7174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7175 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7176 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7177 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7178 << "with display rotation " << ui::toCString(displayRotation)
7179 << ", expected point (" << expected.x << ", " << expected.y << ").";
7180 processUp(mapper);
7181 processSync(mapper);
7182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7183 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7184 WithCoords(expected.x, expected.y))));
7185 }
7186}
7187
Prabir Pradhan3e798762022-12-02 21:02:11 +00007188TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7189 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7190 kMappedCorners = {
7191 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7192 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7193 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7194 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7195 };
7196
7197 const ui::Rotation displayRotation = GetParam();
7198
7199 addConfigurationProperty("touch.deviceType", "touchScreen");
7200 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7201
Arpit Singh56adebc2023-04-25 13:56:05 +00007202 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007203
7204 // Ori 270, so width and height swapped
7205 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7206 prepareDisplay(displayRotation);
7207 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7208
7209 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7210
7211 // Test all four corners.
7212 for (int i = 0; i < 4; i++) {
7213 const auto& expected = expectedPoints[i];
7214 const auto& raw = kRawCorners[i];
7215 processDown(mapper, raw.x, raw.y);
7216 processSync(mapper);
7217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7218 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7219 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7220 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7221 << "with display rotation " << ui::toCString(displayRotation)
7222 << ", expected point (" << expected.x << ", " << expected.y << ").";
7223 processUp(mapper);
7224 processSync(mapper);
7225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7226 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7227 WithCoords(expected.x, expected.y))));
7228 }
7229}
7230
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007231TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7232 const ui::Rotation displayRotation = GetParam();
7233
7234 addConfigurationProperty("touch.deviceType", "touchScreen");
7235 prepareDisplay(displayRotation);
7236
7237 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singh56adebc2023-04-25 13:56:05 +00007238 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007239
7240 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7241 // MotionRanges use display pixels as their units
7242 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7243 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7244
7245 // The MotionRanges should be oriented in the rotated display's coordinate space
7246 const bool displayRotated =
7247 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7248
7249 constexpr float MAX_X = 479.5;
7250 constexpr float MAX_Y = 799.75;
7251 EXPECT_EQ(xRange->min, 0.f);
7252 EXPECT_EQ(yRange->min, 0.f);
7253 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7254 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7255
7256 EXPECT_EQ(xRange->flat, 8.f);
7257 EXPECT_EQ(yRange->flat, 8.f);
7258
7259 EXPECT_EQ(xRange->fuzz, 2.f);
7260 EXPECT_EQ(yRange->fuzz, 2.f);
7261
7262 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7263 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7264}
7265
Prabir Pradhana9df3162022-12-05 23:57:27 +00007266// Run the precision tests for all rotations.
7267INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7268 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7269 ui::ROTATION_270),
7270 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7271 return ftl::enum_string(testParamInfo.param);
7272 });
7273
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007274// --- ExternalStylusFusionTest ---
7275
7276class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7277public:
7278 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7279 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007280 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007281 prepareButtons();
7282 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00007283 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007284
7285 mStylusState.when = ARBITRARY_TIME;
7286 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007287 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007288 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007289 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007290 processExternalStylusState(mapper);
7291 return mapper;
7292 }
7293
7294 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7295 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7296 for (const NotifyArgs& args : generatedArgs) {
7297 mFakeListener->notify(args);
7298 }
7299 // Loop the reader to flush the input listener queue.
7300 mReader->loopOnce();
7301 return generatedArgs;
7302 }
7303
7304protected:
7305 StylusState mStylusState{};
7306 static constexpr uint32_t EXPECTED_SOURCE =
7307 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7308
7309 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7310 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007311 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007312
7313 // The first pointer is withheld.
7314 processDown(mapper, 100, 200);
7315 processSync(mapper);
7316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7317 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7318 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7319
7320 // The external stylus reports pressure. The withheld finger pointer is released as a
7321 // stylus.
7322 mStylusState.pressure = 1.f;
7323 processExternalStylusState(mapper);
7324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7325 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7326 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7327
7328 // Subsequent pointer events are not withheld.
7329 processMove(mapper, 101, 201);
7330 processSync(mapper);
7331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7332 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7333
7334 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7336 }
7337
7338 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7339 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7340
7341 // Releasing the touch pointer ends the gesture.
7342 processUp(mapper);
7343 processSync(mapper);
7344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7345 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007346 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007347
7348 mStylusState.pressure = 0.f;
7349 processExternalStylusState(mapper);
7350 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7352 }
7353
7354 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7355 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007356 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007357
7358 // The first pointer is withheld when an external stylus is connected,
7359 // and a timeout is requested.
7360 processDown(mapper, 100, 200);
7361 processSync(mapper);
7362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7363 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7364 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7365
7366 // If the timeout expires early, it is requested again.
7367 handleTimeout(mapper, ARBITRARY_TIME + 1);
7368 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7369 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7370
7371 // When the timeout expires, the withheld touch is released as a finger pointer.
7372 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7374 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7375
7376 // Subsequent pointer events are not withheld.
7377 processMove(mapper, 101, 201);
7378 processSync(mapper);
7379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7380 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7381 processUp(mapper);
7382 processSync(mapper);
7383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7384 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7385
7386 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7388 }
7389
7390private:
7391 InputDeviceInfo mExternalStylusDeviceInfo{};
7392};
7393
7394TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7395 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7396 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7397}
7398
7399TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7400 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7401 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7402}
7403
7404TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7405 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7406 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7407}
7408
7409// Test a successful stylus fusion gesture where the pressure is reported by the external
7410// before the touch is reported by the touchscreen.
7411TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7412 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7413 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007414 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007415
7416 // The external stylus reports pressure first. It is ignored for now.
7417 mStylusState.pressure = 1.f;
7418 processExternalStylusState(mapper);
7419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7420 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7421
7422 // When the touch goes down afterwards, it is reported as a stylus pointer.
7423 processDown(mapper, 100, 200);
7424 processSync(mapper);
7425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7426 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7427 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7428
7429 processMove(mapper, 101, 201);
7430 processSync(mapper);
7431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7432 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7433 processUp(mapper);
7434 processSync(mapper);
7435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7436 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7437
7438 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7440}
7441
7442TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7443 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7444
7445 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7446 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7447
7448 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7449 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7450 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7451 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7452}
7453
7454TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7455 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7456 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007457 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007458
7459 mStylusState.pressure = 0.8f;
7460 processExternalStylusState(mapper);
7461 processDown(mapper, 100, 200);
7462 processSync(mapper);
7463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7464 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7465 WithPressure(0.8f))));
7466 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7467
7468 // The external stylus reports a pressure change. We wait for some time for a touch event.
7469 mStylusState.pressure = 0.6f;
7470 processExternalStylusState(mapper);
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7472 ASSERT_NO_FATAL_FAILURE(
7473 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7474
7475 // If a touch is reported within the timeout, it reports the updated pressure.
7476 processMove(mapper, 101, 201);
7477 processSync(mapper);
7478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7479 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7480 WithPressure(0.6f))));
7481 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7482
7483 // There is another pressure change.
7484 mStylusState.pressure = 0.5f;
7485 processExternalStylusState(mapper);
7486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7487 ASSERT_NO_FATAL_FAILURE(
7488 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7489
7490 // If a touch is not reported within the timeout, a move event is generated to report
7491 // the new pressure.
7492 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7494 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7495 WithPressure(0.5f))));
7496
7497 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7498 // repeated indefinitely.
7499 mStylusState.pressure = 0.0f;
7500 processExternalStylusState(mapper);
7501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7502 ASSERT_NO_FATAL_FAILURE(
7503 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7504 processMove(mapper, 102, 202);
7505 processSync(mapper);
7506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7507 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7508 WithPressure(0.5f))));
7509 processMove(mapper, 103, 203);
7510 processSync(mapper);
7511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7512 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7513 WithPressure(0.5f))));
7514
7515 processUp(mapper);
7516 processSync(mapper);
7517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7518 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007519 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007520
7521 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7523}
7524
7525TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7526 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7527 auto source = WithSource(EXPECTED_SOURCE);
7528
7529 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007530 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007531 processExternalStylusState(mapper);
7532 processDown(mapper, 100, 200);
7533 processSync(mapper);
7534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7535 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007536 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007537 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7538
7539 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007540 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007541 processExternalStylusState(mapper);
7542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7543 ASSERT_NO_FATAL_FAILURE(
7544 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7545
7546 // If a touch is reported within the timeout, it reports the updated pressure.
7547 processMove(mapper, 101, 201);
7548 processSync(mapper);
7549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7550 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007551 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007552 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7553
7554 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007555 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007556 processExternalStylusState(mapper);
7557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7558 ASSERT_NO_FATAL_FAILURE(
7559 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7560
7561 // If a touch is not reported within the timeout, a move event is generated to report
7562 // the new tool type.
7563 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7565 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007566 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007567
7568 processUp(mapper);
7569 processSync(mapper);
7570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7571 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007572 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007573
7574 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7576}
7577
7578TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7579 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7580 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007581 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007582
7583 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7584
7585 // The external stylus reports a button change. We wait for some time for a touch event.
7586 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7587 processExternalStylusState(mapper);
7588 ASSERT_NO_FATAL_FAILURE(
7589 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7590
7591 // If a touch is reported within the timeout, it reports the updated button state.
7592 processMove(mapper, 101, 201);
7593 processSync(mapper);
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7595 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7596 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7598 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7599 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7600 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7601
7602 // The button is now released.
7603 mStylusState.buttons = 0;
7604 processExternalStylusState(mapper);
7605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7606 ASSERT_NO_FATAL_FAILURE(
7607 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7608
7609 // If a touch is not reported within the timeout, a move event is generated to report
7610 // the new button state.
7611 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7613 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7614 WithButtonState(0))));
7615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007616 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7617 WithButtonState(0))));
7618
7619 processUp(mapper);
7620 processSync(mapper);
7621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007622 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7623
7624 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7626}
7627
Michael Wrightd02c5b62014-02-10 15:10:22 -08007628// --- MultiTouchInputMapperTest ---
7629
7630class MultiTouchInputMapperTest : public TouchInputMapperTest {
7631protected:
7632 void prepareAxes(int axes);
7633
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007634 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7635 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7636 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7637 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7638 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7639 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7640 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7641 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7642 void processId(MultiTouchInputMapper& mapper, int32_t id);
7643 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7644 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7645 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007646 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007647 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007648 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7649 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007650};
7651
7652void MultiTouchInputMapperTest::prepareAxes(int axes) {
7653 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007654 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7655 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007656 }
7657 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007658 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7659 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007660 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007661 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7662 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007663 }
7664 }
7665 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007666 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7667 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007668 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007669 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007670 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007671 }
7672 }
7673 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007674 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7675 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007676 }
7677 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007678 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7679 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007680 }
7681 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007682 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7683 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007684 }
7685 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007686 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7687 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007688 }
7689 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007690 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7691 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007692 }
7693 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007694 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007695 }
7696}
7697
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007698void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7699 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7701 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007702}
7703
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007704void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7705 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007707}
7708
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007709void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7710 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007712}
7713
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007714void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007716}
7717
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007718void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007720}
7721
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007722void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7723 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007724 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007725}
7726
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007727void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007729}
7730
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007731void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007733}
7734
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007735void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007737}
7738
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007739void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007741}
7742
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007743void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007744 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007745}
7746
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007747void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7748 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007749 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007750}
7751
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007752void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7753 int32_t value) {
7754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7755 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7756}
7757
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007758void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007760}
7761
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007762void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7763 nsecs_t readTime) {
7764 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007765}
7766
Michael Wrightd02c5b62014-02-10 15:10:22 -08007767TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007768 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007769 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007770 prepareAxes(POSITION);
7771 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00007772 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007773
arthurhungdcef2dc2020-08-11 14:47:50 +08007774 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007775
7776 NotifyMotionArgs motionArgs;
7777
7778 // Two fingers down at once.
7779 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7780 processPosition(mapper, x1, y1);
7781 processMTSync(mapper);
7782 processPosition(mapper, x2, y2);
7783 processMTSync(mapper);
7784 processSync(mapper);
7785
7786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7787 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7788 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7789 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7790 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7791 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7792 ASSERT_EQ(0, motionArgs.flags);
7793 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7794 ASSERT_EQ(0, motionArgs.buttonState);
7795 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007796 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007797 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007798 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7800 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7801 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7802 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7803 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7804
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7806 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7807 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7808 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7809 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007810 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007811 ASSERT_EQ(0, motionArgs.flags);
7812 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7813 ASSERT_EQ(0, motionArgs.buttonState);
7814 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007815 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007816 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007817 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007818 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007819 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7821 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7823 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7824 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7825 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7826 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7827
7828 // Move.
7829 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7830 processPosition(mapper, x1, y1);
7831 processMTSync(mapper);
7832 processPosition(mapper, x2, y2);
7833 processMTSync(mapper);
7834 processSync(mapper);
7835
7836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7837 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7838 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7839 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7840 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7842 ASSERT_EQ(0, motionArgs.flags);
7843 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7844 ASSERT_EQ(0, motionArgs.buttonState);
7845 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007846 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007847 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007848 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007849 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007850 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7852 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7854 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7855 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7856 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7857 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7858
7859 // First finger up.
7860 x2 += 15; y2 -= 20;
7861 processPosition(mapper, x2, y2);
7862 processMTSync(mapper);
7863 processSync(mapper);
7864
7865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7866 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7867 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7868 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7869 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007870 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007871 ASSERT_EQ(0, motionArgs.flags);
7872 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7873 ASSERT_EQ(0, motionArgs.buttonState);
7874 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007875 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007876 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007877 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007878 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007879 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7881 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7883 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7884 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7885 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7886 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7887
7888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7889 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7890 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7891 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7892 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7893 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7894 ASSERT_EQ(0, motionArgs.flags);
7895 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7896 ASSERT_EQ(0, motionArgs.buttonState);
7897 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007898 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007899 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007900 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7902 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7903 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7904 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7905 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7906
7907 // Move.
7908 x2 += 20; y2 -= 25;
7909 processPosition(mapper, x2, y2);
7910 processMTSync(mapper);
7911 processSync(mapper);
7912
7913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7914 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7915 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7916 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7917 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7919 ASSERT_EQ(0, motionArgs.flags);
7920 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7921 ASSERT_EQ(0, motionArgs.buttonState);
7922 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007923 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007924 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007925 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7927 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7928 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7929 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7930 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7931
7932 // New finger down.
7933 int32_t x3 = 700, y3 = 300;
7934 processPosition(mapper, x2, y2);
7935 processMTSync(mapper);
7936 processPosition(mapper, x3, y3);
7937 processMTSync(mapper);
7938 processSync(mapper);
7939
7940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7941 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7942 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7943 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7944 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007945 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007946 ASSERT_EQ(0, motionArgs.flags);
7947 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7948 ASSERT_EQ(0, motionArgs.buttonState);
7949 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007950 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007951 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007952 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007954 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7956 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7957 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7958 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7959 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7960 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7961 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7962
7963 // Second finger up.
7964 x3 += 30; y3 -= 20;
7965 processPosition(mapper, x3, y3);
7966 processMTSync(mapper);
7967 processSync(mapper);
7968
7969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7970 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7971 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7972 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7973 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007974 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007975 ASSERT_EQ(0, motionArgs.flags);
7976 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7977 ASSERT_EQ(0, motionArgs.buttonState);
7978 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007979 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007980 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007981 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007982 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007983 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007984 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7985 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7987 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7988 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7989 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7990 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7991
7992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7993 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7994 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7995 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7996 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7997 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7998 ASSERT_EQ(0, motionArgs.flags);
7999 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8000 ASSERT_EQ(0, motionArgs.buttonState);
8001 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008002 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008003 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008004 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8006 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8007 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8008 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8009 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8010
8011 // Last finger up.
8012 processMTSync(mapper);
8013 processSync(mapper);
8014
8015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8016 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8017 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8018 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8019 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8020 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8021 ASSERT_EQ(0, motionArgs.flags);
8022 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8023 ASSERT_EQ(0, motionArgs.buttonState);
8024 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008025 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008026 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008027 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8029 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8030 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8031 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8032 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8033
8034 // Should not have sent any more keys or motions.
8035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8037}
8038
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008039TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8040 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008041 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008042
8043 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8044 /*fuzz*/ 0, /*resolution*/ 10);
8045 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8046 /*fuzz*/ 0, /*resolution*/ 11);
8047 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8048 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8049 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8050 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8051 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8052 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8053 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8054 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8055
Arpit Singh56adebc2023-04-25 13:56:05 +00008056 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008057
8058 // X and Y axes
8059 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8060 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8061 // Touch major and minor
8062 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8063 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8064 // Tool major and minor
8065 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8066 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8067}
8068
8069TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8070 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008071 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008072
8073 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8074 /*fuzz*/ 0, /*resolution*/ 10);
8075 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8076 /*fuzz*/ 0, /*resolution*/ 11);
8077
8078 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8079
Arpit Singh56adebc2023-04-25 13:56:05 +00008080 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008081
8082 // Touch major and minor
8083 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8084 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8085 // Tool major and minor
8086 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8087 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8088}
8089
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008091 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008092 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 prepareAxes(POSITION | ID);
8094 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00008095 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008096
arthurhungdcef2dc2020-08-11 14:47:50 +08008097 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008098
8099 NotifyMotionArgs motionArgs;
8100
8101 // Two fingers down at once.
8102 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8103 processPosition(mapper, x1, y1);
8104 processId(mapper, 1);
8105 processMTSync(mapper);
8106 processPosition(mapper, x2, y2);
8107 processId(mapper, 2);
8108 processMTSync(mapper);
8109 processSync(mapper);
8110
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8112 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008113 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008114 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008115 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8117 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8118
8119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008120 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008121 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008122 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008123 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008124 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008125 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8127 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8128 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8129 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8130
8131 // Move.
8132 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8133 processPosition(mapper, x1, y1);
8134 processId(mapper, 1);
8135 processMTSync(mapper);
8136 processPosition(mapper, x2, y2);
8137 processId(mapper, 2);
8138 processMTSync(mapper);
8139 processSync(mapper);
8140
8141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008143 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008144 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008145 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008146 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008147 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8149 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8151 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8152
8153 // First finger up.
8154 x2 += 15; y2 -= 20;
8155 processPosition(mapper, x2, y2);
8156 processId(mapper, 2);
8157 processMTSync(mapper);
8158 processSync(mapper);
8159
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008161 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008162 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008164 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008165 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008166 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8168 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8170 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8171
8172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008174 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008175 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008176 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8178 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8179
8180 // Move.
8181 x2 += 20; y2 -= 25;
8182 processPosition(mapper, x2, y2);
8183 processId(mapper, 2);
8184 processMTSync(mapper);
8185 processSync(mapper);
8186
8187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008189 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008190 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008191 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8193 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8194
8195 // New finger down.
8196 int32_t x3 = 700, y3 = 300;
8197 processPosition(mapper, x2, y2);
8198 processId(mapper, 2);
8199 processMTSync(mapper);
8200 processPosition(mapper, x3, y3);
8201 processId(mapper, 3);
8202 processMTSync(mapper);
8203 processSync(mapper);
8204
8205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008206 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008207 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008208 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008209 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008210 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008211 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008212 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8213 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8215 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8216
8217 // Second finger up.
8218 x3 += 30; y3 -= 20;
8219 processPosition(mapper, x3, y3);
8220 processId(mapper, 3);
8221 processMTSync(mapper);
8222 processSync(mapper);
8223
8224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008225 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008226 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008227 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008228 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008229 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008230 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8232 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8234 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8235
8236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008238 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008240 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8242 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8243
8244 // Last finger up.
8245 processMTSync(mapper);
8246 processSync(mapper);
8247
8248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8249 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008250 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008251 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008252 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8254 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8255
8256 // Should not have sent any more keys or motions.
8257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8259}
8260
8261TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008262 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008263 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264 prepareAxes(POSITION | ID | SLOT);
8265 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00008266 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008267
arthurhungdcef2dc2020-08-11 14:47:50 +08008268 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008269
8270 NotifyMotionArgs motionArgs;
8271
8272 // Two fingers down at once.
8273 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8274 processPosition(mapper, x1, y1);
8275 processId(mapper, 1);
8276 processSlot(mapper, 1);
8277 processPosition(mapper, x2, y2);
8278 processId(mapper, 2);
8279 processSync(mapper);
8280
8281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8282 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008283 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008284 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008285 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8287 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8288
8289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008290 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008291 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008292 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008293 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008294 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008295 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8297 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8299 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8300
8301 // Move.
8302 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8303 processSlot(mapper, 0);
8304 processPosition(mapper, x1, y1);
8305 processSlot(mapper, 1);
8306 processPosition(mapper, x2, y2);
8307 processSync(mapper);
8308
8309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008311 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008312 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008313 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008314 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008315 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8317 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8319 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8320
8321 // First finger up.
8322 x2 += 15; y2 -= 20;
8323 processSlot(mapper, 0);
8324 processId(mapper, -1);
8325 processSlot(mapper, 1);
8326 processPosition(mapper, x2, y2);
8327 processSync(mapper);
8328
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008330 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008331 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008332 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008333 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008334 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008335 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8337 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8339 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8340
8341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008343 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008344 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008345 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8347 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8348
8349 // Move.
8350 x2 += 20; y2 -= 25;
8351 processPosition(mapper, x2, y2);
8352 processSync(mapper);
8353
8354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008356 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008357 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008358 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8360 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8361
8362 // New finger down.
8363 int32_t x3 = 700, y3 = 300;
8364 processPosition(mapper, x2, y2);
8365 processSlot(mapper, 0);
8366 processId(mapper, 3);
8367 processPosition(mapper, x3, y3);
8368 processSync(mapper);
8369
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008371 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008372 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008373 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008374 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008375 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008376 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8378 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8380 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8381
8382 // Second finger up.
8383 x3 += 30; y3 -= 20;
8384 processSlot(mapper, 1);
8385 processId(mapper, -1);
8386 processSlot(mapper, 0);
8387 processPosition(mapper, x3, y3);
8388 processSync(mapper);
8389
8390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008391 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008392 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008393 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008394 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008395 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008396 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8398 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8400 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8401
8402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008404 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008405 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008406 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8408 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8409
8410 // Last finger up.
8411 processId(mapper, -1);
8412 processSync(mapper);
8413
8414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8415 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008416 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008417 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008418 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8420 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8421
8422 // Should not have sent any more keys or motions.
8423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8425}
8426
8427TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008428 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008429 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008430 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singh56adebc2023-04-25 13:56:05 +00008431 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008432
8433 // These calculations are based on the input device calibration documentation.
8434 int32_t rawX = 100;
8435 int32_t rawY = 200;
8436 int32_t rawTouchMajor = 7;
8437 int32_t rawTouchMinor = 6;
8438 int32_t rawToolMajor = 9;
8439 int32_t rawToolMinor = 8;
8440 int32_t rawPressure = 11;
8441 int32_t rawDistance = 0;
8442 int32_t rawOrientation = 3;
8443 int32_t id = 5;
8444
8445 float x = toDisplayX(rawX);
8446 float y = toDisplayY(rawY);
8447 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8448 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8449 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8450 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8451 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8452 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8453 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8454 float distance = float(rawDistance);
8455
8456 processPosition(mapper, rawX, rawY);
8457 processTouchMajor(mapper, rawTouchMajor);
8458 processTouchMinor(mapper, rawTouchMinor);
8459 processToolMajor(mapper, rawToolMajor);
8460 processToolMinor(mapper, rawToolMinor);
8461 processPressure(mapper, rawPressure);
8462 processOrientation(mapper, rawOrientation);
8463 processDistance(mapper, rawDistance);
8464 processId(mapper, id);
8465 processMTSync(mapper);
8466 processSync(mapper);
8467
8468 NotifyMotionArgs args;
8469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8470 ASSERT_EQ(0, args.pointerProperties[0].id);
8471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8472 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8473 orientation, distance));
8474}
8475
8476TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008477 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008478 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008479 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8480 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singh56adebc2023-04-25 13:56:05 +00008481 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008482
8483 // These calculations are based on the input device calibration documentation.
8484 int32_t rawX = 100;
8485 int32_t rawY = 200;
8486 int32_t rawTouchMajor = 140;
8487 int32_t rawTouchMinor = 120;
8488 int32_t rawToolMajor = 180;
8489 int32_t rawToolMinor = 160;
8490
8491 float x = toDisplayX(rawX);
8492 float y = toDisplayY(rawY);
8493 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8494 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8495 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8496 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8497 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8498
8499 processPosition(mapper, rawX, rawY);
8500 processTouchMajor(mapper, rawTouchMajor);
8501 processTouchMinor(mapper, rawTouchMinor);
8502 processToolMajor(mapper, rawToolMajor);
8503 processToolMinor(mapper, rawToolMinor);
8504 processMTSync(mapper);
8505 processSync(mapper);
8506
8507 NotifyMotionArgs args;
8508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8510 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8511}
8512
8513TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008514 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008515 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008516 prepareAxes(POSITION | TOUCH | TOOL);
8517 addConfigurationProperty("touch.size.calibration", "diameter");
8518 addConfigurationProperty("touch.size.scale", "10");
8519 addConfigurationProperty("touch.size.bias", "160");
8520 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singh56adebc2023-04-25 13:56:05 +00008521 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008522
8523 // These calculations are based on the input device calibration documentation.
8524 // Note: We only provide a single common touch/tool value because the device is assumed
8525 // not to emit separate values for each pointer (isSummed = 1).
8526 int32_t rawX = 100;
8527 int32_t rawY = 200;
8528 int32_t rawX2 = 150;
8529 int32_t rawY2 = 250;
8530 int32_t rawTouchMajor = 5;
8531 int32_t rawToolMajor = 8;
8532
8533 float x = toDisplayX(rawX);
8534 float y = toDisplayY(rawY);
8535 float x2 = toDisplayX(rawX2);
8536 float y2 = toDisplayY(rawY2);
8537 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8538 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8539 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8540
8541 processPosition(mapper, rawX, rawY);
8542 processTouchMajor(mapper, rawTouchMajor);
8543 processToolMajor(mapper, rawToolMajor);
8544 processMTSync(mapper);
8545 processPosition(mapper, rawX2, rawY2);
8546 processTouchMajor(mapper, rawTouchMajor);
8547 processToolMajor(mapper, rawToolMajor);
8548 processMTSync(mapper);
8549 processSync(mapper);
8550
8551 NotifyMotionArgs args;
8552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8553 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8554
8555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008556 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008557 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8559 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8561 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8562}
8563
8564TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008565 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008566 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008567 prepareAxes(POSITION | TOUCH | TOOL);
8568 addConfigurationProperty("touch.size.calibration", "area");
8569 addConfigurationProperty("touch.size.scale", "43");
8570 addConfigurationProperty("touch.size.bias", "3");
Arpit Singh56adebc2023-04-25 13:56:05 +00008571 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008572
8573 // These calculations are based on the input device calibration documentation.
8574 int32_t rawX = 100;
8575 int32_t rawY = 200;
8576 int32_t rawTouchMajor = 5;
8577 int32_t rawToolMajor = 8;
8578
8579 float x = toDisplayX(rawX);
8580 float y = toDisplayY(rawY);
8581 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8582 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8583 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8584
8585 processPosition(mapper, rawX, rawY);
8586 processTouchMajor(mapper, rawTouchMajor);
8587 processToolMajor(mapper, rawToolMajor);
8588 processMTSync(mapper);
8589 processSync(mapper);
8590
8591 NotifyMotionArgs args;
8592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8594 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8595}
8596
8597TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008598 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008599 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008600 prepareAxes(POSITION | PRESSURE);
8601 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8602 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singh56adebc2023-04-25 13:56:05 +00008603 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008604
Michael Wrightaa449c92017-12-13 21:21:43 +00008605 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008606 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008607 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8608 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8609 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8610
Michael Wrightd02c5b62014-02-10 15:10:22 -08008611 // These calculations are based on the input device calibration documentation.
8612 int32_t rawX = 100;
8613 int32_t rawY = 200;
8614 int32_t rawPressure = 60;
8615
8616 float x = toDisplayX(rawX);
8617 float y = toDisplayY(rawY);
8618 float pressure = float(rawPressure) * 0.01f;
8619
8620 processPosition(mapper, rawX, rawY);
8621 processPressure(mapper, rawPressure);
8622 processMTSync(mapper);
8623 processSync(mapper);
8624
8625 NotifyMotionArgs args;
8626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8628 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8629}
8630
8631TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008632 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008633 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008634 prepareAxes(POSITION | ID | SLOT);
Arpit Singh56adebc2023-04-25 13:56:05 +00008635 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008636
8637 NotifyMotionArgs motionArgs;
8638 NotifyKeyArgs keyArgs;
8639
8640 processId(mapper, 1);
8641 processPosition(mapper, 100, 200);
8642 processSync(mapper);
8643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8644 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8645 ASSERT_EQ(0, motionArgs.buttonState);
8646
8647 // press BTN_LEFT, release BTN_LEFT
8648 processKey(mapper, BTN_LEFT, 1);
8649 processSync(mapper);
8650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8652 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8653
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8655 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8656 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8657
Michael Wrightd02c5b62014-02-10 15:10:22 -08008658 processKey(mapper, BTN_LEFT, 0);
8659 processSync(mapper);
8660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008661 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008662 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008663
8664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008666 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008667
8668 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8669 processKey(mapper, BTN_RIGHT, 1);
8670 processKey(mapper, BTN_MIDDLE, 1);
8671 processSync(mapper);
8672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8674 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8675 motionArgs.buttonState);
8676
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8678 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8679 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8680
8681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8682 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8683 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8684 motionArgs.buttonState);
8685
Michael Wrightd02c5b62014-02-10 15:10:22 -08008686 processKey(mapper, BTN_RIGHT, 0);
8687 processSync(mapper);
8688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008689 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008690 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008691
8692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008694 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008695
8696 processKey(mapper, BTN_MIDDLE, 0);
8697 processSync(mapper);
8698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008699 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008700 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008701
8702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008703 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008704 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008705
8706 // press BTN_BACK, release BTN_BACK
8707 processKey(mapper, BTN_BACK, 1);
8708 processSync(mapper);
8709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8710 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8711 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008712
Michael Wrightd02c5b62014-02-10 15:10:22 -08008713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008715 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8716
8717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8718 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8719 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008720
8721 processKey(mapper, BTN_BACK, 0);
8722 processSync(mapper);
8723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008724 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008725 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008726
8727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008729 ASSERT_EQ(0, motionArgs.buttonState);
8730
Michael Wrightd02c5b62014-02-10 15:10:22 -08008731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8732 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8733 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8734
8735 // press BTN_SIDE, release BTN_SIDE
8736 processKey(mapper, BTN_SIDE, 1);
8737 processSync(mapper);
8738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8739 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8740 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008741
Michael Wrightd02c5b62014-02-10 15:10:22 -08008742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008744 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8745
8746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8747 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8748 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008749
8750 processKey(mapper, BTN_SIDE, 0);
8751 processSync(mapper);
8752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008753 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008754 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008755
8756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008758 ASSERT_EQ(0, motionArgs.buttonState);
8759
Michael Wrightd02c5b62014-02-10 15:10:22 -08008760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8761 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8762 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8763
8764 // press BTN_FORWARD, release BTN_FORWARD
8765 processKey(mapper, BTN_FORWARD, 1);
8766 processSync(mapper);
8767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8768 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8769 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008770
Michael Wrightd02c5b62014-02-10 15:10:22 -08008771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008772 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008773 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8774
8775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8776 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8777 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008778
8779 processKey(mapper, BTN_FORWARD, 0);
8780 processSync(mapper);
8781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008782 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008783 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008784
8785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008787 ASSERT_EQ(0, motionArgs.buttonState);
8788
Michael Wrightd02c5b62014-02-10 15:10:22 -08008789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8790 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8791 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8792
8793 // press BTN_EXTRA, release BTN_EXTRA
8794 processKey(mapper, BTN_EXTRA, 1);
8795 processSync(mapper);
8796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8797 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8798 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008799
Michael Wrightd02c5b62014-02-10 15:10:22 -08008800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008801 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008802 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8803
8804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8805 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8806 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008807
8808 processKey(mapper, BTN_EXTRA, 0);
8809 processSync(mapper);
8810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008811 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008812 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008813
8814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008816 ASSERT_EQ(0, motionArgs.buttonState);
8817
Michael Wrightd02c5b62014-02-10 15:10:22 -08008818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8819 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8820 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8821
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8823
Michael Wrightd02c5b62014-02-10 15:10:22 -08008824 // press BTN_STYLUS, release BTN_STYLUS
8825 processKey(mapper, BTN_STYLUS, 1);
8826 processSync(mapper);
8827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008829 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8830
8831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8832 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8833 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008834
8835 processKey(mapper, BTN_STYLUS, 0);
8836 processSync(mapper);
8837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008838 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008839 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008840
8841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008843 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008844
8845 // press BTN_STYLUS2, release BTN_STYLUS2
8846 processKey(mapper, BTN_STYLUS2, 1);
8847 processSync(mapper);
8848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008850 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8851
8852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8853 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8854 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008855
8856 processKey(mapper, BTN_STYLUS2, 0);
8857 processSync(mapper);
8858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008859 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008860 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008861
8862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008864 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008865
8866 // release touch
8867 processId(mapper, -1);
8868 processSync(mapper);
8869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8870 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8871 ASSERT_EQ(0, motionArgs.buttonState);
8872}
8873
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008874TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8875 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008876 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008877 prepareAxes(POSITION | ID | SLOT);
Arpit Singh56adebc2023-04-25 13:56:05 +00008878 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008879
8880 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8881 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8882
8883 // Touch down.
8884 processId(mapper, 1);
8885 processPosition(mapper, 100, 200);
8886 processSync(mapper);
8887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8888 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8889
8890 // Press and release button mapped to the primary stylus button.
8891 processKey(mapper, BTN_A, 1);
8892 processSync(mapper);
8893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8894 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8895 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8897 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8898 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8899
8900 processKey(mapper, BTN_A, 0);
8901 processSync(mapper);
8902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8903 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8905 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8906
8907 // Press and release the HID usage mapped to the secondary stylus button.
8908 processHidUsage(mapper, 0xabcd, 1);
8909 processSync(mapper);
8910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8911 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8912 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8914 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8915 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8916
8917 processHidUsage(mapper, 0xabcd, 0);
8918 processSync(mapper);
8919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8920 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8922 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8923
8924 // Release touch.
8925 processId(mapper, -1);
8926 processSync(mapper);
8927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8928 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8929}
8930
Michael Wrightd02c5b62014-02-10 15:10:22 -08008931TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008932 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008933 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008934 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00008935 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008936
8937 NotifyMotionArgs motionArgs;
8938
8939 // default tool type is finger
8940 processId(mapper, 1);
8941 processPosition(mapper, 100, 200);
8942 processSync(mapper);
8943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8944 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008945 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008946
8947 // eraser
8948 processKey(mapper, BTN_TOOL_RUBBER, 1);
8949 processSync(mapper);
8950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008952 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008953
8954 // stylus
8955 processKey(mapper, BTN_TOOL_RUBBER, 0);
8956 processKey(mapper, BTN_TOOL_PEN, 1);
8957 processSync(mapper);
8958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008960 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008961
8962 // brush
8963 processKey(mapper, BTN_TOOL_PEN, 0);
8964 processKey(mapper, BTN_TOOL_BRUSH, 1);
8965 processSync(mapper);
8966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008968 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008969
8970 // pencil
8971 processKey(mapper, BTN_TOOL_BRUSH, 0);
8972 processKey(mapper, BTN_TOOL_PENCIL, 1);
8973 processSync(mapper);
8974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008976 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008977
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008978 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008979 processKey(mapper, BTN_TOOL_PENCIL, 0);
8980 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8981 processSync(mapper);
8982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008984 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008985
8986 // mouse
8987 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8988 processKey(mapper, BTN_TOOL_MOUSE, 1);
8989 processSync(mapper);
8990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008992 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008993
8994 // lens
8995 processKey(mapper, BTN_TOOL_MOUSE, 0);
8996 processKey(mapper, BTN_TOOL_LENS, 1);
8997 processSync(mapper);
8998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009000 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009001
9002 // double-tap
9003 processKey(mapper, BTN_TOOL_LENS, 0);
9004 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9005 processSync(mapper);
9006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009008 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009009
9010 // triple-tap
9011 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9012 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9013 processSync(mapper);
9014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009016 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009017
9018 // quad-tap
9019 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9020 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9021 processSync(mapper);
9022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009024 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009025
9026 // finger
9027 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9028 processKey(mapper, BTN_TOOL_FINGER, 1);
9029 processSync(mapper);
9030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9031 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009032 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009033
9034 // stylus trumps finger
9035 processKey(mapper, BTN_TOOL_PEN, 1);
9036 processSync(mapper);
9037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009039 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009040
9041 // eraser trumps stylus
9042 processKey(mapper, BTN_TOOL_RUBBER, 1);
9043 processSync(mapper);
9044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009046 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009047
9048 // mouse trumps eraser
9049 processKey(mapper, BTN_TOOL_MOUSE, 1);
9050 processSync(mapper);
9051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009053 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009054
9055 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9056 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9057 processSync(mapper);
9058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009060 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009061
9062 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9063 processToolType(mapper, MT_TOOL_PEN);
9064 processSync(mapper);
9065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009067 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009068
9069 // back to default tool type
9070 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9071 processKey(mapper, BTN_TOOL_MOUSE, 0);
9072 processKey(mapper, BTN_TOOL_RUBBER, 0);
9073 processKey(mapper, BTN_TOOL_PEN, 0);
9074 processKey(mapper, BTN_TOOL_FINGER, 0);
9075 processSync(mapper);
9076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009078 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009079}
9080
9081TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009082 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009083 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009084 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009085 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +00009086 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009087
9088 NotifyMotionArgs motionArgs;
9089
9090 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9091 processId(mapper, 1);
9092 processPosition(mapper, 100, 200);
9093 processSync(mapper);
9094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9095 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9097 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9098
9099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9100 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9102 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9103
9104 // move a little
9105 processPosition(mapper, 150, 250);
9106 processSync(mapper);
9107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9108 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9110 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9111
9112 // down when BTN_TOUCH is pressed, pressure defaults to 1
9113 processKey(mapper, BTN_TOUCH, 1);
9114 processSync(mapper);
9115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9116 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9118 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9119
9120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9121 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9122 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9123 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9124
9125 // up when BTN_TOUCH is released, hover restored
9126 processKey(mapper, BTN_TOUCH, 0);
9127 processSync(mapper);
9128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9129 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9130 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9131 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9132
9133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9134 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9135 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9136 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9137
9138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9139 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9140 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9141 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9142
9143 // exit hover when pointer goes away
9144 processId(mapper, -1);
9145 processSync(mapper);
9146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9147 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9149 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9150}
9151
9152TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009153 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009154 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009155 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009156 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009157
9158 NotifyMotionArgs motionArgs;
9159
9160 // initially hovering because pressure is 0
9161 processId(mapper, 1);
9162 processPosition(mapper, 100, 200);
9163 processPressure(mapper, 0);
9164 processSync(mapper);
9165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9166 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9168 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9169
9170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9171 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9172 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9173 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9174
9175 // move a little
9176 processPosition(mapper, 150, 250);
9177 processSync(mapper);
9178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9179 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9181 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9182
9183 // down when pressure becomes non-zero
9184 processPressure(mapper, RAW_PRESSURE_MAX);
9185 processSync(mapper);
9186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9187 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9188 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9189 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9190
9191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9192 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9194 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9195
9196 // up when pressure becomes 0, hover restored
9197 processPressure(mapper, 0);
9198 processSync(mapper);
9199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9200 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9202 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9203
9204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9205 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9207 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9208
9209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9210 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9212 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9213
9214 // exit hover when pointer goes away
9215 processId(mapper, -1);
9216 processSync(mapper);
9217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9218 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9220 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9221}
9222
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009223/**
9224 * Set the input device port <--> display port associations, and check that the
9225 * events are routed to the display that matches the display port.
9226 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9227 */
9228TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009229 const std::string usb2 = "USB2";
9230 const uint8_t hdmi1 = 0;
9231 const uint8_t hdmi2 = 1;
9232 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009233 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009234
9235 addConfigurationProperty("touch.deviceType", "touchScreen");
9236 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009237 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009238
9239 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9240 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9241
9242 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9243 // for this input device is specified, and the matching viewport is not present,
9244 // the input device should be disabled (at the mapper level).
9245
9246 // Add viewport for display 2 on hdmi2
9247 prepareSecondaryDisplay(type, hdmi2);
9248 // Send a touch event
9249 processPosition(mapper, 100, 100);
9250 processSync(mapper);
9251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9252
9253 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009254 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009255 // Send a touch event again
9256 processPosition(mapper, 100, 100);
9257 processSync(mapper);
9258
9259 NotifyMotionArgs args;
9260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9261 ASSERT_EQ(DISPLAY_ID, args.displayId);
9262}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009263
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009264TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9265 addConfigurationProperty("touch.deviceType", "touchScreen");
9266 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009267 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009268
9269 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9270
Michael Wrighta9cf4192022-12-01 23:46:39 +00009271 prepareDisplay(ui::ROTATION_0);
9272 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009273
9274 // Send a touch event
9275 processPosition(mapper, 100, 100);
9276 processSync(mapper);
9277
9278 NotifyMotionArgs args;
9279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9280 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9281}
9282
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009283TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009284 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009285 std::shared_ptr<FakePointerController> fakePointerController =
9286 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009287 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009288 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009289 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009290
Garfield Tan888a6a42020-01-09 11:39:16 -08009291 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009292 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009293
Michael Wrighta9cf4192022-12-01 23:46:39 +00009294 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009295 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009296 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009297
Josep del Río2d8c79a2023-01-23 19:33:50 +00009298 // Check source is mouse that would obtain the PointerController.
9299 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009300
9301 NotifyMotionArgs motionArgs;
9302 processPosition(mapper, 100, 100);
9303 processSync(mapper);
9304
9305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9306 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9307 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9308}
9309
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009310/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009311 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9312 */
9313TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9314 addConfigurationProperty("touch.deviceType", "touchScreen");
9315 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009316 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009317
Michael Wrighta9cf4192022-12-01 23:46:39 +00009318 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009319 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9320 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9321 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9322 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009323
9324 NotifyMotionArgs args;
9325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9326 ASSERT_EQ(26, args.readTime);
9327
Harry Cutts33476232023-01-30 19:57:29 +00009328 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9329 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9330 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009331
9332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9333 ASSERT_EQ(33, args.readTime);
9334}
9335
9336/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009337 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9338 * events should not be delivered to the listener.
9339 */
9340TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9341 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009342 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009343 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009344 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009345 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009346 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009347 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009348
9349 NotifyMotionArgs motionArgs;
9350 processPosition(mapper, 100, 100);
9351 processSync(mapper);
9352
9353 mFakeListener->assertNotifyMotionWasNotCalled();
9354}
9355
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009356/**
9357 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9358 * the touch mapper can process the events and the events can be delivered to the listener.
9359 */
9360TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9361 addConfigurationProperty("touch.deviceType", "touchScreen");
9362 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009363 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009364 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009365 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009366 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009367 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009368
9369 NotifyMotionArgs motionArgs;
9370 processPosition(mapper, 100, 100);
9371 processSync(mapper);
9372
9373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9374 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9375}
9376
Garfield Tanc734e4f2021-01-15 20:01:39 -08009377TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9378 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009379 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009380 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009381 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009382 std::optional<DisplayViewport> optionalDisplayViewport =
9383 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9384 ASSERT_TRUE(optionalDisplayViewport.has_value());
9385 DisplayViewport displayViewport = *optionalDisplayViewport;
9386
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009387 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009388 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009389 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009390
9391 // Finger down
9392 int32_t x = 100, y = 100;
9393 processPosition(mapper, x, y);
9394 processSync(mapper);
9395
9396 NotifyMotionArgs motionArgs;
9397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9398 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9399
9400 // Deactivate display viewport
9401 displayViewport.isActive = false;
9402 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009403 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009404
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009405 // The ongoing touch should be canceled immediately
9406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9407 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9408
9409 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009410 x += 10, y += 10;
9411 processPosition(mapper, x, y);
9412 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009414
9415 // Reactivate display viewport
9416 displayViewport.isActive = true;
9417 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009418 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009419
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009420 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009421 x += 10, y += 10;
9422 processPosition(mapper, x, y);
9423 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9425 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009426}
9427
Arthur Hung7c645402019-01-25 17:45:42 +08009428TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9429 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009430 prepareAxes(POSITION | ID | SLOT);
9431 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009432 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009433
9434 // Create the second touch screen device, and enable multi fingers.
9435 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009436 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009437 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009438 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009439 std::shared_ptr<InputDevice> device2 =
9440 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009441 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009442
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009443 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009444 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009445 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009446 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009447 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009448 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009449 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009450 /*flat=*/0, /*fuzz=*/0);
9451 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009452 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9453 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009454
9455 // Setup the second touch screen device.
Arpit Singh56adebc2023-04-25 13:56:05 +00009456 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9457 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9458 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009459 std::list<NotifyArgs> unused =
9460 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009461 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009462 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009463
9464 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009465 std::shared_ptr<FakePointerController> fakePointerController =
9466 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009467 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009468
9469 // Setup policy for associated displays and show touches.
9470 const uint8_t hdmi1 = 0;
9471 const uint8_t hdmi2 = 1;
9472 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9473 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9474 mFakePolicy->setShowTouches(true);
9475
9476 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009477 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009478 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009479
9480 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009481 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009482 InputReaderConfiguration::Change::DISPLAY_INFO |
9483 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009484
9485 // Two fingers down at default display.
9486 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9487 processPosition(mapper, x1, y1);
9488 processId(mapper, 1);
9489 processSlot(mapper, 1);
9490 processPosition(mapper, x2, y2);
9491 processId(mapper, 2);
9492 processSync(mapper);
9493
9494 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9495 fakePointerController->getSpots().find(DISPLAY_ID);
9496 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9497 ASSERT_EQ(size_t(2), iter->second.size());
9498
9499 // Two fingers down at second display.
9500 processPosition(mapper2, x1, y1);
9501 processId(mapper2, 1);
9502 processSlot(mapper2, 1);
9503 processPosition(mapper2, x2, y2);
9504 processId(mapper2, 2);
9505 processSync(mapper2);
9506
9507 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9508 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9509 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009510
9511 // Disable the show touches configuration and ensure the spots are cleared.
9512 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009513 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009514 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009515
9516 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009517}
9518
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009519TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009520 prepareAxes(POSITION);
9521 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009522 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00009523 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009524
9525 NotifyMotionArgs motionArgs;
9526 // Unrotated video frame
9527 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9528 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009529 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009530 processPosition(mapper, 100, 200);
9531 processSync(mapper);
9532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9533 ASSERT_EQ(frames, motionArgs.videoFrames);
9534
9535 // Subsequent touch events should not have any videoframes
9536 // This is implemented separately in FakeEventHub,
9537 // but that should match the behaviour of TouchVideoDevice.
9538 processPosition(mapper, 200, 200);
9539 processSync(mapper);
9540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9541 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9542}
9543
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009544TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009545 prepareAxes(POSITION);
9546 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009547 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009548 // Unrotated video frame
9549 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9550 NotifyMotionArgs motionArgs;
9551
9552 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009553 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009554 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9555 clearViewports();
9556 prepareDisplay(orientation);
9557 std::vector<TouchVideoFrame> frames{frame};
9558 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9559 processPosition(mapper, 100, 200);
9560 processSync(mapper);
9561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9562 ASSERT_EQ(frames, motionArgs.videoFrames);
9563 }
9564}
9565
9566TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9567 prepareAxes(POSITION);
9568 addConfigurationProperty("touch.deviceType", "touchScreen");
9569 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9570 // orientation-aware are affected by display rotation.
9571 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00009572 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009573 // Unrotated video frame
9574 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9575 NotifyMotionArgs motionArgs;
9576
9577 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009578 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009579 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9580 clearViewports();
9581 prepareDisplay(orientation);
9582 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009583 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009584 processPosition(mapper, 100, 200);
9585 processSync(mapper);
9586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009587 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9588 // compared to the display. This is so that when the window transform (which contains the
9589 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9590 // window's coordinate space.
9591 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009592 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009593
9594 // Release finger.
9595 processSync(mapper);
9596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009597 }
9598}
9599
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009600TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009601 prepareAxes(POSITION);
9602 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009603 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009604 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9605 // so mix these.
9606 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9607 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9608 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9609 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9610 NotifyMotionArgs motionArgs;
9611
Michael Wrighta9cf4192022-12-01 23:46:39 +00009612 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009613 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009614 processPosition(mapper, 100, 200);
9615 processSync(mapper);
9616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009617 ASSERT_EQ(frames, motionArgs.videoFrames);
9618}
9619
9620TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9621 prepareAxes(POSITION);
9622 addConfigurationProperty("touch.deviceType", "touchScreen");
9623 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9624 // orientation-aware are affected by display rotation.
9625 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00009626 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009627 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9628 // so mix these.
9629 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9630 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9631 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9632 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9633 NotifyMotionArgs motionArgs;
9634
Michael Wrighta9cf4192022-12-01 23:46:39 +00009635 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009636 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9637 processPosition(mapper, 100, 200);
9638 processSync(mapper);
9639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9640 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9641 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9642 // compared to the display. This is so that when the window transform (which contains the
9643 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9644 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009645 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009646 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009647 ASSERT_EQ(frames, motionArgs.videoFrames);
9648}
9649
Arthur Hung9da14732019-09-02 16:16:58 +08009650/**
9651 * If we had defined port associations, but the viewport is not ready, the touch device would be
9652 * expected to be disabled, and it should be enabled after the viewport has found.
9653 */
9654TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009655 constexpr uint8_t hdmi2 = 1;
9656 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009657 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009658
9659 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9660
9661 addConfigurationProperty("touch.deviceType", "touchScreen");
9662 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009663 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009664
9665 ASSERT_EQ(mDevice->isEnabled(), false);
9666
9667 // Add display on hdmi2, the device should be enabled and can receive touch event.
9668 prepareSecondaryDisplay(type, hdmi2);
9669 ASSERT_EQ(mDevice->isEnabled(), true);
9670
9671 // Send a touch event.
9672 processPosition(mapper, 100, 100);
9673 processSync(mapper);
9674
9675 NotifyMotionArgs args;
9676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9677 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9678}
9679
Arthur Hung421eb1c2020-01-16 00:09:42 +08009680TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009681 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009682 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009683 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009684 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009685
9686 NotifyMotionArgs motionArgs;
9687
9688 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9689 // finger down
9690 processId(mapper, 1);
9691 processPosition(mapper, x1, y1);
9692 processSync(mapper);
9693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9694 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009695 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009696
9697 // finger move
9698 processId(mapper, 1);
9699 processPosition(mapper, x2, y2);
9700 processSync(mapper);
9701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9702 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009703 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009704
9705 // finger up.
9706 processId(mapper, -1);
9707 processSync(mapper);
9708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9709 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009710 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009711
9712 // new finger down
9713 processId(mapper, 1);
9714 processPosition(mapper, x3, y3);
9715 processSync(mapper);
9716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9717 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009718 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009719}
9720
9721/**
arthurhungcc7f9802020-04-30 17:55:40 +08009722 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9723 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009724 */
arthurhungcc7f9802020-04-30 17:55:40 +08009725TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009726 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009727 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009728 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009729 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009730
9731 NotifyMotionArgs motionArgs;
9732
9733 // default tool type is finger
9734 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009735 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009736 processPosition(mapper, x1, y1);
9737 processSync(mapper);
9738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9739 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009740 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009741
9742 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9743 processToolType(mapper, MT_TOOL_PALM);
9744 processSync(mapper);
9745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9746 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9747
9748 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009749 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009750 processPosition(mapper, x2, y2);
9751 processSync(mapper);
9752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9753
9754 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009755 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009756 processSync(mapper);
9757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9758
9759 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009760 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009761 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009762 processPosition(mapper, x3, y3);
9763 processSync(mapper);
9764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9765 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009766 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009767}
9768
arthurhungbf89a482020-04-17 17:37:55 +08009769/**
arthurhungcc7f9802020-04-30 17:55:40 +08009770 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9771 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009772 */
arthurhungcc7f9802020-04-30 17:55:40 +08009773TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009774 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009775 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009776 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009777 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009778
9779 NotifyMotionArgs motionArgs;
9780
9781 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009782 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9783 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009784 processPosition(mapper, x1, y1);
9785 processSync(mapper);
9786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9787 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009788 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009789
9790 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009791 processSlot(mapper, SECOND_SLOT);
9792 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009793 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009794 processSync(mapper);
9795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009796 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009797 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009798
9799 // If the tool type of the first finger changes to MT_TOOL_PALM,
9800 // we expect to receive ACTION_POINTER_UP with cancel flag.
9801 processSlot(mapper, FIRST_SLOT);
9802 processId(mapper, FIRST_TRACKING_ID);
9803 processToolType(mapper, MT_TOOL_PALM);
9804 processSync(mapper);
9805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009806 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009807 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9808
9809 // The following MOVE events of second finger should be processed.
9810 processSlot(mapper, SECOND_SLOT);
9811 processId(mapper, SECOND_TRACKING_ID);
9812 processPosition(mapper, x2 + 1, y2 + 1);
9813 processSync(mapper);
9814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009816 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009817
9818 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9819 // it. Second finger receive move.
9820 processSlot(mapper, FIRST_SLOT);
9821 processId(mapper, INVALID_TRACKING_ID);
9822 processSync(mapper);
9823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9824 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009825 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009826
9827 // Second finger keeps moving.
9828 processSlot(mapper, SECOND_SLOT);
9829 processId(mapper, SECOND_TRACKING_ID);
9830 processPosition(mapper, x2 + 2, y2 + 2);
9831 processSync(mapper);
9832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009834 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009835
9836 // Second finger up.
9837 processId(mapper, INVALID_TRACKING_ID);
9838 processSync(mapper);
9839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9840 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9841 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9842}
9843
9844/**
9845 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9846 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9847 */
9848TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9849 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009850 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009851 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009852 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009853
9854 NotifyMotionArgs motionArgs;
9855
9856 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9857 // First finger down.
9858 processId(mapper, FIRST_TRACKING_ID);
9859 processPosition(mapper, x1, y1);
9860 processSync(mapper);
9861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9862 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009863 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009864
9865 // Second finger down.
9866 processSlot(mapper, SECOND_SLOT);
9867 processId(mapper, SECOND_TRACKING_ID);
9868 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009869 processSync(mapper);
9870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009871 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009872 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009873
arthurhungcc7f9802020-04-30 17:55:40 +08009874 // If the tool type of the first finger changes to MT_TOOL_PALM,
9875 // we expect to receive ACTION_POINTER_UP with cancel flag.
9876 processSlot(mapper, FIRST_SLOT);
9877 processId(mapper, FIRST_TRACKING_ID);
9878 processToolType(mapper, MT_TOOL_PALM);
9879 processSync(mapper);
9880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009881 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009882 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9883
9884 // Second finger keeps moving.
9885 processSlot(mapper, SECOND_SLOT);
9886 processId(mapper, SECOND_TRACKING_ID);
9887 processPosition(mapper, x2 + 1, y2 + 1);
9888 processSync(mapper);
9889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9891
9892 // second finger becomes palm, receive cancel due to only 1 finger is active.
9893 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009894 processToolType(mapper, MT_TOOL_PALM);
9895 processSync(mapper);
9896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9897 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9898
arthurhungcc7f9802020-04-30 17:55:40 +08009899 // third finger down.
9900 processSlot(mapper, THIRD_SLOT);
9901 processId(mapper, THIRD_TRACKING_ID);
9902 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009903 processPosition(mapper, x3, y3);
9904 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9906 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009907 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009908 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009909
9910 // third finger move
9911 processId(mapper, THIRD_TRACKING_ID);
9912 processPosition(mapper, x3 + 1, y3 + 1);
9913 processSync(mapper);
9914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9915 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9916
9917 // first finger up, third finger receive move.
9918 processSlot(mapper, FIRST_SLOT);
9919 processId(mapper, INVALID_TRACKING_ID);
9920 processSync(mapper);
9921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9922 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009923 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009924
9925 // second finger up, third finger receive move.
9926 processSlot(mapper, SECOND_SLOT);
9927 processId(mapper, INVALID_TRACKING_ID);
9928 processSync(mapper);
9929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009931 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009932
9933 // third finger up.
9934 processSlot(mapper, THIRD_SLOT);
9935 processId(mapper, INVALID_TRACKING_ID);
9936 processSync(mapper);
9937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9938 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9939 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9940}
9941
9942/**
9943 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9944 * and the active finger could still be allowed to receive the events
9945 */
9946TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9947 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009948 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009949 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009950 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009951
9952 NotifyMotionArgs motionArgs;
9953
9954 // default tool type is finger
9955 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9956 processId(mapper, FIRST_TRACKING_ID);
9957 processPosition(mapper, x1, y1);
9958 processSync(mapper);
9959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9960 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009961 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009962
9963 // Second finger down.
9964 processSlot(mapper, SECOND_SLOT);
9965 processId(mapper, SECOND_TRACKING_ID);
9966 processPosition(mapper, x2, y2);
9967 processSync(mapper);
9968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009969 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009970 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009971
9972 // If the tool type of the second finger changes to MT_TOOL_PALM,
9973 // we expect to receive ACTION_POINTER_UP with cancel flag.
9974 processId(mapper, SECOND_TRACKING_ID);
9975 processToolType(mapper, MT_TOOL_PALM);
9976 processSync(mapper);
9977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009978 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009979 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9980
9981 // The following MOVE event should be processed.
9982 processSlot(mapper, FIRST_SLOT);
9983 processId(mapper, FIRST_TRACKING_ID);
9984 processPosition(mapper, x1 + 1, y1 + 1);
9985 processSync(mapper);
9986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9987 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009988 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009989
9990 // second finger up.
9991 processSlot(mapper, SECOND_SLOT);
9992 processId(mapper, INVALID_TRACKING_ID);
9993 processSync(mapper);
9994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9996
9997 // first finger keep moving
9998 processSlot(mapper, FIRST_SLOT);
9999 processId(mapper, FIRST_TRACKING_ID);
10000 processPosition(mapper, x1 + 2, y1 + 2);
10001 processSync(mapper);
10002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10004
10005 // first finger up.
10006 processId(mapper, INVALID_TRACKING_ID);
10007 processSync(mapper);
10008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10009 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10010 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010011}
10012
Arthur Hung9ad18942021-06-19 02:04:46 +000010013/**
10014 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10015 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10016 * cause slot be valid again.
10017 */
10018TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10019 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010020 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010021 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010022 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010023
10024 NotifyMotionArgs motionArgs;
10025
10026 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10027 // First finger down.
10028 processId(mapper, FIRST_TRACKING_ID);
10029 processPosition(mapper, x1, y1);
10030 processPressure(mapper, RAW_PRESSURE_MAX);
10031 processSync(mapper);
10032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10033 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010034 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010035
10036 // First finger move.
10037 processId(mapper, FIRST_TRACKING_ID);
10038 processPosition(mapper, x1 + 1, y1 + 1);
10039 processPressure(mapper, RAW_PRESSURE_MAX);
10040 processSync(mapper);
10041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010043 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010044
10045 // Second finger down.
10046 processSlot(mapper, SECOND_SLOT);
10047 processId(mapper, SECOND_TRACKING_ID);
10048 processPosition(mapper, x2, y2);
10049 processPressure(mapper, RAW_PRESSURE_MAX);
10050 processSync(mapper);
10051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010052 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010053 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010054
10055 // second finger up with some unexpected data.
10056 processSlot(mapper, SECOND_SLOT);
10057 processId(mapper, INVALID_TRACKING_ID);
10058 processPosition(mapper, x2, y2);
10059 processSync(mapper);
10060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010061 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010062 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010063
10064 // first finger up with some unexpected data.
10065 processSlot(mapper, FIRST_SLOT);
10066 processId(mapper, INVALID_TRACKING_ID);
10067 processPosition(mapper, x2, y2);
10068 processPressure(mapper, RAW_PRESSURE_MAX);
10069 processSync(mapper);
10070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10071 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010072 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010073}
10074
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010075TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10076 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010077 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010078 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010079 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010080
10081 // First finger down.
10082 processId(mapper, FIRST_TRACKING_ID);
10083 processPosition(mapper, 100, 200);
10084 processPressure(mapper, RAW_PRESSURE_MAX);
10085 processSync(mapper);
10086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10087 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10088
10089 // Second finger down.
10090 processSlot(mapper, SECOND_SLOT);
10091 processId(mapper, SECOND_TRACKING_ID);
10092 processPosition(mapper, 300, 400);
10093 processPressure(mapper, RAW_PRESSURE_MAX);
10094 processSync(mapper);
10095 ASSERT_NO_FATAL_FAILURE(
10096 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10097
10098 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010099 // preserved. Resetting should cancel the ongoing gesture.
10100 resetMapper(mapper, ARBITRARY_TIME);
10101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10102 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010103
10104 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10105 // the existing touch state to generate a down event.
10106 processPosition(mapper, 301, 302);
10107 processSync(mapper);
10108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10109 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10111 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10112
10113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10114}
10115
10116TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10117 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010118 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010119 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010120 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010121
10122 // First finger touches down and releases.
10123 processId(mapper, FIRST_TRACKING_ID);
10124 processPosition(mapper, 100, 200);
10125 processPressure(mapper, RAW_PRESSURE_MAX);
10126 processSync(mapper);
10127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10128 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10129 processId(mapper, INVALID_TRACKING_ID);
10130 processSync(mapper);
10131 ASSERT_NO_FATAL_FAILURE(
10132 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10133
10134 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10135 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010136 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10138
10139 // Send an empty sync frame. Since there are no pointers, no events are generated.
10140 processSync(mapper);
10141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10142}
10143
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010144TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010145 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010146 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010147 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010148 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010150
10151 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10152 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10153 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10154 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10155 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10156
10157 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010158 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010159 processId(mapper, FIRST_TRACKING_ID);
10160 processToolType(mapper, MT_TOOL_PEN);
10161 processPosition(mapper, 100, 200);
10162 processPressure(mapper, RAW_PRESSURE_MAX);
10163 processSync(mapper);
10164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10165 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10166 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010167 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010168
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010169 // Now that we know the device supports styluses, ensure that the device is re-configured with
10170 // the stylus source.
10171 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10172 {
10173 const auto& devices = mReader->getInputDevices();
10174 auto deviceInfo =
10175 std::find_if(devices.begin(), devices.end(),
10176 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10177 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10178 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10179 }
10180
10181 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10183
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010184 processId(mapper, INVALID_TRACKING_ID);
10185 processSync(mapper);
10186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10187 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10188 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010189 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010190}
10191
Seunghwan Choi356026c2023-02-01 14:37:25 +090010192TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10193 addConfigurationProperty("touch.deviceType", "touchScreen");
10194 prepareDisplay(ui::ROTATION_0);
10195 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10196 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10197 // indicate stylus presence dynamically.
10198 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10199 std::shared_ptr<FakePointerController> fakePointerController =
10200 std::make_shared<FakePointerController>();
10201 mFakePolicy->setPointerController(fakePointerController);
10202 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singh56adebc2023-04-25 13:56:05 +000010203 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010204
10205 processId(mapper, FIRST_TRACKING_ID);
10206 processPressure(mapper, RAW_PRESSURE_MIN);
10207 processPosition(mapper, 100, 200);
10208 processToolType(mapper, MT_TOOL_PEN);
10209 processSync(mapper);
10210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10211 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010212 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010213 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10214 ASSERT_TRUE(fakePointerController->isPointerShown());
10215 ASSERT_NO_FATAL_FAILURE(
10216 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10217}
10218
10219TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10220 addConfigurationProperty("touch.deviceType", "touchScreen");
10221 prepareDisplay(ui::ROTATION_0);
10222 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10223 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10224 // indicate stylus presence dynamically.
10225 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10226 std::shared_ptr<FakePointerController> fakePointerController =
10227 std::make_shared<FakePointerController>();
10228 mFakePolicy->setPointerController(fakePointerController);
10229 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singh56adebc2023-04-25 13:56:05 +000010230 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010231
10232 processId(mapper, FIRST_TRACKING_ID);
10233 processPressure(mapper, RAW_PRESSURE_MIN);
10234 processPosition(mapper, 100, 200);
10235 processToolType(mapper, MT_TOOL_PEN);
10236 processSync(mapper);
10237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10238 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010239 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010240 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10241 ASSERT_FALSE(fakePointerController->isPointerShown());
10242}
10243
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010244// --- MultiTouchInputMapperTest_ExternalDevice ---
10245
10246class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10247protected:
Chris Yea52ade12020-08-27 16:49:20 -070010248 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010249};
10250
10251/**
10252 * Expect fallback to internal viewport if device is external and external viewport is not present.
10253 */
10254TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10255 prepareAxes(POSITION);
10256 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010257 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +000010258 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010259
10260 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10261
10262 NotifyMotionArgs motionArgs;
10263
10264 // Expect the event to be sent to the internal viewport,
10265 // because an external viewport is not present.
10266 processPosition(mapper, 100, 100);
10267 processSync(mapper);
10268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10269 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10270
10271 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010272 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010273 processPosition(mapper, 100, 100);
10274 processSync(mapper);
10275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10276 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10277}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010278
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010279TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10280 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10281 std::shared_ptr<FakePointerController> fakePointerController =
10282 std::make_shared<FakePointerController>();
10283 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10284 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010285
10286 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010287 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010288 prepareAxes(POSITION | ID | SLOT);
10289 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10290 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10291 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010292 mFakePolicy->setPointerController(fakePointerController);
Arpit Singh56adebc2023-04-25 13:56:05 +000010293 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010294
10295 // captured touchpad should be a touchpad source
10296 NotifyDeviceResetArgs resetArgs;
10297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10298 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10299
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010300 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010301
10302 const InputDeviceInfo::MotionRange* relRangeX =
10303 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10304 ASSERT_NE(relRangeX, nullptr);
10305 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10306 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10307 const InputDeviceInfo::MotionRange* relRangeY =
10308 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10309 ASSERT_NE(relRangeY, nullptr);
10310 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10311 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10312
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010313 // run captured pointer tests - note that this is unscaled, so input listener events should be
10314 // identical to what the hardware sends (accounting for any
10315 // calibration).
10316 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010317 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010318 processId(mapper, 1);
10319 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10320 processKey(mapper, BTN_TOUCH, 1);
10321 processSync(mapper);
10322
10323 // expect coord[0] to contain initial location of touch 0
10324 NotifyMotionArgs args;
10325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10326 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010327 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010328 ASSERT_EQ(0, args.pointerProperties[0].id);
10329 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10330 ASSERT_NO_FATAL_FAILURE(
10331 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10332
10333 // FINGER 1 DOWN
10334 processSlot(mapper, 1);
10335 processId(mapper, 2);
10336 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10337 processSync(mapper);
10338
10339 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010341 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010342 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010343 ASSERT_EQ(0, args.pointerProperties[0].id);
10344 ASSERT_EQ(1, args.pointerProperties[1].id);
10345 ASSERT_NO_FATAL_FAILURE(
10346 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10347 ASSERT_NO_FATAL_FAILURE(
10348 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10349
10350 // FINGER 1 MOVE
10351 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10352 processSync(mapper);
10353
10354 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10355 // from move
10356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10358 ASSERT_NO_FATAL_FAILURE(
10359 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10360 ASSERT_NO_FATAL_FAILURE(
10361 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10362
10363 // FINGER 0 MOVE
10364 processSlot(mapper, 0);
10365 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10366 processSync(mapper);
10367
10368 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10371 ASSERT_NO_FATAL_FAILURE(
10372 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10373 ASSERT_NO_FATAL_FAILURE(
10374 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10375
10376 // BUTTON DOWN
10377 processKey(mapper, BTN_LEFT, 1);
10378 processSync(mapper);
10379
10380 // touchinputmapper design sends a move before button press
10381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10384 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10385
10386 // BUTTON UP
10387 processKey(mapper, BTN_LEFT, 0);
10388 processSync(mapper);
10389
10390 // touchinputmapper design sends a move after button release
10391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10392 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10395
10396 // FINGER 0 UP
10397 processId(mapper, -1);
10398 processSync(mapper);
10399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10400 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10401
10402 // FINGER 1 MOVE
10403 processSlot(mapper, 1);
10404 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10405 processSync(mapper);
10406
10407 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010410 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010411 ASSERT_EQ(1, args.pointerProperties[0].id);
10412 ASSERT_NO_FATAL_FAILURE(
10413 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10414
10415 // FINGER 1 UP
10416 processId(mapper, -1);
10417 processKey(mapper, BTN_TOUCH, 0);
10418 processSync(mapper);
10419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10420 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10421
Josep del Río2d8c79a2023-01-23 19:33:50 +000010422 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010423 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010424 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010426 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010427}
10428
10429TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10430 std::shared_ptr<FakePointerController> fakePointerController =
10431 std::make_shared<FakePointerController>();
10432 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10433 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010434
10435 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010436 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010437 prepareAxes(POSITION | ID | SLOT);
10438 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10439 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010440 mFakePolicy->setPointerController(fakePointerController);
Arpit Singh56adebc2023-04-25 13:56:05 +000010441 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010442 // run uncaptured pointer tests - pushes out generic events
10443 // FINGER 0 DOWN
10444 processId(mapper, 3);
10445 processPosition(mapper, 100, 100);
10446 processKey(mapper, BTN_TOUCH, 1);
10447 processSync(mapper);
10448
10449 // start at (100,100), cursor should be at (0,0) * scale
10450 NotifyMotionArgs args;
10451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10452 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10453 ASSERT_NO_FATAL_FAILURE(
10454 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10455
10456 // FINGER 0 MOVE
10457 processPosition(mapper, 200, 200);
10458 processSync(mapper);
10459
10460 // compute scaling to help with touch position checking
10461 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10462 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10463 float scale =
10464 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10465
10466 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10468 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10470 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010471
10472 // BUTTON DOWN
10473 processKey(mapper, BTN_LEFT, 1);
10474 processSync(mapper);
10475
10476 // touchinputmapper design sends a move before button press
10477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10478 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10480 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10481
10482 // BUTTON UP
10483 processKey(mapper, BTN_LEFT, 0);
10484 processSync(mapper);
10485
10486 // touchinputmapper design sends a move after button release
10487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10488 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10490 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010491}
10492
10493TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10494 std::shared_ptr<FakePointerController> fakePointerController =
10495 std::make_shared<FakePointerController>();
10496
Michael Wrighta9cf4192022-12-01 23:46:39 +000010497 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010498 prepareAxes(POSITION | ID | SLOT);
10499 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010500 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010501 mFakePolicy->setPointerCapture(false);
Arpit Singh56adebc2023-04-25 13:56:05 +000010502 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010503
Josep del Río2d8c79a2023-01-23 19:33:50 +000010504 // uncaptured touchpad should be a pointer device
10505 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010506
Josep del Río2d8c79a2023-01-23 19:33:50 +000010507 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010508 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010509 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010510 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10511}
10512
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010513// --- BluetoothMultiTouchInputMapperTest ---
10514
10515class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10516protected:
10517 void SetUp() override {
10518 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10519 }
10520};
10521
10522TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10523 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010524 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010525 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010526 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010527
10528 nsecs_t kernelEventTime = ARBITRARY_TIME;
10529 nsecs_t expectedEventTime = ARBITRARY_TIME;
10530 // Touch down.
10531 processId(mapper, FIRST_TRACKING_ID);
10532 processPosition(mapper, 100, 200);
10533 processPressure(mapper, RAW_PRESSURE_MAX);
10534 processSync(mapper, ARBITRARY_TIME);
10535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10536 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10537
10538 // Process several events that come in quick succession, according to their timestamps.
10539 for (int i = 0; i < 3; i++) {
10540 constexpr static nsecs_t delta = ms2ns(1);
10541 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10542 kernelEventTime += delta;
10543 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10544
10545 processPosition(mapper, 101 + i, 201 + i);
10546 processSync(mapper, kernelEventTime);
10547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10548 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10549 WithEventTime(expectedEventTime))));
10550 }
10551
10552 // Release the touch.
10553 processId(mapper, INVALID_TRACKING_ID);
10554 processPressure(mapper, RAW_PRESSURE_MIN);
10555 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10557 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10558 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10559}
10560
10561// --- MultiTouchPointerModeTest ---
10562
HQ Liue6983c72022-04-19 22:14:56 +000010563class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10564protected:
10565 float mPointerMovementScale;
10566 float mPointerXZoomScale;
10567 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10568 addConfigurationProperty("touch.deviceType", "pointer");
10569 std::shared_ptr<FakePointerController> fakePointerController =
10570 std::make_shared<FakePointerController>();
10571 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10572 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010573 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010574
10575 prepareAxes(POSITION);
10576 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10577 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10578 // needs to be disabled, and the pointer gesture needs to be enabled.
10579 mFakePolicy->setPointerCapture(false);
10580 mFakePolicy->setPointerGestureEnabled(true);
10581 mFakePolicy->setPointerController(fakePointerController);
10582
10583 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10584 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10585 mPointerMovementScale =
10586 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10587 mPointerXZoomScale =
10588 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10589 }
10590
10591 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10592 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10593 /*flat*/ 0,
10594 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10595 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10596 /*flat*/ 0,
10597 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10598 }
10599};
10600
10601/**
10602 * Two fingers down on a pointer mode touch pad. The width
10603 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10604 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10605 * be greater than the both value to be freeform gesture, so that after two
10606 * fingers start to move downwards, the gesture should be swipe.
10607 */
10608TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10609 // The min freeform gesture width is 25units/mm x 30mm = 750
10610 // which is greater than fraction of the diagnal length of the touchpad (349).
10611 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010612 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010613 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010614 NotifyMotionArgs motionArgs;
10615
10616 // Two fingers down at once.
10617 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10618 // Pointer's initial position is used the [0,0] coordinate.
10619 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10620
10621 processId(mapper, FIRST_TRACKING_ID);
10622 processPosition(mapper, x1, y1);
10623 processMTSync(mapper);
10624 processId(mapper, SECOND_TRACKING_ID);
10625 processPosition(mapper, x2, y2);
10626 processMTSync(mapper);
10627 processSync(mapper);
10628
10629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010630 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010631 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010632 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010633 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010634 ASSERT_NO_FATAL_FAILURE(
10635 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10636
10637 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10638 // that there should be 1 pointer.
10639 int32_t movingDistance = 200;
10640 y1 += movingDistance;
10641 y2 += movingDistance;
10642
10643 processId(mapper, FIRST_TRACKING_ID);
10644 processPosition(mapper, x1, y1);
10645 processMTSync(mapper);
10646 processId(mapper, SECOND_TRACKING_ID);
10647 processPosition(mapper, x2, y2);
10648 processMTSync(mapper);
10649 processSync(mapper);
10650
10651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010652 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010653 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010654 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010655 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10657 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10658 0, 0, 0, 0));
10659}
10660
10661/**
10662 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10663 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10664 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10665 * value to be freeform gesture, so that after two fingers start to move downwards,
10666 * the gesture should be swipe.
10667 */
10668TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10669 // The min freeform gesture width is 5units/mm x 30mm = 150
10670 // which is greater than fraction of the diagnal length of the touchpad (349).
10671 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010672 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singh56adebc2023-04-25 13:56:05 +000010673 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010674 NotifyMotionArgs motionArgs;
10675
10676 // Two fingers down at once.
10677 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10678 // Pointer's initial position is used the [0,0] coordinate.
10679 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10680
10681 processId(mapper, FIRST_TRACKING_ID);
10682 processPosition(mapper, x1, y1);
10683 processMTSync(mapper);
10684 processId(mapper, SECOND_TRACKING_ID);
10685 processPosition(mapper, x2, y2);
10686 processMTSync(mapper);
10687 processSync(mapper);
10688
10689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010690 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010691 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010692 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010693 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010694 ASSERT_NO_FATAL_FAILURE(
10695 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10696
10697 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10698 // and there should be 1 pointer.
10699 int32_t movingDistance = 200;
10700 y1 += movingDistance;
10701 y2 += movingDistance;
10702
10703 processId(mapper, FIRST_TRACKING_ID);
10704 processPosition(mapper, x1, y1);
10705 processMTSync(mapper);
10706 processId(mapper, SECOND_TRACKING_ID);
10707 processPosition(mapper, x2, y2);
10708 processMTSync(mapper);
10709 processSync(mapper);
10710
10711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010712 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010714 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010715 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010716 // New coordinate is the scaled relative coordinate from the initial coordinate.
10717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10718 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10719 0, 0, 0, 0));
10720}
10721
10722/**
10723 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10724 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10725 * freeform gestures after two fingers start to move downwards.
10726 */
10727TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010728 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010729 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010730
10731 NotifyMotionArgs motionArgs;
10732
10733 // Two fingers down at once. Wider than the max swipe width.
10734 // The gesture is expected to be PRESS, then transformed to FREEFORM
10735 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10736
10737 processId(mapper, FIRST_TRACKING_ID);
10738 processPosition(mapper, x1, y1);
10739 processMTSync(mapper);
10740 processId(mapper, SECOND_TRACKING_ID);
10741 processPosition(mapper, x2, y2);
10742 processMTSync(mapper);
10743 processSync(mapper);
10744
10745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010746 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010747 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010748 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010749 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010750 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10751 ASSERT_NO_FATAL_FAILURE(
10752 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10753
10754 int32_t movingDistance = 200;
10755
10756 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10757 // then two down events for two pointers.
10758 y1 += movingDistance;
10759 y2 += movingDistance;
10760
10761 processId(mapper, FIRST_TRACKING_ID);
10762 processPosition(mapper, x1, y1);
10763 processMTSync(mapper);
10764 processId(mapper, SECOND_TRACKING_ID);
10765 processPosition(mapper, x2, y2);
10766 processMTSync(mapper);
10767 processSync(mapper);
10768
10769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10770 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010771 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010772 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010774 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010775 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010776 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010778 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010779 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010780 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010781 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010782 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010783 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010784 // Two pointers' scaled relative coordinates from their initial centroid.
10785 // Initial y coordinates are 0 as y1 and y2 have the same value.
10786 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10787 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10788 // When pointers move, the new coordinates equal to the initial coordinates plus
10789 // scaled moving distance.
10790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10791 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10792 0, 0, 0, 0));
10793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10794 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10795 0, 0, 0, 0));
10796
10797 // Move two fingers down again, expect one MOVE motion event.
10798 y1 += movingDistance;
10799 y2 += movingDistance;
10800
10801 processId(mapper, FIRST_TRACKING_ID);
10802 processPosition(mapper, x1, y1);
10803 processMTSync(mapper);
10804 processId(mapper, SECOND_TRACKING_ID);
10805 processPosition(mapper, x2, y2);
10806 processMTSync(mapper);
10807 processSync(mapper);
10808
10809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010810 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010812 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010813 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10815 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10816 0, 0, 0, 0, 0));
10817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10818 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10819 0, 0, 0, 0, 0));
10820}
10821
Harry Cutts39b7ca22022-10-05 15:55:48 +000010822TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010823 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010824 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010825 NotifyMotionArgs motionArgs;
10826
10827 // Place two fingers down.
10828 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10829
10830 processId(mapper, FIRST_TRACKING_ID);
10831 processPosition(mapper, x1, y1);
10832 processMTSync(mapper);
10833 processId(mapper, SECOND_TRACKING_ID);
10834 processPosition(mapper, x2, y2);
10835 processMTSync(mapper);
10836 processSync(mapper);
10837
10838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010839 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010840 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10841 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10842 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10843 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10844
10845 // Move the two fingers down and to the left.
10846 int32_t movingDistance = 200;
10847 x1 -= movingDistance;
10848 y1 += movingDistance;
10849 x2 -= movingDistance;
10850 y2 += movingDistance;
10851
10852 processId(mapper, FIRST_TRACKING_ID);
10853 processPosition(mapper, x1, y1);
10854 processMTSync(mapper);
10855 processId(mapper, SECOND_TRACKING_ID);
10856 processPosition(mapper, x2, y2);
10857 processMTSync(mapper);
10858 processSync(mapper);
10859
10860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010861 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10863 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10864 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10865 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10866}
10867
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010868TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010869 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010870 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +000010871 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10873
10874 // Start a stylus gesture.
10875 processKey(mapper, BTN_TOOL_PEN, 1);
10876 processId(mapper, FIRST_TRACKING_ID);
10877 processPosition(mapper, 100, 200);
10878 processSync(mapper);
10879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10880 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10881 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010882 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010883 // TODO(b/257078296): Pointer mode generates extra event.
10884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10885 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10886 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010887 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10889
10890 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10891 // gesture should be disabled.
10892 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10893 viewport->isActive = false;
10894 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010895 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10897 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10898 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010899 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010900 // TODO(b/257078296): Pointer mode generates extra event.
10901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10902 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10903 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010904 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10906}
10907
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010908// --- JoystickInputMapperTest ---
10909
10910class JoystickInputMapperTest : public InputMapperTest {
10911protected:
10912 static const int32_t RAW_X_MIN;
10913 static const int32_t RAW_X_MAX;
10914 static const int32_t RAW_Y_MIN;
10915 static const int32_t RAW_Y_MAX;
10916
10917 void SetUp() override {
10918 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10919 }
10920 void prepareAxes() {
10921 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10922 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10923 }
10924
10925 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10926 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10927 }
10928
10929 void processSync(JoystickInputMapper& mapper) {
10930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10931 }
10932
Michael Wrighta9cf4192022-12-01 23:46:39 +000010933 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010934 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10935 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10936 NO_PORT, ViewportType::VIRTUAL);
10937 }
10938};
10939
10940const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10941const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10942const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10943const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10944
10945TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10946 prepareAxes();
Arpit Singh2be4a362023-04-26 14:16:50 +000010947 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010948
10949 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10950
Michael Wrighta9cf4192022-12-01 23:46:39 +000010951 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010952
10953 // Send an axis event
10954 processAxis(mapper, ABS_X, 100);
10955 processSync(mapper);
10956
10957 NotifyMotionArgs args;
10958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10959 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10960
10961 // Send another axis event
10962 processAxis(mapper, ABS_Y, 100);
10963 processSync(mapper);
10964
10965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10966 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10967}
10968
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010969// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010970
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010971class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010972protected:
10973 static const char* DEVICE_NAME;
10974 static const char* DEVICE_LOCATION;
10975 static const int32_t DEVICE_ID;
10976 static const int32_t DEVICE_GENERATION;
10977 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010978 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010979 static const int32_t EVENTHUB_ID;
10980
10981 std::shared_ptr<FakeEventHub> mFakeEventHub;
10982 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010983 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010984 std::unique_ptr<InstrumentedInputReader> mReader;
10985 std::shared_ptr<InputDevice> mDevice;
10986
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010987 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010988 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010989 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010990 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010991 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010992 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010993 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10994 }
10995
10996 void SetUp() override { SetUp(DEVICE_CLASSES); }
10997
10998 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010999 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011000 mFakePolicy.clear();
11001 }
11002
Chris Yee2b1e5c2021-03-10 22:45:12 -080011003 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11004 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011005 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011006 InputDeviceIdentifier identifier;
11007 identifier.name = name;
11008 identifier.location = location;
11009 std::shared_ptr<InputDevice> device =
11010 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11011 identifier);
11012 mReader->pushNextDevice(device);
11013 mFakeEventHub->addDevice(eventHubId, name, classes);
11014 mReader->loopOnce();
11015 return device;
11016 }
11017
11018 template <class T, typename... Args>
11019 T& addControllerAndConfigure(Args... args) {
11020 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11021
11022 return controller;
11023 }
11024};
11025
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011026const char* PeripheralControllerTest::DEVICE_NAME = "device";
11027const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11028const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11029const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11030const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011031const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11032 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011033const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011034
11035// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011036class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011037protected:
11038 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011039 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011040 }
11041};
11042
11043TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011044 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011045
Harry Cuttsa5b71292022-11-28 12:56:17 +000011046 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11047 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11048 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011049}
11050
11051TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011052 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011053
Harry Cuttsa5b71292022-11-28 12:56:17 +000011054 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11055 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11056 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011057}
11058
11059// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011060class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011061protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011062 void SetUp() override {
11063 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11064 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011065};
11066
Chris Ye85758332021-05-16 23:05:17 -070011067TEST_F(LightControllerTest, MonoLight) {
11068 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011069 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011070 .maxBrightness = 255,
11071 .flags = InputLightClass::BRIGHTNESS,
11072 .path = ""};
11073 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011074
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011075 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011076 InputDeviceInfo info;
11077 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011078 std::vector<InputDeviceLightInfo> lights = info.getLights();
11079 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011080 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11081 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11082
11083 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11084 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11085}
11086
11087TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11088 RawLightInfo infoMono = {.id = 1,
11089 .name = "mono_keyboard_backlight",
11090 .maxBrightness = 255,
11091 .flags = InputLightClass::BRIGHTNESS |
11092 InputLightClass::KEYBOARD_BACKLIGHT,
11093 .path = ""};
11094 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11095
11096 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11097 InputDeviceInfo info;
11098 controller.populateDeviceInfo(&info);
11099 std::vector<InputDeviceLightInfo> lights = info.getLights();
11100 ASSERT_EQ(1U, lights.size());
11101 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11102 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011103
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011104 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11105 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011106}
11107
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011108TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11109 RawLightInfo infoMono = {.id = 1,
11110 .name = "mono_light",
11111 .maxBrightness = 255,
11112 .flags = InputLightClass::BRIGHTNESS,
11113 .path = ""};
11114 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11115 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11116 "0,100,200");
11117
11118 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11119 std::list<NotifyArgs> unused =
11120 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11121 /*changes=*/{});
11122
11123 InputDeviceInfo info;
11124 controller.populateDeviceInfo(&info);
11125 std::vector<InputDeviceLightInfo> lights = info.getLights();
11126 ASSERT_EQ(1U, lights.size());
11127 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11128}
11129
11130TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11131 RawLightInfo infoMono = {.id = 1,
11132 .name = "mono_keyboard_backlight",
11133 .maxBrightness = 255,
11134 .flags = InputLightClass::BRIGHTNESS |
11135 InputLightClass::KEYBOARD_BACKLIGHT,
11136 .path = ""};
11137 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11138
11139 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11140 std::list<NotifyArgs> unused =
11141 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11142 /*changes=*/{});
11143
11144 InputDeviceInfo info;
11145 controller.populateDeviceInfo(&info);
11146 std::vector<InputDeviceLightInfo> lights = info.getLights();
11147 ASSERT_EQ(1U, lights.size());
11148 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11149}
11150
11151TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11152 RawLightInfo infoMono = {.id = 1,
11153 .name = "mono_keyboard_backlight",
11154 .maxBrightness = 255,
11155 .flags = InputLightClass::BRIGHTNESS |
11156 InputLightClass::KEYBOARD_BACKLIGHT,
11157 .path = ""};
11158 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11159 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11160 "0,100,200");
11161
11162 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11163 std::list<NotifyArgs> unused =
11164 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11165 /*changes=*/{});
11166
11167 InputDeviceInfo info;
11168 controller.populateDeviceInfo(&info);
11169 std::vector<InputDeviceLightInfo> lights = info.getLights();
11170 ASSERT_EQ(1U, lights.size());
11171 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
11172 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
11173 ASSERT_EQ(BrightnessLevel(0), *it);
11174 std::advance(it, 1);
11175 ASSERT_EQ(BrightnessLevel(100), *it);
11176 std::advance(it, 1);
11177 ASSERT_EQ(BrightnessLevel(200), *it);
11178}
11179
11180TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
11181 RawLightInfo infoMono = {.id = 1,
11182 .name = "mono_keyboard_backlight",
11183 .maxBrightness = 255,
11184 .flags = InputLightClass::BRIGHTNESS |
11185 InputLightClass::KEYBOARD_BACKLIGHT,
11186 .path = ""};
11187 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11188 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11189 "0,100,200,300,400,500");
11190
11191 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11192 std::list<NotifyArgs> unused =
11193 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11194 /*changes=*/{});
11195
11196 InputDeviceInfo info;
11197 controller.populateDeviceInfo(&info);
11198 std::vector<InputDeviceLightInfo> lights = info.getLights();
11199 ASSERT_EQ(1U, lights.size());
11200 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11201}
11202
Chris Yee2b1e5c2021-03-10 22:45:12 -080011203TEST_F(LightControllerTest, RGBLight) {
11204 RawLightInfo infoRed = {.id = 1,
11205 .name = "red",
11206 .maxBrightness = 255,
11207 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11208 .path = ""};
11209 RawLightInfo infoGreen = {.id = 2,
11210 .name = "green",
11211 .maxBrightness = 255,
11212 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11213 .path = ""};
11214 RawLightInfo infoBlue = {.id = 3,
11215 .name = "blue",
11216 .maxBrightness = 255,
11217 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11218 .path = ""};
11219 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11220 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11221 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11222
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011223 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011224 InputDeviceInfo info;
11225 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011226 std::vector<InputDeviceLightInfo> lights = info.getLights();
11227 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011228 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11229 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11230 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11231
11232 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11233 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11234}
11235
11236TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11237 RawLightInfo infoRed = {.id = 1,
11238 .name = "red_keyboard_backlight",
11239 .maxBrightness = 255,
11240 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11241 InputLightClass::KEYBOARD_BACKLIGHT,
11242 .path = ""};
11243 RawLightInfo infoGreen = {.id = 2,
11244 .name = "green_keyboard_backlight",
11245 .maxBrightness = 255,
11246 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11247 InputLightClass::KEYBOARD_BACKLIGHT,
11248 .path = ""};
11249 RawLightInfo infoBlue = {.id = 3,
11250 .name = "blue_keyboard_backlight",
11251 .maxBrightness = 255,
11252 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11253 InputLightClass::KEYBOARD_BACKLIGHT,
11254 .path = ""};
11255 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11256 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11257 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11258
11259 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11260 InputDeviceInfo info;
11261 controller.populateDeviceInfo(&info);
11262 std::vector<InputDeviceLightInfo> lights = info.getLights();
11263 ASSERT_EQ(1U, lights.size());
11264 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11265 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11266 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11267
11268 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11269 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11270}
11271
11272TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11273 RawLightInfo infoRed = {.id = 1,
11274 .name = "red",
11275 .maxBrightness = 255,
11276 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11277 .path = ""};
11278 RawLightInfo infoGreen = {.id = 2,
11279 .name = "green",
11280 .maxBrightness = 255,
11281 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11282 .path = ""};
11283 RawLightInfo infoBlue = {.id = 3,
11284 .name = "blue",
11285 .maxBrightness = 255,
11286 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11287 .path = ""};
11288 RawLightInfo infoGlobal = {.id = 3,
11289 .name = "global_keyboard_backlight",
11290 .maxBrightness = 255,
11291 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11292 InputLightClass::KEYBOARD_BACKLIGHT,
11293 .path = ""};
11294 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11295 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11296 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11297 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11298
11299 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11300 InputDeviceInfo info;
11301 controller.populateDeviceInfo(&info);
11302 std::vector<InputDeviceLightInfo> lights = info.getLights();
11303 ASSERT_EQ(1U, lights.size());
11304 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11305 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11306 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011307
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011308 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11309 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011310}
11311
11312TEST_F(LightControllerTest, MultiColorRGBLight) {
11313 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011314 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011315 .maxBrightness = 255,
11316 .flags = InputLightClass::BRIGHTNESS |
11317 InputLightClass::MULTI_INTENSITY |
11318 InputLightClass::MULTI_INDEX,
11319 .path = ""};
11320
11321 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11322
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011323 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011324 InputDeviceInfo info;
11325 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011326 std::vector<InputDeviceLightInfo> lights = info.getLights();
11327 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011328 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11329 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11330 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11331
11332 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11333 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11334}
11335
11336TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11337 RawLightInfo infoColor = {.id = 1,
11338 .name = "multi_color_keyboard_backlight",
11339 .maxBrightness = 255,
11340 .flags = InputLightClass::BRIGHTNESS |
11341 InputLightClass::MULTI_INTENSITY |
11342 InputLightClass::MULTI_INDEX |
11343 InputLightClass::KEYBOARD_BACKLIGHT,
11344 .path = ""};
11345
11346 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11347
11348 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11349 InputDeviceInfo info;
11350 controller.populateDeviceInfo(&info);
11351 std::vector<InputDeviceLightInfo> lights = info.getLights();
11352 ASSERT_EQ(1U, lights.size());
11353 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11354 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11355 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011356
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011357 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11358 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011359}
11360
11361TEST_F(LightControllerTest, PlayerIdLight) {
11362 RawLightInfo info1 = {.id = 1,
11363 .name = "player1",
11364 .maxBrightness = 255,
11365 .flags = InputLightClass::BRIGHTNESS,
11366 .path = ""};
11367 RawLightInfo info2 = {.id = 2,
11368 .name = "player2",
11369 .maxBrightness = 255,
11370 .flags = InputLightClass::BRIGHTNESS,
11371 .path = ""};
11372 RawLightInfo info3 = {.id = 3,
11373 .name = "player3",
11374 .maxBrightness = 255,
11375 .flags = InputLightClass::BRIGHTNESS,
11376 .path = ""};
11377 RawLightInfo info4 = {.id = 4,
11378 .name = "player4",
11379 .maxBrightness = 255,
11380 .flags = InputLightClass::BRIGHTNESS,
11381 .path = ""};
11382 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11383 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11384 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11385 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11386
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011387 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011388 InputDeviceInfo info;
11389 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011390 std::vector<InputDeviceLightInfo> lights = info.getLights();
11391 ASSERT_EQ(1U, lights.size());
11392 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011393 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11394 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011395
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011396 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11397 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11398 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011399}
11400
Michael Wrightd02c5b62014-02-10 15:10:22 -080011401} // namespace android