blob: 56a2258e806d8d07aeb6ad7c6a27cc8373a1de63 [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
Prabir Pradhan05f6c602023-08-18 20:02:01 +00001479TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1480 std::unique_ptr<UinputKeyboard> keyboard =
1481 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1482 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1483 KEY_R, KEY_T, KEY_Y,
1484 BTN_STYLUS, BTN_STYLUS2,
1485 BTN_STYLUS3});
1486 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1487
1488 const auto device = findDeviceByName(keyboard->getName());
1489 ASSERT_TRUE(device.has_value());
1490
1491 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1492 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1493 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1494 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1495}
1496
Prabir Pradhanada1a2a2023-08-22 23:20:16 +00001497TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1498 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1499 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1500 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1501 createUinputDevice<UinputKeyboardWithHidUsage>(
1502 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1503 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1504
1505 const auto device = findDeviceByName(keyboard->getName());
1506 ASSERT_TRUE(device.has_value());
1507
1508 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1509 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1510
1511 // If a device supports reporting HID usage codes, it shouldn't automatically support
1512 // stylus keys.
1513 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1514 uint8_t outFlags[] = {0};
1515 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1516 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1517}
1518
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001519/**
1520 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1521 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1522 * are passed to the listener.
1523 */
1524static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1525TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1526 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1527 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1528 NotifyKeyArgs keyArgs;
1529
1530 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1531 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1532 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1533 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1534
1535 controller->pressAndReleaseKey(BTN_GEAR_UP);
1536 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1537 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1538 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1539}
1540
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001541// --- TouchIntegrationTest ---
1542
Arthur Hungaab25622020-01-16 11:22:11 +08001543class TouchIntegrationTest : public InputReaderIntegrationTest {
1544protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001545 const std::string UNIQUE_ID = "local:0";
1546
Chris Yea52ade12020-08-27 16:49:20 -07001547 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001548#if !defined(__ANDROID__)
1549 GTEST_SKIP();
1550#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001551 InputReaderIntegrationTest::SetUp();
1552 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001553 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1554 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001555
1556 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1557 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1558 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001559 const auto info = findDeviceByName(mDevice->getName());
1560 ASSERT_TRUE(info);
1561 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001562 }
1563
1564 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001565 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001566 std::optional<uint8_t> physicalPort,
1567 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001568 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001569 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001570 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001571 }
1572
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001573 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1574 NotifyMotionArgs args;
1575 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1576 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001577 ASSERT_EQ(points.size(), args.getPointerCount());
1578 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001579 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1580 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1581 }
1582 }
1583
Arthur Hungaab25622020-01-16 11:22:11 +08001584 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001585 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001586};
1587
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001588TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1589 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1590 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1591 // presses).
1592 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1593 mDeviceInfo.getSources());
1594}
1595
Arthur Hungaab25622020-01-16 11:22:11 +08001596TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1597 NotifyMotionArgs args;
1598 const Point centerPoint = mDevice->getCenterPoint();
1599
1600 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001601 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001602 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001603 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001604 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1605 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1606
1607 // ACTION_MOVE
1608 mDevice->sendMove(centerPoint + Point(1, 1));
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));
1611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1612
1613 // ACTION_UP
1614 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001615 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001616 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1617 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1618}
1619
1620TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1621 NotifyMotionArgs args;
1622 const Point centerPoint = mDevice->getCenterPoint();
1623
1624 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001625 mDevice->sendSlot(FIRST_SLOT);
1626 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001627 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001628 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001629 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1630 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1631
1632 // ACTION_POINTER_DOWN (Second slot)
1633 const Point secondPoint = centerPoint + Point(100, 100);
1634 mDevice->sendSlot(SECOND_SLOT);
1635 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001636 mDevice->sendDown(secondPoint);
1637 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001638 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001639 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001640
1641 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001642 mDevice->sendMove(secondPoint + Point(1, 1));
1643 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001644 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1645 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1646
1647 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001648 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001649 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001650 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001651 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001652
1653 // ACTION_UP
1654 mDevice->sendSlot(FIRST_SLOT);
1655 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001656 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001657 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1658 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1659}
1660
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001661/**
1662 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1663 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1664 * data?
1665 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1666 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1667 * for Pointer 0 only is generated after.
1668 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1669 * events, we will not miss any information.
1670 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1671 * event generated afterwards that contains the newest movement of pointer 0.
1672 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1673 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1674 * losing information about non-palm pointers.
1675 */
1676TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1677 NotifyMotionArgs args;
1678 const Point centerPoint = mDevice->getCenterPoint();
1679
1680 // ACTION_DOWN
1681 mDevice->sendSlot(FIRST_SLOT);
1682 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1683 mDevice->sendDown(centerPoint);
1684 mDevice->sendSync();
1685 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1686
1687 // ACTION_POINTER_DOWN (Second slot)
1688 const Point secondPoint = centerPoint + Point(100, 100);
1689 mDevice->sendSlot(SECOND_SLOT);
1690 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1691 mDevice->sendDown(secondPoint);
1692 mDevice->sendSync();
1693 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1694
1695 // ACTION_MOVE (First slot)
1696 mDevice->sendSlot(FIRST_SLOT);
1697 mDevice->sendMove(centerPoint + Point(5, 5));
1698 // ACTION_POINTER_UP (Second slot)
1699 mDevice->sendSlot(SECOND_SLOT);
1700 mDevice->sendPointerUp();
1701 // Send a single sync for the above 2 pointer updates
1702 mDevice->sendSync();
1703
1704 // First, we should get POINTER_UP for the second pointer
1705 assertReceivedMotion(ACTION_POINTER_1_UP,
1706 {/*first pointer */ centerPoint + Point(5, 5),
1707 /*second pointer*/ secondPoint});
1708
1709 // Next, the MOVE event for the first pointer
1710 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1711}
1712
1713/**
1714 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1715 * move, and then it will go up, all in the same frame.
1716 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1717 * gets sent to the listener.
1718 */
1719TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1720 NotifyMotionArgs args;
1721 const Point centerPoint = mDevice->getCenterPoint();
1722
1723 // ACTION_DOWN
1724 mDevice->sendSlot(FIRST_SLOT);
1725 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1726 mDevice->sendDown(centerPoint);
1727 mDevice->sendSync();
1728 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1729
1730 // ACTION_POINTER_DOWN (Second slot)
1731 const Point secondPoint = centerPoint + Point(100, 100);
1732 mDevice->sendSlot(SECOND_SLOT);
1733 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1734 mDevice->sendDown(secondPoint);
1735 mDevice->sendSync();
1736 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1737
1738 // ACTION_MOVE (First slot)
1739 mDevice->sendSlot(FIRST_SLOT);
1740 mDevice->sendMove(centerPoint + Point(5, 5));
1741 // ACTION_POINTER_UP (Second slot)
1742 mDevice->sendSlot(SECOND_SLOT);
1743 mDevice->sendMove(secondPoint + Point(6, 6));
1744 mDevice->sendPointerUp();
1745 // Send a single sync for the above 2 pointer updates
1746 mDevice->sendSync();
1747
1748 // First, we should get POINTER_UP for the second pointer
1749 // The movement of the second pointer during the liftoff frame is ignored.
1750 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1751 assertReceivedMotion(ACTION_POINTER_1_UP,
1752 {/*first pointer */ centerPoint + Point(5, 5),
1753 /*second pointer*/ secondPoint});
1754
1755 // Next, the MOVE event for the first pointer
1756 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1757}
1758
Arthur Hungaab25622020-01-16 11:22:11 +08001759TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1760 NotifyMotionArgs args;
1761 const Point centerPoint = mDevice->getCenterPoint();
1762
1763 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001764 mDevice->sendSlot(FIRST_SLOT);
1765 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001766 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001767 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001768 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1769 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1770
arthurhungcc7f9802020-04-30 17:55:40 +08001771 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001772 const Point secondPoint = centerPoint + Point(100, 100);
1773 mDevice->sendSlot(SECOND_SLOT);
1774 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1775 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001776 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001777 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001778 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001779
arthurhungcc7f9802020-04-30 17:55:40 +08001780 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001781 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001782 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001783 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1784 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1785
arthurhungcc7f9802020-04-30 17:55:40 +08001786 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1787 // a palm event.
1788 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001789 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001790 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001791 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001792 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001793 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001794
arthurhungcc7f9802020-04-30 17:55:40 +08001795 // Send up to second slot, expect first slot send moving.
1796 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001797 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001798 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001800
arthurhungcc7f9802020-04-30 17:55:40 +08001801 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001802 mDevice->sendSlot(FIRST_SLOT);
1803 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001804 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001805
arthurhungcc7f9802020-04-30 17:55:40 +08001806 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1807 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001808}
1809
Prabir Pradhanda20b172022-09-26 17:01:18 +00001810TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1811 const Point centerPoint = mDevice->getCenterPoint();
1812
1813 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1814 mDevice->sendSlot(FIRST_SLOT);
1815 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1816 mDevice->sendToolType(MT_TOOL_PEN);
1817 mDevice->sendDown(centerPoint);
1818 mDevice->sendSync();
1819 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1820 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001821 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001822
1823 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1824
1825 // Release the stylus touch.
1826 mDevice->sendUp();
1827 mDevice->sendSync();
1828 ASSERT_NO_FATAL_FAILURE(
1829 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1830
1831 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1832
1833 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1834 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1835 mDevice->sendToolType(MT_TOOL_FINGER);
1836 mDevice->sendDown(centerPoint);
1837 mDevice->sendSync();
1838 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1839 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001840 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001841
1842 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1843
1844 mDevice->sendUp();
1845 mDevice->sendSync();
1846 ASSERT_NO_FATAL_FAILURE(
1847 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1848
1849 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1850 // The policy should be notified of the stylus presence.
1851 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1852 mDevice->sendToolType(MT_TOOL_PEN);
1853 mDevice->sendMove(centerPoint);
1854 mDevice->sendSync();
1855 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1856 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001857 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001858
1859 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1860}
1861
Prabir Pradhan124ea442022-10-28 20:27:44 +00001862// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001863
Prabir Pradhan124ea442022-10-28 20:27:44 +00001864// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1865// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1866// stylus.
1867template <typename UinputStylusDevice>
1868class StylusButtonIntegrationTest : public TouchIntegrationTest {
1869protected:
1870 void SetUp() override {
1871#if !defined(__ANDROID__)
1872 GTEST_SKIP();
1873#endif
1874 TouchIntegrationTest::SetUp();
1875 mTouchscreen = mDevice.get();
1876 mTouchscreenInfo = mDeviceInfo;
1877
1878 setUpStylusDevice();
1879 }
1880
1881 UinputStylusDevice* mStylus{nullptr};
1882 InputDeviceInfo mStylusInfo{};
1883
1884 UinputTouchScreen* mTouchscreen{nullptr};
1885 InputDeviceInfo mTouchscreenInfo{};
1886
1887private:
1888 // When we are attempting to test stylus button events that are sent from the touchscreen,
1889 // use the same Uinput device for the touchscreen and the stylus.
1890 template <typename T = UinputStylusDevice>
1891 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1892 mStylus = mDevice.get();
1893 mStylusInfo = mDeviceInfo;
1894 }
1895
1896 // When we are attempting to stylus buttons from an external stylus being merged with touches
1897 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1898 template <typename T = UinputStylusDevice>
1899 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1900 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1901 mStylus = mStylusDeviceLifecycleTracker.get();
1902 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1903 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1904 const auto info = findDeviceByName(mStylus->getName());
1905 ASSERT_TRUE(info);
1906 mStylusInfo = *info;
1907 }
1908
1909 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1910
1911 // Hide the base class's device to expose it with a different name for readability.
1912 using TouchIntegrationTest::mDevice;
1913 using TouchIntegrationTest::mDeviceInfo;
1914};
1915
1916using StylusButtonIntegrationTestTypes =
1917 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1918TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1919
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001920TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001921 const auto stylusId = TestFixture::mStylusInfo.getId();
1922
1923 TestFixture::mStylus->pressKey(BTN_STYLUS);
1924 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1925 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1926 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1927
1928 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1929 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001930 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001931 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001932}
1933
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001934TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001935 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1936 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1937 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001938
1939 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001940 TestFixture::mStylus->pressKey(BTN_STYLUS);
1941 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001942 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001943 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001944
1945 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001946 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1947 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1948 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1949 TestFixture::mTouchscreen->sendDown(centerPoint);
1950 TestFixture::mTouchscreen->sendSync();
1951 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001952 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001953 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001954 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1955 WithDeviceId(touchscreenId))));
1956 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001957 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001958 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001959 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1960 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001961
Prabir Pradhan124ea442022-10-28 20:27:44 +00001962 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1963 TestFixture::mTouchscreen->sendSync();
1964 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001965 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001966 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001967 WithDeviceId(touchscreenId))));
1968 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001969 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001970 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001971 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001972
1973 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001974 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1975 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001976 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001977 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001978}
1979
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001980TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001981 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1982 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1983 const auto stylusId = TestFixture::mStylusInfo.getId();
1984 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001985 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001986
1987 // Press the stylus button.
1988 TestFixture::mStylus->pressKey(BTN_STYLUS);
1989 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1990 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1991 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1992
1993 // Start hovering with the stylus.
1994 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1995 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1996 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1997 TestFixture::mTouchscreen->sendMove(centerPoint);
1998 TestFixture::mTouchscreen->sendSync();
1999 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2000 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2001 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2002 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2003 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2004 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2005 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2006 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2007 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2008
2009 // Touch down with the stylus.
2010 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2011 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2012 TestFixture::mTouchscreen->sendDown(centerPoint);
2013 TestFixture::mTouchscreen->sendSync();
2014 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2015 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2016 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2017
2018 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2019 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2020 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2021
2022 // Stop touching with the stylus, and start hovering.
2023 TestFixture::mTouchscreen->sendUp();
2024 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2025 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2026 TestFixture::mTouchscreen->sendMove(centerPoint);
2027 TestFixture::mTouchscreen->sendSync();
2028 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2029 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2030 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2031 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2032 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2033 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2034 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2035 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2036 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2037
2038 // Stop hovering.
2039 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2040 TestFixture::mTouchscreen->sendSync();
2041 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2042 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2043 WithButtonState(0))));
2044 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2045 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2046 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2047 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2048
2049 // Release the stylus button.
2050 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2051 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2052 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2053 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2054}
2055
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002056TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002057 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2058 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2059 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002060
2061 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002062 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2063 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2064 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2065 TestFixture::mTouchscreen->sendDown(centerPoint);
2066 TestFixture::mTouchscreen->sendSync();
2067 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002068 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002069 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002070 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002071
2072 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002073 TestFixture::mStylus->pressKey(BTN_STYLUS);
2074 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002075 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002076 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2077 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002078 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002079 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002080 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2081 WithDeviceId(touchscreenId))));
2082 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002083 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002084 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002085 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2086 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002087
Prabir Pradhan124ea442022-10-28 20:27:44 +00002088 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2089 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002090 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002091 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2092 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002093 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002094 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002095 WithDeviceId(touchscreenId))));
2096 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002097 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002098 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002099 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002100
2101 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002102 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2103 TestFixture::mTouchscreen->sendSync();
2104 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002105 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002106 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002107 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002108}
2109
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002110TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002111 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2112 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002113 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002114
2115 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2116 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2117 const auto stylusId = TestFixture::mStylusInfo.getId();
2118
2119 // Start a stylus gesture. By the time this event is processed, the configuration change that
2120 // was requested is guaranteed to be completed.
2121 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2122 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2123 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2124 TestFixture::mTouchscreen->sendDown(centerPoint);
2125 TestFixture::mTouchscreen->sendSync();
2126 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2127 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002128 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002129 WithDeviceId(touchscreenId))));
2130
2131 // Press and release a stylus button. Each change only generates a MOVE motion event.
2132 // Key events are unaffected.
2133 TestFixture::mStylus->pressKey(BTN_STYLUS);
2134 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2135 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2136 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2137 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2138 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002139 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002140 WithDeviceId(touchscreenId))));
2141
2142 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2143 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2144 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2145 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2146 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2147 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002148 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002149 WithDeviceId(touchscreenId))));
2150
2151 // Finish the stylus gesture.
2152 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2153 TestFixture::mTouchscreen->sendSync();
2154 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2155 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002156 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002157 WithDeviceId(touchscreenId))));
2158}
2159
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002160// --- ExternalStylusIntegrationTest ---
2161
2162// Verify the behavior of an external stylus. An external stylus can report pressure or button
2163// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2164// ongoing stylus gesture that is being emitted by the touchscreen.
2165using ExternalStylusIntegrationTest = TouchIntegrationTest;
2166
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002167TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002168 const Point centerPoint = mDevice->getCenterPoint();
2169
2170 // Create an external stylus capable of reporting pressure data that
2171 // should be fused with a touch pointer.
2172 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2173 createUinputDevice<UinputExternalStylusWithPressure>();
2174 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2175 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2176 const auto stylusInfo = findDeviceByName(stylus->getName());
2177 ASSERT_TRUE(stylusInfo);
2178
2179 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2180
2181 const auto touchscreenId = mDeviceInfo.getId();
2182
2183 // Set a pressure value on the stylus. It doesn't generate any events.
2184 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2185 stylus->setPressure(100);
2186 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2187
2188 // Start a finger gesture, and ensure it shows up as stylus gesture
2189 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002190 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002191 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002192 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002193 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002194 mDevice->sendSync();
2195 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2196 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002197 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002198 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002199
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002200 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2201 // event with the updated pressure.
2202 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002203 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2204 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002205 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002206 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002207
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002208 // The external stylus did not generate any events.
2209 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2210 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2211}
2212
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002213TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002214 const Point centerPoint = mDevice->getCenterPoint();
2215
2216 // Create an external stylus capable of reporting pressure data that
2217 // should be fused with a touch pointer.
2218 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2219 createUinputDevice<UinputExternalStylusWithPressure>();
2220 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2221 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2222 const auto stylusInfo = findDeviceByName(stylus->getName());
2223 ASSERT_TRUE(stylusInfo);
2224
2225 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2226
2227 const auto touchscreenId = mDeviceInfo.getId();
2228
2229 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2230 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002231 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2232 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002233 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002234 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002235
2236 // Start a finger gesture. The touch device will withhold generating any touches for
2237 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2238 mDevice->sendSlot(FIRST_SLOT);
2239 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2240 mDevice->sendToolType(MT_TOOL_FINGER);
2241 mDevice->sendDown(centerPoint);
2242 auto waitUntil = std::chrono::system_clock::now() +
2243 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002244 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002245 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002246
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002247 // Since the external stylus did not report a pressure value within the timeout,
2248 // it shows up as a finger pointer.
2249 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2250 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002251 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002252 WithPressure(1.f))));
2253
2254 // Change the pressure on the external stylus. Since the pressure was not present at the start
2255 // of the gesture, it is ignored for now.
2256 stylus->setPressure(200);
2257 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2258
2259 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002260 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2261 mDevice->sendSync();
2262 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2263 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002264 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002265
2266 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2267 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2268 mDevice->sendToolType(MT_TOOL_FINGER);
2269 mDevice->sendDown(centerPoint);
2270 mDevice->sendSync();
2271 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2272 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002273 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002274 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2275
2276 // The external stylus did not generate any events.
2277 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2278 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002279}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002280
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002281TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002282 const Point centerPoint = mDevice->getCenterPoint();
2283
2284 // Create an external stylus device that does not support pressure. It should not affect any
2285 // touch pointers.
2286 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2287 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2288 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2289 const auto stylusInfo = findDeviceByName(stylus->getName());
2290 ASSERT_TRUE(stylusInfo);
2291
2292 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2293
2294 const auto touchscreenId = mDeviceInfo.getId();
2295
2296 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2297 // pressure data from the external stylus.
2298 mDevice->sendSlot(FIRST_SLOT);
2299 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2300 mDevice->sendToolType(MT_TOOL_FINGER);
2301 mDevice->sendDown(centerPoint);
2302 auto waitUntil = std::chrono::system_clock::now() +
2303 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2304 mDevice->sendSync();
2305 ASSERT_NO_FATAL_FAILURE(
2306 mTestListener
2307 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2308 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002309 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002310 WithButtonState(0),
2311 WithDeviceId(touchscreenId),
2312 WithPressure(1.f)),
2313 waitUntil));
2314
2315 // The external stylus did not generate any events.
2316 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2317 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2318}
2319
Michael Wrightd02c5b62014-02-10 15:10:22 -08002320// --- InputDeviceTest ---
2321class InputDeviceTest : public testing::Test {
2322protected:
2323 static const char* DEVICE_NAME;
2324 static const char* DEVICE_LOCATION;
2325 static const int32_t DEVICE_ID;
2326 static const int32_t DEVICE_GENERATION;
2327 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002328 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002329 static const int32_t EVENTHUB_ID;
2330 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2331
2332 std::shared_ptr<FakeEventHub> mFakeEventHub;
2333 sp<FakeInputReaderPolicy> mFakePolicy;
2334 std::unique_ptr<TestInputListener> mFakeListener;
2335 std::unique_ptr<InstrumentedInputReader> mReader;
2336 std::shared_ptr<InputDevice> mDevice;
2337
2338 void SetUp() override {
2339 mFakeEventHub = std::make_unique<FakeEventHub>();
2340 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2341 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002342 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002343 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002344 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002345 identifier.name = DEVICE_NAME;
2346 identifier.location = DEVICE_LOCATION;
2347 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2348 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2349 identifier);
2350 mReader->pushNextDevice(mDevice);
2351 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002352 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002353 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002354
2355 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002356 mFakeListener.reset();
2357 mFakePolicy.clear();
2358 }
2359};
2360
2361const char* InputDeviceTest::DEVICE_NAME = "device";
2362const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2363const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2364const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002365const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002366const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2367 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002368const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002369const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2370
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002371TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002372 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002373 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2374 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002375}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002376
Michael Wrightd02c5b62014-02-10 15:10:22 -08002377TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2378 ASSERT_EQ(mDevice->isEnabled(), false);
2379}
2380
2381TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2382 // Configuration.
2383 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002384 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002385
2386 // Reset.
2387 unused += mDevice->reset(ARBITRARY_TIME);
2388
2389 NotifyDeviceResetArgs resetArgs;
2390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2391 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2392 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2393
2394 // Metadata.
2395 ASSERT_TRUE(mDevice->isIgnored());
2396 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2397
2398 InputDeviceInfo info = mDevice->getDeviceInfo();
2399 ASSERT_EQ(DEVICE_ID, info.getId());
2400 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2401 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2402 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2403
2404 // State queries.
2405 ASSERT_EQ(0, mDevice->getMetaState());
2406
2407 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2408 << "Ignored device should return unknown key code state.";
2409 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2410 << "Ignored device should return unknown scan code state.";
2411 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2412 << "Ignored device should return unknown switch state.";
2413
2414 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2415 uint8_t flags[2] = { 0, 1 };
2416 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2417 << "Ignored device should never mark any key codes.";
2418 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2419 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2420}
2421
2422TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2423 // Configuration.
2424 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2425
2426 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002427 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2428 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002429 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2430 mapper1.setMetaState(AMETA_ALT_ON);
2431 mapper1.addSupportedKeyCode(AKEYCODE_A);
2432 mapper1.addSupportedKeyCode(AKEYCODE_B);
2433 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2434 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2435 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2436 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2437 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2438
2439 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002440 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2441 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002442 mapper2.setMetaState(AMETA_SHIFT_ON);
2443
2444 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002445 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002446
Harry Cuttsf13161a2023-03-08 14:15:49 +00002447 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2448 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002449 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002450 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002451
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002452 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2453 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454
2455 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002456 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002457 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2458 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002459
2460 NotifyDeviceResetArgs resetArgs;
2461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2462 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2463 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2464
2465 // Metadata.
2466 ASSERT_FALSE(mDevice->isIgnored());
2467 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2468
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002469 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002470 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002471 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2473 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2474
2475 // State queries.
2476 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2477 << "Should query mappers and combine meta states.";
2478
2479 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2480 << "Should return unknown key code state when source not supported.";
2481 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2482 << "Should return unknown scan code state when source not supported.";
2483 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2484 << "Should return unknown switch state when source not supported.";
2485
2486 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2487 << "Should query mapper when source is supported.";
2488 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2489 << "Should query mapper when source is supported.";
2490 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2491 << "Should query mapper when source is supported.";
2492
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002493 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002494 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002495 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002496 << "Should do nothing when source is unsupported.";
2497 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2498 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2499 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2500 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2501
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002502 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002503 << "Should query mapper when source is supported.";
2504 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2505 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2506 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2507 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2508
2509 // Event handling.
2510 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002511 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002512 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002513
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002514 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2515 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516}
2517
Arthur Hung2c9a3342019-07-23 14:18:59 +08002518// A single input device is associated with a specific display. Check that:
2519// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh3e56f7e2023-07-07 13:12:37 +00002520// 2. Device is disabled when setEnabled API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002521TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002522 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2523 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002524
2525 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002526 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002527 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2528 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002529
2530 // Device should be enabled by default.
2531 ASSERT_TRUE(mDevice->isEnabled());
2532
2533 // Prepare associated info.
2534 constexpr uint8_t hdmi = 1;
2535 const std::string UNIQUE_ID = "local:1";
2536
2537 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002538 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002539 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002540 // Device should be disabled because it is associated with a specific display via
2541 // input port <-> display port association, but the corresponding display is not found
2542 ASSERT_FALSE(mDevice->isEnabled());
2543
2544 // Prepare displays.
2545 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002546 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002547 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002548 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002549 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002550 ASSERT_TRUE(mDevice->isEnabled());
2551
2552 // Device should be disabled after set disable.
2553 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002554 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002555 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002556 ASSERT_FALSE(mDevice->isEnabled());
2557
2558 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002559 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002560 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002561 ASSERT_FALSE(mDevice->isEnabled());
2562}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002563
Christine Franks1ba71cc2021-04-07 14:37:42 -07002564TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2565 // Device should be enabled by default.
2566 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002567 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2568 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002569 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002570 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2571 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002572 ASSERT_TRUE(mDevice->isEnabled());
2573
2574 // Device should be disabled because it is associated with a specific display, but the
2575 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002576 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002577 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002578 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002579 ASSERT_FALSE(mDevice->isEnabled());
2580
2581 // Device should be enabled when a display is found.
2582 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002583 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002584 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002585 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002586 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002587 ASSERT_TRUE(mDevice->isEnabled());
2588
2589 // Device should be disabled after set disable.
2590 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002591 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002592 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002593 ASSERT_FALSE(mDevice->isEnabled());
2594
2595 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002596 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002597 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002598 ASSERT_FALSE(mDevice->isEnabled());
2599}
2600
Christine Franks2a2293c2022-01-18 11:51:16 -08002601TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2602 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002603 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2604 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002605 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002606 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2607 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002608
Christine Franks2a2293c2022-01-18 11:51:16 -08002609 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2610 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002611 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002612 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002613 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002614 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002615 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2616}
2617
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002618/**
2619 * This test reproduces a crash caused by a dangling reference that remains after device is added
2620 * and removed. The reference is accessed in InputDevice::dump(..);
2621 */
2622TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2623 constexpr int32_t TEST_EVENTHUB_ID = 10;
2624 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2625
Harry Cutts33476232023-01-30 19:57:29 +00002626 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh7f1765e2023-07-07 13:12:37 +00002627 device.addEventHubDevice(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002628 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2629 std::string dumpStr, eventHubDevStr;
2630 device.dump(dumpStr, eventHubDevStr);
2631}
2632
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002633TEST_F(InputDeviceTest, GetBluetoothAddress) {
2634 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2635 ASSERT_TRUE(address);
2636 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2637}
2638
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639// --- SwitchInputMapperTest ---
2640
2641class SwitchInputMapperTest : public InputMapperTest {
2642protected:
2643};
2644
2645TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002646 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002647
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002648 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649}
2650
2651TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002652 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002654 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002655 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002657 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002658 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002659}
2660
2661TEST_F(SwitchInputMapperTest, Process) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002662 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002663 std::list<NotifyArgs> out;
2664 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2665 ASSERT_TRUE(out.empty());
2666 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2667 ASSERT_TRUE(out.empty());
2668 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2669 ASSERT_TRUE(out.empty());
2670 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002672 ASSERT_EQ(1u, out.size());
2673 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002675 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2676 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677 args.switchMask);
2678 ASSERT_EQ(uint32_t(0), args.policyFlags);
2679}
2680
Chris Ye87143712020-11-10 05:05:58 +00002681// --- VibratorInputMapperTest ---
2682class VibratorInputMapperTest : public InputMapperTest {
2683protected:
2684 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2685};
2686
2687TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singhc68f85b2023-04-26 16:23:13 +00002688 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002689
2690 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2691}
2692
2693TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singhc68f85b2023-04-26 16:23:13 +00002694 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002695
2696 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2697}
2698
2699TEST_F(VibratorInputMapperTest, Vibrate) {
2700 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002701 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singhc68f85b2023-04-26 16:23:13 +00002702 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002703
2704 VibrationElement pattern(2);
2705 VibrationSequence sequence(2);
2706 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002707 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2708 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002709 sequence.addElement(pattern);
2710 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002711 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2712 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002713 sequence.addElement(pattern);
2714
2715 std::vector<int64_t> timings = {0, 1};
2716 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2717
2718 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002719 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002720 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002721 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002722 // Verify vibrator state listener was notified.
2723 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002724 ASSERT_EQ(1u, out.size());
2725 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2726 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2727 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002728 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002729 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002730 ASSERT_FALSE(mapper.isVibrating());
2731 // Verify vibrator state listener was notified.
2732 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002733 ASSERT_EQ(1u, out.size());
2734 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2735 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2736 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002737}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002738
Chris Yef59a2f42020-10-16 12:55:26 -07002739// --- SensorInputMapperTest ---
2740
2741class SensorInputMapperTest : public InputMapperTest {
2742protected:
2743 static const int32_t ACCEL_RAW_MIN;
2744 static const int32_t ACCEL_RAW_MAX;
2745 static const int32_t ACCEL_RAW_FUZZ;
2746 static const int32_t ACCEL_RAW_FLAT;
2747 static const int32_t ACCEL_RAW_RESOLUTION;
2748
2749 static const int32_t GYRO_RAW_MIN;
2750 static const int32_t GYRO_RAW_MAX;
2751 static const int32_t GYRO_RAW_FUZZ;
2752 static const int32_t GYRO_RAW_FLAT;
2753 static const int32_t GYRO_RAW_RESOLUTION;
2754
2755 static const float GRAVITY_MS2_UNIT;
2756 static const float DEGREE_RADIAN_UNIT;
2757
2758 void prepareAccelAxes();
2759 void prepareGyroAxes();
2760 void setAccelProperties();
2761 void setGyroProperties();
2762 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2763};
2764
2765const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2766const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2767const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2768const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2769const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2770
2771const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2772const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2773const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2774const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2775const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2776
2777const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2778const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2779
2780void SensorInputMapperTest::prepareAccelAxes() {
2781 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2782 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2783 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2784 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2785 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2786 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2787}
2788
2789void SensorInputMapperTest::prepareGyroAxes() {
2790 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2791 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2792 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2793 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2794 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2795 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2796}
2797
2798void SensorInputMapperTest::setAccelProperties() {
2799 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2800 /* sensorDataIndex */ 0);
2801 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2802 /* sensorDataIndex */ 1);
2803 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2804 /* sensorDataIndex */ 2);
2805 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2806 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2807 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2808 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2809 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2810}
2811
2812void SensorInputMapperTest::setGyroProperties() {
2813 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2814 /* sensorDataIndex */ 0);
2815 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2816 /* sensorDataIndex */ 1);
2817 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2818 /* sensorDataIndex */ 2);
2819 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2820 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2821 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2822 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2823 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2824}
2825
2826TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singh61dd33e2023-04-26 15:07:55 +00002827 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002828
2829 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2830}
2831
2832TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2833 setAccelProperties();
2834 prepareAccelAxes();
Arpit Singh61dd33e2023-04-26 15:07:55 +00002835 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002836
2837 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2838 std::chrono::microseconds(10000),
2839 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002840 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2842 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002846
2847 NotifySensorArgs args;
2848 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2849 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2850 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2851
2852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2853 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2854 ASSERT_EQ(args.deviceId, DEVICE_ID);
2855 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2856 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2857 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2858 ASSERT_EQ(args.values, values);
2859 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2860}
2861
2862TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2863 setGyroProperties();
2864 prepareGyroAxes();
Arpit Singh61dd33e2023-04-26 15:07:55 +00002865 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002866
2867 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2868 std::chrono::microseconds(10000),
2869 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002870 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2872 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2873 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2874 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002876
2877 NotifySensorArgs args;
2878 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2879 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2880 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2881
2882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2883 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2884 ASSERT_EQ(args.deviceId, DEVICE_ID);
2885 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2886 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2887 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2888 ASSERT_EQ(args.values, values);
2889 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2890}
2891
Michael Wrightd02c5b62014-02-10 15:10:22 -08002892// --- KeyboardInputMapperTest ---
2893
2894class KeyboardInputMapperTest : public InputMapperTest {
2895protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002896 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002897 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002898 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002899
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002900 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002901 int32_t originalKeyCode, int32_t rotatedKeyCode,
2902 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002903};
2904
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002905/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2906 * orientation.
2907 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002908void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002909 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2910 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002911}
2912
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002913void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002914 int32_t originalScanCode, int32_t originalKeyCode,
2915 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002916 NotifyKeyArgs args;
2917
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2920 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2921 ASSERT_EQ(originalScanCode, args.scanCode);
2922 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002923 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2927 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2928 ASSERT_EQ(originalScanCode, args.scanCode);
2929 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002930 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002931}
2932
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002934 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00002935 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002936 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002937
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002938 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002939}
2940
2941TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2942 const int32_t USAGE_A = 0x070004;
2943 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002944 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2945 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002946 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2947 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2948 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002949
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002950 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00002951 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002952 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002953 // Initial metastate is AMETA_NONE.
2954 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002955
2956 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 NotifyKeyArgs args;
2959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2960 ASSERT_EQ(DEVICE_ID, args.deviceId);
2961 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2962 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2963 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2964 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2965 ASSERT_EQ(KEY_HOME, args.scanCode);
2966 ASSERT_EQ(AMETA_NONE, args.metaState);
2967 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2968 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2969 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2970
2971 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002972 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2974 ASSERT_EQ(DEVICE_ID, args.deviceId);
2975 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2976 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2977 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2978 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2979 ASSERT_EQ(KEY_HOME, args.scanCode);
2980 ASSERT_EQ(AMETA_NONE, args.metaState);
2981 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2982 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2983 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2984
2985 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002986 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2989 ASSERT_EQ(DEVICE_ID, args.deviceId);
2990 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2991 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2992 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2993 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2994 ASSERT_EQ(0, args.scanCode);
2995 ASSERT_EQ(AMETA_NONE, args.metaState);
2996 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2997 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2998 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2999
3000 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3002 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3004 ASSERT_EQ(DEVICE_ID, args.deviceId);
3005 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3006 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3007 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3008 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3009 ASSERT_EQ(0, args.scanCode);
3010 ASSERT_EQ(AMETA_NONE, args.metaState);
3011 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3012 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3013 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3014
3015 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3019 ASSERT_EQ(DEVICE_ID, args.deviceId);
3020 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3021 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3022 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3023 ASSERT_EQ(0, args.keyCode);
3024 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3025 ASSERT_EQ(AMETA_NONE, args.metaState);
3026 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3027 ASSERT_EQ(0U, args.policyFlags);
3028 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3029
3030 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3032 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3034 ASSERT_EQ(DEVICE_ID, args.deviceId);
3035 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3036 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3037 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3038 ASSERT_EQ(0, args.keyCode);
3039 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3040 ASSERT_EQ(AMETA_NONE, args.metaState);
3041 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3042 ASSERT_EQ(0U, args.policyFlags);
3043 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3044}
3045
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003046TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3047 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3048 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3049 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3050
3051 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003052 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003053 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3054
3055 // Key down by scan code.
3056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3057 NotifyKeyArgs args;
3058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3059 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3060
3061 // Key up by scan code.
3062 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3064 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3065}
3066
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003067/**
3068 * Ensure that the readTime is set to the time when the EV_KEY is received.
3069 */
3070TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3071 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3072
3073 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003074 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003075 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3076 NotifyKeyArgs args;
3077
3078 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003079 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3081 ASSERT_EQ(12, args.readTime);
3082
3083 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003084 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3086 ASSERT_EQ(15, args.readTime);
3087}
3088
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003090 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3091 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003092 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3093 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3094 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003096 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003097 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003098 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099
Arthur Hung95f68612022-04-07 14:08:22 +08003100 // Initial metastate is AMETA_NONE.
3101 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102
3103 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105 NotifyKeyArgs args;
3106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3107 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003108 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003109 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110
3111 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003112 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3114 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003115 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003116
3117 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003118 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3120 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003121 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003122
3123 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003124 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3126 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003127 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003128 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003129}
3130
3131TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003132 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3133 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3134 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3135 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003137 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003138 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003139 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003140
Michael Wrighta9cf4192022-12-01 23:46:39 +00003141 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003142 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3143 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3144 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3145 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3146 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3147 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3148 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3149 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3150}
3151
3152TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003153 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3154 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3155 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3156 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157
Michael Wrightd02c5b62014-02-10 15:10:22 -08003158 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003159 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003160 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003161 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162
Michael Wrighta9cf4192022-12-01 23:46:39 +00003163 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003164 ASSERT_NO_FATAL_FAILURE(
3165 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3166 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3167 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3168 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3169 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3170 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3171 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003173 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003174 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003175 ASSERT_NO_FATAL_FAILURE(
3176 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3177 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3178 AKEYCODE_DPAD_UP, DISPLAY_ID));
3179 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3180 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3181 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3182 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003184 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003185 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003186 ASSERT_NO_FATAL_FAILURE(
3187 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3188 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3189 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3190 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3191 AKEYCODE_DPAD_UP, DISPLAY_ID));
3192 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3193 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003195 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003196 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003197 ASSERT_NO_FATAL_FAILURE(
3198 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3199 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3200 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3201 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3202 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3203 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3204 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003205
3206 // Special case: if orientation changes while key is down, we still emit the same keycode
3207 // in the key up as we did in the key down.
3208 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003209 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003210 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3213 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3214 ASSERT_EQ(KEY_UP, args.scanCode);
3215 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3216
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003217 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003218 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3221 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3222 ASSERT_EQ(KEY_UP, args.scanCode);
3223 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3224}
3225
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003226TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3227 // If the keyboard is not orientation aware,
3228 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003229 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003230
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003231 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003232 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003233 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003234 NotifyKeyArgs args;
3235
3236 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3241 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3242
Michael Wrighta9cf4192022-12-01 23:46:39 +00003243 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3248 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3249}
3250
3251TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3252 // If the keyboard is orientation aware,
3253 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003254 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003255
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003256 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003257 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003258 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003259 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003260 NotifyKeyArgs args;
3261
3262 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3263 // ^--- already checked by the previous test
3264
Michael Wrighta9cf4192022-12-01 23:46:39 +00003265 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003266 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3271 ASSERT_EQ(DISPLAY_ID, args.displayId);
3272
3273 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003274 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003275 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003276 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3281 ASSERT_EQ(newDisplayId, args.displayId);
3282}
3283
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003285 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003286 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003287 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003289 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003290 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003292 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003293 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003294}
3295
Philip Junker4af3b3d2021-12-14 10:36:55 +01003296TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3297 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003298 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003299 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3300
3301 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3302 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3303 << "If a mapping is available, the result is equal to the mapping";
3304
3305 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3306 << "If no mapping is available, the result is the key location";
3307}
3308
Michael Wrightd02c5b62014-02-10 15:10:22 -08003309TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003310 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003311 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003312 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003314 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003315 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003316
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003317 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003318 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319}
3320
3321TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003322 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003323 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003324 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003326 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003329 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330 ASSERT_TRUE(flags[0]);
3331 ASSERT_FALSE(flags[1]);
3332}
3333
3334TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003335 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3336 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3337 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3338 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3339 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3340 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003342 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003343 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003344 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003345 // Initial metastate is AMETA_NONE.
3346 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347
3348 // Initialization should have turned all of the lights off.
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_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352
3353 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003356 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3357 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3358 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003359 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360
3361 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003364 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3365 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3366 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003367 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368
3369 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003372 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3373 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3374 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003375 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003376
3377 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003380 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3381 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3382 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003383 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384
3385 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003388 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3389 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3390 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003391 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003392
3393 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003394 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003396 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3397 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3398 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003399 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003400}
3401
Chris Yea52ade12020-08-27 16:49:20 -07003402TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3403 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3404 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3405 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3406 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3407
3408 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003409 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003410 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3411
Chris Yea52ade12020-08-27 16:49:20 -07003412 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003413 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003414 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3415 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3416 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3417 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3418
3419 NotifyKeyArgs args;
3420 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3423 ASSERT_EQ(AMETA_NONE, args.metaState);
3424 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3425 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3426 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3427
3428 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003429 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3431 ASSERT_EQ(AMETA_NONE, args.metaState);
3432 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3433 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3434 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3435}
3436
Arthur Hung2c9a3342019-07-23 14:18:59 +08003437TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3438 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003439 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3440 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3441 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3442 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003443
3444 // keyboard 2.
3445 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003446 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003447 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003448 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003449 std::shared_ptr<InputDevice> device2 =
3450 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003451 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003452
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003453 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3454 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3455 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3456 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003457
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003458 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003459 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003460 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003461
Arpit Singh033e3ec2023-04-26 14:43:16 +00003462 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003463 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003464 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3465 mFakePolicy
3466 ->getReaderConfiguration(),
3467 AINPUT_SOURCE_KEYBOARD,
3468 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003469 std::list<NotifyArgs> unused =
3470 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003471 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003472 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003473
3474 // Prepared displays and associated info.
3475 constexpr uint8_t hdmi1 = 0;
3476 constexpr uint8_t hdmi2 = 1;
3477 const std::string SECONDARY_UNIQUE_ID = "local:1";
3478
3479 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3480 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3481
3482 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003483 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003484 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003485 ASSERT_FALSE(device2->isEnabled());
3486
3487 // Prepare second display.
3488 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003489 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003490 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003491 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003492 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003493 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003494 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003495 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003496
3497 // Device should be enabled after the associated display is found.
3498 ASSERT_TRUE(mDevice->isEnabled());
3499 ASSERT_TRUE(device2->isEnabled());
3500
3501 // Test pad key events
3502 ASSERT_NO_FATAL_FAILURE(
3503 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3504 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3505 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3506 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3507 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3508 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3509 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3510
3511 ASSERT_NO_FATAL_FAILURE(
3512 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3513 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3514 AKEYCODE_DPAD_RIGHT, newDisplayId));
3515 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3516 AKEYCODE_DPAD_DOWN, newDisplayId));
3517 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3518 AKEYCODE_DPAD_LEFT, newDisplayId));
3519}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520
arthurhungc903df12020-08-11 15:08:42 +08003521TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3522 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3523 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3524 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3525 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3526 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3527 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3528
3529 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003530 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003531 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003532 // Initial metastate is AMETA_NONE.
3533 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003534
3535 // Initialization should have turned all of the lights off.
3536 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3537 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3538 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3539
3540 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003543 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3544 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3545
3546 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003549 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3550 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3551
3552 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003555 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3556 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3557
3558 mFakeEventHub->removeDevice(EVENTHUB_ID);
3559 mReader->loopOnce();
3560
3561 // keyboard 2 should default toggle keys.
3562 const std::string USB2 = "USB2";
3563 const std::string DEVICE_NAME2 = "KEYBOARD2";
3564 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3565 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3566 std::shared_ptr<InputDevice> device2 =
3567 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003568 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003569 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3570 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3571 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3572 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3573 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3574 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3575
Arpit Singh033e3ec2023-04-26 14:43:16 +00003576 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003577 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003578 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3579 mFakePolicy
3580 ->getReaderConfiguration(),
3581 AINPUT_SOURCE_KEYBOARD,
3582 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003583 std::list<NotifyArgs> unused =
3584 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003585 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003586 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003587
3588 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3589 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3590 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003591 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3592 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003593}
3594
Arthur Hungcb40a002021-08-03 14:31:01 +00003595TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3596 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3597 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3598 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3599
3600 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh033e3ec2023-04-26 14:43:16 +00003601 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003602 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3603 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003604 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003605 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003606 // Initial metastate is AMETA_NONE.
3607 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003608
3609 mReader->toggleCapsLockState(DEVICE_ID);
3610 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3611}
3612
Arthur Hungfb3cc112022-04-13 07:39:50 +00003613TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3614 // keyboard 1.
3615 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3616 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3617 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3618 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3619 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3620 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3621
3622 KeyboardInputMapper& mapper1 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003623 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003624 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3625
3626 // keyboard 2.
3627 const std::string USB2 = "USB2";
3628 const std::string DEVICE_NAME2 = "KEYBOARD2";
3629 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3630 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3631 std::shared_ptr<InputDevice> device2 =
3632 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3633 ftl::Flags<InputDeviceClass>(0));
3634 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3635 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3636 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3637 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3638 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3639 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3640
Arpit Singh033e3ec2023-04-26 14:43:16 +00003641 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003642 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003643 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3644 mFakePolicy
3645 ->getReaderConfiguration(),
3646 AINPUT_SOURCE_KEYBOARD,
3647 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003648 std::list<NotifyArgs> unused =
3649 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003650 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003651 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003652
Arthur Hung95f68612022-04-07 14:08:22 +08003653 // Initial metastate is AMETA_NONE.
3654 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3655 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3656
3657 // Toggle num lock on and off.
3658 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3659 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003660 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3661 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3662 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3663
3664 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3665 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3666 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3667 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3668 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3669
3670 // Toggle caps lock on and off.
3671 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3672 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3673 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3674 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3675 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3676
3677 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3678 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3679 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3680 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3681 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3682
3683 // Toggle scroll lock on and off.
3684 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3685 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3686 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3687 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3688 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3689
3690 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3691 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3692 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3693 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3694 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3695}
3696
Arthur Hung2141d542022-08-23 07:45:21 +00003697TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3698 const int32_t USAGE_A = 0x070004;
3699 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3700 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3701
3702 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003703 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003704 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3705 // Key down by scan code.
3706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3707 NotifyKeyArgs args;
3708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3709 ASSERT_EQ(DEVICE_ID, args.deviceId);
3710 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3711 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3712 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3713 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3714 ASSERT_EQ(KEY_HOME, args.scanCode);
3715 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3716
3717 // Disable device, it should synthesize cancellation events for down events.
3718 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003719 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003720
3721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3722 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3723 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3724 ASSERT_EQ(KEY_HOME, args.scanCode);
3725 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3726}
3727
Zixuan Qufecb6062022-11-12 04:44:31 +00003728TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh033e3ec2023-04-26 14:43:16 +00003729 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3730 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003731 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003732 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3733 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003734
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003735 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003736 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3737
3738 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003739 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003740
3741 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3742 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3743 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3744 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3745 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003746 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3747
3748 // Call change layout association with the same values: Generation shouldn't change
3749 generation = mReader->getContext()->getGeneration();
3750 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3751 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3752 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3753 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003754}
3755
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003756TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3757 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3758 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3759
3760 // Configuration
Arpit Singh033e3ec2023-04-26 14:43:16 +00003761 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003762 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3763 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003764 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003765
3766 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3767 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3768}
3769
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003770// --- KeyboardInputMapperTest_ExternalDevice ---
3771
3772class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3773protected:
Chris Yea52ade12020-08-27 16:49:20 -07003774 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003775};
3776
3777TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003778 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3779 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003780
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003781 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3782 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3783 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3784 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003785
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003786 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003787 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003788 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003789
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003791 NotifyKeyArgs args;
3792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3793 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3794
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003795 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3797 ASSERT_EQ(uint32_t(0), args.policyFlags);
3798
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003801 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003802
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003803 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3805 ASSERT_EQ(uint32_t(0), args.policyFlags);
3806
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3809 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3810
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003811 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3813 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3814}
3815
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003816TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003817 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003818
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003819 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3820 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3821 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003822
Powei Fengd041c5d2019-05-03 17:11:33 -07003823 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003824 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003825 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003826 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003827
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003829 NotifyKeyArgs args;
3830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3831 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3832
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003833 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3835 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3836
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3839 ASSERT_EQ(uint32_t(0), args.policyFlags);
3840
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003841 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3843 ASSERT_EQ(uint32_t(0), args.policyFlags);
3844
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3847 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3848
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003849 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3851 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3852}
3853
Michael Wrightd02c5b62014-02-10 15:10:22 -08003854// --- CursorInputMapperTest ---
3855
3856class CursorInputMapperTest : public InputMapperTest {
3857protected:
3858 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3859
Michael Wright17db18e2020-06-26 20:51:44 +01003860 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003861
Chris Yea52ade12020-08-27 16:49:20 -07003862 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863 InputMapperTest::SetUp();
3864
Michael Wright17db18e2020-06-26 20:51:44 +01003865 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003866 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867 }
3868
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003869 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3870 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003871
Michael Wrighta9cf4192022-12-01 23:46:39 +00003872 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003873 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3874 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3875 }
3876
3877 void prepareSecondaryDisplay() {
3878 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003879 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003880 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003881 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003882
3883 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3884 float pressure) {
3885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3886 0.0f, 0.0f, 0.0f, EPSILON));
3887 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003888};
3889
3890const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3891
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003892void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3893 int32_t originalY, int32_t rotatedX,
3894 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895 NotifyMotionArgs args;
3896
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3901 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003902 ASSERT_NO_FATAL_FAILURE(
3903 assertCursorPointerCoords(args.pointerCoords[0],
3904 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3905 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906}
3907
3908TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003910 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003911
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003912 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913}
3914
3915TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003917 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003919 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003920}
3921
3922TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003923 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003924 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003925
3926 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003927 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003928
3929 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003930 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3931 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003932 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3933 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3934
3935 // When the bounds are set, then there should be a valid motion range.
3936 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3937
3938 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003939 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003940
3941 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3942 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3943 1, 800 - 1, 0.0f, 0.0f));
3944 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3945 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3946 2, 480 - 1, 0.0f, 0.0f));
3947 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3948 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3949 0.0f, 1.0f, 0.0f, 0.0f));
3950}
3951
3952TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003954 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955
3956 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003957 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958
3959 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3960 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3961 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3962 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3963 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3964 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3965 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3966 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3967 0.0f, 1.0f, 0.0f, 0.0f));
3968}
3969
3970TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003971 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003972 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973
arthurhungdcef2dc2020-08-11 14:47:50 +08003974 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003975
3976 NotifyMotionArgs args;
3977
3978 // Button press.
3979 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3981 process(mapper, ARBITRARY_TIME, 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, 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);
3987 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3988 ASSERT_EQ(0, args.flags);
3989 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3990 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3991 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07003992 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -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, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003996 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3997 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3998 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3999
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4001 ASSERT_EQ(ARBITRARY_TIME, 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);
4005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4006 ASSERT_EQ(0, args.flags);
4007 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4008 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4009 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004010 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -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, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004014 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4015 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4016 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4017
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004019 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4020 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4022 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4023 ASSERT_EQ(DEVICE_ID, args.deviceId);
4024 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4025 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004026 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4027 ASSERT_EQ(0, args.flags);
4028 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4029 ASSERT_EQ(0, args.buttonState);
4030 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004031 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004032 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004033 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004034 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004035 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4036 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4037 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4038
4039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4040 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4041 ASSERT_EQ(DEVICE_ID, args.deviceId);
4042 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4043 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4045 ASSERT_EQ(0, args.flags);
4046 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4047 ASSERT_EQ(0, args.buttonState);
4048 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004049 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004050 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004051 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004052 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004053 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4054 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4055 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4056}
4057
4058TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004060 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061
4062 NotifyMotionArgs args;
4063
4064 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4066 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004069 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4070 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4071 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072
4073 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004078 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4079 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080}
4081
4082TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004084 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085
4086 NotifyMotionArgs args;
4087
4088 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004089 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4092 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004093 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4096 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004097 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004098
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004100 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004103 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004104 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004105
4106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004108 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109}
4110
4111TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004113 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114
4115 NotifyMotionArgs args;
4116
4117 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4123 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004124 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4125 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4126 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4129 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004130 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4131 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4132 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004133
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004140 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4141 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4142 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143
4144 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004148 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004149 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004150
4151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004153 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154}
4155
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004156TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004157 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004158 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004159 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4160 // need to be rotated.
4161 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004162 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004163
Michael Wrighta9cf4192022-12-01 23:46:39 +00004164 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4166 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4167 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4168 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4169 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4170 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4171 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4172 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4173}
4174
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004175TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004176 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004178 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4179 // orientation-aware are affected by display rotation.
Arpit Singhd8510bd2023-04-27 12:48:15 +00004180 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004182 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004183 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4187 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4188 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4189 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4190 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4191 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4192
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004193 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004194 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004195 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4196 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4197 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4198 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4199 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4200 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4201 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4202 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004204 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004205 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4207 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4208 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4209 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4210 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4211 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4212 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4213 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4214
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004215 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004216 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004217 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4218 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4219 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4220 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4221 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4222 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4223 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4224 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225}
4226
4227TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004229 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230
4231 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4232 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233
4234 NotifyMotionArgs motionArgs;
4235 NotifyKeyArgs keyArgs;
4236
4237 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4241 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4242 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004243 ASSERT_NO_FATAL_FAILURE(
4244 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4248 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004249 ASSERT_NO_FATAL_FAILURE(
4250 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004251
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004252 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004255 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004257 ASSERT_NO_FATAL_FAILURE(
4258 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259
4260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004261 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004263 ASSERT_NO_FATAL_FAILURE(
4264 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004265
4266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004268 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004269 ASSERT_NO_FATAL_FAILURE(
4270 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271
4272 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4277 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4278 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004279 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004280 ASSERT_NO_FATAL_FAILURE(
4281 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4284 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4285 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004286 ASSERT_NO_FATAL_FAILURE(
4287 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004288
4289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4290 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4291 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004292 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004293 ASSERT_NO_FATAL_FAILURE(
4294 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004295
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004301 ASSERT_NO_FATAL_FAILURE(
4302 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004303
4304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004306 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004307 ASSERT_NO_FATAL_FAILURE(
4308 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004313 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4314 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004315 ASSERT_NO_FATAL_FAILURE(
4316 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
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(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004323 ASSERT_NO_FATAL_FAILURE(
4324 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004325
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4327 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004329 ASSERT_NO_FATAL_FAILURE(
4330 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331
4332 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4336 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4337 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004338
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004340 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004342 ASSERT_NO_FATAL_FAILURE(
4343 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004344
4345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4346 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4347 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004348 ASSERT_NO_FATAL_FAILURE(
4349 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004356 ASSERT_NO_FATAL_FAILURE(
4357 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004358
4359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004361 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004362
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004363 ASSERT_NO_FATAL_FAILURE(
4364 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4366 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4367 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4368
4369 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4373 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4374 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004375
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004377 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004379 ASSERT_NO_FATAL_FAILURE(
4380 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004381
4382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4383 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4384 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004385 ASSERT_NO_FATAL_FAILURE(
4386 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004391 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004393 ASSERT_NO_FATAL_FAILURE(
4394 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004395
4396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4397 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4398 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004399 ASSERT_NO_FATAL_FAILURE(
4400 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004401
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4403 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4404 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4405
4406 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4410 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4411 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004414 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004416 ASSERT_NO_FATAL_FAILURE(
4417 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004418
4419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4420 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4421 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004422 ASSERT_NO_FATAL_FAILURE(
4423 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004428 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004430 ASSERT_NO_FATAL_FAILURE(
4431 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004432
4433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4434 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4435 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004436 ASSERT_NO_FATAL_FAILURE(
4437 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004438
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4440 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4441 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4442
4443 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4447 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4448 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004449
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004451 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004453 ASSERT_NO_FATAL_FAILURE(
4454 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004455
4456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4457 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4458 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004459 ASSERT_NO_FATAL_FAILURE(
4460 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004465 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004467 ASSERT_NO_FATAL_FAILURE(
4468 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004469
4470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4471 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4472 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004473 ASSERT_NO_FATAL_FAILURE(
4474 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004475
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4477 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4478 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4479}
4480
4481TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004483 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484
4485 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4486 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487
4488 NotifyMotionArgs args;
4489
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4492 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004494 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4495 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4497 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 +00004498 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004499}
4500
4501TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004502 addConfigurationProperty("cursor.mode", "pointer");
4503 mFakePolicy->setPointerCapture(true);
Arpit Singhd8510bd2023-04-27 12:48:15 +00004504 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004505
4506 NotifyDeviceResetArgs resetArgs;
4507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4508 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4509 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4510
4511 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4512 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004513
4514 NotifyMotionArgs args;
4515
4516 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
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 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 +00004525 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004526
4527 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004528 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4529 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4531 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4532 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4534 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4536 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4537 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4539 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4540
4541 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004542 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4543 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4545 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4546 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4548 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4550 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4551 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4553 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4554
4555 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4560 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4563 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 +00004564 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004565
4566 // Disable pointer capture and check that the device generation got bumped
4567 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004568 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004569 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004570 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004571 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004572
4573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004574 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4575
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4580 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4583 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 +00004584 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585}
4586
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004587/**
4588 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4589 * pointer acceleration or speed processing should not be applied.
4590 */
4591TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4592 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004593 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4594 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004595 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhd8510bd2023-04-27 12:48:15 +00004596 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004597
4598 NotifyDeviceResetArgs resetArgs;
4599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4600 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4601 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4602
4603 NotifyMotionArgs args;
4604
4605 // Move and verify scale is applied.
4606 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4607 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4608 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4610 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4611 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4612 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4613 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4614 ASSERT_GT(relX, 10);
4615 ASSERT_GT(relY, 20);
4616
4617 // Enable Pointer Capture
4618 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004619 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004620 NotifyPointerCaptureChangedArgs captureArgs;
4621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4622 ASSERT_TRUE(captureArgs.request.enable);
4623
4624 // Move and verify scale is not applied.
4625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4629 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4630 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4631 ASSERT_EQ(10, args.pointerCoords[0].getX());
4632 ASSERT_EQ(20, args.pointerCoords[0].getY());
4633}
4634
Prabir Pradhan208360b2022-06-24 18:37:04 +00004635TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4636 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004637 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004638
4639 NotifyDeviceResetArgs resetArgs;
4640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4641 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4642 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4643
4644 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004645 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004646
4647 NotifyMotionArgs args;
4648
4649 // Verify that the coordinates are rotated.
4650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4654 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4655 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4656 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4657 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4658
4659 // Enable Pointer Capture.
4660 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004661 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004662 NotifyPointerCaptureChangedArgs captureArgs;
4663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4664 ASSERT_TRUE(captureArgs.request.enable);
4665
4666 // Move and verify rotation is not applied.
4667 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4671 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4673 ASSERT_EQ(10, args.pointerCoords[0].getX());
4674 ASSERT_EQ(20, args.pointerCoords[0].getY());
4675}
4676
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004677TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004678 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004679
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004680 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004681 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004682
4683 // Set up the secondary display as the display on which the pointer should be shown.
4684 // The InputDevice is not associated with any display.
4685 prepareSecondaryDisplay();
4686 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004687 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004688
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004689 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004690 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004691
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004692 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4695 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004697 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4698 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4699 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004700 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004701}
4702
4703TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004704 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004705
4706 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004707 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004708
4709 // Set up the secondary display as the display on which the pointer should be shown,
4710 // and associate the InputDevice with the secondary display.
4711 prepareSecondaryDisplay();
4712 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4713 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004714 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004715
4716 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4717 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004718
4719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4721 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004723 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4724 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4725 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004726 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004727}
4728
4729TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004730 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004731
4732 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004733 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004734 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4735
4736 // Associate the InputDevice with the secondary display.
4737 prepareSecondaryDisplay();
4738 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004739 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004740
4741 // The mapper should not generate any events because it is associated with a display that is
4742 // different from the pointer display.
4743 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4744 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004747}
4748
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004749// --- BluetoothCursorInputMapperTest ---
4750
4751class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4752protected:
4753 void SetUp() override {
4754 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4755
4756 mFakePointerController = std::make_shared<FakePointerController>();
4757 mFakePolicy->setPointerController(mFakePointerController);
4758 }
4759};
4760
4761TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4762 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004763 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004764
4765 nsecs_t kernelEventTime = ARBITRARY_TIME;
4766 nsecs_t expectedEventTime = ARBITRARY_TIME;
4767 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4768 process(mapper, kernelEventTime, 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 // Process several events that come in quick succession, according to their timestamps.
4774 for (int i = 0; i < 3; i++) {
4775 constexpr static nsecs_t delta = ms2ns(1);
4776 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4777 kernelEventTime += delta;
4778 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4779
4780 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4781 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4783 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4784 WithEventTime(expectedEventTime))));
4785 }
4786}
4787
4788TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4789 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004790 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004791
4792 nsecs_t expectedEventTime = ARBITRARY_TIME;
4793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4796 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4797 WithEventTime(expectedEventTime))));
4798
4799 // Process several events with the same timestamp from the kernel.
4800 // Ensure that we do not generate events too far into the future.
4801 constexpr static int32_t numEvents =
4802 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4803 for (int i = 0; i < numEvents; i++) {
4804 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4805
4806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4809 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4810 WithEventTime(expectedEventTime))));
4811 }
4812
4813 // By processing more events with the same timestamp, we should not generate events with a
4814 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4815 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4816 for (int i = 0; i < 3; i++) {
4817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4818 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4820 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4821 WithEventTime(cappedEventTime))));
4822 }
4823}
4824
4825TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4826 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004827 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004828
4829 nsecs_t kernelEventTime = ARBITRARY_TIME;
4830 nsecs_t expectedEventTime = ARBITRARY_TIME;
4831 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4832 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4834 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4835 WithEventTime(expectedEventTime))));
4836
4837 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4838 // smoothening is not needed, its timestamp is not affected.
4839 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4840 expectedEventTime = kernelEventTime;
4841
4842 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4843 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4845 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4846 WithEventTime(expectedEventTime))));
4847}
4848
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849// --- TouchInputMapperTest ---
4850
4851class TouchInputMapperTest : public InputMapperTest {
4852protected:
4853 static const int32_t RAW_X_MIN;
4854 static const int32_t RAW_X_MAX;
4855 static const int32_t RAW_Y_MIN;
4856 static const int32_t RAW_Y_MAX;
4857 static const int32_t RAW_TOUCH_MIN;
4858 static const int32_t RAW_TOUCH_MAX;
4859 static const int32_t RAW_TOOL_MIN;
4860 static const int32_t RAW_TOOL_MAX;
4861 static const int32_t RAW_PRESSURE_MIN;
4862 static const int32_t RAW_PRESSURE_MAX;
4863 static const int32_t RAW_ORIENTATION_MIN;
4864 static const int32_t RAW_ORIENTATION_MAX;
4865 static const int32_t RAW_DISTANCE_MIN;
4866 static const int32_t RAW_DISTANCE_MAX;
4867 static const int32_t RAW_TILT_MIN;
4868 static const int32_t RAW_TILT_MAX;
4869 static const int32_t RAW_ID_MIN;
4870 static const int32_t RAW_ID_MAX;
4871 static const int32_t RAW_SLOT_MIN;
4872 static const int32_t RAW_SLOT_MAX;
4873 static const float X_PRECISION;
4874 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004875 static const float X_PRECISION_VIRTUAL;
4876 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877
4878 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004879 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880
4881 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4882
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004883 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004884 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004885
Michael Wrightd02c5b62014-02-10 15:10:22 -08004886 enum Axes {
4887 POSITION = 1 << 0,
4888 TOUCH = 1 << 1,
4889 TOOL = 1 << 2,
4890 PRESSURE = 1 << 3,
4891 ORIENTATION = 1 << 4,
4892 MINOR = 1 << 5,
4893 ID = 1 << 6,
4894 DISTANCE = 1 << 7,
4895 TILT = 1 << 8,
4896 SLOT = 1 << 9,
4897 TOOL_TYPE = 1 << 10,
4898 };
4899
Michael Wrighta9cf4192022-12-01 23:46:39 +00004900 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004901 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004902 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004904 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905 int32_t toRawX(float displayX);
4906 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004907 int32_t toRotatedRawX(float displayX);
4908 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004909 float toCookedX(float rawX, float rawY);
4910 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004911 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004912 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004913 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004914 float toDisplayY(int32_t rawY, int32_t displayHeight);
4915
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916};
4917
4918const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4919const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4920const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4921const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4922const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4923const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4924const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4925const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004926const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4927const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4929const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4930const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4931const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4932const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4933const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4934const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4935const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4936const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4937const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4938const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4939const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004940const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4941 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4942const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4943 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004944const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4945 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946
4947const float TouchInputMapperTest::GEOMETRIC_SCALE =
4948 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4949 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4950
4951const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4952 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4953 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4954};
4955
Michael Wrighta9cf4192022-12-01 23:46:39 +00004956void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004957 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4958 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004959}
4960
4961void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4962 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004963 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964}
4965
Michael Wrighta9cf4192022-12-01 23:46:39 +00004966void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004967 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4968 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4969 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004970}
4971
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004973 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4974 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4975 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4976 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004977}
4978
Jason Gerecke489fda82012-09-07 17:19:40 -07004979void TouchInputMapperTest::prepareLocationCalibration() {
4980 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4981}
4982
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983int32_t TouchInputMapperTest::toRawX(float displayX) {
4984 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4985}
4986
4987int32_t TouchInputMapperTest::toRawY(float displayY) {
4988 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4989}
4990
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004991int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4992 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4993}
4994
4995int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4996 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4997}
4998
Jason Gerecke489fda82012-09-07 17:19:40 -07004999float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5000 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5001 return rawX;
5002}
5003
5004float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5005 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5006 return rawY;
5007}
5008
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005010 return toDisplayX(rawX, DISPLAY_WIDTH);
5011}
5012
5013float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5014 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015}
5016
5017float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005018 return toDisplayY(rawY, DISPLAY_HEIGHT);
5019}
5020
5021float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5022 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023}
5024
5025
5026// --- SingleTouchInputMapperTest ---
5027
5028class SingleTouchInputMapperTest : public TouchInputMapperTest {
5029protected:
5030 void prepareButtons();
5031 void prepareAxes(int axes);
5032
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005033 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5034 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5035 void processUp(SingleTouchInputMapper& mappery);
5036 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5037 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5038 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5039 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5040 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5041 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042};
5043
5044void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005045 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046}
5047
5048void SingleTouchInputMapperTest::prepareAxes(int axes) {
5049 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005050 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5051 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052 }
5053 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005054 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5055 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005056 }
5057 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005058 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5059 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060 }
5061 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005062 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5063 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064 }
5065 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005066 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5067 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068 }
5069}
5070
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005071void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075}
5076
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005077void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080}
5081
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005082void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005084}
5085
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005086void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005087 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088}
5089
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005090void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5091 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005093}
5094
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005095void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005097}
5098
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005099void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5100 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5102 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005103}
5104
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005105void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5106 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108}
5109
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005110void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005112}
5113
Michael Wrightd02c5b62014-02-10 15:10:22 -08005114TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115 prepareButtons();
5116 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00005117 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118
Josep del Río2d8c79a2023-01-23 19:33:50 +00005119 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005120}
5121
Michael Wrightd02c5b62014-02-10 15:10:22 -08005122TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123 prepareButtons();
5124 prepareAxes(POSITION);
5125 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00005126 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005127
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005128 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129}
5130
5131TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005132 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005133 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134 prepareButtons();
5135 prepareAxes(POSITION);
5136 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005137 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138
5139 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005140 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005141
5142 // Virtual key is down.
5143 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5144 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5145 processDown(mapper, x, y);
5146 processSync(mapper);
5147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005149 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150
5151 // Virtual key is up.
5152 processUp(mapper);
5153 processSync(mapper);
5154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5155
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005156 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157}
5158
5159TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005160 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005161 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005162 prepareButtons();
5163 prepareAxes(POSITION);
5164 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005165 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166
5167 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005168 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169
5170 // Virtual key is down.
5171 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5172 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5173 processDown(mapper, x, y);
5174 processSync(mapper);
5175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5176
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005177 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178
5179 // Virtual key is up.
5180 processUp(mapper);
5181 processSync(mapper);
5182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5183
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005184 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185}
5186
5187TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005188 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005189 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005190 prepareButtons();
5191 prepareAxes(POSITION);
5192 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005193 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194
Michael Wrightd02c5b62014-02-10 15:10:22 -08005195 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005196 ASSERT_TRUE(
5197 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005198 ASSERT_TRUE(flags[0]);
5199 ASSERT_FALSE(flags[1]);
5200}
5201
5202TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005203 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005204 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205 prepareButtons();
5206 prepareAxes(POSITION);
5207 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005208 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209
arthurhungdcef2dc2020-08-11 14:47:50 +08005210 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005211
5212 NotifyKeyArgs args;
5213
5214 // Press virtual key.
5215 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5216 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5217 processDown(mapper, x, y);
5218 processSync(mapper);
5219
5220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5221 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5222 ASSERT_EQ(DEVICE_ID, args.deviceId);
5223 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5224 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5225 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5226 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5227 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5228 ASSERT_EQ(KEY_HOME, args.scanCode);
5229 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5230 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5231
5232 // Release virtual key.
5233 processUp(mapper);
5234 processSync(mapper);
5235
5236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5237 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5238 ASSERT_EQ(DEVICE_ID, args.deviceId);
5239 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5240 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5241 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5242 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5243 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5244 ASSERT_EQ(KEY_HOME, args.scanCode);
5245 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5246 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5247
5248 // Should not have sent any motions.
5249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5250}
5251
5252TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005253 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005254 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005255 prepareButtons();
5256 prepareAxes(POSITION);
5257 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005258 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005259
arthurhungdcef2dc2020-08-11 14:47:50 +08005260 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005261
5262 NotifyKeyArgs keyArgs;
5263
5264 // Press virtual key.
5265 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5266 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5267 processDown(mapper, x, y);
5268 processSync(mapper);
5269
5270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5271 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5272 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5273 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5274 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5275 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5276 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5277 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5278 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5280 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5281
5282 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5283 // into the display area.
5284 y -= 100;
5285 processMove(mapper, x, y);
5286 processSync(mapper);
5287
5288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5289 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5290 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5291 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5292 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5293 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5294 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5295 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5296 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5297 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5298 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5299 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5300
5301 NotifyMotionArgs motionArgs;
5302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5303 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5304 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5305 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5306 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5307 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5308 ASSERT_EQ(0, motionArgs.flags);
5309 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5310 ASSERT_EQ(0, motionArgs.buttonState);
5311 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005312 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005313 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005314 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5316 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5317 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5318 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5319 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5320
5321 // Keep moving out of bounds. Should generate a pointer move.
5322 y -= 50;
5323 processMove(mapper, x, y);
5324 processSync(mapper);
5325
5326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5327 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5328 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5329 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5330 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5332 ASSERT_EQ(0, motionArgs.flags);
5333 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5334 ASSERT_EQ(0, motionArgs.buttonState);
5335 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005336 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005338 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5340 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5341 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5342 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5343 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5344
5345 // Release out of bounds. Should generate a pointer up.
5346 processUp(mapper);
5347 processSync(mapper);
5348
5349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5351 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5352 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5353 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5354 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5355 ASSERT_EQ(0, motionArgs.flags);
5356 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5357 ASSERT_EQ(0, motionArgs.buttonState);
5358 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005359 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005361 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5363 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5364 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5365 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5366 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5367
5368 // Should not have sent any more keys or motions.
5369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5371}
5372
5373TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005375 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376 prepareButtons();
5377 prepareAxes(POSITION);
5378 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005379 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005380
arthurhungdcef2dc2020-08-11 14:47:50 +08005381 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382
5383 NotifyMotionArgs motionArgs;
5384
5385 // Initially go down out of bounds.
5386 int32_t x = -10;
5387 int32_t y = -10;
5388 processDown(mapper, x, y);
5389 processSync(mapper);
5390
5391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5392
5393 // Move into the display area. Should generate a pointer down.
5394 x = 50;
5395 y = 75;
5396 processMove(mapper, x, y);
5397 processSync(mapper);
5398
5399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5400 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5401 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5402 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5403 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5404 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5405 ASSERT_EQ(0, motionArgs.flags);
5406 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5407 ASSERT_EQ(0, motionArgs.buttonState);
5408 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005409 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005411 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5413 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5414 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5415 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5416 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5417
5418 // Release. Should generate a pointer up.
5419 processUp(mapper);
5420 processSync(mapper);
5421
5422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5423 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5424 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5425 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5426 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5427 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5428 ASSERT_EQ(0, motionArgs.flags);
5429 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5430 ASSERT_EQ(0, motionArgs.buttonState);
5431 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005432 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005433 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005434 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5436 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5437 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5438 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5439 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5440
5441 // Should not have sent any more keys or motions.
5442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5444}
5445
Santos Cordonfa5cf462017-04-05 10:37:00 -07005446TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005447 addConfigurationProperty("touch.deviceType", "touchScreen");
5448 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5449
Michael Wrighta9cf4192022-12-01 23:46:39 +00005450 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005451 prepareButtons();
5452 prepareAxes(POSITION);
5453 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005454 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005455
arthurhungdcef2dc2020-08-11 14:47:50 +08005456 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005457
5458 NotifyMotionArgs motionArgs;
5459
5460 // Down.
5461 int32_t x = 100;
5462 int32_t y = 125;
5463 processDown(mapper, x, y);
5464 processSync(mapper);
5465
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5467 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5468 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5469 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5470 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5471 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5472 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5473 ASSERT_EQ(0, motionArgs.flags);
5474 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5475 ASSERT_EQ(0, motionArgs.buttonState);
5476 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005477 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005478 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005479 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5481 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5482 1, 0, 0, 0, 0, 0, 0, 0));
5483 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5484 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5485 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5486
5487 // Move.
5488 x += 50;
5489 y += 75;
5490 processMove(mapper, x, y);
5491 processSync(mapper);
5492
5493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5494 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5495 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5496 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5497 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5498 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5500 ASSERT_EQ(0, motionArgs.flags);
5501 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5502 ASSERT_EQ(0, motionArgs.buttonState);
5503 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005504 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005505 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005506 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5508 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5509 1, 0, 0, 0, 0, 0, 0, 0));
5510 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5511 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5512 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5513
5514 // Up.
5515 processUp(mapper);
5516 processSync(mapper);
5517
5518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5519 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5520 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5521 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5522 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5523 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5524 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5525 ASSERT_EQ(0, motionArgs.flags);
5526 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5527 ASSERT_EQ(0, motionArgs.buttonState);
5528 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005529 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005530 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005531 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5533 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5534 1, 0, 0, 0, 0, 0, 0, 0));
5535 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5536 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5537 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5538
5539 // Should not have sent any more keys or motions.
5540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5542}
5543
Michael Wrightd02c5b62014-02-10 15:10:22 -08005544TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005545 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005546 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005547 prepareButtons();
5548 prepareAxes(POSITION);
5549 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005550 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005551
arthurhungdcef2dc2020-08-11 14:47:50 +08005552 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553
5554 NotifyMotionArgs motionArgs;
5555
5556 // Down.
5557 int32_t x = 100;
5558 int32_t y = 125;
5559 processDown(mapper, x, y);
5560 processSync(mapper);
5561
5562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5563 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5564 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5565 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5566 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5567 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5568 ASSERT_EQ(0, motionArgs.flags);
5569 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5570 ASSERT_EQ(0, motionArgs.buttonState);
5571 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005572 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005573 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005574 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5576 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5577 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5578 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5579 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5580
5581 // Move.
5582 x += 50;
5583 y += 75;
5584 processMove(mapper, x, y);
5585 processSync(mapper);
5586
5587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5588 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5589 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5590 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5591 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5593 ASSERT_EQ(0, motionArgs.flags);
5594 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5595 ASSERT_EQ(0, motionArgs.buttonState);
5596 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005597 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005598 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005599 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5601 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5602 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5603 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5604 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5605
5606 // Up.
5607 processUp(mapper);
5608 processSync(mapper);
5609
5610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5611 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5612 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5613 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5614 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5615 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5616 ASSERT_EQ(0, motionArgs.flags);
5617 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5618 ASSERT_EQ(0, motionArgs.buttonState);
5619 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005620 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005621 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005622 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5624 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5625 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5626 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5627 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5628
5629 // Should not have sent any more keys or motions.
5630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5632}
5633
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005634TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005635 addConfigurationProperty("touch.deviceType", "touchScreen");
5636 prepareButtons();
5637 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005638 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5639 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singh56adebc2023-04-25 13:56:05 +00005640 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005641
5642 NotifyMotionArgs args;
5643
5644 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005645 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005646 processDown(mapper, toRawX(50), toRawY(75));
5647 processSync(mapper);
5648
5649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5650 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5651 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5652
5653 processUp(mapper);
5654 processSync(mapper);
5655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5656}
5657
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005658TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005659 addConfigurationProperty("touch.deviceType", "touchScreen");
5660 prepareButtons();
5661 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005662 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5663 // orientation-aware are affected by display rotation.
5664 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00005665 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666
5667 NotifyMotionArgs args;
5668
5669 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005670 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005671 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005672 processDown(mapper, toRawX(50), toRawY(75));
5673 processSync(mapper);
5674
5675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5676 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5677 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5678
5679 processUp(mapper);
5680 processSync(mapper);
5681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5682
5683 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005684 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005685 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005686 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005687 processSync(mapper);
5688
5689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5690 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5691 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5692
5693 processUp(mapper);
5694 processSync(mapper);
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5696
5697 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005698 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005699 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005700 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5701 processSync(mapper);
5702
5703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5704 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5705 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5706
5707 processUp(mapper);
5708 processSync(mapper);
5709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5710
5711 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005712 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005713 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005714 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005715 processSync(mapper);
5716
5717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5718 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5719 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5720
5721 processUp(mapper);
5722 processSync(mapper);
5723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5724}
5725
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005726TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5727 addConfigurationProperty("touch.deviceType", "touchScreen");
5728 prepareButtons();
5729 prepareAxes(POSITION);
5730 addConfigurationProperty("touch.orientationAware", "1");
5731 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5732 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005733 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005734 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005735 NotifyMotionArgs args;
5736
5737 // Orientation 0.
5738 processDown(mapper, toRawX(50), toRawY(75));
5739 processSync(mapper);
5740
5741 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5742 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5743 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5744
5745 processUp(mapper);
5746 processSync(mapper);
5747 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5748}
5749
5750TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5751 addConfigurationProperty("touch.deviceType", "touchScreen");
5752 prepareButtons();
5753 prepareAxes(POSITION);
5754 addConfigurationProperty("touch.orientationAware", "1");
5755 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5756 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005757 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005758 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005759 NotifyMotionArgs args;
5760
5761 // Orientation 90.
5762 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5763 processSync(mapper);
5764
5765 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5766 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5767 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5768
5769 processUp(mapper);
5770 processSync(mapper);
5771 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5772}
5773
5774TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5775 addConfigurationProperty("touch.deviceType", "touchScreen");
5776 prepareButtons();
5777 prepareAxes(POSITION);
5778 addConfigurationProperty("touch.orientationAware", "1");
5779 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5780 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005781 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005782 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005783 NotifyMotionArgs args;
5784
5785 // Orientation 180.
5786 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5787 processSync(mapper);
5788
5789 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5790 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5791 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5792
5793 processUp(mapper);
5794 processSync(mapper);
5795 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5796}
5797
5798TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5799 addConfigurationProperty("touch.deviceType", "touchScreen");
5800 prepareButtons();
5801 prepareAxes(POSITION);
5802 addConfigurationProperty("touch.orientationAware", "1");
5803 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5804 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005805 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005806 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005807 NotifyMotionArgs args;
5808
5809 // Orientation 270.
5810 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5811 processSync(mapper);
5812
5813 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5814 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5815 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5816
5817 processUp(mapper);
5818 processSync(mapper);
5819 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5820}
5821
5822TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5823 addConfigurationProperty("touch.deviceType", "touchScreen");
5824 prepareButtons();
5825 prepareAxes(POSITION);
5826 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5827 // orientation-aware are affected by display rotation.
5828 addConfigurationProperty("touch.orientationAware", "0");
5829 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singh56adebc2023-04-25 13:56:05 +00005830 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005831
5832 NotifyMotionArgs args;
5833
5834 // Orientation 90, Rotation 0.
5835 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005836 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005837 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5838 processSync(mapper);
5839
5840 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5841 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5842 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5843
5844 processUp(mapper);
5845 processSync(mapper);
5846 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5847
5848 // Orientation 90, Rotation 90.
5849 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005850 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005851 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005852 processSync(mapper);
5853
5854 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5855 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5856 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5857
5858 processUp(mapper);
5859 processSync(mapper);
5860 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5861
5862 // Orientation 90, Rotation 180.
5863 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005864 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005865 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5866 processSync(mapper);
5867
5868 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5869 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5870 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5871
5872 processUp(mapper);
5873 processSync(mapper);
5874 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5875
5876 // Orientation 90, Rotation 270.
5877 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005878 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005879 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 -07005880 processSync(mapper);
5881
5882 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5883 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5884 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5885
5886 processUp(mapper);
5887 processSync(mapper);
5888 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5889}
5890
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005891TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5892 addConfigurationProperty("touch.deviceType", "touchScreen");
5893 prepareButtons();
5894 prepareAxes(POSITION);
5895 addConfigurationProperty("touch.orientationAware", "1");
5896 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005897 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005898
5899 // Set a physical frame in the display viewport.
5900 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5901 viewport->physicalLeft = 20;
5902 viewport->physicalTop = 600;
5903 viewport->physicalRight = 30;
5904 viewport->physicalBottom = 610;
5905 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005906 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005907
5908 // Start the touch.
5909 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5910 processSync(mapper);
5911
5912 // Expect all input starting outside the physical frame to be ignored.
5913 const std::array<Point, 6> outsidePoints = {
5914 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5915 for (const auto& p : outsidePoints) {
5916 processMove(mapper, toRawX(p.x), toRawY(p.y));
5917 processSync(mapper);
5918 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5919 }
5920
5921 // Move the touch into the physical frame.
5922 processMove(mapper, toRawX(25), toRawY(605));
5923 processSync(mapper);
5924 NotifyMotionArgs args;
5925 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5926 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5927 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5928 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5929
5930 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5931 for (const auto& p : outsidePoints) {
5932 processMove(mapper, toRawX(p.x), toRawY(p.y));
5933 processSync(mapper);
5934 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5935 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5936 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5937 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5938 }
5939
5940 processUp(mapper);
5941 processSync(mapper);
5942 EXPECT_NO_FATAL_FAILURE(
5943 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5944}
5945
Harry Cutts1db43992023-06-19 17:05:07 +00005946TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
5947 std::shared_ptr<FakePointerController> fakePointerController =
5948 std::make_shared<FakePointerController>();
5949 mFakePolicy->setPointerController(fakePointerController);
5950
5951 addConfigurationProperty("touch.deviceType", "pointer");
5952 prepareAxes(POSITION);
5953 prepareDisplay(ui::ROTATION_0);
5954 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5955
5956 // Set a physical frame in the display viewport.
5957 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5958 viewport->physicalLeft = 20;
5959 viewport->physicalTop = 600;
5960 viewport->physicalRight = 30;
5961 viewport->physicalBottom = 610;
5962 mFakePolicy->updateViewport(*viewport);
5963 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5964
5965 // Start the touch.
5966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5967 processSync(mapper);
5968
5969 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5970 // produced.
5971 const std::array<Point, 6> outsidePoints = {
5972 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5973 for (const auto& p : outsidePoints) {
5974 processMove(mapper, toRawX(p.x), toRawY(p.y));
5975 processSync(mapper);
5976 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5977 }
5978}
5979
Michael Wrightd02c5b62014-02-10 15:10:22 -08005980TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005981 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005982 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005983 prepareButtons();
5984 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singh56adebc2023-04-25 13:56:05 +00005985 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005986
5987 // These calculations are based on the input device calibration documentation.
5988 int32_t rawX = 100;
5989 int32_t rawY = 200;
5990 int32_t rawPressure = 10;
5991 int32_t rawToolMajor = 12;
5992 int32_t rawDistance = 2;
5993 int32_t rawTiltX = 30;
5994 int32_t rawTiltY = 110;
5995
5996 float x = toDisplayX(rawX);
5997 float y = toDisplayY(rawY);
5998 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5999 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6000 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6001 float distance = float(rawDistance);
6002
6003 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6004 float tiltScale = M_PI / 180;
6005 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6006 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6007 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6008 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6009
6010 processDown(mapper, rawX, rawY);
6011 processPressure(mapper, rawPressure);
6012 processToolMajor(mapper, rawToolMajor);
6013 processDistance(mapper, rawDistance);
6014 processTilt(mapper, rawTiltX, rawTiltY);
6015 processSync(mapper);
6016
6017 NotifyMotionArgs args;
6018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6020 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6021 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6022}
6023
Jason Gerecke489fda82012-09-07 17:19:40 -07006024TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006025 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006026 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006027 prepareLocationCalibration();
6028 prepareButtons();
6029 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006030 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006031
6032 int32_t rawX = 100;
6033 int32_t rawY = 200;
6034
6035 float x = toDisplayX(toCookedX(rawX, rawY));
6036 float y = toDisplayY(toCookedY(rawX, rawY));
6037
6038 processDown(mapper, rawX, rawY);
6039 processSync(mapper);
6040
6041 NotifyMotionArgs args;
6042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6044 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6045}
6046
Michael Wrightd02c5b62014-02-10 15:10:22 -08006047TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006048 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006049 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006050 prepareButtons();
6051 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006052 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006053
6054 NotifyMotionArgs motionArgs;
6055 NotifyKeyArgs keyArgs;
6056
6057 processDown(mapper, 100, 200);
6058 processSync(mapper);
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6060 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6061 ASSERT_EQ(0, motionArgs.buttonState);
6062
6063 // press BTN_LEFT, release BTN_LEFT
6064 processKey(mapper, BTN_LEFT, 1);
6065 processSync(mapper);
6066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6068 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6069
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6071 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6072 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6073
Michael Wrightd02c5b62014-02-10 15:10:22 -08006074 processKey(mapper, BTN_LEFT, 0);
6075 processSync(mapper);
6076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006077 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006078 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006079
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006082 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006083
6084 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6085 processKey(mapper, BTN_RIGHT, 1);
6086 processKey(mapper, BTN_MIDDLE, 1);
6087 processSync(mapper);
6088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6090 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6091 motionArgs.buttonState);
6092
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6094 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6095 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6096
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6099 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6100 motionArgs.buttonState);
6101
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102 processKey(mapper, BTN_RIGHT, 0);
6103 processSync(mapper);
6104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006105 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006106 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006107
6108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006110 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111
6112 processKey(mapper, BTN_MIDDLE, 0);
6113 processSync(mapper);
6114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006116 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006117
6118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006119 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006120 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121
6122 // press BTN_BACK, release BTN_BACK
6123 processKey(mapper, BTN_BACK, 1);
6124 processSync(mapper);
6125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6126 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6127 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006128
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006131 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6132
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6134 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6135 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136
6137 processKey(mapper, BTN_BACK, 0);
6138 processSync(mapper);
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006140 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006142
6143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006145 ASSERT_EQ(0, motionArgs.buttonState);
6146
Michael Wrightd02c5b62014-02-10 15:10:22 -08006147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6148 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6149 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6150
6151 // press BTN_SIDE, release BTN_SIDE
6152 processKey(mapper, BTN_SIDE, 1);
6153 processSync(mapper);
6154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6155 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6156 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006157
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006160 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6161
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6163 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6164 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165
6166 processKey(mapper, BTN_SIDE, 0);
6167 processSync(mapper);
6168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006169 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006170 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006171
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006174 ASSERT_EQ(0, motionArgs.buttonState);
6175
Michael Wrightd02c5b62014-02-10 15:10:22 -08006176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6177 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6178 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6179
6180 // press BTN_FORWARD, release BTN_FORWARD
6181 processKey(mapper, BTN_FORWARD, 1);
6182 processSync(mapper);
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6184 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6185 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006186
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006189 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6190
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6192 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6193 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194
6195 processKey(mapper, BTN_FORWARD, 0);
6196 processSync(mapper);
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006198 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006199 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006200
6201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006203 ASSERT_EQ(0, motionArgs.buttonState);
6204
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6206 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6207 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6208
6209 // press BTN_EXTRA, release BTN_EXTRA
6210 processKey(mapper, BTN_EXTRA, 1);
6211 processSync(mapper);
6212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6213 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6214 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006215
Michael Wrightd02c5b62014-02-10 15:10:22 -08006216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006218 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6219
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6221 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6222 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006223
6224 processKey(mapper, BTN_EXTRA, 0);
6225 processSync(mapper);
6226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006227 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006228 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006229
6230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006232 ASSERT_EQ(0, motionArgs.buttonState);
6233
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6235 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6236 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6237
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6239
Michael Wrightd02c5b62014-02-10 15:10:22 -08006240 // press BTN_STYLUS, release BTN_STYLUS
6241 processKey(mapper, BTN_STYLUS, 1);
6242 processSync(mapper);
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006245 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6246
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6248 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6249 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250
6251 processKey(mapper, BTN_STYLUS, 0);
6252 processSync(mapper);
6253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006254 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006255 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006256
6257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006259 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006260
6261 // press BTN_STYLUS2, release BTN_STYLUS2
6262 processKey(mapper, BTN_STYLUS2, 1);
6263 processSync(mapper);
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006266 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6267
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6269 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6270 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006271
6272 processKey(mapper, BTN_STYLUS2, 0);
6273 processSync(mapper);
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006275 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006276 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006280 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281
6282 // release touch
6283 processUp(mapper);
6284 processSync(mapper);
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6286 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6287 ASSERT_EQ(0, motionArgs.buttonState);
6288}
6289
6290TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006291 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006292 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006293 prepareButtons();
6294 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006295 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296
6297 NotifyMotionArgs motionArgs;
6298
6299 // default tool type is finger
6300 processDown(mapper, 100, 200);
6301 processSync(mapper);
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6303 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006304 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305
6306 // eraser
6307 processKey(mapper, BTN_TOOL_RUBBER, 1);
6308 processSync(mapper);
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006311 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006312
6313 // stylus
6314 processKey(mapper, BTN_TOOL_RUBBER, 0);
6315 processKey(mapper, BTN_TOOL_PEN, 1);
6316 processSync(mapper);
6317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006319 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320
6321 // brush
6322 processKey(mapper, BTN_TOOL_PEN, 0);
6323 processKey(mapper, BTN_TOOL_BRUSH, 1);
6324 processSync(mapper);
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006327 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006328
6329 // pencil
6330 processKey(mapper, BTN_TOOL_BRUSH, 0);
6331 processKey(mapper, BTN_TOOL_PENCIL, 1);
6332 processSync(mapper);
6333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006335 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006337 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338 processKey(mapper, BTN_TOOL_PENCIL, 0);
6339 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6340 processSync(mapper);
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006343 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344
6345 // mouse
6346 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6347 processKey(mapper, BTN_TOOL_MOUSE, 1);
6348 processSync(mapper);
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006351 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006352
6353 // lens
6354 processKey(mapper, BTN_TOOL_MOUSE, 0);
6355 processKey(mapper, BTN_TOOL_LENS, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360
6361 // double-tap
6362 processKey(mapper, BTN_TOOL_LENS, 0);
6363 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6364 processSync(mapper);
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006367 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006368
6369 // triple-tap
6370 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6371 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6372 processSync(mapper);
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006375 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006376
6377 // quad-tap
6378 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6379 processKey(mapper, BTN_TOOL_QUADTAP, 1);
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 // finger
6386 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6387 processKey(mapper, BTN_TOOL_FINGER, 1);
6388 processSync(mapper);
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006391 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006392
6393 // stylus trumps finger
6394 processKey(mapper, BTN_TOOL_PEN, 1);
6395 processSync(mapper);
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006398 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006399
6400 // eraser trumps stylus
6401 processKey(mapper, BTN_TOOL_RUBBER, 1);
6402 processSync(mapper);
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006405 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406
6407 // mouse trumps eraser
6408 processKey(mapper, BTN_TOOL_MOUSE, 1);
6409 processSync(mapper);
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006412 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006413
6414 // back to default tool type
6415 processKey(mapper, BTN_TOOL_MOUSE, 0);
6416 processKey(mapper, BTN_TOOL_RUBBER, 0);
6417 processKey(mapper, BTN_TOOL_PEN, 0);
6418 processKey(mapper, BTN_TOOL_FINGER, 0);
6419 processSync(mapper);
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6421 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006422 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006423}
6424
6425TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006426 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006427 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006428 prepareButtons();
6429 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006430 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +00006431 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006432
6433 NotifyMotionArgs motionArgs;
6434
6435 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6436 processKey(mapper, BTN_TOOL_FINGER, 1);
6437 processMove(mapper, 100, 200);
6438 processSync(mapper);
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(100), toDisplayY(200), 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(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6448
6449 // move a little
6450 processMove(mapper, 150, 250);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // down when BTN_TOUCH is pressed, pressure defaults to 1
6458 processKey(mapper, BTN_TOUCH, 1);
6459 processSync(mapper);
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6461 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6463 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6464
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6466 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6468 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6469
6470 // up when BTN_TOUCH is released, hover restored
6471 processKey(mapper, BTN_TOUCH, 0);
6472 processSync(mapper);
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6474 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6476 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6477
6478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6479 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6481 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6482
6483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6484 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6486 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6487
6488 // exit hover when pointer goes away
6489 processKey(mapper, BTN_TOOL_FINGER, 0);
6490 processSync(mapper);
6491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6492 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6494 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6495}
6496
6497TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006498 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006499 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006500 prepareButtons();
6501 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006502 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006503
6504 NotifyMotionArgs motionArgs;
6505
6506 // initially hovering because pressure is 0
6507 processDown(mapper, 100, 200);
6508 processPressure(mapper, 0);
6509 processSync(mapper);
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(100), toDisplayY(200), 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(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6519
6520 // move a little
6521 processMove(mapper, 150, 250);
6522 processSync(mapper);
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6524 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // down when pressure is non-zero
6529 processPressure(mapper, RAW_PRESSURE_MAX);
6530 processSync(mapper);
6531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6532 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6534 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6535
6536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6537 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6539 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6540
6541 // up when pressure becomes 0, hover restored
6542 processPressure(mapper, 0);
6543 processSync(mapper);
6544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6545 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6547 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6548
6549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6550 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6552 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6553
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6555 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6557 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6558
6559 // exit hover when pointer goes away
6560 processUp(mapper);
6561 processSync(mapper);
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6563 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6565 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6566}
6567
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006568TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6569 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006570 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006571 prepareButtons();
6572 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006573 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006574
6575 // Touch down.
6576 processDown(mapper, 100, 200);
6577 processPressure(mapper, 1);
6578 processSync(mapper);
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6580 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6581
6582 // Reset the mapper. This should cancel the ongoing gesture.
6583 resetMapper(mapper, ARBITRARY_TIME);
6584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6585 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6586
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6588}
6589
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006590TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6591 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006592 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006593 prepareButtons();
6594 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006595 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006596
6597 // Set the initial state for the touch pointer.
6598 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6599 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6600 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6601 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6602
6603 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006604 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6605 // does not generate any events.
6606 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006607
6608 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6609 // the recreated touch state to generate a down event.
6610 processSync(mapper);
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6612 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6613
6614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6615}
6616
lilinnan687e58f2022-07-19 16:00:50 +08006617TEST_F(SingleTouchInputMapperTest,
6618 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6619 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006620 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006621 prepareButtons();
6622 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006623 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006624 NotifyMotionArgs motionArgs;
6625
6626 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006627 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006628 processSync(mapper);
6629
6630 // We should receive a down event
6631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6632 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6633
6634 // Change display id
6635 clearViewports();
6636 prepareSecondaryDisplay(ViewportType::INTERNAL);
6637
6638 // We should receive a cancel event
6639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6640 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6641 // Then receive reset called
6642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6643}
6644
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006645TEST_F(SingleTouchInputMapperTest,
6646 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6647 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006648 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006649 prepareButtons();
6650 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006651 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6653 NotifyMotionArgs motionArgs;
6654
6655 // Start a new gesture.
6656 processDown(mapper, 100, 200);
6657 processSync(mapper);
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6659 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6660
6661 // Make the viewport inactive. This will put the device in disabled mode.
6662 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6663 viewport->isActive = false;
6664 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006665 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006666
6667 // We should receive a cancel event for the ongoing gesture.
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6669 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6670 // Then we should be notified that the device was reset.
6671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6672
6673 // No events are generated while the viewport is inactive.
6674 processMove(mapper, 101, 201);
6675 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006676 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006677 processSync(mapper);
6678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6679
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006680 // Start a new gesture while the viewport is still inactive.
6681 processDown(mapper, 300, 400);
6682 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6683 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6684 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6685 processSync(mapper);
6686
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006687 // Make the viewport active again. The device should resume processing events.
6688 viewport->isActive = true;
6689 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006690 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006691
6692 // The device is reset because it changes back to direct mode, without generating any events.
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6695
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006696 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006697 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6699 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006700
6701 // No more events.
6702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6704}
6705
Prabir Pradhan211ba622022-10-31 21:09:21 +00006706TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6707 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006708 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006709 prepareButtons();
6710 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006711 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6713
6714 // Press a stylus button.
6715 processKey(mapper, BTN_STYLUS, 1);
6716 processSync(mapper);
6717
6718 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6719 processDown(mapper, 100, 200);
6720 processSync(mapper);
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6722 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6723 WithCoords(toDisplayX(100), toDisplayY(200)),
6724 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6726 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6727 WithCoords(toDisplayX(100), toDisplayY(200)),
6728 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6729
6730 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6731 // the button has not actually been released, since there will be no pointers through which the
6732 // button state can be reported. The event is generated at the location of the pointer before
6733 // it went up.
6734 processUp(mapper);
6735 processSync(mapper);
6736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6737 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6738 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6740 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6741 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6742}
6743
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006744TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6745 addConfigurationProperty("touch.deviceType", "touchScreen");
6746 prepareDisplay(ui::ROTATION_0);
6747 prepareButtons();
6748 prepareAxes(POSITION);
6749
6750 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6751
Arpit Singh56adebc2023-04-25 13:56:05 +00006752 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6754
6755 // Press a stylus button.
6756 processKey(mapper, BTN_STYLUS, 1);
6757 processSync(mapper);
6758
6759 // Start a touch gesture and ensure that the stylus button is not reported.
6760 processDown(mapper, 100, 200);
6761 processSync(mapper);
6762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6763 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6764
6765 // Release and press the stylus button again.
6766 processKey(mapper, BTN_STYLUS, 0);
6767 processSync(mapper);
6768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6769 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6770 processKey(mapper, BTN_STYLUS, 1);
6771 processSync(mapper);
6772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6773 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6774
6775 // Release the touch gesture.
6776 processUp(mapper);
6777 processSync(mapper);
6778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6779 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6780
6781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6782}
6783
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006784TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6785 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6786 prepareDisplay(ui::ROTATION_0);
6787 prepareButtons();
6788 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006789 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6791
6792 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6793}
6794
Seunghwan Choi356026c2023-02-01 14:37:25 +09006795TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6796 std::shared_ptr<FakePointerController> fakePointerController =
6797 std::make_shared<FakePointerController>();
6798 addConfigurationProperty("touch.deviceType", "touchScreen");
6799 prepareDisplay(ui::ROTATION_0);
6800 prepareButtons();
6801 prepareAxes(POSITION);
6802 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6803 mFakePolicy->setPointerController(fakePointerController);
6804 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singh56adebc2023-04-25 13:56:05 +00006805 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006806
6807 processKey(mapper, BTN_TOOL_PEN, 1);
6808 processMove(mapper, 100, 200);
6809 processSync(mapper);
6810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6811 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006812 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006813 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6814 ASSERT_TRUE(fakePointerController->isPointerShown());
6815 ASSERT_NO_FATAL_FAILURE(
6816 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6817}
6818
6819TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6820 std::shared_ptr<FakePointerController> fakePointerController =
6821 std::make_shared<FakePointerController>();
6822 addConfigurationProperty("touch.deviceType", "touchScreen");
6823 prepareDisplay(ui::ROTATION_0);
6824 prepareButtons();
6825 prepareAxes(POSITION);
6826 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6827 mFakePolicy->setPointerController(fakePointerController);
6828 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singh56adebc2023-04-25 13:56:05 +00006829 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006830
6831 processKey(mapper, BTN_TOOL_PEN, 1);
6832 processMove(mapper, 100, 200);
6833 processSync(mapper);
6834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6835 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006836 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006837 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6838 ASSERT_FALSE(fakePointerController->isPointerShown());
6839}
6840
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006841TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6842 // Initialize the device without setting device source to touch navigation.
6843 addConfigurationProperty("touch.deviceType", "touchScreen");
6844 prepareDisplay(ui::ROTATION_0);
6845 prepareButtons();
6846 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006847 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006848
6849 // Ensure that the device is created as a touchscreen, not touch navigation.
6850 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6851
6852 // Add device type association after the device was created.
6853 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6854
6855 // Send update to the mapper.
6856 std::list<NotifyArgs> unused2 =
6857 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006858 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006859
6860 // Check whether device type update was successful.
6861 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6862}
6863
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006864TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6865 // Initialize the device without setting device source to touch navigation.
6866 addConfigurationProperty("touch.deviceType", "touchScreen");
6867 prepareDisplay(ui::ROTATION_0);
6868 prepareButtons();
6869 prepareAxes(POSITION);
6870 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6871
6872 // Set a physical frame in the display viewport.
6873 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6874 viewport->physicalLeft = 0;
6875 viewport->physicalTop = 0;
6876 viewport->physicalRight = DISPLAY_WIDTH / 2;
6877 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6878 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006879 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006880
Arpit Singh56adebc2023-04-25 13:56:05 +00006881 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006882
6883 // Hovering inside the physical frame produces events.
6884 processKey(mapper, BTN_TOOL_PEN, 1);
6885 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6886 processSync(mapper);
6887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6888 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6890 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6891
6892 // Leaving the physical frame ends the hovering gesture.
6893 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6894 processSync(mapper);
6895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6896 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6897
6898 // Moving outside the physical frame does not produce events.
6899 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6900 processSync(mapper);
6901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6902
6903 // Re-entering the physical frame produces events.
6904 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6905 processSync(mapper);
6906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6907 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6909 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6910}
6911
Prabir Pradhan5632d622021-09-06 07:57:20 -07006912// --- TouchDisplayProjectionTest ---
6913
6914class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6915public:
6916 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6917 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6918 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006919 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6920 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6921 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006922 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006923 auto rotatedWidth = naturalDisplayWidth;
6924 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006925 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006926 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006927 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006928 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006929 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006930 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006931 inverseRotationFlags = ui::Transform::ROT_180;
6932 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006933 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006934 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006935 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006936 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006937 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006938 inverseRotationFlags = ui::Transform::ROT_0;
6939 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006940 }
6941
Prabir Pradhana9df3162022-12-05 23:57:27 +00006942 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006943 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6944
6945 std::optional<DisplayViewport> internalViewport =
6946 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6947 DisplayViewport& v = *internalViewport;
6948 v.displayId = DISPLAY_ID;
6949 v.orientation = orientation;
6950
6951 v.logicalLeft = 0;
6952 v.logicalTop = 0;
6953 v.logicalRight = 100;
6954 v.logicalBottom = 100;
6955
6956 v.physicalLeft = rotatedPhysicalDisplay.left;
6957 v.physicalTop = rotatedPhysicalDisplay.top;
6958 v.physicalRight = rotatedPhysicalDisplay.right;
6959 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6960
Prabir Pradhana9df3162022-12-05 23:57:27 +00006961 v.deviceWidth = rotatedWidth;
6962 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006963
6964 v.isActive = true;
6965 v.uniqueId = UNIQUE_ID;
6966 v.type = ViewportType::INTERNAL;
6967 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006968 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006969 }
6970
6971 void assertReceivedMove(const Point& point) {
6972 NotifyMotionArgs motionArgs;
6973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006975 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6977 1, 0, 0, 0, 0, 0, 0, 0));
6978 }
6979};
6980
6981TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6982 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006983 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006984
6985 prepareButtons();
6986 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006987 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006988
6989 NotifyMotionArgs motionArgs;
6990
6991 // Configure the DisplayViewport such that the logical display maps to a subsection of
6992 // the display panel called the physical display. Here, the physical display is bounded by the
6993 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6994 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6995 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6996 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6997
Michael Wrighta9cf4192022-12-01 23:46:39 +00006998 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006999 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7000
7001 // Touches outside the physical display should be ignored, and should not generate any
7002 // events. Ensure touches at the following points that lie outside of the physical display
7003 // area do not generate any events.
7004 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7005 processDown(mapper, toRawX(point.x), toRawY(point.y));
7006 processSync(mapper);
7007 processUp(mapper);
7008 processSync(mapper);
7009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7010 << "Unexpected event generated for touch outside physical display at point: "
7011 << point.x << ", " << point.y;
7012 }
7013 }
7014}
7015
7016TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7017 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007018 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007019
7020 prepareButtons();
7021 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00007022 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007023
7024 NotifyMotionArgs motionArgs;
7025
7026 // Configure the DisplayViewport such that the logical display maps to a subsection of
7027 // the display panel called the physical display. Here, the physical display is bounded by the
7028 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7029 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7030
Michael Wrighta9cf4192022-12-01 23:46:39 +00007031 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007032 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7033
7034 // Touches that start outside the physical display should be ignored until it enters the
7035 // physical display bounds, at which point it should generate a down event. Start a touch at
7036 // the point (5, 100), which is outside the physical display bounds.
7037 static const Point kOutsidePoint{5, 100};
7038 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7039 processSync(mapper);
7040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7041
7042 // Move the touch into the physical display area. This should generate a pointer down.
7043 processMove(mapper, toRawX(11), toRawY(21));
7044 processSync(mapper);
7045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7046 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007047 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007048 ASSERT_NO_FATAL_FAILURE(
7049 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7050
7051 // Move the touch inside the physical display area. This should generate a pointer move.
7052 processMove(mapper, toRawX(69), toRawY(159));
7053 processSync(mapper);
7054 assertReceivedMove({69, 159});
7055
7056 // Move outside the physical display area. Since the pointer is already down, this should
7057 // now continue generating events.
7058 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7059 processSync(mapper);
7060 assertReceivedMove(kOutsidePoint);
7061
7062 // Release. This should generate a pointer up.
7063 processUp(mapper);
7064 processSync(mapper);
7065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7066 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7068 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7069
7070 // Ensure no more events were generated.
7071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7073 }
7074}
7075
Prabir Pradhana9df3162022-12-05 23:57:27 +00007076// --- TouchscreenPrecisionTests ---
7077
7078// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7079// in various orientations and with different display rotations. We configure the touchscreen to
7080// have a higher resolution than that of the display by an integer scale factor in each axis so that
7081// we can enforce that coordinates match precisely as expected.
7082class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7083 public ::testing::WithParamInterface<ui::Rotation> {
7084public:
7085 void SetUp() override {
7086 SingleTouchInputMapperTest::SetUp();
7087
7088 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7089 // four times the resolution of the display in the Y axis.
7090 prepareButtons();
7091 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007092 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7093 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007094 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007095 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7096 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007097 }
7098
7099 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7100 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7101 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7102 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7103
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007104 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7105 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7106
7107 static const int32_t PRECISION_RAW_X_FLAT = 16;
7108 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7109
7110 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7111 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7112
Prabir Pradhana9df3162022-12-05 23:57:27 +00007113 static const std::array<Point, 4> kRawCorners;
7114};
7115
7116const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7117 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7118 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7119 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7120 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7121}};
7122
7123// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7124// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7125// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7126TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7127 enum class Orientation {
7128 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7129 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7130 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7131 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7132 ftl_last = ORIENTATION_270,
7133 };
7134 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7135 Orientation::ORIENTATION_270;
7136 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7137 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7138 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7139 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7140 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7141 };
7142
7143 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7144
7145 // Configure the touchscreen as being installed in the one of the four different orientations
7146 // relative to the display.
7147 addConfigurationProperty("touch.deviceType", "touchScreen");
7148 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7149 prepareDisplay(ui::ROTATION_0);
7150
Arpit Singh56adebc2023-04-25 13:56:05 +00007151 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007152
7153 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7154 // orientations of either 90 or 270) this means the display's natural resolution will be
7155 // flipped.
7156 const bool displayRotated =
7157 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7158 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7159 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7160 const Rect physicalFrame{0, 0, width, height};
7161 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7162
7163 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7164 const float expectedPrecisionX = displayRotated ? 4 : 2;
7165 const float expectedPrecisionY = displayRotated ? 2 : 4;
7166
7167 // Test all four corners.
7168 for (int i = 0; i < 4; i++) {
7169 const auto& raw = kRawCorners[i];
7170 processDown(mapper, raw.x, raw.y);
7171 processSync(mapper);
7172 const auto& expected = expectedPoints[i];
7173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7174 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7175 WithCoords(expected.x, expected.y),
7176 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7177 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7178 << "with touchscreen orientation "
7179 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7180 << expected.x << ", " << expected.y << ").";
7181 processUp(mapper);
7182 processSync(mapper);
7183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7184 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7185 WithCoords(expected.x, expected.y))));
7186 }
7187}
7188
Prabir Pradhan82687402022-12-06 01:32:53 +00007189TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7190 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7191 kMappedCorners = {
7192 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7193 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7194 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7195 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7196 };
7197
7198 const ui::Rotation displayRotation = GetParam();
7199
7200 addConfigurationProperty("touch.deviceType", "touchScreen");
7201 prepareDisplay(displayRotation);
7202
Arpit Singh56adebc2023-04-25 13:56:05 +00007203 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007204
7205 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7206
7207 // Test all four corners.
7208 for (int i = 0; i < 4; i++) {
7209 const auto& expected = expectedPoints[i];
7210 const auto& raw = kRawCorners[i];
7211 processDown(mapper, raw.x, raw.y);
7212 processSync(mapper);
7213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7214 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7215 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7216 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7217 << "with display rotation " << ui::toCString(displayRotation)
7218 << ", expected point (" << expected.x << ", " << expected.y << ").";
7219 processUp(mapper);
7220 processSync(mapper);
7221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7222 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7223 WithCoords(expected.x, expected.y))));
7224 }
7225}
7226
Prabir Pradhan3e798762022-12-02 21:02:11 +00007227TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7228 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7229 kMappedCorners = {
7230 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7231 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7232 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7233 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7234 };
7235
7236 const ui::Rotation displayRotation = GetParam();
7237
7238 addConfigurationProperty("touch.deviceType", "touchScreen");
7239 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7240
Arpit Singh56adebc2023-04-25 13:56:05 +00007241 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007242
7243 // Ori 270, so width and height swapped
7244 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7245 prepareDisplay(displayRotation);
7246 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7247
7248 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7249
7250 // Test all four corners.
7251 for (int i = 0; i < 4; i++) {
7252 const auto& expected = expectedPoints[i];
7253 const auto& raw = kRawCorners[i];
7254 processDown(mapper, raw.x, raw.y);
7255 processSync(mapper);
7256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7257 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7258 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7259 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7260 << "with display rotation " << ui::toCString(displayRotation)
7261 << ", expected point (" << expected.x << ", " << expected.y << ").";
7262 processUp(mapper);
7263 processSync(mapper);
7264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7265 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7266 WithCoords(expected.x, expected.y))));
7267 }
7268}
7269
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007270TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7271 const ui::Rotation displayRotation = GetParam();
7272
7273 addConfigurationProperty("touch.deviceType", "touchScreen");
7274 prepareDisplay(displayRotation);
7275
7276 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singh56adebc2023-04-25 13:56:05 +00007277 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007278
7279 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7280 // MotionRanges use display pixels as their units
7281 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7282 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7283
7284 // The MotionRanges should be oriented in the rotated display's coordinate space
7285 const bool displayRotated =
7286 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7287
7288 constexpr float MAX_X = 479.5;
7289 constexpr float MAX_Y = 799.75;
7290 EXPECT_EQ(xRange->min, 0.f);
7291 EXPECT_EQ(yRange->min, 0.f);
7292 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7293 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7294
7295 EXPECT_EQ(xRange->flat, 8.f);
7296 EXPECT_EQ(yRange->flat, 8.f);
7297
7298 EXPECT_EQ(xRange->fuzz, 2.f);
7299 EXPECT_EQ(yRange->fuzz, 2.f);
7300
7301 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7302 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7303}
7304
Prabir Pradhana9df3162022-12-05 23:57:27 +00007305// Run the precision tests for all rotations.
7306INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7307 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7308 ui::ROTATION_270),
7309 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7310 return ftl::enum_string(testParamInfo.param);
7311 });
7312
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007313// --- ExternalStylusFusionTest ---
7314
7315class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7316public:
7317 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7318 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007319 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007320 prepareButtons();
7321 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00007322 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007323
7324 mStylusState.when = ARBITRARY_TIME;
7325 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007326 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007327 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007328 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007329 processExternalStylusState(mapper);
7330 return mapper;
7331 }
7332
7333 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7334 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7335 for (const NotifyArgs& args : generatedArgs) {
7336 mFakeListener->notify(args);
7337 }
7338 // Loop the reader to flush the input listener queue.
7339 mReader->loopOnce();
7340 return generatedArgs;
7341 }
7342
7343protected:
7344 StylusState mStylusState{};
7345 static constexpr uint32_t EXPECTED_SOURCE =
7346 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7347
7348 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7349 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007350 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007351
7352 // The first pointer is withheld.
7353 processDown(mapper, 100, 200);
7354 processSync(mapper);
7355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7356 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7357 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7358
7359 // The external stylus reports pressure. The withheld finger pointer is released as a
7360 // stylus.
7361 mStylusState.pressure = 1.f;
7362 processExternalStylusState(mapper);
7363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7364 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7365 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7366
7367 // Subsequent pointer events are not withheld.
7368 processMove(mapper, 101, 201);
7369 processSync(mapper);
7370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7371 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7372
7373 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7375 }
7376
7377 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7378 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7379
7380 // Releasing the touch pointer ends the gesture.
7381 processUp(mapper);
7382 processSync(mapper);
7383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7384 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007385 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007386
7387 mStylusState.pressure = 0.f;
7388 processExternalStylusState(mapper);
7389 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7391 }
7392
7393 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7394 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007395 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007396
7397 // The first pointer is withheld when an external stylus is connected,
7398 // and a timeout is requested.
7399 processDown(mapper, 100, 200);
7400 processSync(mapper);
7401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7402 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7403 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7404
7405 // If the timeout expires early, it is requested again.
7406 handleTimeout(mapper, ARBITRARY_TIME + 1);
7407 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7408 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7409
7410 // When the timeout expires, the withheld touch is released as a finger pointer.
7411 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7413 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7414
7415 // Subsequent pointer events are not withheld.
7416 processMove(mapper, 101, 201);
7417 processSync(mapper);
7418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7419 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7420 processUp(mapper);
7421 processSync(mapper);
7422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7423 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7424
7425 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7427 }
7428
7429private:
7430 InputDeviceInfo mExternalStylusDeviceInfo{};
7431};
7432
7433TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7434 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7435 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7436}
7437
7438TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7439 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7440 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7441}
7442
7443TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7444 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7445 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7446}
7447
7448// Test a successful stylus fusion gesture where the pressure is reported by the external
7449// before the touch is reported by the touchscreen.
7450TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7451 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7452 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007453 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007454
7455 // The external stylus reports pressure first. It is ignored for now.
7456 mStylusState.pressure = 1.f;
7457 processExternalStylusState(mapper);
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7459 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7460
7461 // When the touch goes down afterwards, it is reported as a stylus pointer.
7462 processDown(mapper, 100, 200);
7463 processSync(mapper);
7464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7465 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7466 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7467
7468 processMove(mapper, 101, 201);
7469 processSync(mapper);
7470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7471 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7472 processUp(mapper);
7473 processSync(mapper);
7474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7475 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7476
7477 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7479}
7480
7481TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7482 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7483
7484 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7485 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7486
7487 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7488 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7489 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7490 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7491}
7492
7493TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7494 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7495 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007496 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007497
7498 mStylusState.pressure = 0.8f;
7499 processExternalStylusState(mapper);
7500 processDown(mapper, 100, 200);
7501 processSync(mapper);
7502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7503 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7504 WithPressure(0.8f))));
7505 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7506
7507 // The external stylus reports a pressure change. We wait for some time for a touch event.
7508 mStylusState.pressure = 0.6f;
7509 processExternalStylusState(mapper);
7510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7511 ASSERT_NO_FATAL_FAILURE(
7512 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7513
7514 // If a touch is reported within the timeout, it reports the updated pressure.
7515 processMove(mapper, 101, 201);
7516 processSync(mapper);
7517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7518 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7519 WithPressure(0.6f))));
7520 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7521
7522 // There is another pressure change.
7523 mStylusState.pressure = 0.5f;
7524 processExternalStylusState(mapper);
7525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7526 ASSERT_NO_FATAL_FAILURE(
7527 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7528
7529 // If a touch is not reported within the timeout, a move event is generated to report
7530 // the new pressure.
7531 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7533 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7534 WithPressure(0.5f))));
7535
7536 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7537 // repeated indefinitely.
7538 mStylusState.pressure = 0.0f;
7539 processExternalStylusState(mapper);
7540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7541 ASSERT_NO_FATAL_FAILURE(
7542 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7543 processMove(mapper, 102, 202);
7544 processSync(mapper);
7545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7546 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7547 WithPressure(0.5f))));
7548 processMove(mapper, 103, 203);
7549 processSync(mapper);
7550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7551 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7552 WithPressure(0.5f))));
7553
7554 processUp(mapper);
7555 processSync(mapper);
7556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7557 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007558 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007559
7560 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7562}
7563
7564TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7565 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7566 auto source = WithSource(EXPECTED_SOURCE);
7567
7568 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007569 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007570 processExternalStylusState(mapper);
7571 processDown(mapper, 100, 200);
7572 processSync(mapper);
7573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7574 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007575 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007576 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7577
7578 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007579 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007580 processExternalStylusState(mapper);
7581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7582 ASSERT_NO_FATAL_FAILURE(
7583 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7584
7585 // If a touch is reported within the timeout, it reports the updated pressure.
7586 processMove(mapper, 101, 201);
7587 processSync(mapper);
7588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7589 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007590 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007591 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7592
7593 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007594 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007595 processExternalStylusState(mapper);
7596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7597 ASSERT_NO_FATAL_FAILURE(
7598 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7599
7600 // If a touch is not reported within the timeout, a move event is generated to report
7601 // the new tool type.
7602 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7604 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007605 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007606
7607 processUp(mapper);
7608 processSync(mapper);
7609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7610 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007611 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007612
7613 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7615}
7616
7617TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7618 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7619 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007620 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007621
7622 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7623
7624 // The external stylus reports a button change. We wait for some time for a touch event.
7625 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7626 processExternalStylusState(mapper);
7627 ASSERT_NO_FATAL_FAILURE(
7628 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7629
7630 // If a touch is reported within the timeout, it reports the updated button state.
7631 processMove(mapper, 101, 201);
7632 processSync(mapper);
7633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7634 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7635 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7637 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7638 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7639 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7640
7641 // The button is now released.
7642 mStylusState.buttons = 0;
7643 processExternalStylusState(mapper);
7644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7645 ASSERT_NO_FATAL_FAILURE(
7646 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7647
7648 // If a touch is not reported within the timeout, a move event is generated to report
7649 // the new button state.
7650 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7652 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7653 WithButtonState(0))));
7654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007655 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7656 WithButtonState(0))));
7657
7658 processUp(mapper);
7659 processSync(mapper);
7660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007661 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7662
7663 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7665}
7666
Michael Wrightd02c5b62014-02-10 15:10:22 -08007667// --- MultiTouchInputMapperTest ---
7668
7669class MultiTouchInputMapperTest : public TouchInputMapperTest {
7670protected:
7671 void prepareAxes(int axes);
7672
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007673 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7674 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7675 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7676 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7677 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7678 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7679 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7680 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7681 void processId(MultiTouchInputMapper& mapper, int32_t id);
7682 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7683 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7684 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007685 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007686 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007687 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7688 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007689};
7690
7691void MultiTouchInputMapperTest::prepareAxes(int axes) {
7692 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007693 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7694 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007695 }
7696 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007697 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7698 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007699 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007700 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7701 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007702 }
7703 }
7704 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007705 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7706 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007707 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007708 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007709 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007710 }
7711 }
7712 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007713 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7714 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007715 }
7716 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007717 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7718 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007719 }
7720 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007721 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7722 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007723 }
7724 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007725 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7726 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007727 }
7728 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007729 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7730 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007731 }
7732 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007733 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007734 }
7735}
7736
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007737void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7738 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007741}
7742
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007743void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7744 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007746}
7747
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007748void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7749 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007750 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007751}
7752
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007753void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007755}
7756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007757void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007759}
7760
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007761void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7762 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007764}
7765
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007766void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007768}
7769
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007770void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007772}
7773
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007774void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007776}
7777
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007778void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007780}
7781
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007782void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007784}
7785
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007786void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7787 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007789}
7790
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007791void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7792 int32_t value) {
7793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7795}
7796
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007797void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007799}
7800
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007801void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7802 nsecs_t readTime) {
7803 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007804}
7805
Michael Wrightd02c5b62014-02-10 15:10:22 -08007806TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007807 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007808 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007809 prepareAxes(POSITION);
7810 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00007811 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007812
arthurhungdcef2dc2020-08-11 14:47:50 +08007813 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007814
7815 NotifyMotionArgs motionArgs;
7816
7817 // Two fingers down at once.
7818 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7819 processPosition(mapper, x1, y1);
7820 processMTSync(mapper);
7821 processPosition(mapper, x2, y2);
7822 processMTSync(mapper);
7823 processSync(mapper);
7824
7825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7826 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7827 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7828 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7829 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7830 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7831 ASSERT_EQ(0, motionArgs.flags);
7832 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7833 ASSERT_EQ(0, motionArgs.buttonState);
7834 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007835 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007836 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007837 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7839 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7840 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7841 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7842 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7843
7844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7845 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7846 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7847 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7848 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007849 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007850 ASSERT_EQ(0, motionArgs.flags);
7851 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7852 ASSERT_EQ(0, motionArgs.buttonState);
7853 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007854 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007855 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007856 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007857 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007858 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7860 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7862 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7863 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7864 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7865 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7866
7867 // Move.
7868 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7869 processPosition(mapper, x1, y1);
7870 processMTSync(mapper);
7871 processPosition(mapper, x2, y2);
7872 processMTSync(mapper);
7873 processSync(mapper);
7874
7875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7876 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7877 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7878 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7879 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7881 ASSERT_EQ(0, motionArgs.flags);
7882 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7883 ASSERT_EQ(0, motionArgs.buttonState);
7884 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007885 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007886 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007887 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007888 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007889 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7891 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7893 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7894 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7895 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7896 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7897
7898 // First finger up.
7899 x2 += 15; y2 -= 20;
7900 processPosition(mapper, x2, y2);
7901 processMTSync(mapper);
7902 processSync(mapper);
7903
7904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7905 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7906 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7907 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7908 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007909 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007910 ASSERT_EQ(0, motionArgs.flags);
7911 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7912 ASSERT_EQ(0, motionArgs.buttonState);
7913 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007914 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007915 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007916 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007917 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007918 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7920 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7922 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7923 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7924 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7925 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7926
7927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7928 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7929 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7930 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7931 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7933 ASSERT_EQ(0, motionArgs.flags);
7934 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7935 ASSERT_EQ(0, motionArgs.buttonState);
7936 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007937 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007938 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007939 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7941 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7942 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7943 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7944 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7945
7946 // Move.
7947 x2 += 20; y2 -= 25;
7948 processPosition(mapper, x2, y2);
7949 processMTSync(mapper);
7950 processSync(mapper);
7951
7952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7953 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7954 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7955 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7956 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7958 ASSERT_EQ(0, motionArgs.flags);
7959 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7960 ASSERT_EQ(0, motionArgs.buttonState);
7961 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007962 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007963 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007964 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7966 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7967 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7968 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7969 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7970
7971 // New finger down.
7972 int32_t x3 = 700, y3 = 300;
7973 processPosition(mapper, x2, y2);
7974 processMTSync(mapper);
7975 processPosition(mapper, x3, y3);
7976 processMTSync(mapper);
7977 processSync(mapper);
7978
7979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7980 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7981 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7982 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7983 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007984 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007985 ASSERT_EQ(0, motionArgs.flags);
7986 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7987 ASSERT_EQ(0, motionArgs.buttonState);
7988 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007989 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007990 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007991 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007992 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007993 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7995 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7997 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7998 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7999 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8000 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8001
8002 // Second finger up.
8003 x3 += 30; y3 -= 20;
8004 processPosition(mapper, x3, y3);
8005 processMTSync(mapper);
8006 processSync(mapper);
8007
8008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8009 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8010 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8011 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8012 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008013 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008014 ASSERT_EQ(0, motionArgs.flags);
8015 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8016 ASSERT_EQ(0, motionArgs.buttonState);
8017 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008018 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008019 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008020 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008021 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008022 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8024 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8026 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8027 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8028 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8029 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8030
8031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8032 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8033 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8034 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8035 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8037 ASSERT_EQ(0, motionArgs.flags);
8038 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8039 ASSERT_EQ(0, motionArgs.buttonState);
8040 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008041 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008043 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8045 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8046 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8047 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8048 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8049
8050 // Last finger up.
8051 processMTSync(mapper);
8052 processSync(mapper);
8053
8054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8055 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8056 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8057 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8058 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8059 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8060 ASSERT_EQ(0, motionArgs.flags);
8061 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8062 ASSERT_EQ(0, motionArgs.buttonState);
8063 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008064 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008065 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008066 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8068 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8069 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8070 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8071 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8072
8073 // Should not have sent any more keys or motions.
8074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8076}
8077
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008078TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8079 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008080 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008081
8082 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8083 /*fuzz*/ 0, /*resolution*/ 10);
8084 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8085 /*fuzz*/ 0, /*resolution*/ 11);
8086 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8087 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8088 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8089 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8090 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8091 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8092 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8093 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8094
Arpit Singh56adebc2023-04-25 13:56:05 +00008095 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008096
8097 // X and Y axes
8098 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8099 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8100 // Touch major and minor
8101 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8102 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8103 // Tool major and minor
8104 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8105 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8106}
8107
8108TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8109 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008110 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008111
8112 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8113 /*fuzz*/ 0, /*resolution*/ 10);
8114 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8115 /*fuzz*/ 0, /*resolution*/ 11);
8116
8117 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8118
Arpit Singh56adebc2023-04-25 13:56:05 +00008119 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008120
8121 // Touch major and minor
8122 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8123 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8124 // Tool major and minor
8125 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8126 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8127}
8128
Michael Wrightd02c5b62014-02-10 15:10:22 -08008129TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008130 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008131 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008132 prepareAxes(POSITION | ID);
8133 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00008134 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008135
arthurhungdcef2dc2020-08-11 14:47:50 +08008136 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008137
8138 NotifyMotionArgs motionArgs;
8139
8140 // Two fingers down at once.
8141 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8142 processPosition(mapper, x1, y1);
8143 processId(mapper, 1);
8144 processMTSync(mapper);
8145 processPosition(mapper, x2, y2);
8146 processId(mapper, 2);
8147 processMTSync(mapper);
8148 processSync(mapper);
8149
8150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8151 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008152 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008153 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008154 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8156 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8157
8158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008159 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008160 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008162 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008164 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008165 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8166 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8168 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8169
8170 // Move.
8171 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8172 processPosition(mapper, x1, y1);
8173 processId(mapper, 1);
8174 processMTSync(mapper);
8175 processPosition(mapper, x2, y2);
8176 processId(mapper, 2);
8177 processMTSync(mapper);
8178 processSync(mapper);
8179
8180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008182 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008183 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008184 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008185 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008186 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8188 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8190 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8191
8192 // First finger up.
8193 x2 += 15; y2 -= 20;
8194 processPosition(mapper, x2, y2);
8195 processId(mapper, 2);
8196 processMTSync(mapper);
8197 processSync(mapper);
8198
8199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008200 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008201 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008202 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008203 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008204 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008205 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8207 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8209 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8210
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008213 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008215 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8217 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8218
8219 // Move.
8220 x2 += 20; y2 -= 25;
8221 processPosition(mapper, x2, y2);
8222 processId(mapper, 2);
8223 processMTSync(mapper);
8224 processSync(mapper);
8225
8226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008228 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008229 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008230 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8232 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8233
8234 // New finger down.
8235 int32_t x3 = 700, y3 = 300;
8236 processPosition(mapper, x2, y2);
8237 processId(mapper, 2);
8238 processMTSync(mapper);
8239 processPosition(mapper, x3, y3);
8240 processId(mapper, 3);
8241 processMTSync(mapper);
8242 processSync(mapper);
8243
8244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008245 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008246 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008247 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008248 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008250 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8252 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8254 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8255
8256 // Second finger up.
8257 x3 += 30; y3 -= 20;
8258 processPosition(mapper, x3, y3);
8259 processId(mapper, 3);
8260 processMTSync(mapper);
8261 processSync(mapper);
8262
8263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008264 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008265 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008266 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008267 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008268 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008269 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8271 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8273 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8274
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008277 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008278 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008279 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8281 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8282
8283 // Last finger up.
8284 processMTSync(mapper);
8285 processSync(mapper);
8286
8287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8288 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008289 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008290 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008291 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8293 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8294
8295 // Should not have sent any more keys or motions.
8296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8298}
8299
8300TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008301 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008302 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008303 prepareAxes(POSITION | ID | SLOT);
8304 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00008305 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008306
arthurhungdcef2dc2020-08-11 14:47:50 +08008307 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008308
8309 NotifyMotionArgs motionArgs;
8310
8311 // Two fingers down at once.
8312 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8313 processPosition(mapper, x1, y1);
8314 processId(mapper, 1);
8315 processSlot(mapper, 1);
8316 processPosition(mapper, x2, y2);
8317 processId(mapper, 2);
8318 processSync(mapper);
8319
8320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8321 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008322 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008323 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008324 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8326 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8327
8328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008329 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008330 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008331 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008332 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008333 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008334 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8336 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8338 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8339
8340 // Move.
8341 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8342 processSlot(mapper, 0);
8343 processPosition(mapper, x1, y1);
8344 processSlot(mapper, 1);
8345 processPosition(mapper, x2, y2);
8346 processSync(mapper);
8347
8348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008350 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008351 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008352 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008353 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008354 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8356 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8358 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8359
8360 // First finger up.
8361 x2 += 15; y2 -= 20;
8362 processSlot(mapper, 0);
8363 processId(mapper, -1);
8364 processSlot(mapper, 1);
8365 processPosition(mapper, x2, y2);
8366 processSync(mapper);
8367
8368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008369 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008370 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008371 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008372 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008373 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008374 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8376 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8378 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8379
8380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008382 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008383 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008384 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8386 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8387
8388 // Move.
8389 x2 += 20; y2 -= 25;
8390 processPosition(mapper, x2, y2);
8391 processSync(mapper);
8392
8393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008395 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008396 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008397 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8399 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8400
8401 // New finger down.
8402 int32_t x3 = 700, y3 = 300;
8403 processPosition(mapper, x2, y2);
8404 processSlot(mapper, 0);
8405 processId(mapper, 3);
8406 processPosition(mapper, x3, y3);
8407 processSync(mapper);
8408
8409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008410 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008411 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008412 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008413 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008414 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008415 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8417 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8419 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8420
8421 // Second finger up.
8422 x3 += 30; y3 -= 20;
8423 processSlot(mapper, 1);
8424 processId(mapper, -1);
8425 processSlot(mapper, 0);
8426 processPosition(mapper, x3, y3);
8427 processSync(mapper);
8428
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008430 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008431 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008432 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008433 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008434 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008435 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8437 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8439 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8440
8441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008443 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008444 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008445 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8447 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8448
8449 // Last finger up.
8450 processId(mapper, -1);
8451 processSync(mapper);
8452
8453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8454 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008455 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008456 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008457 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8459 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8460
8461 // Should not have sent any more keys or motions.
8462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8464}
8465
8466TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008467 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008468 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008469 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singh56adebc2023-04-25 13:56:05 +00008470 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008471
8472 // These calculations are based on the input device calibration documentation.
8473 int32_t rawX = 100;
8474 int32_t rawY = 200;
8475 int32_t rawTouchMajor = 7;
8476 int32_t rawTouchMinor = 6;
8477 int32_t rawToolMajor = 9;
8478 int32_t rawToolMinor = 8;
8479 int32_t rawPressure = 11;
8480 int32_t rawDistance = 0;
8481 int32_t rawOrientation = 3;
8482 int32_t id = 5;
8483
8484 float x = toDisplayX(rawX);
8485 float y = toDisplayY(rawY);
8486 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8487 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8488 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8489 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8490 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8491 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8492 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8493 float distance = float(rawDistance);
8494
8495 processPosition(mapper, rawX, rawY);
8496 processTouchMajor(mapper, rawTouchMajor);
8497 processTouchMinor(mapper, rawTouchMinor);
8498 processToolMajor(mapper, rawToolMajor);
8499 processToolMinor(mapper, rawToolMinor);
8500 processPressure(mapper, rawPressure);
8501 processOrientation(mapper, rawOrientation);
8502 processDistance(mapper, rawDistance);
8503 processId(mapper, id);
8504 processMTSync(mapper);
8505 processSync(mapper);
8506
8507 NotifyMotionArgs args;
8508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8509 ASSERT_EQ(0, args.pointerProperties[0].id);
8510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8511 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8512 orientation, distance));
8513}
8514
8515TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008516 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008517 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008518 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8519 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singh56adebc2023-04-25 13:56:05 +00008520 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008521
8522 // These calculations are based on the input device calibration documentation.
8523 int32_t rawX = 100;
8524 int32_t rawY = 200;
8525 int32_t rawTouchMajor = 140;
8526 int32_t rawTouchMinor = 120;
8527 int32_t rawToolMajor = 180;
8528 int32_t rawToolMinor = 160;
8529
8530 float x = toDisplayX(rawX);
8531 float y = toDisplayY(rawY);
8532 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8533 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8534 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8535 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8536 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8537
8538 processPosition(mapper, rawX, rawY);
8539 processTouchMajor(mapper, rawTouchMajor);
8540 processTouchMinor(mapper, rawTouchMinor);
8541 processToolMajor(mapper, rawToolMajor);
8542 processToolMinor(mapper, rawToolMinor);
8543 processMTSync(mapper);
8544 processSync(mapper);
8545
8546 NotifyMotionArgs args;
8547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8549 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8550}
8551
8552TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008553 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008554 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008555 prepareAxes(POSITION | TOUCH | TOOL);
8556 addConfigurationProperty("touch.size.calibration", "diameter");
8557 addConfigurationProperty("touch.size.scale", "10");
8558 addConfigurationProperty("touch.size.bias", "160");
8559 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singh56adebc2023-04-25 13:56:05 +00008560 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008561
8562 // These calculations are based on the input device calibration documentation.
8563 // Note: We only provide a single common touch/tool value because the device is assumed
8564 // not to emit separate values for each pointer (isSummed = 1).
8565 int32_t rawX = 100;
8566 int32_t rawY = 200;
8567 int32_t rawX2 = 150;
8568 int32_t rawY2 = 250;
8569 int32_t rawTouchMajor = 5;
8570 int32_t rawToolMajor = 8;
8571
8572 float x = toDisplayX(rawX);
8573 float y = toDisplayY(rawY);
8574 float x2 = toDisplayX(rawX2);
8575 float y2 = toDisplayY(rawY2);
8576 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8577 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8578 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8579
8580 processPosition(mapper, rawX, rawY);
8581 processTouchMajor(mapper, rawTouchMajor);
8582 processToolMajor(mapper, rawToolMajor);
8583 processMTSync(mapper);
8584 processPosition(mapper, rawX2, rawY2);
8585 processTouchMajor(mapper, rawTouchMajor);
8586 processToolMajor(mapper, rawToolMajor);
8587 processMTSync(mapper);
8588 processSync(mapper);
8589
8590 NotifyMotionArgs args;
8591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8592 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8593
8594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008595 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008596 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8598 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8600 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8601}
8602
8603TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008604 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008605 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008606 prepareAxes(POSITION | TOUCH | TOOL);
8607 addConfigurationProperty("touch.size.calibration", "area");
8608 addConfigurationProperty("touch.size.scale", "43");
8609 addConfigurationProperty("touch.size.bias", "3");
Arpit Singh56adebc2023-04-25 13:56:05 +00008610 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008611
8612 // These calculations are based on the input device calibration documentation.
8613 int32_t rawX = 100;
8614 int32_t rawY = 200;
8615 int32_t rawTouchMajor = 5;
8616 int32_t rawToolMajor = 8;
8617
8618 float x = toDisplayX(rawX);
8619 float y = toDisplayY(rawY);
8620 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8621 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8622 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8623
8624 processPosition(mapper, rawX, rawY);
8625 processTouchMajor(mapper, rawTouchMajor);
8626 processToolMajor(mapper, rawToolMajor);
8627 processMTSync(mapper);
8628 processSync(mapper);
8629
8630 NotifyMotionArgs args;
8631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8633 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8634}
8635
8636TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008637 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008638 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008639 prepareAxes(POSITION | PRESSURE);
8640 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8641 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singh56adebc2023-04-25 13:56:05 +00008642 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008643
Michael Wrightaa449c92017-12-13 21:21:43 +00008644 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008645 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008646 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8647 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8648 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8649
Michael Wrightd02c5b62014-02-10 15:10:22 -08008650 // These calculations are based on the input device calibration documentation.
8651 int32_t rawX = 100;
8652 int32_t rawY = 200;
8653 int32_t rawPressure = 60;
8654
8655 float x = toDisplayX(rawX);
8656 float y = toDisplayY(rawY);
8657 float pressure = float(rawPressure) * 0.01f;
8658
8659 processPosition(mapper, rawX, rawY);
8660 processPressure(mapper, rawPressure);
8661 processMTSync(mapper);
8662 processSync(mapper);
8663
8664 NotifyMotionArgs args;
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8667 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8668}
8669
8670TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008671 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008672 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008673 prepareAxes(POSITION | ID | SLOT);
Arpit Singh56adebc2023-04-25 13:56:05 +00008674 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008675
8676 NotifyMotionArgs motionArgs;
8677 NotifyKeyArgs keyArgs;
8678
8679 processId(mapper, 1);
8680 processPosition(mapper, 100, 200);
8681 processSync(mapper);
8682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8683 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8684 ASSERT_EQ(0, motionArgs.buttonState);
8685
8686 // press BTN_LEFT, release BTN_LEFT
8687 processKey(mapper, BTN_LEFT, 1);
8688 processSync(mapper);
8689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8691 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8692
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8694 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8695 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8696
Michael Wrightd02c5b62014-02-10 15:10:22 -08008697 processKey(mapper, BTN_LEFT, 0);
8698 processSync(mapper);
8699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008700 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008701 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008702
8703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008704 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008705 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008706
8707 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8708 processKey(mapper, BTN_RIGHT, 1);
8709 processKey(mapper, BTN_MIDDLE, 1);
8710 processSync(mapper);
8711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8713 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8714 motionArgs.buttonState);
8715
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8717 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8718 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8719
8720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8721 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8722 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8723 motionArgs.buttonState);
8724
Michael Wrightd02c5b62014-02-10 15:10:22 -08008725 processKey(mapper, BTN_RIGHT, 0);
8726 processSync(mapper);
8727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008728 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008729 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008730
8731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008732 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008733 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008734
8735 processKey(mapper, BTN_MIDDLE, 0);
8736 processSync(mapper);
8737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008738 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008739 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008740
8741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008743 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008744
8745 // press BTN_BACK, release BTN_BACK
8746 processKey(mapper, BTN_BACK, 1);
8747 processSync(mapper);
8748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8749 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8750 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008751
Michael Wrightd02c5b62014-02-10 15:10:22 -08008752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008753 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008754 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8755
8756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8757 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8758 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008759
8760 processKey(mapper, BTN_BACK, 0);
8761 processSync(mapper);
8762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008763 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008764 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008765
8766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008768 ASSERT_EQ(0, motionArgs.buttonState);
8769
Michael Wrightd02c5b62014-02-10 15:10:22 -08008770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8771 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8772 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8773
8774 // press BTN_SIDE, release BTN_SIDE
8775 processKey(mapper, BTN_SIDE, 1);
8776 processSync(mapper);
8777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8778 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8779 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008780
Michael Wrightd02c5b62014-02-10 15:10:22 -08008781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008782 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008783 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8784
8785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8786 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8787 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008788
8789 processKey(mapper, BTN_SIDE, 0);
8790 processSync(mapper);
8791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008792 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008793 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008794
8795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008797 ASSERT_EQ(0, motionArgs.buttonState);
8798
Michael Wrightd02c5b62014-02-10 15:10:22 -08008799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8800 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8801 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8802
8803 // press BTN_FORWARD, release BTN_FORWARD
8804 processKey(mapper, BTN_FORWARD, 1);
8805 processSync(mapper);
8806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8807 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8808 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008809
Michael Wrightd02c5b62014-02-10 15:10:22 -08008810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008812 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8813
8814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8815 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8816 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008817
8818 processKey(mapper, BTN_FORWARD, 0);
8819 processSync(mapper);
8820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008821 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008822 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008823
8824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008826 ASSERT_EQ(0, motionArgs.buttonState);
8827
Michael Wrightd02c5b62014-02-10 15:10:22 -08008828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8829 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8830 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8831
8832 // press BTN_EXTRA, release BTN_EXTRA
8833 processKey(mapper, BTN_EXTRA, 1);
8834 processSync(mapper);
8835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8836 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8837 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008838
Michael Wrightd02c5b62014-02-10 15:10:22 -08008839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008841 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8842
8843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8844 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8845 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008846
8847 processKey(mapper, BTN_EXTRA, 0);
8848 processSync(mapper);
8849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008850 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008851 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008852
8853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008855 ASSERT_EQ(0, motionArgs.buttonState);
8856
Michael Wrightd02c5b62014-02-10 15:10:22 -08008857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8858 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8859 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8860
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8862
Michael Wrightd02c5b62014-02-10 15:10:22 -08008863 // press BTN_STYLUS, release BTN_STYLUS
8864 processKey(mapper, BTN_STYLUS, 1);
8865 processSync(mapper);
8866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8867 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008868 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8869
8870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8871 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8872 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008873
8874 processKey(mapper, BTN_STYLUS, 0);
8875 processSync(mapper);
8876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008877 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008878 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008879
8880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008882 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008883
8884 // press BTN_STYLUS2, release BTN_STYLUS2
8885 processKey(mapper, BTN_STYLUS2, 1);
8886 processSync(mapper);
8887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008889 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8890
8891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8892 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8893 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008894
8895 processKey(mapper, BTN_STYLUS2, 0);
8896 processSync(mapper);
8897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008898 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008899 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008900
8901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008903 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008904
8905 // release touch
8906 processId(mapper, -1);
8907 processSync(mapper);
8908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8909 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8910 ASSERT_EQ(0, motionArgs.buttonState);
8911}
8912
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008913TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8914 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008915 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008916 prepareAxes(POSITION | ID | SLOT);
Arpit Singh56adebc2023-04-25 13:56:05 +00008917 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008918
8919 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8920 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8921
8922 // Touch down.
8923 processId(mapper, 1);
8924 processPosition(mapper, 100, 200);
8925 processSync(mapper);
8926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8927 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8928
8929 // Press and release button mapped to the primary stylus button.
8930 processKey(mapper, BTN_A, 1);
8931 processSync(mapper);
8932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8933 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8934 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8936 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8937 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8938
8939 processKey(mapper, BTN_A, 0);
8940 processSync(mapper);
8941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8942 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8944 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8945
8946 // Press and release the HID usage mapped to the secondary stylus button.
8947 processHidUsage(mapper, 0xabcd, 1);
8948 processSync(mapper);
8949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8950 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8951 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8953 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8954 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8955
8956 processHidUsage(mapper, 0xabcd, 0);
8957 processSync(mapper);
8958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8959 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8961 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8962
8963 // Release touch.
8964 processId(mapper, -1);
8965 processSync(mapper);
8966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8967 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8968}
8969
Michael Wrightd02c5b62014-02-10 15:10:22 -08008970TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008971 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008972 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008973 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00008974 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008975
8976 NotifyMotionArgs motionArgs;
8977
8978 // default tool type is finger
8979 processId(mapper, 1);
8980 processPosition(mapper, 100, 200);
8981 processSync(mapper);
8982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8983 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008984 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008985
8986 // eraser
8987 processKey(mapper, BTN_TOOL_RUBBER, 1);
8988 processSync(mapper);
8989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008991 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008992
8993 // stylus
8994 processKey(mapper, BTN_TOOL_RUBBER, 0);
8995 processKey(mapper, BTN_TOOL_PEN, 1);
8996 processSync(mapper);
8997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8998 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008999 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009000
9001 // brush
9002 processKey(mapper, BTN_TOOL_PEN, 0);
9003 processKey(mapper, BTN_TOOL_BRUSH, 1);
9004 processSync(mapper);
9005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009007 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009008
9009 // pencil
9010 processKey(mapper, BTN_TOOL_BRUSH, 0);
9011 processKey(mapper, BTN_TOOL_PENCIL, 1);
9012 processSync(mapper);
9013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009015 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009016
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009017 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009018 processKey(mapper, BTN_TOOL_PENCIL, 0);
9019 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9020 processSync(mapper);
9021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009023 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009024
9025 // mouse
9026 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9027 processKey(mapper, BTN_TOOL_MOUSE, 1);
9028 processSync(mapper);
9029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9030 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009031 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009032
9033 // lens
9034 processKey(mapper, BTN_TOOL_MOUSE, 0);
9035 processKey(mapper, BTN_TOOL_LENS, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009040
9041 // double-tap
9042 processKey(mapper, BTN_TOOL_LENS, 0);
9043 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9044 processSync(mapper);
9045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009047 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009048
9049 // triple-tap
9050 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9051 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9052 processSync(mapper);
9053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009055 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009056
9057 // quad-tap
9058 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9059 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9060 processSync(mapper);
9061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009063 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009064
9065 // finger
9066 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9067 processKey(mapper, BTN_TOOL_FINGER, 1);
9068 processSync(mapper);
9069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009071 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009072
9073 // stylus trumps finger
9074 processKey(mapper, BTN_TOOL_PEN, 1);
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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009079
9080 // eraser trumps stylus
9081 processKey(mapper, BTN_TOOL_RUBBER, 1);
9082 processSync(mapper);
9083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009085 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009086
9087 // mouse trumps eraser
9088 processKey(mapper, BTN_TOOL_MOUSE, 1);
9089 processSync(mapper);
9090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009092 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009093
9094 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9095 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9096 processSync(mapper);
9097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009099 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009100
9101 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9102 processToolType(mapper, MT_TOOL_PEN);
9103 processSync(mapper);
9104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009106 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009107
9108 // back to default tool type
9109 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9110 processKey(mapper, BTN_TOOL_MOUSE, 0);
9111 processKey(mapper, BTN_TOOL_RUBBER, 0);
9112 processKey(mapper, BTN_TOOL_PEN, 0);
9113 processKey(mapper, BTN_TOOL_FINGER, 0);
9114 processSync(mapper);
9115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009117 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009118}
9119
9120TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009121 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009122 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009123 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009124 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +00009125 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009126
9127 NotifyMotionArgs motionArgs;
9128
9129 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9130 processId(mapper, 1);
9131 processPosition(mapper, 100, 200);
9132 processSync(mapper);
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(100), toDisplayY(200), 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(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9142
9143 // move a little
9144 processPosition(mapper, 150, 250);
9145 processSync(mapper);
9146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9147 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // down when BTN_TOUCH is pressed, pressure defaults to 1
9152 processKey(mapper, BTN_TOUCH, 1);
9153 processSync(mapper);
9154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9155 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9157 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9158
9159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9160 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9161 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9162 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9163
9164 // up when BTN_TOUCH is released, hover restored
9165 processKey(mapper, BTN_TOUCH, 0);
9166 processSync(mapper);
9167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9168 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9170 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9171
9172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9173 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9175 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9176
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9178 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9180 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9181
9182 // exit hover when pointer goes away
9183 processId(mapper, -1);
9184 processSync(mapper);
9185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9186 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9188 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9189}
9190
9191TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009192 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009193 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009194 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009195 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009196
9197 NotifyMotionArgs motionArgs;
9198
9199 // initially hovering because pressure is 0
9200 processId(mapper, 1);
9201 processPosition(mapper, 100, 200);
9202 processPressure(mapper, 0);
9203 processSync(mapper);
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(100), toDisplayY(200), 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(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9213
9214 // move a little
9215 processPosition(mapper, 150, 250);
9216 processSync(mapper);
9217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9218 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // down when pressure becomes non-zero
9223 processPressure(mapper, RAW_PRESSURE_MAX);
9224 processSync(mapper);
9225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9226 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9228 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9229
9230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9231 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9233 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9234
9235 // up when pressure becomes 0, hover restored
9236 processPressure(mapper, 0);
9237 processSync(mapper);
9238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9239 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9241 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9242
9243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9244 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9246 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9247
9248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9249 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9251 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9252
9253 // exit hover when pointer goes away
9254 processId(mapper, -1);
9255 processSync(mapper);
9256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9257 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9259 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9260}
9261
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009262/**
9263 * Set the input device port <--> display port associations, and check that the
9264 * events are routed to the display that matches the display port.
9265 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9266 */
9267TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009268 const std::string usb2 = "USB2";
9269 const uint8_t hdmi1 = 0;
9270 const uint8_t hdmi2 = 1;
9271 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009272 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009273
9274 addConfigurationProperty("touch.deviceType", "touchScreen");
9275 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009276 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009277
9278 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9279 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9280
9281 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9282 // for this input device is specified, and the matching viewport is not present,
9283 // the input device should be disabled (at the mapper level).
9284
9285 // Add viewport for display 2 on hdmi2
9286 prepareSecondaryDisplay(type, hdmi2);
9287 // Send a touch event
9288 processPosition(mapper, 100, 100);
9289 processSync(mapper);
9290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9291
9292 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009293 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009294 // Send a touch event again
9295 processPosition(mapper, 100, 100);
9296 processSync(mapper);
9297
9298 NotifyMotionArgs args;
9299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9300 ASSERT_EQ(DISPLAY_ID, args.displayId);
9301}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009302
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009303TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9304 addConfigurationProperty("touch.deviceType", "touchScreen");
9305 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009306 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009307
9308 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9309
Michael Wrighta9cf4192022-12-01 23:46:39 +00009310 prepareDisplay(ui::ROTATION_0);
9311 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009312
9313 // Send a touch event
9314 processPosition(mapper, 100, 100);
9315 processSync(mapper);
9316
9317 NotifyMotionArgs args;
9318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9319 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9320}
9321
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009322TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009323 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009324 std::shared_ptr<FakePointerController> fakePointerController =
9325 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009326 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009327 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009328 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009329
Garfield Tan888a6a42020-01-09 11:39:16 -08009330 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009331 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009332
Michael Wrighta9cf4192022-12-01 23:46:39 +00009333 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009334 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009335 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009336
Josep del Río2d8c79a2023-01-23 19:33:50 +00009337 // Check source is mouse that would obtain the PointerController.
9338 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009339
9340 NotifyMotionArgs motionArgs;
9341 processPosition(mapper, 100, 100);
9342 processSync(mapper);
9343
9344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9345 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9346 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9347}
9348
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009349/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009350 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9351 */
9352TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9353 addConfigurationProperty("touch.deviceType", "touchScreen");
9354 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009355 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009356
Michael Wrighta9cf4192022-12-01 23:46:39 +00009357 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009358 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9359 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9360 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9361 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009362
9363 NotifyMotionArgs args;
9364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9365 ASSERT_EQ(26, args.readTime);
9366
Harry Cutts33476232023-01-30 19:57:29 +00009367 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9368 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9369 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009370
9371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9372 ASSERT_EQ(33, args.readTime);
9373}
9374
9375/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009376 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9377 * events should not be delivered to the listener.
9378 */
9379TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9380 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009381 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009382 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009383 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009384 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009385 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009386 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009387
9388 NotifyMotionArgs motionArgs;
9389 processPosition(mapper, 100, 100);
9390 processSync(mapper);
9391
9392 mFakeListener->assertNotifyMotionWasNotCalled();
9393}
9394
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009395/**
9396 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9397 * the touch mapper can process the events and the events can be delivered to the listener.
9398 */
9399TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9400 addConfigurationProperty("touch.deviceType", "touchScreen");
9401 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009402 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009403 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009404 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009405 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009406 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009407
9408 NotifyMotionArgs motionArgs;
9409 processPosition(mapper, 100, 100);
9410 processSync(mapper);
9411
9412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9413 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9414}
9415
Garfield Tanc734e4f2021-01-15 20:01:39 -08009416TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9417 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009418 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009419 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009420 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009421 std::optional<DisplayViewport> optionalDisplayViewport =
9422 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9423 ASSERT_TRUE(optionalDisplayViewport.has_value());
9424 DisplayViewport displayViewport = *optionalDisplayViewport;
9425
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009426 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009427 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009428 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009429
9430 // Finger down
9431 int32_t x = 100, y = 100;
9432 processPosition(mapper, x, y);
9433 processSync(mapper);
9434
9435 NotifyMotionArgs motionArgs;
9436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9437 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9438
9439 // Deactivate display viewport
9440 displayViewport.isActive = false;
9441 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009442 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009443
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009444 // The ongoing touch should be canceled immediately
9445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9446 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9447
9448 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009449 x += 10, y += 10;
9450 processPosition(mapper, x, y);
9451 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009453
9454 // Reactivate display viewport
9455 displayViewport.isActive = true;
9456 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009457 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009458
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009459 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009460 x += 10, y += 10;
9461 processPosition(mapper, x, y);
9462 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9464 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009465}
9466
Arthur Hung7c645402019-01-25 17:45:42 +08009467TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9468 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009469 prepareAxes(POSITION | ID | SLOT);
9470 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009471 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009472
9473 // Create the second touch screen device, and enable multi fingers.
9474 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009475 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009476 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009477 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009478 std::shared_ptr<InputDevice> device2 =
9479 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009480 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009481
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009482 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009483 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009484 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009485 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009486 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009487 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009488 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009489 /*flat=*/0, /*fuzz=*/0);
9490 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009491 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9492 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009493
9494 // Setup the second touch screen device.
Arpit Singh56adebc2023-04-25 13:56:05 +00009495 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9496 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9497 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009498 std::list<NotifyArgs> unused =
9499 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009500 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009501 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009502
9503 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009504 std::shared_ptr<FakePointerController> fakePointerController =
9505 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009506 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009507
9508 // Setup policy for associated displays and show touches.
9509 const uint8_t hdmi1 = 0;
9510 const uint8_t hdmi2 = 1;
9511 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9512 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9513 mFakePolicy->setShowTouches(true);
9514
9515 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009516 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009517 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009518
9519 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009520 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009521 InputReaderConfiguration::Change::DISPLAY_INFO |
9522 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009523
9524 // Two fingers down at default display.
9525 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9526 processPosition(mapper, x1, y1);
9527 processId(mapper, 1);
9528 processSlot(mapper, 1);
9529 processPosition(mapper, x2, y2);
9530 processId(mapper, 2);
9531 processSync(mapper);
9532
9533 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9534 fakePointerController->getSpots().find(DISPLAY_ID);
9535 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9536 ASSERT_EQ(size_t(2), iter->second.size());
9537
9538 // Two fingers down at second display.
9539 processPosition(mapper2, x1, y1);
9540 processId(mapper2, 1);
9541 processSlot(mapper2, 1);
9542 processPosition(mapper2, x2, y2);
9543 processId(mapper2, 2);
9544 processSync(mapper2);
9545
9546 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9547 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9548 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009549
9550 // Disable the show touches configuration and ensure the spots are cleared.
9551 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009552 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009553 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009554
9555 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009556}
9557
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009558TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009559 prepareAxes(POSITION);
9560 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009561 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00009562 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009563
9564 NotifyMotionArgs motionArgs;
9565 // Unrotated video frame
9566 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9567 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009568 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009569 processPosition(mapper, 100, 200);
9570 processSync(mapper);
9571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9572 ASSERT_EQ(frames, motionArgs.videoFrames);
9573
9574 // Subsequent touch events should not have any videoframes
9575 // This is implemented separately in FakeEventHub,
9576 // but that should match the behaviour of TouchVideoDevice.
9577 processPosition(mapper, 200, 200);
9578 processSync(mapper);
9579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9580 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9581}
9582
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009583TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009584 prepareAxes(POSITION);
9585 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009586 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009587 // Unrotated video frame
9588 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9589 NotifyMotionArgs motionArgs;
9590
9591 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009592 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009593 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9594 clearViewports();
9595 prepareDisplay(orientation);
9596 std::vector<TouchVideoFrame> frames{frame};
9597 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9598 processPosition(mapper, 100, 200);
9599 processSync(mapper);
9600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9601 ASSERT_EQ(frames, motionArgs.videoFrames);
9602 }
9603}
9604
9605TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9606 prepareAxes(POSITION);
9607 addConfigurationProperty("touch.deviceType", "touchScreen");
9608 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9609 // orientation-aware are affected by display rotation.
9610 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00009611 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009612 // Unrotated video frame
9613 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9614 NotifyMotionArgs motionArgs;
9615
9616 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009617 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009618 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9619 clearViewports();
9620 prepareDisplay(orientation);
9621 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009622 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009623 processPosition(mapper, 100, 200);
9624 processSync(mapper);
9625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009626 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9627 // compared to the display. This is so that when the window transform (which contains the
9628 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9629 // window's coordinate space.
9630 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009631 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009632
9633 // Release finger.
9634 processSync(mapper);
9635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009636 }
9637}
9638
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009639TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009640 prepareAxes(POSITION);
9641 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009642 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009643 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9644 // so mix these.
9645 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9646 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9647 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9648 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9649 NotifyMotionArgs motionArgs;
9650
Michael Wrighta9cf4192022-12-01 23:46:39 +00009651 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009652 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009653 processPosition(mapper, 100, 200);
9654 processSync(mapper);
9655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009656 ASSERT_EQ(frames, motionArgs.videoFrames);
9657}
9658
9659TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9660 prepareAxes(POSITION);
9661 addConfigurationProperty("touch.deviceType", "touchScreen");
9662 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9663 // orientation-aware are affected by display rotation.
9664 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00009665 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009666 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9667 // so mix these.
9668 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9669 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9670 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9671 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9672 NotifyMotionArgs motionArgs;
9673
Michael Wrighta9cf4192022-12-01 23:46:39 +00009674 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009675 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9676 processPosition(mapper, 100, 200);
9677 processSync(mapper);
9678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9679 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9680 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9681 // compared to the display. This is so that when the window transform (which contains the
9682 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9683 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009684 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009685 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009686 ASSERT_EQ(frames, motionArgs.videoFrames);
9687}
9688
Arthur Hung9da14732019-09-02 16:16:58 +08009689/**
9690 * If we had defined port associations, but the viewport is not ready, the touch device would be
9691 * expected to be disabled, and it should be enabled after the viewport has found.
9692 */
9693TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009694 constexpr uint8_t hdmi2 = 1;
9695 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009696 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009697
9698 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9699
9700 addConfigurationProperty("touch.deviceType", "touchScreen");
9701 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009702 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009703
9704 ASSERT_EQ(mDevice->isEnabled(), false);
9705
9706 // Add display on hdmi2, the device should be enabled and can receive touch event.
9707 prepareSecondaryDisplay(type, hdmi2);
9708 ASSERT_EQ(mDevice->isEnabled(), true);
9709
9710 // Send a touch event.
9711 processPosition(mapper, 100, 100);
9712 processSync(mapper);
9713
9714 NotifyMotionArgs args;
9715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9716 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9717}
9718
Arthur Hung421eb1c2020-01-16 00:09:42 +08009719TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009720 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009721 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009722 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009723 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009724
9725 NotifyMotionArgs motionArgs;
9726
9727 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9728 // finger down
9729 processId(mapper, 1);
9730 processPosition(mapper, x1, y1);
9731 processSync(mapper);
9732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9733 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009734 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009735
9736 // finger move
9737 processId(mapper, 1);
9738 processPosition(mapper, x2, y2);
9739 processSync(mapper);
9740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9741 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009742 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009743
9744 // finger up.
9745 processId(mapper, -1);
9746 processSync(mapper);
9747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9748 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009749 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009750
9751 // new finger down
9752 processId(mapper, 1);
9753 processPosition(mapper, x3, y3);
9754 processSync(mapper);
9755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9756 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009757 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009758}
9759
9760/**
arthurhungcc7f9802020-04-30 17:55:40 +08009761 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9762 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009763 */
arthurhungcc7f9802020-04-30 17:55:40 +08009764TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009765 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009766 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009767 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009768 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009769
9770 NotifyMotionArgs motionArgs;
9771
9772 // default tool type is finger
9773 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009774 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009775 processPosition(mapper, x1, y1);
9776 processSync(mapper);
9777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9778 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009779 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009780
9781 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9782 processToolType(mapper, MT_TOOL_PALM);
9783 processSync(mapper);
9784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9785 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9786
9787 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009788 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009789 processPosition(mapper, x2, y2);
9790 processSync(mapper);
9791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9792
9793 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009794 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009795 processSync(mapper);
9796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9797
9798 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009799 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009800 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009801 processPosition(mapper, x3, y3);
9802 processSync(mapper);
9803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9804 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009805 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009806}
9807
arthurhungbf89a482020-04-17 17:37:55 +08009808/**
arthurhungcc7f9802020-04-30 17:55:40 +08009809 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9810 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009811 */
arthurhungcc7f9802020-04-30 17:55:40 +08009812TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009813 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009814 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009815 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009816 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009817
9818 NotifyMotionArgs motionArgs;
9819
9820 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009821 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9822 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009823 processPosition(mapper, x1, y1);
9824 processSync(mapper);
9825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9826 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009827 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009828
9829 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009830 processSlot(mapper, SECOND_SLOT);
9831 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009832 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009833 processSync(mapper);
9834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009835 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009836 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009837
9838 // If the tool type of the first finger changes to MT_TOOL_PALM,
9839 // we expect to receive ACTION_POINTER_UP with cancel flag.
9840 processSlot(mapper, FIRST_SLOT);
9841 processId(mapper, FIRST_TRACKING_ID);
9842 processToolType(mapper, MT_TOOL_PALM);
9843 processSync(mapper);
9844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009845 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009846 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9847
9848 // The following MOVE events of second finger should be processed.
9849 processSlot(mapper, SECOND_SLOT);
9850 processId(mapper, SECOND_TRACKING_ID);
9851 processPosition(mapper, x2 + 1, y2 + 1);
9852 processSync(mapper);
9853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009855 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009856
9857 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9858 // it. Second finger receive move.
9859 processSlot(mapper, FIRST_SLOT);
9860 processId(mapper, INVALID_TRACKING_ID);
9861 processSync(mapper);
9862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009864 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009865
9866 // Second finger keeps moving.
9867 processSlot(mapper, SECOND_SLOT);
9868 processId(mapper, SECOND_TRACKING_ID);
9869 processPosition(mapper, x2 + 2, y2 + 2);
9870 processSync(mapper);
9871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009873 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009874
9875 // Second finger up.
9876 processId(mapper, INVALID_TRACKING_ID);
9877 processSync(mapper);
9878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9879 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9880 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9881}
9882
9883/**
9884 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9885 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9886 */
9887TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9888 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009889 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009890 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009891 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009892
9893 NotifyMotionArgs motionArgs;
9894
9895 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9896 // First finger down.
9897 processId(mapper, FIRST_TRACKING_ID);
9898 processPosition(mapper, x1, y1);
9899 processSync(mapper);
9900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9901 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009902 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009903
9904 // Second finger down.
9905 processSlot(mapper, SECOND_SLOT);
9906 processId(mapper, SECOND_TRACKING_ID);
9907 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009908 processSync(mapper);
9909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009910 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009911 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009912
arthurhungcc7f9802020-04-30 17:55:40 +08009913 // If the tool type of the first finger changes to MT_TOOL_PALM,
9914 // we expect to receive ACTION_POINTER_UP with cancel flag.
9915 processSlot(mapper, FIRST_SLOT);
9916 processId(mapper, FIRST_TRACKING_ID);
9917 processToolType(mapper, MT_TOOL_PALM);
9918 processSync(mapper);
9919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009920 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009921 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9922
9923 // Second finger keeps moving.
9924 processSlot(mapper, SECOND_SLOT);
9925 processId(mapper, SECOND_TRACKING_ID);
9926 processPosition(mapper, x2 + 1, y2 + 1);
9927 processSync(mapper);
9928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9930
9931 // second finger becomes palm, receive cancel due to only 1 finger is active.
9932 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009933 processToolType(mapper, MT_TOOL_PALM);
9934 processSync(mapper);
9935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9936 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9937
arthurhungcc7f9802020-04-30 17:55:40 +08009938 // third finger down.
9939 processSlot(mapper, THIRD_SLOT);
9940 processId(mapper, THIRD_TRACKING_ID);
9941 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009942 processPosition(mapper, x3, y3);
9943 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9945 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009946 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009947 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009948
9949 // third finger move
9950 processId(mapper, THIRD_TRACKING_ID);
9951 processPosition(mapper, x3 + 1, y3 + 1);
9952 processSync(mapper);
9953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9955
9956 // first finger up, third finger receive move.
9957 processSlot(mapper, FIRST_SLOT);
9958 processId(mapper, INVALID_TRACKING_ID);
9959 processSync(mapper);
9960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9961 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009962 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009963
9964 // second finger up, third finger receive move.
9965 processSlot(mapper, SECOND_SLOT);
9966 processId(mapper, INVALID_TRACKING_ID);
9967 processSync(mapper);
9968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009970 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009971
9972 // third finger up.
9973 processSlot(mapper, THIRD_SLOT);
9974 processId(mapper, INVALID_TRACKING_ID);
9975 processSync(mapper);
9976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9977 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9978 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9979}
9980
9981/**
9982 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9983 * and the active finger could still be allowed to receive the events
9984 */
9985TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9986 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009987 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009988 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009989 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009990
9991 NotifyMotionArgs motionArgs;
9992
9993 // default tool type is finger
9994 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9995 processId(mapper, FIRST_TRACKING_ID);
9996 processPosition(mapper, x1, y1);
9997 processSync(mapper);
9998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9999 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010000 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010001
10002 // Second finger down.
10003 processSlot(mapper, SECOND_SLOT);
10004 processId(mapper, SECOND_TRACKING_ID);
10005 processPosition(mapper, x2, y2);
10006 processSync(mapper);
10007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010008 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010009 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010010
10011 // If the tool type of the second finger changes to MT_TOOL_PALM,
10012 // we expect to receive ACTION_POINTER_UP with cancel flag.
10013 processId(mapper, SECOND_TRACKING_ID);
10014 processToolType(mapper, MT_TOOL_PALM);
10015 processSync(mapper);
10016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010017 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010018 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10019
10020 // The following MOVE event should be processed.
10021 processSlot(mapper, FIRST_SLOT);
10022 processId(mapper, FIRST_TRACKING_ID);
10023 processPosition(mapper, x1 + 1, y1 + 1);
10024 processSync(mapper);
10025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010027 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010028
10029 // second finger up.
10030 processSlot(mapper, SECOND_SLOT);
10031 processId(mapper, INVALID_TRACKING_ID);
10032 processSync(mapper);
10033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10035
10036 // first finger keep moving
10037 processSlot(mapper, FIRST_SLOT);
10038 processId(mapper, FIRST_TRACKING_ID);
10039 processPosition(mapper, x1 + 2, y1 + 2);
10040 processSync(mapper);
10041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10043
10044 // first finger up.
10045 processId(mapper, INVALID_TRACKING_ID);
10046 processSync(mapper);
10047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10048 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10049 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010050}
10051
Arthur Hung9ad18942021-06-19 02:04:46 +000010052/**
10053 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10054 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10055 * cause slot be valid again.
10056 */
10057TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10058 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010059 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010060 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010061 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010062
10063 NotifyMotionArgs motionArgs;
10064
10065 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10066 // First finger down.
10067 processId(mapper, FIRST_TRACKING_ID);
10068 processPosition(mapper, x1, y1);
10069 processPressure(mapper, RAW_PRESSURE_MAX);
10070 processSync(mapper);
10071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10072 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010073 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010074
10075 // First finger move.
10076 processId(mapper, FIRST_TRACKING_ID);
10077 processPosition(mapper, x1 + 1, y1 + 1);
10078 processPressure(mapper, RAW_PRESSURE_MAX);
10079 processSync(mapper);
10080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010082 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010083
10084 // Second finger down.
10085 processSlot(mapper, SECOND_SLOT);
10086 processId(mapper, SECOND_TRACKING_ID);
10087 processPosition(mapper, x2, y2);
10088 processPressure(mapper, RAW_PRESSURE_MAX);
10089 processSync(mapper);
10090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010091 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010092 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010093
10094 // second finger up with some unexpected data.
10095 processSlot(mapper, SECOND_SLOT);
10096 processId(mapper, INVALID_TRACKING_ID);
10097 processPosition(mapper, x2, y2);
10098 processSync(mapper);
10099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010100 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010101 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010102
10103 // first finger up with some unexpected data.
10104 processSlot(mapper, FIRST_SLOT);
10105 processId(mapper, INVALID_TRACKING_ID);
10106 processPosition(mapper, x2, y2);
10107 processPressure(mapper, RAW_PRESSURE_MAX);
10108 processSync(mapper);
10109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10110 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010111 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010112}
10113
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010114TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10115 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010116 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010117 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010118 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010119
10120 // First finger down.
10121 processId(mapper, FIRST_TRACKING_ID);
10122 processPosition(mapper, 100, 200);
10123 processPressure(mapper, RAW_PRESSURE_MAX);
10124 processSync(mapper);
10125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10126 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10127
10128 // Second finger down.
10129 processSlot(mapper, SECOND_SLOT);
10130 processId(mapper, SECOND_TRACKING_ID);
10131 processPosition(mapper, 300, 400);
10132 processPressure(mapper, RAW_PRESSURE_MAX);
10133 processSync(mapper);
10134 ASSERT_NO_FATAL_FAILURE(
10135 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10136
10137 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010138 // preserved. Resetting should cancel the ongoing gesture.
10139 resetMapper(mapper, ARBITRARY_TIME);
10140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10141 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010142
10143 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10144 // the existing touch state to generate a down event.
10145 processPosition(mapper, 301, 302);
10146 processSync(mapper);
10147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10148 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10150 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10151
10152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10153}
10154
10155TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10156 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010157 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010158 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010159 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010160
10161 // First finger touches down and releases.
10162 processId(mapper, FIRST_TRACKING_ID);
10163 processPosition(mapper, 100, 200);
10164 processPressure(mapper, RAW_PRESSURE_MAX);
10165 processSync(mapper);
10166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10167 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10168 processId(mapper, INVALID_TRACKING_ID);
10169 processSync(mapper);
10170 ASSERT_NO_FATAL_FAILURE(
10171 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10172
10173 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10174 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010175 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10177
10178 // Send an empty sync frame. Since there are no pointers, no events are generated.
10179 processSync(mapper);
10180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10181}
10182
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010183TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010184 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010185 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010186 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010187 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010189
10190 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10191 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10192 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10193 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10194 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10195
10196 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010197 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010198 processId(mapper, FIRST_TRACKING_ID);
10199 processToolType(mapper, MT_TOOL_PEN);
10200 processPosition(mapper, 100, 200);
10201 processPressure(mapper, RAW_PRESSURE_MAX);
10202 processSync(mapper);
10203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10204 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10205 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010206 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010207
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010208 // Now that we know the device supports styluses, ensure that the device is re-configured with
10209 // the stylus source.
10210 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10211 {
10212 const auto& devices = mReader->getInputDevices();
10213 auto deviceInfo =
10214 std::find_if(devices.begin(), devices.end(),
10215 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10216 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10217 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10218 }
10219
10220 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10222
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010223 processId(mapper, INVALID_TRACKING_ID);
10224 processSync(mapper);
10225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10226 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10227 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010228 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010229}
10230
Seunghwan Choi356026c2023-02-01 14:37:25 +090010231TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10232 addConfigurationProperty("touch.deviceType", "touchScreen");
10233 prepareDisplay(ui::ROTATION_0);
10234 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10235 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10236 // indicate stylus presence dynamically.
10237 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10238 std::shared_ptr<FakePointerController> fakePointerController =
10239 std::make_shared<FakePointerController>();
10240 mFakePolicy->setPointerController(fakePointerController);
10241 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singh56adebc2023-04-25 13:56:05 +000010242 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010243
10244 processId(mapper, FIRST_TRACKING_ID);
10245 processPressure(mapper, RAW_PRESSURE_MIN);
10246 processPosition(mapper, 100, 200);
10247 processToolType(mapper, MT_TOOL_PEN);
10248 processSync(mapper);
10249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10250 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010251 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010252 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10253 ASSERT_TRUE(fakePointerController->isPointerShown());
10254 ASSERT_NO_FATAL_FAILURE(
10255 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10256}
10257
10258TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10259 addConfigurationProperty("touch.deviceType", "touchScreen");
10260 prepareDisplay(ui::ROTATION_0);
10261 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10262 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10263 // indicate stylus presence dynamically.
10264 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10265 std::shared_ptr<FakePointerController> fakePointerController =
10266 std::make_shared<FakePointerController>();
10267 mFakePolicy->setPointerController(fakePointerController);
10268 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singh56adebc2023-04-25 13:56:05 +000010269 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010270
10271 processId(mapper, FIRST_TRACKING_ID);
10272 processPressure(mapper, RAW_PRESSURE_MIN);
10273 processPosition(mapper, 100, 200);
10274 processToolType(mapper, MT_TOOL_PEN);
10275 processSync(mapper);
10276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10277 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010278 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010279 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10280 ASSERT_FALSE(fakePointerController->isPointerShown());
10281}
10282
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010283// --- MultiTouchInputMapperTest_ExternalDevice ---
10284
10285class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10286protected:
Chris Yea52ade12020-08-27 16:49:20 -070010287 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010288};
10289
10290/**
10291 * Expect fallback to internal viewport if device is external and external viewport is not present.
10292 */
10293TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10294 prepareAxes(POSITION);
10295 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010296 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +000010297 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010298
10299 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10300
10301 NotifyMotionArgs motionArgs;
10302
10303 // Expect the event to be sent to the internal viewport,
10304 // because an external viewport is not present.
10305 processPosition(mapper, 100, 100);
10306 processSync(mapper);
10307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10308 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10309
10310 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010311 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010312 processPosition(mapper, 100, 100);
10313 processSync(mapper);
10314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10315 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10316}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010317
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010318TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10319 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10320 std::shared_ptr<FakePointerController> fakePointerController =
10321 std::make_shared<FakePointerController>();
10322 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10323 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010324
10325 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010326 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010327 prepareAxes(POSITION | ID | SLOT);
10328 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10329 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10330 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010331 mFakePolicy->setPointerController(fakePointerController);
Arpit Singh56adebc2023-04-25 13:56:05 +000010332 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010333
10334 // captured touchpad should be a touchpad source
10335 NotifyDeviceResetArgs resetArgs;
10336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10337 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10338
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010339 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010340
10341 const InputDeviceInfo::MotionRange* relRangeX =
10342 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10343 ASSERT_NE(relRangeX, nullptr);
10344 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10345 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10346 const InputDeviceInfo::MotionRange* relRangeY =
10347 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10348 ASSERT_NE(relRangeY, nullptr);
10349 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10350 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10351
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010352 // run captured pointer tests - note that this is unscaled, so input listener events should be
10353 // identical to what the hardware sends (accounting for any
10354 // calibration).
10355 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010356 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010357 processId(mapper, 1);
10358 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10359 processKey(mapper, BTN_TOUCH, 1);
10360 processSync(mapper);
10361
10362 // expect coord[0] to contain initial location of touch 0
10363 NotifyMotionArgs args;
10364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10365 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010366 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010367 ASSERT_EQ(0, args.pointerProperties[0].id);
10368 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10369 ASSERT_NO_FATAL_FAILURE(
10370 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10371
10372 // FINGER 1 DOWN
10373 processSlot(mapper, 1);
10374 processId(mapper, 2);
10375 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10376 processSync(mapper);
10377
10378 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010380 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010381 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010382 ASSERT_EQ(0, args.pointerProperties[0].id);
10383 ASSERT_EQ(1, args.pointerProperties[1].id);
10384 ASSERT_NO_FATAL_FAILURE(
10385 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10386 ASSERT_NO_FATAL_FAILURE(
10387 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10388
10389 // FINGER 1 MOVE
10390 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10391 processSync(mapper);
10392
10393 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10394 // from move
10395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10397 ASSERT_NO_FATAL_FAILURE(
10398 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10399 ASSERT_NO_FATAL_FAILURE(
10400 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10401
10402 // FINGER 0 MOVE
10403 processSlot(mapper, 0);
10404 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10405 processSync(mapper);
10406
10407 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10410 ASSERT_NO_FATAL_FAILURE(
10411 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10412 ASSERT_NO_FATAL_FAILURE(
10413 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10414
10415 // BUTTON DOWN
10416 processKey(mapper, BTN_LEFT, 1);
10417 processSync(mapper);
10418
10419 // touchinputmapper design sends a move before button press
10420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10421 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10423 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10424
10425 // BUTTON UP
10426 processKey(mapper, BTN_LEFT, 0);
10427 processSync(mapper);
10428
10429 // touchinputmapper design sends a move after button release
10430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10431 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10434
10435 // FINGER 0 UP
10436 processId(mapper, -1);
10437 processSync(mapper);
10438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10439 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10440
10441 // FINGER 1 MOVE
10442 processSlot(mapper, 1);
10443 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10444 processSync(mapper);
10445
10446 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010449 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010450 ASSERT_EQ(1, args.pointerProperties[0].id);
10451 ASSERT_NO_FATAL_FAILURE(
10452 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10453
10454 // FINGER 1 UP
10455 processId(mapper, -1);
10456 processKey(mapper, BTN_TOUCH, 0);
10457 processSync(mapper);
10458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10459 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10460
Josep del Río2d8c79a2023-01-23 19:33:50 +000010461 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010462 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010463 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010465 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010466}
10467
10468TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10469 std::shared_ptr<FakePointerController> fakePointerController =
10470 std::make_shared<FakePointerController>();
10471 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10472 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010473
10474 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010475 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010476 prepareAxes(POSITION | ID | SLOT);
10477 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10478 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010479 mFakePolicy->setPointerController(fakePointerController);
Arpit Singh56adebc2023-04-25 13:56:05 +000010480 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010481 // run uncaptured pointer tests - pushes out generic events
10482 // FINGER 0 DOWN
10483 processId(mapper, 3);
10484 processPosition(mapper, 100, 100);
10485 processKey(mapper, BTN_TOUCH, 1);
10486 processSync(mapper);
10487
10488 // start at (100,100), cursor should be at (0,0) * scale
10489 NotifyMotionArgs args;
10490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10491 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10492 ASSERT_NO_FATAL_FAILURE(
10493 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10494
10495 // FINGER 0 MOVE
10496 processPosition(mapper, 200, 200);
10497 processSync(mapper);
10498
10499 // compute scaling to help with touch position checking
10500 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10501 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10502 float scale =
10503 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10504
10505 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10507 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10509 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010510
10511 // BUTTON DOWN
10512 processKey(mapper, BTN_LEFT, 1);
10513 processSync(mapper);
10514
10515 // touchinputmapper design sends a move before button press
10516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10517 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10519 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10520
10521 // BUTTON UP
10522 processKey(mapper, BTN_LEFT, 0);
10523 processSync(mapper);
10524
10525 // touchinputmapper design sends a move after button release
10526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10527 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10529 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010530}
10531
10532TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10533 std::shared_ptr<FakePointerController> fakePointerController =
10534 std::make_shared<FakePointerController>();
10535
Michael Wrighta9cf4192022-12-01 23:46:39 +000010536 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010537 prepareAxes(POSITION | ID | SLOT);
10538 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010539 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010540 mFakePolicy->setPointerCapture(false);
Arpit Singh56adebc2023-04-25 13:56:05 +000010541 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010542
Josep del Río2d8c79a2023-01-23 19:33:50 +000010543 // uncaptured touchpad should be a pointer device
10544 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010545
Josep del Río2d8c79a2023-01-23 19:33:50 +000010546 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010547 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010548 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010549 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10550}
10551
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010552// --- BluetoothMultiTouchInputMapperTest ---
10553
10554class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10555protected:
10556 void SetUp() override {
10557 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10558 }
10559};
10560
10561TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10562 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010563 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010564 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010565 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010566
10567 nsecs_t kernelEventTime = ARBITRARY_TIME;
10568 nsecs_t expectedEventTime = ARBITRARY_TIME;
10569 // Touch down.
10570 processId(mapper, FIRST_TRACKING_ID);
10571 processPosition(mapper, 100, 200);
10572 processPressure(mapper, RAW_PRESSURE_MAX);
10573 processSync(mapper, ARBITRARY_TIME);
10574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10575 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10576
10577 // Process several events that come in quick succession, according to their timestamps.
10578 for (int i = 0; i < 3; i++) {
10579 constexpr static nsecs_t delta = ms2ns(1);
10580 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10581 kernelEventTime += delta;
10582 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10583
10584 processPosition(mapper, 101 + i, 201 + i);
10585 processSync(mapper, kernelEventTime);
10586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10587 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10588 WithEventTime(expectedEventTime))));
10589 }
10590
10591 // Release the touch.
10592 processId(mapper, INVALID_TRACKING_ID);
10593 processPressure(mapper, RAW_PRESSURE_MIN);
10594 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10596 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10597 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10598}
10599
10600// --- MultiTouchPointerModeTest ---
10601
HQ Liue6983c72022-04-19 22:14:56 +000010602class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10603protected:
10604 float mPointerMovementScale;
10605 float mPointerXZoomScale;
10606 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10607 addConfigurationProperty("touch.deviceType", "pointer");
10608 std::shared_ptr<FakePointerController> fakePointerController =
10609 std::make_shared<FakePointerController>();
10610 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10611 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010612 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010613
10614 prepareAxes(POSITION);
10615 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10616 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10617 // needs to be disabled, and the pointer gesture needs to be enabled.
10618 mFakePolicy->setPointerCapture(false);
10619 mFakePolicy->setPointerGestureEnabled(true);
10620 mFakePolicy->setPointerController(fakePointerController);
10621
10622 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10623 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10624 mPointerMovementScale =
10625 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10626 mPointerXZoomScale =
10627 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10628 }
10629
10630 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10631 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10632 /*flat*/ 0,
10633 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10634 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10635 /*flat*/ 0,
10636 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10637 }
10638};
10639
10640/**
10641 * Two fingers down on a pointer mode touch pad. The width
10642 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10643 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10644 * be greater than the both value to be freeform gesture, so that after two
10645 * fingers start to move downwards, the gesture should be swipe.
10646 */
10647TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10648 // The min freeform gesture width is 25units/mm x 30mm = 750
10649 // which is greater than fraction of the diagnal length of the touchpad (349).
10650 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010651 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010652 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010653 NotifyMotionArgs motionArgs;
10654
10655 // Two fingers down at once.
10656 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10657 // Pointer's initial position is used the [0,0] coordinate.
10658 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10659
10660 processId(mapper, FIRST_TRACKING_ID);
10661 processPosition(mapper, x1, y1);
10662 processMTSync(mapper);
10663 processId(mapper, SECOND_TRACKING_ID);
10664 processPosition(mapper, x2, y2);
10665 processMTSync(mapper);
10666 processSync(mapper);
10667
10668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010669 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010670 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010671 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010672 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010673 ASSERT_NO_FATAL_FAILURE(
10674 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10675
10676 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10677 // that there should be 1 pointer.
10678 int32_t movingDistance = 200;
10679 y1 += movingDistance;
10680 y2 += movingDistance;
10681
10682 processId(mapper, FIRST_TRACKING_ID);
10683 processPosition(mapper, x1, y1);
10684 processMTSync(mapper);
10685 processId(mapper, SECOND_TRACKING_ID);
10686 processPosition(mapper, x2, y2);
10687 processMTSync(mapper);
10688 processSync(mapper);
10689
10690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010691 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010693 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010694 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10696 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10697 0, 0, 0, 0));
10698}
10699
10700/**
10701 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10702 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10703 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10704 * value to be freeform gesture, so that after two fingers start to move downwards,
10705 * the gesture should be swipe.
10706 */
10707TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10708 // The min freeform gesture width is 5units/mm x 30mm = 150
10709 // which is greater than fraction of the diagnal length of the touchpad (349).
10710 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010711 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singh56adebc2023-04-25 13:56:05 +000010712 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010713 NotifyMotionArgs motionArgs;
10714
10715 // Two fingers down at once.
10716 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10717 // Pointer's initial position is used the [0,0] coordinate.
10718 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10719
10720 processId(mapper, FIRST_TRACKING_ID);
10721 processPosition(mapper, x1, y1);
10722 processMTSync(mapper);
10723 processId(mapper, SECOND_TRACKING_ID);
10724 processPosition(mapper, x2, y2);
10725 processMTSync(mapper);
10726 processSync(mapper);
10727
10728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010729 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010730 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010731 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010732 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010733 ASSERT_NO_FATAL_FAILURE(
10734 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10735
10736 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10737 // and there should be 1 pointer.
10738 int32_t movingDistance = 200;
10739 y1 += movingDistance;
10740 y2 += movingDistance;
10741
10742 processId(mapper, FIRST_TRACKING_ID);
10743 processPosition(mapper, x1, y1);
10744 processMTSync(mapper);
10745 processId(mapper, SECOND_TRACKING_ID);
10746 processPosition(mapper, x2, y2);
10747 processMTSync(mapper);
10748 processSync(mapper);
10749
10750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010751 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010752 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010753 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010754 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010755 // New coordinate is the scaled relative coordinate from the initial coordinate.
10756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10757 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10758 0, 0, 0, 0));
10759}
10760
10761/**
10762 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10763 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10764 * freeform gestures after two fingers start to move downwards.
10765 */
10766TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010767 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010768 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010769
10770 NotifyMotionArgs motionArgs;
10771
10772 // Two fingers down at once. Wider than the max swipe width.
10773 // The gesture is expected to be PRESS, then transformed to FREEFORM
10774 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10775
10776 processId(mapper, FIRST_TRACKING_ID);
10777 processPosition(mapper, x1, y1);
10778 processMTSync(mapper);
10779 processId(mapper, SECOND_TRACKING_ID);
10780 processPosition(mapper, x2, y2);
10781 processMTSync(mapper);
10782 processSync(mapper);
10783
10784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010785 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010786 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010787 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010788 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010789 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10790 ASSERT_NO_FATAL_FAILURE(
10791 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10792
10793 int32_t movingDistance = 200;
10794
10795 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10796 // then two down events for two pointers.
10797 y1 += movingDistance;
10798 y2 += movingDistance;
10799
10800 processId(mapper, FIRST_TRACKING_ID);
10801 processPosition(mapper, x1, y1);
10802 processMTSync(mapper);
10803 processId(mapper, SECOND_TRACKING_ID);
10804 processPosition(mapper, x2, y2);
10805 processMTSync(mapper);
10806 processSync(mapper);
10807
10808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10809 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010810 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010811 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010813 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010814 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010815 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010817 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010818 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010819 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010820 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010821 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010822 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010823 // Two pointers' scaled relative coordinates from their initial centroid.
10824 // Initial y coordinates are 0 as y1 and y2 have the same value.
10825 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10826 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10827 // When pointers move, the new coordinates equal to the initial coordinates plus
10828 // scaled moving distance.
10829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10830 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10831 0, 0, 0, 0));
10832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10833 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10834 0, 0, 0, 0));
10835
10836 // Move two fingers down again, expect one MOVE motion event.
10837 y1 += movingDistance;
10838 y2 += movingDistance;
10839
10840 processId(mapper, FIRST_TRACKING_ID);
10841 processPosition(mapper, x1, y1);
10842 processMTSync(mapper);
10843 processId(mapper, SECOND_TRACKING_ID);
10844 processPosition(mapper, x2, y2);
10845 processMTSync(mapper);
10846 processSync(mapper);
10847
10848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010849 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010851 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010852 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10854 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10855 0, 0, 0, 0, 0));
10856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10857 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10858 0, 0, 0, 0, 0));
10859}
10860
Harry Cutts39b7ca22022-10-05 15:55:48 +000010861TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010862 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010863 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010864 NotifyMotionArgs motionArgs;
10865
10866 // Place two fingers down.
10867 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10868
10869 processId(mapper, FIRST_TRACKING_ID);
10870 processPosition(mapper, x1, y1);
10871 processMTSync(mapper);
10872 processId(mapper, SECOND_TRACKING_ID);
10873 processPosition(mapper, x2, y2);
10874 processMTSync(mapper);
10875 processSync(mapper);
10876
10877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010878 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010879 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10880 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10881 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10882 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10883
10884 // Move the two fingers down and to the left.
10885 int32_t movingDistance = 200;
10886 x1 -= movingDistance;
10887 y1 += movingDistance;
10888 x2 -= movingDistance;
10889 y2 += movingDistance;
10890
10891 processId(mapper, FIRST_TRACKING_ID);
10892 processPosition(mapper, x1, y1);
10893 processMTSync(mapper);
10894 processId(mapper, SECOND_TRACKING_ID);
10895 processPosition(mapper, x2, y2);
10896 processMTSync(mapper);
10897 processSync(mapper);
10898
10899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010900 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010901 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10902 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10903 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10904 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10905}
10906
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010907TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010908 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010909 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +000010910 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10912
10913 // Start a stylus gesture.
10914 processKey(mapper, BTN_TOOL_PEN, 1);
10915 processId(mapper, FIRST_TRACKING_ID);
10916 processPosition(mapper, 100, 200);
10917 processSync(mapper);
10918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10919 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10920 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010921 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010922 // TODO(b/257078296): Pointer mode generates extra event.
10923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10924 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10925 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010926 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10928
10929 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10930 // gesture should be disabled.
10931 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10932 viewport->isActive = false;
10933 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010934 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10936 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10937 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010938 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010939 // TODO(b/257078296): Pointer mode generates extra event.
10940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10941 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10942 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010943 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10945}
10946
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010947// --- JoystickInputMapperTest ---
10948
10949class JoystickInputMapperTest : public InputMapperTest {
10950protected:
10951 static const int32_t RAW_X_MIN;
10952 static const int32_t RAW_X_MAX;
10953 static const int32_t RAW_Y_MIN;
10954 static const int32_t RAW_Y_MAX;
10955
10956 void SetUp() override {
10957 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10958 }
10959 void prepareAxes() {
10960 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10961 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10962 }
10963
10964 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10965 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10966 }
10967
10968 void processSync(JoystickInputMapper& mapper) {
10969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10970 }
10971
Michael Wrighta9cf4192022-12-01 23:46:39 +000010972 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010973 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10974 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10975 NO_PORT, ViewportType::VIRTUAL);
10976 }
10977};
10978
10979const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10980const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10981const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10982const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10983
10984TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10985 prepareAxes();
Arpit Singh2be4a362023-04-26 14:16:50 +000010986 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010987
10988 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10989
Michael Wrighta9cf4192022-12-01 23:46:39 +000010990 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010991
10992 // Send an axis event
10993 processAxis(mapper, ABS_X, 100);
10994 processSync(mapper);
10995
10996 NotifyMotionArgs args;
10997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10998 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10999
11000 // Send another axis event
11001 processAxis(mapper, ABS_Y, 100);
11002 processSync(mapper);
11003
11004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11005 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11006}
11007
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011008// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011009
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011010class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011011protected:
11012 static const char* DEVICE_NAME;
11013 static const char* DEVICE_LOCATION;
11014 static const int32_t DEVICE_ID;
11015 static const int32_t DEVICE_GENERATION;
11016 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011017 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011018 static const int32_t EVENTHUB_ID;
11019
11020 std::shared_ptr<FakeEventHub> mFakeEventHub;
11021 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011022 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011023 std::unique_ptr<InstrumentedInputReader> mReader;
11024 std::shared_ptr<InputDevice> mDevice;
11025
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011026 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011027 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011028 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011029 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011030 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011031 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011032 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11033 }
11034
11035 void SetUp() override { SetUp(DEVICE_CLASSES); }
11036
11037 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011038 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011039 mFakePolicy.clear();
11040 }
11041
Chris Yee2b1e5c2021-03-10 22:45:12 -080011042 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11043 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011044 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011045 InputDeviceIdentifier identifier;
11046 identifier.name = name;
11047 identifier.location = location;
11048 std::shared_ptr<InputDevice> device =
11049 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11050 identifier);
11051 mReader->pushNextDevice(device);
11052 mFakeEventHub->addDevice(eventHubId, name, classes);
11053 mReader->loopOnce();
11054 return device;
11055 }
11056
11057 template <class T, typename... Args>
11058 T& addControllerAndConfigure(Args... args) {
11059 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11060
11061 return controller;
11062 }
11063};
11064
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011065const char* PeripheralControllerTest::DEVICE_NAME = "device";
11066const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11067const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11068const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11069const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011070const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11071 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011072const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011073
11074// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011075class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011076protected:
11077 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011078 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011079 }
11080};
11081
11082TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011083 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011084
Harry Cuttsa5b71292022-11-28 12:56:17 +000011085 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11086 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11087 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011088}
11089
11090TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011091 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011092
Harry Cuttsa5b71292022-11-28 12:56:17 +000011093 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11094 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11095 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011096}
11097
11098// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011099class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011100protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011101 void SetUp() override {
11102 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11103 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011104};
11105
Chris Ye85758332021-05-16 23:05:17 -070011106TEST_F(LightControllerTest, MonoLight) {
11107 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011108 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011109 .maxBrightness = 255,
11110 .flags = InputLightClass::BRIGHTNESS,
11111 .path = ""};
11112 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011113
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011114 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011115 InputDeviceInfo info;
11116 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011117 std::vector<InputDeviceLightInfo> lights = info.getLights();
11118 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011119 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11120 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11121
11122 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11123 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11124}
11125
11126TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11127 RawLightInfo infoMono = {.id = 1,
11128 .name = "mono_keyboard_backlight",
11129 .maxBrightness = 255,
11130 .flags = InputLightClass::BRIGHTNESS |
11131 InputLightClass::KEYBOARD_BACKLIGHT,
11132 .path = ""};
11133 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11134
11135 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11136 InputDeviceInfo info;
11137 controller.populateDeviceInfo(&info);
11138 std::vector<InputDeviceLightInfo> lights = info.getLights();
11139 ASSERT_EQ(1U, lights.size());
11140 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11141 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011142
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011143 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11144 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011145}
11146
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011147TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11148 RawLightInfo infoMono = {.id = 1,
11149 .name = "mono_light",
11150 .maxBrightness = 255,
11151 .flags = InputLightClass::BRIGHTNESS,
11152 .path = ""};
11153 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11154 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11155 "0,100,200");
11156
11157 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11158 std::list<NotifyArgs> unused =
11159 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11160 /*changes=*/{});
11161
11162 InputDeviceInfo info;
11163 controller.populateDeviceInfo(&info);
11164 std::vector<InputDeviceLightInfo> lights = info.getLights();
11165 ASSERT_EQ(1U, lights.size());
11166 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11167}
11168
11169TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11170 RawLightInfo infoMono = {.id = 1,
11171 .name = "mono_keyboard_backlight",
11172 .maxBrightness = 255,
11173 .flags = InputLightClass::BRIGHTNESS |
11174 InputLightClass::KEYBOARD_BACKLIGHT,
11175 .path = ""};
11176 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11177
11178 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11179 std::list<NotifyArgs> unused =
11180 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11181 /*changes=*/{});
11182
11183 InputDeviceInfo info;
11184 controller.populateDeviceInfo(&info);
11185 std::vector<InputDeviceLightInfo> lights = info.getLights();
11186 ASSERT_EQ(1U, lights.size());
11187 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11188}
11189
11190TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11191 RawLightInfo infoMono = {.id = 1,
11192 .name = "mono_keyboard_backlight",
11193 .maxBrightness = 255,
11194 .flags = InputLightClass::BRIGHTNESS |
11195 InputLightClass::KEYBOARD_BACKLIGHT,
11196 .path = ""};
11197 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11198 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11199 "0,100,200");
11200
11201 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11202 std::list<NotifyArgs> unused =
11203 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11204 /*changes=*/{});
11205
11206 InputDeviceInfo info;
11207 controller.populateDeviceInfo(&info);
11208 std::vector<InputDeviceLightInfo> lights = info.getLights();
11209 ASSERT_EQ(1U, lights.size());
11210 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
11211 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
11212 ASSERT_EQ(BrightnessLevel(0), *it);
11213 std::advance(it, 1);
11214 ASSERT_EQ(BrightnessLevel(100), *it);
11215 std::advance(it, 1);
11216 ASSERT_EQ(BrightnessLevel(200), *it);
11217}
11218
11219TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
11220 RawLightInfo infoMono = {.id = 1,
11221 .name = "mono_keyboard_backlight",
11222 .maxBrightness = 255,
11223 .flags = InputLightClass::BRIGHTNESS |
11224 InputLightClass::KEYBOARD_BACKLIGHT,
11225 .path = ""};
11226 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11227 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11228 "0,100,200,300,400,500");
11229
11230 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11231 std::list<NotifyArgs> unused =
11232 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11233 /*changes=*/{});
11234
11235 InputDeviceInfo info;
11236 controller.populateDeviceInfo(&info);
11237 std::vector<InputDeviceLightInfo> lights = info.getLights();
11238 ASSERT_EQ(1U, lights.size());
11239 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11240}
11241
Chris Yee2b1e5c2021-03-10 22:45:12 -080011242TEST_F(LightControllerTest, RGBLight) {
11243 RawLightInfo infoRed = {.id = 1,
11244 .name = "red",
11245 .maxBrightness = 255,
11246 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11247 .path = ""};
11248 RawLightInfo infoGreen = {.id = 2,
11249 .name = "green",
11250 .maxBrightness = 255,
11251 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11252 .path = ""};
11253 RawLightInfo infoBlue = {.id = 3,
11254 .name = "blue",
11255 .maxBrightness = 255,
11256 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11257 .path = ""};
11258 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11259 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11260 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11261
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011262 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011263 InputDeviceInfo info;
11264 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011265 std::vector<InputDeviceLightInfo> lights = info.getLights();
11266 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011267 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11268 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11269 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11270
11271 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11272 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11273}
11274
11275TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11276 RawLightInfo infoRed = {.id = 1,
11277 .name = "red_keyboard_backlight",
11278 .maxBrightness = 255,
11279 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11280 InputLightClass::KEYBOARD_BACKLIGHT,
11281 .path = ""};
11282 RawLightInfo infoGreen = {.id = 2,
11283 .name = "green_keyboard_backlight",
11284 .maxBrightness = 255,
11285 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11286 InputLightClass::KEYBOARD_BACKLIGHT,
11287 .path = ""};
11288 RawLightInfo infoBlue = {.id = 3,
11289 .name = "blue_keyboard_backlight",
11290 .maxBrightness = 255,
11291 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
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
11298 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11299 InputDeviceInfo info;
11300 controller.populateDeviceInfo(&info);
11301 std::vector<InputDeviceLightInfo> lights = info.getLights();
11302 ASSERT_EQ(1U, lights.size());
11303 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11304 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11305 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11306
11307 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11308 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11309}
11310
11311TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11312 RawLightInfo infoRed = {.id = 1,
11313 .name = "red",
11314 .maxBrightness = 255,
11315 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11316 .path = ""};
11317 RawLightInfo infoGreen = {.id = 2,
11318 .name = "green",
11319 .maxBrightness = 255,
11320 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11321 .path = ""};
11322 RawLightInfo infoBlue = {.id = 3,
11323 .name = "blue",
11324 .maxBrightness = 255,
11325 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11326 .path = ""};
11327 RawLightInfo infoGlobal = {.id = 3,
11328 .name = "global_keyboard_backlight",
11329 .maxBrightness = 255,
11330 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11331 InputLightClass::KEYBOARD_BACKLIGHT,
11332 .path = ""};
11333 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11334 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11335 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11336 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11337
11338 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11339 InputDeviceInfo info;
11340 controller.populateDeviceInfo(&info);
11341 std::vector<InputDeviceLightInfo> lights = info.getLights();
11342 ASSERT_EQ(1U, lights.size());
11343 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11344 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11345 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011346
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011347 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11348 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011349}
11350
11351TEST_F(LightControllerTest, MultiColorRGBLight) {
11352 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011353 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011354 .maxBrightness = 255,
11355 .flags = InputLightClass::BRIGHTNESS |
11356 InputLightClass::MULTI_INTENSITY |
11357 InputLightClass::MULTI_INDEX,
11358 .path = ""};
11359
11360 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11361
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011362 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011363 InputDeviceInfo info;
11364 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011365 std::vector<InputDeviceLightInfo> lights = info.getLights();
11366 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011367 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11368 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11369 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11370
11371 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11372 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11373}
11374
11375TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11376 RawLightInfo infoColor = {.id = 1,
11377 .name = "multi_color_keyboard_backlight",
11378 .maxBrightness = 255,
11379 .flags = InputLightClass::BRIGHTNESS |
11380 InputLightClass::MULTI_INTENSITY |
11381 InputLightClass::MULTI_INDEX |
11382 InputLightClass::KEYBOARD_BACKLIGHT,
11383 .path = ""};
11384
11385 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11386
11387 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11388 InputDeviceInfo info;
11389 controller.populateDeviceInfo(&info);
11390 std::vector<InputDeviceLightInfo> lights = info.getLights();
11391 ASSERT_EQ(1U, lights.size());
11392 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11393 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11394 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011395
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011396 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11397 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011398}
11399
11400TEST_F(LightControllerTest, PlayerIdLight) {
11401 RawLightInfo info1 = {.id = 1,
11402 .name = "player1",
11403 .maxBrightness = 255,
11404 .flags = InputLightClass::BRIGHTNESS,
11405 .path = ""};
11406 RawLightInfo info2 = {.id = 2,
11407 .name = "player2",
11408 .maxBrightness = 255,
11409 .flags = InputLightClass::BRIGHTNESS,
11410 .path = ""};
11411 RawLightInfo info3 = {.id = 3,
11412 .name = "player3",
11413 .maxBrightness = 255,
11414 .flags = InputLightClass::BRIGHTNESS,
11415 .path = ""};
11416 RawLightInfo info4 = {.id = 4,
11417 .name = "player4",
11418 .maxBrightness = 255,
11419 .flags = InputLightClass::BRIGHTNESS,
11420 .path = ""};
11421 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11422 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11423 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11424 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11425
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011426 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011427 InputDeviceInfo info;
11428 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011429 std::vector<InputDeviceLightInfo> lights = info.getLights();
11430 ASSERT_EQ(1U, lights.size());
11431 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011432 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11433 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011434
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011435 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11436 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11437 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011438}
11439
Michael Wrightd02c5b62014-02-10 15:10:22 -080011440} // namespace android