blob: 8941d16534ce363a2da40534bcf496c3cf09d3b0 [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 Pradhanf1198582023-09-14 22:28:32 +000094static constexpr uint32_t STYLUS_FUSION_SOURCE =
95 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
96
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000097// Minimum timestamp separation between subsequent input events from a Bluetooth device.
98static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
99// Maximum smoothing time delta so that we don't generate events too far into the future.
100constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
101
Michael Wrightd02c5b62014-02-10 15:10:22 -0800102template<typename T>
103static inline T min(T a, T b) {
104 return a < b ? a : b;
105}
106
107static inline float avg(float x, float y) {
108 return (x + y) / 2;
109}
110
Chris Ye3fdbfef2021-01-06 18:45:18 -0800111// Mapping for light color name and the light color
112const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
113 {"green", LightColor::GREEN},
114 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115
Michael Wrighta9cf4192022-12-01 23:46:39 +0000116static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700117 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000118 case ui::ROTATION_90:
119 return ui::ROTATION_270;
120 case ui::ROTATION_270:
121 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700122 default:
123 return orientation;
124 }
125}
126
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800127static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
128 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000129 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800130
131 const InputDeviceInfo::MotionRange* motionRange =
132 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
133 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
134}
135
136static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
137 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000138 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800139
140 const InputDeviceInfo::MotionRange* motionRange =
141 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
142 ASSERT_EQ(nullptr, motionRange);
143}
144
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700145[[maybe_unused]] static void dumpReader(InputReader& reader) {
146 std::string dump;
147 reader.dump(dump);
148 std::istringstream iss(dump);
149 for (std::string line; std::getline(iss, line);) {
150 ALOGE("%s", line.c_str());
151 std::this_thread::sleep_for(std::chrono::milliseconds(1));
152 }
153}
154
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155// --- FakeInputMapper ---
156
157class FakeInputMapper : public InputMapper {
158 uint32_t mSources;
159 int32_t mKeyboardType;
160 int32_t mMetaState;
161 KeyedVector<int32_t, int32_t> mKeyCodeStates;
162 KeyedVector<int32_t, int32_t> mScanCodeStates;
163 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100164 // fake mapping which would normally come from keyCharacterMap
165 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800166 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700168 std::mutex mLock;
169 std::condition_variable mStateChangedCondition;
170 bool mConfigureWasCalled GUARDED_BY(mLock);
171 bool mResetWasCalled GUARDED_BY(mLock);
172 bool mProcessWasCalled GUARDED_BY(mLock);
173 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174
Arthur Hungc23540e2018-11-29 20:42:11 +0800175 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000177 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
178 uint32_t sources)
179 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800180 mSources(sources),
181 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800182 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800183 mConfigureWasCalled(false),
184 mResetWasCalled(false),
185 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186
Chris Yea52ade12020-08-27 16:49:20 -0700187 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
189 void setKeyboardType(int32_t keyboardType) {
190 mKeyboardType = keyboardType;
191 }
192
193 void setMetaState(int32_t metaState) {
194 mMetaState = metaState;
195 }
196
197 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700198 std::unique_lock<std::mutex> lock(mLock);
199 base::ScopedLockAssertion assumeLocked(mLock);
200 const bool configureCalled =
201 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
202 return mConfigureWasCalled;
203 });
204 if (!configureCalled) {
205 FAIL() << "Expected configure() to have been called.";
206 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 mConfigureWasCalled = false;
208 }
209
210 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700211 std::unique_lock<std::mutex> lock(mLock);
212 base::ScopedLockAssertion assumeLocked(mLock);
213 const bool resetCalled =
214 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
215 return mResetWasCalled;
216 });
217 if (!resetCalled) {
218 FAIL() << "Expected reset() to have been called.";
219 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800220 mResetWasCalled = false;
221 }
222
Yi Kong9b14ac62018-07-17 13:48:38 -0700223 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700224 std::unique_lock<std::mutex> lock(mLock);
225 base::ScopedLockAssertion assumeLocked(mLock);
226 const bool processCalled =
227 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
228 return mProcessWasCalled;
229 });
230 if (!processCalled) {
231 FAIL() << "Expected process() to have been called.";
232 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800233 if (outLastEvent) {
234 *outLastEvent = mLastEvent;
235 }
236 mProcessWasCalled = false;
237 }
238
239 void setKeyCodeState(int32_t keyCode, int32_t state) {
240 mKeyCodeStates.replaceValueFor(keyCode, state);
241 }
242
243 void setScanCodeState(int32_t scanCode, int32_t state) {
244 mScanCodeStates.replaceValueFor(scanCode, state);
245 }
246
247 void setSwitchState(int32_t switchCode, int32_t state) {
248 mSwitchStates.replaceValueFor(switchCode, state);
249 }
250
251 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800252 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800253 }
254
Philip Junker4af3b3d2021-12-14 10:36:55 +0100255 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
256 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
257 }
258
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100260 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261
Harry Cuttsd02ea102023-03-17 18:21:30 +0000262 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263 InputMapper::populateDeviceInfo(deviceInfo);
264
265 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000266 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800267 }
268 }
269
Arpit Singhed6c3de2023-04-05 19:24:37 +0000270 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000271 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700272 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800274
275 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800276 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000277 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000278 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800279 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700280
281 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700282 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283 }
284
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700285 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700286 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700288 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700289 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800290 }
291
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700292 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700293 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800294 mLastEvent = *rawEvent;
295 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700296 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700297 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298 }
299
Chris Yea52ade12020-08-27 16:49:20 -0700300 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800301 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
302 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
303 }
304
Philip Junker4af3b3d2021-12-14 10:36:55 +0100305 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
306 auto it = mKeyCodeMapping.find(locationKeyCode);
307 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
308 }
309
Chris Yea52ade12020-08-27 16:49:20 -0700310 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800311 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
312 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
313 }
314
Chris Yea52ade12020-08-27 16:49:20 -0700315 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800316 ssize_t index = mSwitchStates.indexOfKey(switchCode);
317 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
318 }
319
Chris Yea52ade12020-08-27 16:49:20 -0700320 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700321 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700322 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700323 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800324 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
325 if (keyCodes[i] == mSupportedKeyCodes[j]) {
326 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 }
328 }
329 }
Chris Yea52ade12020-08-27 16:49:20 -0700330 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 return result;
332 }
333
334 virtual int32_t getMetaState() {
335 return mMetaState;
336 }
337
338 virtual void fadePointer() {
339 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800340
341 virtual std::optional<int32_t> getAssociatedDisplay() {
342 if (mViewport) {
343 return std::make_optional(mViewport->displayId);
344 }
345 return std::nullopt;
346 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347};
348
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700349// --- InputReaderPolicyTest ---
350class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700351protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700352 sp<FakeInputReaderPolicy> mFakePolicy;
353
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700354 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700355 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700356};
357
358/**
359 * Check that empty set of viewports is an acceptable configuration.
360 * Also try to get internal viewport two different ways - by type and by uniqueId.
361 *
362 * There will be confusion if two viewports with empty uniqueId and identical type are present.
363 * Such configuration is not currently allowed.
364 */
365TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700366 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700367
368 // We didn't add any viewports yet, so there shouldn't be any.
369 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100370 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700371 ASSERT_FALSE(internalViewport);
372
373 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000374 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000375 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700376
377 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700378 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700379 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100380 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700381
382 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100383 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700384 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700385 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700386
387 mFakePolicy->clearViewports();
388 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700389 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700390 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100391 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700392 ASSERT_FALSE(internalViewport);
393}
394
395TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
396 const std::string internalUniqueId = "local:0";
397 const std::string externalUniqueId = "local:1";
398 const std::string virtualUniqueId1 = "virtual:2";
399 const std::string virtualUniqueId2 = "virtual:3";
400 constexpr int32_t virtualDisplayId1 = 2;
401 constexpr int32_t virtualDisplayId2 = 3;
402
403 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000404 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000405 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000406 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700407 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000408 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000409 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000410 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700411 // Add an virtual viewport
412 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000413 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000414 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700415 // Add another virtual viewport
416 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000417 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000418 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700419
420 // Check matching by type for internal
421 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100422 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700423 ASSERT_TRUE(internalViewport);
424 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
425
426 // Check matching by type for external
427 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100428 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700429 ASSERT_TRUE(externalViewport);
430 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
431
432 // Check matching by uniqueId for virtual viewport #1
433 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700434 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700435 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100436 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700437 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
438 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
439
440 // Check matching by uniqueId for virtual viewport #2
441 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700442 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700443 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100444 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700445 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
446 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
447}
448
449
450/**
451 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
452 * that lookup works by checking display id.
453 * Check that 2 viewports of each kind is possible, for all existing viewport types.
454 */
455TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
456 const std::string uniqueId1 = "uniqueId1";
457 const std::string uniqueId2 = "uniqueId2";
458 constexpr int32_t displayId1 = 2;
459 constexpr int32_t displayId2 = 3;
460
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100461 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
462 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700463 for (const ViewportType& type : types) {
464 mFakePolicy->clearViewports();
465 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000466 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000467 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700468 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000469 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000470 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700471
472 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700473 std::optional<DisplayViewport> viewport1 =
474 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700475 ASSERT_TRUE(viewport1);
476 ASSERT_EQ(displayId1, viewport1->displayId);
477 ASSERT_EQ(type, viewport1->type);
478
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700479 std::optional<DisplayViewport> viewport2 =
480 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700481 ASSERT_TRUE(viewport2);
482 ASSERT_EQ(displayId2, viewport2->displayId);
483 ASSERT_EQ(type, viewport2->type);
484
485 // When there are multiple viewports of the same kind, and uniqueId is not specified
486 // in the call to getDisplayViewport, then that situation is not supported.
487 // The viewports can be stored in any order, so we cannot rely on the order, since that
488 // is just implementation detail.
489 // However, we can check that it still returns *a* viewport, we just cannot assert
490 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700491 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700492 ASSERT_TRUE(someViewport);
493 }
494}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800495
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700496/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000497 * When we have multiple internal displays make sure we always return the default display when
498 * querying by type.
499 */
500TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
501 const std::string uniqueId1 = "uniqueId1";
502 const std::string uniqueId2 = "uniqueId2";
503 constexpr int32_t nonDefaultDisplayId = 2;
504 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
505 "Test display ID should not be ADISPLAY_ID_DEFAULT");
506
507 // Add the default display first and ensure it gets returned.
508 mFakePolicy->clearViewports();
509 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000510 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000511 ViewportType::INTERNAL);
512 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000513 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000514 ViewportType::INTERNAL);
515
516 std::optional<DisplayViewport> viewport =
517 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
518 ASSERT_TRUE(viewport);
519 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
520 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
521
522 // Add the default display second to make sure order doesn't matter.
523 mFakePolicy->clearViewports();
524 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000525 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000526 ViewportType::INTERNAL);
527 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000528 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000529 ViewportType::INTERNAL);
530
531 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
532 ASSERT_TRUE(viewport);
533 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
534 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
535}
536
537/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700538 * Check getDisplayViewportByPort
539 */
540TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100541 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700542 const std::string uniqueId1 = "uniqueId1";
543 const std::string uniqueId2 = "uniqueId2";
544 constexpr int32_t displayId1 = 1;
545 constexpr int32_t displayId2 = 2;
546 const uint8_t hdmi1 = 0;
547 const uint8_t hdmi2 = 1;
548 const uint8_t hdmi3 = 2;
549
550 mFakePolicy->clearViewports();
551 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000552 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000553 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700554 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000555 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000556 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700557
558 // Check that correct display viewport was returned by comparing the display ports.
559 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
560 ASSERT_TRUE(hdmi1Viewport);
561 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
562 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
563
564 // Check that we can still get the same viewport using the uniqueId
565 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
566 ASSERT_TRUE(hdmi1Viewport);
567 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
568 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
569 ASSERT_EQ(type, hdmi1Viewport->type);
570
571 // Check that we cannot find a port with "HDMI2", because we never added one
572 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
573 ASSERT_FALSE(hdmi2Viewport);
574}
575
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576// --- InputReaderTest ---
577
578class InputReaderTest : public testing::Test {
579protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700580 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700582 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000583 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800584
Chris Yea52ade12020-08-27 16:49:20 -0700585 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700586 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700587 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700588 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589
Prabir Pradhan28efc192019-11-05 01:10:04 +0000590 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700591 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592 }
593
Chris Yea52ade12020-08-27 16:49:20 -0700594 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700595 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597 }
598
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700599 void addDevice(int32_t eventHubId, const std::string& name,
600 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800601 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602
603 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800604 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605 }
606 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000607 mReader->loopOnce();
608 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700609 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700611 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 }
613
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800614 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700615 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000616 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700617 }
618
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800619 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700620 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000621 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700622 }
623
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800624 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700625 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700626 ftl::Flags<InputDeviceClass> classes,
627 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800628 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800629 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000630 FakeInputMapper& mapper =
631 device->addMapper<FakeInputMapper>(eventHubId,
632 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800633 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800634 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635 return mapper;
636 }
637};
638
Chris Ye98d3f532020-10-01 21:48:59 -0700639TEST_F(InputReaderTest, PolicyGetInputDevices) {
640 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700641 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700642 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643
644 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700645 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800646 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800647 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100648 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
650 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000651 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800652}
653
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000654TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
655 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
656 mFakeEventHub->setSysfsRootPath(1, "xyz");
657
658 // Should also have received a notification describing the new input device.
659 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
660 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
661 ASSERT_EQ(0U, inputDevice.getLights().size());
662
663 RawLightInfo infoMonolight = {.id = 123,
664 .name = "mono_keyboard_backlight",
665 .maxBrightness = 255,
666 .flags = InputLightClass::BRIGHTNESS,
667 .path = ""};
668 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
669 mReader->sysfsNodeChanged("xyz");
670 mReader->loopOnce();
671
672 // Should also have received a notification describing the new recreated input device.
673 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
674 inputDevice = mFakePolicy->getInputDevices()[0];
675 ASSERT_EQ(1U, inputDevice.getLights().size());
676}
677
Chris Yee7310032020-09-22 15:36:28 -0700678TEST_F(InputReaderTest, GetMergedInputDevices) {
679 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
680 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
681 // Add two subdevices to device
682 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
683 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000684 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
685 AINPUT_SOURCE_KEYBOARD);
686 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
687 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700688
689 // Push same device instance for next device to be added, so they'll have same identifier.
690 mReader->pushNextDevice(device);
691 mReader->pushNextDevice(device);
692 ASSERT_NO_FATAL_FAILURE(
693 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
694 ASSERT_NO_FATAL_FAILURE(
695 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
696
697 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000698 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700699}
700
Chris Yee14523a2020-12-19 13:46:00 -0800701TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
702 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
703 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
704 // Add two subdevices to device
705 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
706 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000707 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
708 AINPUT_SOURCE_KEYBOARD);
709 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
710 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800711
712 // Push same device instance for next device to be added, so they'll have same identifier.
713 mReader->pushNextDevice(device);
714 mReader->pushNextDevice(device);
715 // Sensor device is initially disabled
716 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
717 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
718 nullptr));
719 // Device is disabled because the only sub device is a sensor device and disabled initially.
720 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
721 ASSERT_FALSE(device->isEnabled());
722 ASSERT_NO_FATAL_FAILURE(
723 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
724 // The merged device is enabled if any sub device is enabled
725 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
726 ASSERT_TRUE(device->isEnabled());
727}
728
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700729TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800730 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700731 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800732 constexpr int32_t eventHubId = 1;
733 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700734 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000735 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
736 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800737 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800738 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700739
Yi Kong9b14ac62018-07-17 13:48:38 -0700740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700741
742 NotifyDeviceResetArgs resetArgs;
743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700744 ASSERT_EQ(deviceId, resetArgs.deviceId);
745
746 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800747 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000748 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700749
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700751 ASSERT_EQ(deviceId, resetArgs.deviceId);
752 ASSERT_EQ(device->isEnabled(), false);
753
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800754 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000755 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700758 ASSERT_EQ(device->isEnabled(), false);
759
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800760 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000761 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700763 ASSERT_EQ(deviceId, resetArgs.deviceId);
764 ASSERT_EQ(device->isEnabled(), true);
765}
766
Michael Wrightd02c5b62014-02-10 15:10:22 -0800767TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800768 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700769 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800770 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800771 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800772 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800773 AINPUT_SOURCE_KEYBOARD, nullptr);
774 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800775
776 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
777 AINPUT_SOURCE_ANY, AKEYCODE_A))
778 << "Should return unknown when the device id is >= 0 but unknown.";
779
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800780 ASSERT_EQ(AKEY_STATE_UNKNOWN,
781 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
782 << "Should return unknown when the device id is valid but the sources are not "
783 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800784
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800785 ASSERT_EQ(AKEY_STATE_DOWN,
786 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
787 AKEYCODE_A))
788 << "Should return value provided by mapper when device id is valid and the device "
789 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790
791 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
792 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
793 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
794
795 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
796 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
797 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
798}
799
Philip Junker4af3b3d2021-12-14 10:36:55 +0100800TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
801 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
802 constexpr int32_t eventHubId = 1;
803 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
804 InputDeviceClass::KEYBOARD,
805 AINPUT_SOURCE_KEYBOARD, nullptr);
806 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
807
808 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
809 << "Should return unknown when the device with the specified id is not found.";
810
811 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
812 << "Should return correct mapping when device id is valid and mapping exists.";
813
814 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
815 << "Should return the location key code when device id is valid and there's no "
816 "mapping.";
817}
818
819TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
820 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
821 constexpr int32_t eventHubId = 1;
822 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
823 InputDeviceClass::JOYSTICK,
824 AINPUT_SOURCE_GAMEPAD, nullptr);
825 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
826
827 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
828 << "Should return unknown when the device id is valid but there is no keyboard mapper";
829}
830
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800832 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700833 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800834 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800835 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800836 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800837 AINPUT_SOURCE_KEYBOARD, nullptr);
838 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839
840 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
841 AINPUT_SOURCE_ANY, KEY_A))
842 << "Should return unknown when the device id is >= 0 but unknown.";
843
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800844 ASSERT_EQ(AKEY_STATE_UNKNOWN,
845 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
846 << "Should return unknown when the device id is valid but the sources are not "
847 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800849 ASSERT_EQ(AKEY_STATE_DOWN,
850 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
851 KEY_A))
852 << "Should return value provided by mapper when device id is valid and the device "
853 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854
855 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
856 AINPUT_SOURCE_TRACKBALL, KEY_A))
857 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
858
859 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
860 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
861 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
862}
863
864TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800865 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700866 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800867 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800868 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800869 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800870 AINPUT_SOURCE_KEYBOARD, nullptr);
871 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800872
873 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
874 AINPUT_SOURCE_ANY, SW_LID))
875 << "Should return unknown when the device id is >= 0 but unknown.";
876
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800877 ASSERT_EQ(AKEY_STATE_UNKNOWN,
878 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
879 << "Should return unknown when the device id is valid but the sources are not "
880 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800881
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800882 ASSERT_EQ(AKEY_STATE_DOWN,
883 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
884 SW_LID))
885 << "Should return value provided by mapper when device id is valid and the device "
886 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887
888 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
889 AINPUT_SOURCE_TRACKBALL, SW_LID))
890 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
891
892 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
893 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
894 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
895}
896
897TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800898 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700899 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800900 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800901 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800902 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800903 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800905 mapper.addSupportedKeyCode(AKEYCODE_A);
906 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700908 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 uint8_t flags[4] = { 0, 0, 0, 1 };
910
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700911 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912 << "Should return false when device id is >= 0 but unknown.";
913 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
914
915 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700916 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800917 << "Should return false when device id is valid but the sources are not supported by "
918 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800919 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
920
921 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700922 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800923 keyCodes, flags))
924 << "Should return value provided by mapper when device id is valid and the device "
925 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
927
928 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700929 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
930 << "Should return false when the device id is < 0 but the sources are not supported by "
931 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
933
934 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700935 ASSERT_TRUE(
936 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
937 << "Should return value provided by mapper when device id is < 0 and one of the "
938 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
940}
941
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000942TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800943 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700944 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945
946 NotifyConfigurationChangedArgs args;
947
948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
949 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
950}
951
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000952TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800953 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700954 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000955 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800956 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000957 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800958 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800959 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800960 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000962 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000963 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800964 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
965
966 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800967 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000968 ASSERT_EQ(when, event.when);
969 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800970 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800971 ASSERT_EQ(EV_KEY, event.type);
972 ASSERT_EQ(KEY_A, event.code);
973 ASSERT_EQ(1, event.value);
974}
975
Garfield Tan1c7bc862020-01-28 13:24:04 -0800976TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800977 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700978 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800979 constexpr int32_t eventHubId = 1;
980 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800981 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000982 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
983 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800984 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800985 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800986
987 NotifyDeviceResetArgs resetArgs;
988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800989 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800991 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000992 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800994 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800995 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800996
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800997 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000998 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001000 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001001 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001002
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001003 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001004 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001006 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001007 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001008}
1009
Garfield Tan1c7bc862020-01-28 13:24:04 -08001010TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1011 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001012 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001013 constexpr int32_t eventHubId = 1;
1014 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1015 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001016 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1017 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001018 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001019 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1020
1021 NotifyDeviceResetArgs resetArgs;
1022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1023 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1024}
1025
Arthur Hungc23540e2018-11-29 20:42:11 +08001026TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001027 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001028 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001029 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001030 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001031 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1032 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001033 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1034 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001035 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001036
1037 const uint8_t hdmi1 = 1;
1038
1039 // Associated touch screen with second display.
1040 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1041
1042 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001043 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001044 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001045 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001046 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001047 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001048 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001049 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001050 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001051
1052 // Add the device, and make sure all of the callbacks are triggered.
1053 // The device is added after the input port associations are processed since
1054 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001055 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001058 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001059
Arthur Hung2c9a3342019-07-23 14:18:59 +08001060 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001061 ASSERT_EQ(deviceId, device->getId());
1062 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1063 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001064
1065 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001066 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001067 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001068 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001069}
1070
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001071TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1072 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001073 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001074 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1075 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1076 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001077 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1078 AINPUT_SOURCE_KEYBOARD);
1079 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1080 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001081 mReader->pushNextDevice(device);
1082 mReader->pushNextDevice(device);
1083 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1084 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1085
1086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1087
1088 NotifyDeviceResetArgs resetArgs;
1089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1090 ASSERT_EQ(deviceId, resetArgs.deviceId);
1091 ASSERT_TRUE(device->isEnabled());
1092 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1093 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1094
1095 disableDevice(deviceId);
1096 mReader->loopOnce();
1097
1098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1099 ASSERT_EQ(deviceId, resetArgs.deviceId);
1100 ASSERT_FALSE(device->isEnabled());
1101 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1102 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1103
1104 enableDevice(deviceId);
1105 mReader->loopOnce();
1106
1107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1108 ASSERT_EQ(deviceId, resetArgs.deviceId);
1109 ASSERT_TRUE(device->isEnabled());
1110 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1111 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1112}
1113
1114TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1115 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001116 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001117 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1118 // Add two subdevices to device
1119 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1120 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001121 device->addMapper<FakeInputMapper>(eventHubIds[0],
1122 mFakePolicy->getReaderConfiguration(),
1123 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001124 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001125 device->addMapper<FakeInputMapper>(eventHubIds[1],
1126 mFakePolicy->getReaderConfiguration(),
1127 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001128 mReader->pushNextDevice(device);
1129 mReader->pushNextDevice(device);
1130 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1131 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1132
1133 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1134 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1135
1136 ASSERT_EQ(AKEY_STATE_DOWN,
1137 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1138 ASSERT_EQ(AKEY_STATE_DOWN,
1139 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1140 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1141 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1142}
1143
Prabir Pradhan7e186182020-11-10 13:56:45 -08001144TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1145 NotifyPointerCaptureChangedArgs args;
1146
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001147 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001148 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001149 mReader->loopOnce();
1150 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001151 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1152 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001153
1154 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001155 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001156 mReader->loopOnce();
1157 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001158 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001159
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001160 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001161 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001162 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001163 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001164 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001165}
1166
Chris Ye87143712020-11-10 05:05:58 +00001167class FakeVibratorInputMapper : public FakeInputMapper {
1168public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001169 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1170 const InputReaderConfiguration& readerConfig, uint32_t sources)
1171 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001172
1173 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1174};
1175
1176TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1177 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001178 ftl::Flags<InputDeviceClass> deviceClass =
1179 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001180 constexpr int32_t eventHubId = 1;
1181 const char* DEVICE_LOCATION = "BLUETOOTH";
1182 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1183 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001184 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1185 mFakePolicy->getReaderConfiguration(),
1186 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001187 mReader->pushNextDevice(device);
1188
1189 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1190 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1191
1192 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1193 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1194}
1195
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001196// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001197
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001198class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001199public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001200 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001201
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001202 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001203
Andy Chenf9f1a022022-08-29 20:07:10 -04001204 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1205
Chris Yee2b1e5c2021-03-10 22:45:12 -08001206 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1207
1208 void dump(std::string& dump) override {}
1209
1210 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1211 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001212 }
1213
Chris Yee2b1e5c2021-03-10 22:45:12 -08001214 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1215 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001216 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001217
1218 bool setLightColor(int32_t lightId, int32_t color) override {
1219 getDeviceContext().setLightBrightness(lightId, color >> 24);
1220 return true;
1221 }
1222
1223 std::optional<int32_t> getLightColor(int32_t lightId) override {
1224 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1225 if (!result.has_value()) {
1226 return std::nullopt;
1227 }
1228 return result.value() << 24;
1229 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001230
1231 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1232
1233 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1234
1235private:
1236 InputDeviceContext& mDeviceContext;
1237 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1238 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001239 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001240};
1241
Chris Yee2b1e5c2021-03-10 22:45:12 -08001242TEST_F(InputReaderTest, BatteryGetCapacity) {
1243 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001244 ftl::Flags<InputDeviceClass> deviceClass =
1245 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001246 constexpr int32_t eventHubId = 1;
1247 const char* DEVICE_LOCATION = "BLUETOOTH";
1248 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001249 FakePeripheralController& controller =
1250 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001251 mReader->pushNextDevice(device);
1252
1253 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1254
Harry Cuttsa5b71292022-11-28 12:56:17 +00001255 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1256 FakeEventHub::BATTERY_CAPACITY);
1257 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001258}
1259
1260TEST_F(InputReaderTest, BatteryGetStatus) {
1261 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001262 ftl::Flags<InputDeviceClass> deviceClass =
1263 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001264 constexpr int32_t eventHubId = 1;
1265 const char* DEVICE_LOCATION = "BLUETOOTH";
1266 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001267 FakePeripheralController& controller =
1268 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001269 mReader->pushNextDevice(device);
1270
1271 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1272
Harry Cuttsa5b71292022-11-28 12:56:17 +00001273 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1274 FakeEventHub::BATTERY_STATUS);
1275 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001276}
1277
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001278TEST_F(InputReaderTest, BatteryGetDevicePath) {
1279 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1280 ftl::Flags<InputDeviceClass> deviceClass =
1281 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1282 constexpr int32_t eventHubId = 1;
1283 const char* DEVICE_LOCATION = "BLUETOOTH";
1284 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1285 device->addController<FakePeripheralController>(eventHubId);
1286 mReader->pushNextDevice(device);
1287
1288 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1289
Harry Cuttsa5b71292022-11-28 12:56:17 +00001290 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001291}
1292
Chris Ye3fdbfef2021-01-06 18:45:18 -08001293TEST_F(InputReaderTest, LightGetColor) {
1294 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001295 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001296 constexpr int32_t eventHubId = 1;
1297 const char* DEVICE_LOCATION = "BLUETOOTH";
1298 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001299 FakePeripheralController& controller =
1300 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001301 mReader->pushNextDevice(device);
1302 RawLightInfo info = {.id = 1,
1303 .name = "Mono",
1304 .maxBrightness = 255,
1305 .flags = InputLightClass::BRIGHTNESS,
1306 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001307 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1308 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001309
1310 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001311
Harry Cutts33476232023-01-30 19:57:29 +00001312 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1313 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1314 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1315 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001316}
1317
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001318// --- InputReaderIntegrationTest ---
1319
1320// These tests create and interact with the InputReader only through its interface.
1321// The InputReader is started during SetUp(), which starts its processing in its own
1322// thread. The tests use linux uinput to emulate input devices.
1323// NOTE: Interacting with the physical device while these tests are running may cause
1324// the tests to fail.
1325class InputReaderIntegrationTest : public testing::Test {
1326protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001327 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001328 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001329 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001330
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001331 std::shared_ptr<FakePointerController> mFakePointerController;
1332
Chris Yea52ade12020-08-27 16:49:20 -07001333 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001334#if !defined(__ANDROID__)
1335 GTEST_SKIP();
1336#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001337 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001338 mFakePointerController = std::make_shared<FakePointerController>();
1339 mFakePolicy->setPointerController(mFakePointerController);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001340
Arpit Singha11fc942023-08-09 09:23:43 +00001341 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001342 }
1343
Chris Yea52ade12020-08-27 16:49:20 -07001344 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001345#if !defined(__ANDROID__)
1346 return;
1347#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001348 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001349 mReader.reset();
1350 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001351 mFakePolicy.clear();
1352 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001353
1354 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1355 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1356 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1357 [&name](const InputDeviceInfo& info) {
1358 return info.getIdentifier().name == name;
1359 });
1360 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1361 }
Arpit Singha11fc942023-08-09 09:23:43 +00001362
1363 void setupInputReader() {
1364 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1365 /*eventDidNotHappenTimeout=*/30ms);
1366
1367 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1368 *mTestListener);
1369 ASSERT_EQ(mReader->start(), OK);
1370
1371 // Since this test is run on a real device, all the input devices connected
1372 // to the test device will show up in mReader. We wait for those input devices to
1373 // show up before beginning the tests.
1374 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1375 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1376 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1377 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001378};
1379
1380TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1381 // An invalid input device that is only used for this test.
1382 class InvalidUinputDevice : public UinputDevice {
1383 public:
Harry Cutts33476232023-01-30 19:57:29 +00001384 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001385
1386 private:
1387 void configureDevice(int fd, uinput_user_dev* device) override {}
1388 };
1389
1390 const size_t numDevices = mFakePolicy->getInputDevices().size();
1391
1392 // UinputDevice does not set any event or key bits, so InputReader should not
1393 // consider it as a valid device.
1394 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1395 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1396 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1397 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1398
1399 invalidDevice.reset();
1400 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1401 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1402 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1403}
1404
1405TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1406 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1407
1408 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1409 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1410 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1411 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1412
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001413 const auto device = findDeviceByName(keyboard->getName());
1414 ASSERT_TRUE(device.has_value());
1415 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1416 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1417 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001418
1419 keyboard.reset();
1420 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1421 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1422 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1423}
1424
1425TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1426 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1427 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1428
1429 NotifyConfigurationChangedArgs configChangedArgs;
1430 ASSERT_NO_FATAL_FAILURE(
1431 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001432 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001433 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1434
1435 NotifyKeyArgs keyArgs;
1436 keyboard->pressAndReleaseHomeKey();
1437 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1438 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001439 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001440 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001441 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001442 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001443 prevTimestamp = keyArgs.eventTime;
1444
1445 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1446 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001447 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001448 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001449 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001450}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001451
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001452TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1453 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1454 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1455
1456 const auto device = findDeviceByName(stylus->getName());
1457 ASSERT_TRUE(device.has_value());
1458
Prabir Pradhana3621852022-10-14 18:57:23 +00001459 // An external stylus with buttons should also be recognized as a keyboard.
1460 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001461 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1462 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1463
1464 const auto DOWN =
1465 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1466 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1467
1468 stylus->pressAndReleaseKey(BTN_STYLUS);
1469 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1470 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1471 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1472 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1473
1474 stylus->pressAndReleaseKey(BTN_STYLUS2);
1475 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1476 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1477 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1478 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1479
1480 stylus->pressAndReleaseKey(BTN_STYLUS3);
1481 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1482 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1483 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1484 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1485}
1486
Prabir Pradhan05f6c602023-08-18 20:02:01 +00001487TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1488 std::unique_ptr<UinputKeyboard> keyboard =
1489 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1490 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1491 KEY_R, KEY_T, KEY_Y,
1492 BTN_STYLUS, BTN_STYLUS2,
1493 BTN_STYLUS3});
1494 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1495
1496 const auto device = findDeviceByName(keyboard->getName());
1497 ASSERT_TRUE(device.has_value());
1498
1499 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1500 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1501 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1502 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1503}
1504
Prabir Pradhanada1a2a2023-08-22 23:20:16 +00001505TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1506 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1507 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1508 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1509 createUinputDevice<UinputKeyboardWithHidUsage>(
1510 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1511 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1512
1513 const auto device = findDeviceByName(keyboard->getName());
1514 ASSERT_TRUE(device.has_value());
1515
1516 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1517 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1518
1519 // If a device supports reporting HID usage codes, it shouldn't automatically support
1520 // stylus keys.
1521 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1522 uint8_t outFlags[] = {0};
1523 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1524 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1525}
1526
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001527/**
1528 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1529 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1530 * are passed to the listener.
1531 */
1532static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1533TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1534 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1535 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1536 NotifyKeyArgs keyArgs;
1537
1538 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1539 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1540 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1541 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1542
1543 controller->pressAndReleaseKey(BTN_GEAR_UP);
1544 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1545 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1546 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1547}
1548
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001549// --- TouchIntegrationTest ---
1550
Arpit Singha11fc942023-08-09 09:23:43 +00001551class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001552protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001553 const std::string UNIQUE_ID = "local:0";
1554
Chris Yea52ade12020-08-27 16:49:20 -07001555 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001556#if !defined(__ANDROID__)
1557 GTEST_SKIP();
1558#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001559 InputReaderIntegrationTest::SetUp();
1560 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001561 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1562 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001563
1564 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1565 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1566 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001567 const auto info = findDeviceByName(mDevice->getName());
1568 ASSERT_TRUE(info);
1569 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001570 }
1571
1572 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001573 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001574 std::optional<uint8_t> physicalPort,
1575 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001576 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001577 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001578 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001579 }
1580
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001581 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1582 NotifyMotionArgs args;
1583 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1584 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001585 ASSERT_EQ(points.size(), args.getPointerCount());
1586 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001587 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1588 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1589 }
1590 }
1591
Arthur Hungaab25622020-01-16 11:22:11 +08001592 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001593 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001594};
1595
Arpit Singha11fc942023-08-09 09:23:43 +00001596enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1597
1598class TouchIntegrationTest : public BaseTouchIntegrationTest,
1599 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1600protected:
1601 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1602 const std::string INPUT_PORT = "uinput_touch/input0";
1603
1604 void SetUp() override {
1605#if !defined(__ANDROID__)
1606 GTEST_SKIP();
1607#endif
1608 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1609 BaseTouchIntegrationTest::SetUp();
1610 return;
1611 }
1612
1613 // setup policy with a input-port or UniqueId association to the display
1614 bool isInputPortAssociation =
1615 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1616
1617 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1618 if (isInputPortAssociation) {
1619 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1620 } else {
1621 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1622 }
1623 mFakePointerController = std::make_shared<FakePointerController>();
1624 mFakePolicy->setPointerController(mFakePointerController);
1625
1626 InputReaderIntegrationTest::setupInputReader();
1627
1628 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1629 INPUT_PORT);
1630 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1631
1632 // Add a display linked to a physical port or UniqueId.
1633 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1634 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1635 ViewportType::INTERNAL);
1636 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1637 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1638 const auto info = findDeviceByName(mDevice->getName());
1639 ASSERT_TRUE(info);
1640 mDeviceInfo = *info;
1641 }
1642};
1643
1644TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001645 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1646 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1647 // presses).
1648 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1649 mDeviceInfo.getSources());
1650}
1651
Arpit Singha11fc942023-08-09 09:23:43 +00001652TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001653 NotifyMotionArgs args;
1654 const Point centerPoint = mDevice->getCenterPoint();
1655
1656 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001657 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001658 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001659 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001660 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1661 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1662
1663 // ACTION_MOVE
1664 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001665 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001666 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1668
1669 // ACTION_UP
1670 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001671 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001672 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1673 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1674}
1675
Arpit Singha11fc942023-08-09 09:23:43 +00001676TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001677 NotifyMotionArgs args;
1678 const Point centerPoint = mDevice->getCenterPoint();
1679
1680 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001681 mDevice->sendSlot(FIRST_SLOT);
1682 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001683 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001684 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001685 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1686 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1687
1688 // ACTION_POINTER_DOWN (Second slot)
1689 const Point secondPoint = centerPoint + Point(100, 100);
1690 mDevice->sendSlot(SECOND_SLOT);
1691 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001692 mDevice->sendDown(secondPoint);
1693 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001694 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001695 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001696
1697 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001698 mDevice->sendMove(secondPoint + Point(1, 1));
1699 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001700 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1702
1703 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001704 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001705 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001706 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001707 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001708
1709 // ACTION_UP
1710 mDevice->sendSlot(FIRST_SLOT);
1711 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001712 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001713 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1714 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1715}
1716
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001717/**
1718 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1719 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1720 * data?
1721 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1722 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1723 * for Pointer 0 only is generated after.
1724 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1725 * events, we will not miss any information.
1726 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1727 * event generated afterwards that contains the newest movement of pointer 0.
1728 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1729 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1730 * losing information about non-palm pointers.
1731 */
Arpit Singha11fc942023-08-09 09:23:43 +00001732TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001733 NotifyMotionArgs args;
1734 const Point centerPoint = mDevice->getCenterPoint();
1735
1736 // ACTION_DOWN
1737 mDevice->sendSlot(FIRST_SLOT);
1738 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1739 mDevice->sendDown(centerPoint);
1740 mDevice->sendSync();
1741 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1742
1743 // ACTION_POINTER_DOWN (Second slot)
1744 const Point secondPoint = centerPoint + Point(100, 100);
1745 mDevice->sendSlot(SECOND_SLOT);
1746 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1747 mDevice->sendDown(secondPoint);
1748 mDevice->sendSync();
1749 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1750
1751 // ACTION_MOVE (First slot)
1752 mDevice->sendSlot(FIRST_SLOT);
1753 mDevice->sendMove(centerPoint + Point(5, 5));
1754 // ACTION_POINTER_UP (Second slot)
1755 mDevice->sendSlot(SECOND_SLOT);
1756 mDevice->sendPointerUp();
1757 // Send a single sync for the above 2 pointer updates
1758 mDevice->sendSync();
1759
1760 // First, we should get POINTER_UP for the second pointer
1761 assertReceivedMotion(ACTION_POINTER_1_UP,
1762 {/*first pointer */ centerPoint + Point(5, 5),
1763 /*second pointer*/ secondPoint});
1764
1765 // Next, the MOVE event for the first pointer
1766 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1767}
1768
1769/**
1770 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1771 * move, and then it will go up, all in the same frame.
1772 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1773 * gets sent to the listener.
1774 */
Arpit Singha11fc942023-08-09 09:23:43 +00001775TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001776 NotifyMotionArgs args;
1777 const Point centerPoint = mDevice->getCenterPoint();
1778
1779 // ACTION_DOWN
1780 mDevice->sendSlot(FIRST_SLOT);
1781 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1782 mDevice->sendDown(centerPoint);
1783 mDevice->sendSync();
1784 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1785
1786 // ACTION_POINTER_DOWN (Second slot)
1787 const Point secondPoint = centerPoint + Point(100, 100);
1788 mDevice->sendSlot(SECOND_SLOT);
1789 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1790 mDevice->sendDown(secondPoint);
1791 mDevice->sendSync();
1792 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1793
1794 // ACTION_MOVE (First slot)
1795 mDevice->sendSlot(FIRST_SLOT);
1796 mDevice->sendMove(centerPoint + Point(5, 5));
1797 // ACTION_POINTER_UP (Second slot)
1798 mDevice->sendSlot(SECOND_SLOT);
1799 mDevice->sendMove(secondPoint + Point(6, 6));
1800 mDevice->sendPointerUp();
1801 // Send a single sync for the above 2 pointer updates
1802 mDevice->sendSync();
1803
1804 // First, we should get POINTER_UP for the second pointer
1805 // The movement of the second pointer during the liftoff frame is ignored.
1806 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1807 assertReceivedMotion(ACTION_POINTER_1_UP,
1808 {/*first pointer */ centerPoint + Point(5, 5),
1809 /*second pointer*/ secondPoint});
1810
1811 // Next, the MOVE event for the first pointer
1812 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1813}
1814
Arpit Singha11fc942023-08-09 09:23:43 +00001815TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001816 NotifyMotionArgs args;
1817 const Point centerPoint = mDevice->getCenterPoint();
1818
1819 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001820 mDevice->sendSlot(FIRST_SLOT);
1821 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001822 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001823 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001824 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1825 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1826
arthurhungcc7f9802020-04-30 17:55:40 +08001827 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001828 const Point secondPoint = centerPoint + Point(100, 100);
1829 mDevice->sendSlot(SECOND_SLOT);
1830 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1831 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001832 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001833 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001834 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001835
arthurhungcc7f9802020-04-30 17:55:40 +08001836 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001837 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001838 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001839 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1841
arthurhungcc7f9802020-04-30 17:55:40 +08001842 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1843 // a palm event.
1844 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001845 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001846 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001847 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001848 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001849 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001850
arthurhungcc7f9802020-04-30 17:55:40 +08001851 // Send up to second slot, expect first slot send moving.
1852 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001853 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001854 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1855 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001856
arthurhungcc7f9802020-04-30 17:55:40 +08001857 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001858 mDevice->sendSlot(FIRST_SLOT);
1859 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001860 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001861
arthurhungcc7f9802020-04-30 17:55:40 +08001862 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1863 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001864}
1865
Arpit Singha11fc942023-08-09 09:23:43 +00001866TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001867 const Point centerPoint = mDevice->getCenterPoint();
1868
1869 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1870 mDevice->sendSlot(FIRST_SLOT);
1871 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1872 mDevice->sendToolType(MT_TOOL_PEN);
1873 mDevice->sendDown(centerPoint);
1874 mDevice->sendSync();
1875 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1876 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001877 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001878
1879 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1880
1881 // Release the stylus touch.
1882 mDevice->sendUp();
1883 mDevice->sendSync();
1884 ASSERT_NO_FATAL_FAILURE(
1885 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1886
1887 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1888
1889 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1890 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1891 mDevice->sendToolType(MT_TOOL_FINGER);
1892 mDevice->sendDown(centerPoint);
1893 mDevice->sendSync();
1894 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1895 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001896 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001897
1898 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1899
1900 mDevice->sendUp();
1901 mDevice->sendSync();
1902 ASSERT_NO_FATAL_FAILURE(
1903 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1904
1905 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1906 // The policy should be notified of the stylus presence.
1907 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1908 mDevice->sendToolType(MT_TOOL_PEN);
1909 mDevice->sendMove(centerPoint);
1910 mDevice->sendSync();
1911 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1912 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001913 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001914
1915 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1916}
1917
Arpit Singha11fc942023-08-09 09:23:43 +00001918INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
1919 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
1920 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
1921 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
1922
Prabir Pradhan124ea442022-10-28 20:27:44 +00001923// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001924
Prabir Pradhan124ea442022-10-28 20:27:44 +00001925// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1926// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1927// stylus.
1928template <typename UinputStylusDevice>
Arpit Singha11fc942023-08-09 09:23:43 +00001929class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001930protected:
1931 void SetUp() override {
1932#if !defined(__ANDROID__)
1933 GTEST_SKIP();
1934#endif
Arpit Singha11fc942023-08-09 09:23:43 +00001935 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00001936 mTouchscreen = mDevice.get();
1937 mTouchscreenInfo = mDeviceInfo;
1938
1939 setUpStylusDevice();
1940 }
1941
1942 UinputStylusDevice* mStylus{nullptr};
1943 InputDeviceInfo mStylusInfo{};
1944
1945 UinputTouchScreen* mTouchscreen{nullptr};
1946 InputDeviceInfo mTouchscreenInfo{};
1947
1948private:
1949 // When we are attempting to test stylus button events that are sent from the touchscreen,
1950 // use the same Uinput device for the touchscreen and the stylus.
1951 template <typename T = UinputStylusDevice>
1952 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1953 mStylus = mDevice.get();
1954 mStylusInfo = mDeviceInfo;
1955 }
1956
1957 // When we are attempting to stylus buttons from an external stylus being merged with touches
1958 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1959 template <typename T = UinputStylusDevice>
1960 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1961 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1962 mStylus = mStylusDeviceLifecycleTracker.get();
1963 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1964 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1965 const auto info = findDeviceByName(mStylus->getName());
1966 ASSERT_TRUE(info);
1967 mStylusInfo = *info;
1968 }
1969
1970 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1971
1972 // Hide the base class's device to expose it with a different name for readability.
Arpit Singha11fc942023-08-09 09:23:43 +00001973 using BaseTouchIntegrationTest::mDevice;
1974 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00001975};
1976
1977using StylusButtonIntegrationTestTypes =
1978 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1979TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1980
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001981TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001982 const auto stylusId = TestFixture::mStylusInfo.getId();
1983
1984 TestFixture::mStylus->pressKey(BTN_STYLUS);
1985 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1986 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1987 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1988
1989 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1990 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001991 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001992 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001993}
1994
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001995TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001996 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1997 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1998 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001999
2000 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002001 TestFixture::mStylus->pressKey(BTN_STYLUS);
2002 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002003 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002004 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002005
2006 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002007 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2008 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2009 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2010 TestFixture::mTouchscreen->sendDown(centerPoint);
2011 TestFixture::mTouchscreen->sendSync();
2012 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002013 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002014 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002015 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2016 WithDeviceId(touchscreenId))));
2017 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002018 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002019 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002020 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2021 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002022
Prabir Pradhan124ea442022-10-28 20:27:44 +00002023 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2024 TestFixture::mTouchscreen->sendSync();
2025 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002026 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002027 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002028 WithDeviceId(touchscreenId))));
2029 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002030 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002031 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002032 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002033
2034 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002035 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2036 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002037 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002038 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002039}
2040
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002041TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002042 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2043 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2044 const auto stylusId = TestFixture::mStylusInfo.getId();
2045 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002046 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002047
2048 // Press the stylus button.
2049 TestFixture::mStylus->pressKey(BTN_STYLUS);
2050 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2051 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2052 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2053
2054 // Start hovering with the stylus.
2055 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2056 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2057 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2058 TestFixture::mTouchscreen->sendMove(centerPoint);
2059 TestFixture::mTouchscreen->sendSync();
2060 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2061 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2062 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2063 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2064 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2065 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2066 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2067 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2068 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2069
2070 // Touch down with the stylus.
2071 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2072 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2073 TestFixture::mTouchscreen->sendDown(centerPoint);
2074 TestFixture::mTouchscreen->sendSync();
2075 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2076 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2077 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2078
2079 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2080 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2081 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2082
2083 // Stop touching with the stylus, and start hovering.
2084 TestFixture::mTouchscreen->sendUp();
2085 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2086 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2087 TestFixture::mTouchscreen->sendMove(centerPoint);
2088 TestFixture::mTouchscreen->sendSync();
2089 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2090 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2091 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2092 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2093 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2094 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2095 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2096 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2097 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2098
2099 // Stop hovering.
2100 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2101 TestFixture::mTouchscreen->sendSync();
2102 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2103 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2104 WithButtonState(0))));
2105 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2106 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2107 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2108 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2109
2110 // Release the stylus button.
2111 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2112 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2113 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2114 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2115}
2116
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002117TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002118 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2119 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2120 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002121
2122 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002123 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2124 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2125 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2126 TestFixture::mTouchscreen->sendDown(centerPoint);
2127 TestFixture::mTouchscreen->sendSync();
2128 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002129 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002130 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002131 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002132
2133 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002134 TestFixture::mStylus->pressKey(BTN_STYLUS);
2135 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002136 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002137 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2138 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002139 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002140 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002141 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2142 WithDeviceId(touchscreenId))));
2143 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002144 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002145 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002146 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2147 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002148
Prabir Pradhan124ea442022-10-28 20:27:44 +00002149 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2150 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002151 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002152 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2153 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002154 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002155 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002156 WithDeviceId(touchscreenId))));
2157 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002158 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002159 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002160 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002161
2162 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002163 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2164 TestFixture::mTouchscreen->sendSync();
2165 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002166 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002167 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002168 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002169}
2170
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002171TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002172 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2173 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002174 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002175
2176 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2177 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2178 const auto stylusId = TestFixture::mStylusInfo.getId();
2179
2180 // Start a stylus gesture. By the time this event is processed, the configuration change that
2181 // was requested is guaranteed to be completed.
2182 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2183 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2184 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2185 TestFixture::mTouchscreen->sendDown(centerPoint);
2186 TestFixture::mTouchscreen->sendSync();
2187 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2188 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002189 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002190 WithDeviceId(touchscreenId))));
2191
2192 // Press and release a stylus button. Each change only generates a MOVE motion event.
2193 // Key events are unaffected.
2194 TestFixture::mStylus->pressKey(BTN_STYLUS);
2195 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2196 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2197 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2198 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2199 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002200 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002201 WithDeviceId(touchscreenId))));
2202
2203 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2204 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2205 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2206 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2207 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2208 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002209 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002210 WithDeviceId(touchscreenId))));
2211
2212 // Finish the stylus gesture.
2213 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2214 TestFixture::mTouchscreen->sendSync();
2215 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2216 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002217 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002218 WithDeviceId(touchscreenId))));
2219}
2220
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002221// --- ExternalStylusIntegrationTest ---
2222
2223// Verify the behavior of an external stylus. An external stylus can report pressure or button
2224// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2225// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singha11fc942023-08-09 09:23:43 +00002226using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002227
Prabir Pradhanf1198582023-09-14 22:28:32 +00002228TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreenSource) {
2229 // Create an external stylus capable of reporting pressure data that
2230 // should be fused with a touch pointer.
2231 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2232 createUinputDevice<UinputExternalStylusWithPressure>();
2233 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2234 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2235 const auto stylusInfo = findDeviceByName(stylus->getName());
2236 ASSERT_TRUE(stylusInfo);
2237
2238 // Connecting an external stylus changes the source of the touchscreen.
2239 const auto deviceInfo = findDeviceByName(mDevice->getName());
2240 ASSERT_TRUE(deviceInfo);
2241 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
2242}
2243
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002244TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002245 const Point centerPoint = mDevice->getCenterPoint();
2246
2247 // Create an external stylus capable of reporting pressure data that
2248 // should be fused with a touch pointer.
2249 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2250 createUinputDevice<UinputExternalStylusWithPressure>();
2251 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2252 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2253 const auto stylusInfo = findDeviceByName(stylus->getName());
2254 ASSERT_TRUE(stylusInfo);
2255
2256 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2257
2258 const auto touchscreenId = mDeviceInfo.getId();
2259
2260 // Set a pressure value on the stylus. It doesn't generate any events.
2261 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2262 stylus->setPressure(100);
2263 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2264
2265 // Start a finger gesture, and ensure it shows up as stylus gesture
2266 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002267 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002268 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002269 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002270 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002271 mDevice->sendSync();
2272 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanf1198582023-09-14 22:28:32 +00002273 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithToolType(ToolType::STYLUS),
2274 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2275 WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002276
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002277 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2278 // event with the updated pressure.
2279 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002280 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanf1198582023-09-14 22:28:32 +00002281 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithToolType(ToolType::STYLUS),
2282 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2283 WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002284
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002285 // The external stylus did not generate any events.
2286 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2287 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2288}
2289
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002290TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002291 const Point centerPoint = mDevice->getCenterPoint();
2292
2293 // Create an external stylus capable of reporting pressure data that
2294 // should be fused with a touch pointer.
2295 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2296 createUinputDevice<UinputExternalStylusWithPressure>();
2297 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2298 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2299 const auto stylusInfo = findDeviceByName(stylus->getName());
2300 ASSERT_TRUE(stylusInfo);
2301
2302 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2303
2304 const auto touchscreenId = mDeviceInfo.getId();
2305
2306 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2307 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002308 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2309 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002310 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002311 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002312
2313 // Start a finger gesture. The touch device will withhold generating any touches for
2314 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2315 mDevice->sendSlot(FIRST_SLOT);
2316 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2317 mDevice->sendToolType(MT_TOOL_FINGER);
2318 mDevice->sendDown(centerPoint);
2319 auto waitUntil = std::chrono::system_clock::now() +
2320 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002321 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002322 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002323
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002324 // Since the external stylus did not report a pressure value within the timeout,
2325 // it shows up as a finger pointer.
2326 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2327 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhanf1198582023-09-14 22:28:32 +00002328 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
2329 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId), WithPressure(1.f))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002330
2331 // Change the pressure on the external stylus. Since the pressure was not present at the start
2332 // of the gesture, it is ignored for now.
2333 stylus->setPressure(200);
2334 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2335
2336 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002337 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2338 mDevice->sendSync();
2339 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2340 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhanf1198582023-09-14 22:28:32 +00002341 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002342 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002343
2344 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2345 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2346 mDevice->sendToolType(MT_TOOL_FINGER);
2347 mDevice->sendDown(centerPoint);
2348 mDevice->sendSync();
2349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanf1198582023-09-14 22:28:32 +00002350 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithSource(STYLUS_FUSION_SOURCE),
2351 WithToolType(ToolType::STYLUS), WithButtonState(0), WithDeviceId(touchscreenId),
2352 WithPressure(200.f / RAW_PRESSURE_MAX))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002353
2354 // The external stylus did not generate any events.
2355 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2356 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002357}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002358
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002359TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002360 const Point centerPoint = mDevice->getCenterPoint();
2361
2362 // Create an external stylus device that does not support pressure. It should not affect any
2363 // touch pointers.
2364 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2365 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2366 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2367 const auto stylusInfo = findDeviceByName(stylus->getName());
2368 ASSERT_TRUE(stylusInfo);
2369
2370 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2371
2372 const auto touchscreenId = mDeviceInfo.getId();
2373
2374 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2375 // pressure data from the external stylus.
2376 mDevice->sendSlot(FIRST_SLOT);
2377 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2378 mDevice->sendToolType(MT_TOOL_FINGER);
2379 mDevice->sendDown(centerPoint);
2380 auto waitUntil = std::chrono::system_clock::now() +
2381 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2382 mDevice->sendSync();
2383 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhanf1198582023-09-14 22:28:32 +00002384 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2385 AMOTION_EVENT_ACTION_DOWN),
2386 WithToolType(ToolType::FINGER),
2387 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2388 AINPUT_SOURCE_STYLUS),
2389 WithButtonState(0),
2390 WithDeviceId(touchscreenId),
2391 WithPressure(1.f)),
2392 waitUntil));
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002393
2394 // The external stylus did not generate any events.
2395 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2396 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2397}
2398
Michael Wrightd02c5b62014-02-10 15:10:22 -08002399// --- InputDeviceTest ---
2400class InputDeviceTest : public testing::Test {
2401protected:
2402 static const char* DEVICE_NAME;
2403 static const char* DEVICE_LOCATION;
2404 static const int32_t DEVICE_ID;
2405 static const int32_t DEVICE_GENERATION;
2406 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002407 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002408 static const int32_t EVENTHUB_ID;
2409 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2410
2411 std::shared_ptr<FakeEventHub> mFakeEventHub;
2412 sp<FakeInputReaderPolicy> mFakePolicy;
2413 std::unique_ptr<TestInputListener> mFakeListener;
2414 std::unique_ptr<InstrumentedInputReader> mReader;
2415 std::shared_ptr<InputDevice> mDevice;
2416
2417 void SetUp() override {
2418 mFakeEventHub = std::make_unique<FakeEventHub>();
2419 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2420 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002421 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002422 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002423 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424 identifier.name = DEVICE_NAME;
2425 identifier.location = DEVICE_LOCATION;
2426 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2427 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2428 identifier);
2429 mReader->pushNextDevice(mDevice);
2430 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002431 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002433
2434 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002435 mFakeListener.reset();
2436 mFakePolicy.clear();
2437 }
2438};
2439
2440const char* InputDeviceTest::DEVICE_NAME = "device";
2441const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2442const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2443const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002444const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002445const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2446 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002447const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002448const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2449
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002450TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002451 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002452 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2453 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002455
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2457 ASSERT_EQ(mDevice->isEnabled(), false);
2458}
2459
2460TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2461 // Configuration.
2462 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002463 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002464
2465 // Reset.
2466 unused += mDevice->reset(ARBITRARY_TIME);
2467
2468 NotifyDeviceResetArgs resetArgs;
2469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2470 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2471 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2472
2473 // Metadata.
2474 ASSERT_TRUE(mDevice->isIgnored());
2475 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2476
2477 InputDeviceInfo info = mDevice->getDeviceInfo();
2478 ASSERT_EQ(DEVICE_ID, info.getId());
2479 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2480 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2481 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2482
2483 // State queries.
2484 ASSERT_EQ(0, mDevice->getMetaState());
2485
2486 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2487 << "Ignored device should return unknown key code state.";
2488 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2489 << "Ignored device should return unknown scan code state.";
2490 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2491 << "Ignored device should return unknown switch state.";
2492
2493 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2494 uint8_t flags[2] = { 0, 1 };
2495 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2496 << "Ignored device should never mark any key codes.";
2497 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2498 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2499}
2500
2501TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2502 // Configuration.
2503 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2504
2505 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002506 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2507 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002508 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2509 mapper1.setMetaState(AMETA_ALT_ON);
2510 mapper1.addSupportedKeyCode(AKEYCODE_A);
2511 mapper1.addSupportedKeyCode(AKEYCODE_B);
2512 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2513 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2514 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2515 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2516 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2517
2518 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002519 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2520 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002521 mapper2.setMetaState(AMETA_SHIFT_ON);
2522
2523 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002524 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002525
Harry Cuttsf13161a2023-03-08 14:15:49 +00002526 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2527 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002528 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002529 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002530
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002531 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2532 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002533
2534 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002535 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002536 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2537 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002538
2539 NotifyDeviceResetArgs resetArgs;
2540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2541 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2542 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2543
2544 // Metadata.
2545 ASSERT_FALSE(mDevice->isIgnored());
2546 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2547
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002548 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002549 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002550 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002551 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2552 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2553
2554 // State queries.
2555 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2556 << "Should query mappers and combine meta states.";
2557
2558 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2559 << "Should return unknown key code state when source not supported.";
2560 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2561 << "Should return unknown scan code state when source not supported.";
2562 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2563 << "Should return unknown switch state when source not supported.";
2564
2565 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2566 << "Should query mapper when source is supported.";
2567 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2568 << "Should query mapper when source is supported.";
2569 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2570 << "Should query mapper when source is supported.";
2571
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002572 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002573 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002574 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002575 << "Should do nothing when source is unsupported.";
2576 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2577 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2578 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2579 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2580
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002581 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002582 << "Should query mapper when source is supported.";
2583 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2584 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2585 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2586 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2587
2588 // Event handling.
2589 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002590 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002591 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002592
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002593 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2594 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002595}
2596
Arthur Hung2c9a3342019-07-23 14:18:59 +08002597// A single input device is associated with a specific display. Check that:
2598// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh3e56f7e2023-07-07 13:12:37 +00002599// 2. Device is disabled when setEnabled API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002600TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002601 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2602 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002603
2604 // First Configuration.
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=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002608
2609 // Device should be enabled by default.
2610 ASSERT_TRUE(mDevice->isEnabled());
2611
2612 // Prepare associated info.
2613 constexpr uint8_t hdmi = 1;
2614 const std::string UNIQUE_ID = "local:1";
2615
2616 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002617 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002618 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002619 // Device should be disabled because it is associated with a specific display via
2620 // input port <-> display port association, but the corresponding display is not found
2621 ASSERT_FALSE(mDevice->isEnabled());
2622
2623 // Prepare displays.
2624 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002625 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002626 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002627 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002628 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002629 ASSERT_TRUE(mDevice->isEnabled());
2630
2631 // Device should be disabled after set disable.
2632 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002633 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002634 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002635 ASSERT_FALSE(mDevice->isEnabled());
2636
2637 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002638 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002639 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002640 ASSERT_FALSE(mDevice->isEnabled());
2641}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642
Christine Franks1ba71cc2021-04-07 14:37:42 -07002643TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2644 // Device should be enabled by default.
2645 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002646 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2647 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002648 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002649 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2650 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002651 ASSERT_TRUE(mDevice->isEnabled());
2652
2653 // Device should be disabled because it is associated with a specific display, but the
2654 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002655 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002656 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002657 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002658 ASSERT_FALSE(mDevice->isEnabled());
2659
2660 // Device should be enabled when a display is found.
2661 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002662 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002663 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002664 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002665 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002666 ASSERT_TRUE(mDevice->isEnabled());
2667
2668 // Device should be disabled after set disable.
2669 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002670 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002671 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002672 ASSERT_FALSE(mDevice->isEnabled());
2673
2674 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002675 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002676 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002677 ASSERT_FALSE(mDevice->isEnabled());
2678}
2679
Christine Franks2a2293c2022-01-18 11:51:16 -08002680TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2681 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002682 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2683 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002684 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002685 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2686 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002687
Christine Franks2a2293c2022-01-18 11:51:16 -08002688 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2689 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002690 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002691 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002692 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002693 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002694 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2695}
2696
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002697/**
2698 * This test reproduces a crash caused by a dangling reference that remains after device is added
2699 * and removed. The reference is accessed in InputDevice::dump(..);
2700 */
2701TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2702 constexpr int32_t TEST_EVENTHUB_ID = 10;
2703 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2704
Harry Cutts33476232023-01-30 19:57:29 +00002705 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh7f1765e2023-07-07 13:12:37 +00002706 device.addEventHubDevice(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002707 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2708 std::string dumpStr, eventHubDevStr;
2709 device.dump(dumpStr, eventHubDevStr);
2710}
2711
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002712TEST_F(InputDeviceTest, GetBluetoothAddress) {
2713 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2714 ASSERT_TRUE(address);
2715 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2716}
2717
Michael Wrightd02c5b62014-02-10 15:10:22 -08002718// --- SwitchInputMapperTest ---
2719
2720class SwitchInputMapperTest : public InputMapperTest {
2721protected:
2722};
2723
2724TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002725 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002726
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002727 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002728}
2729
2730TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002731 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002732
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002733 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002734 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002735
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002736 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002737 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002738}
2739
2740TEST_F(SwitchInputMapperTest, Process) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002741 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002742 std::list<NotifyArgs> out;
2743 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2744 ASSERT_TRUE(out.empty());
2745 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2746 ASSERT_TRUE(out.empty());
2747 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2748 ASSERT_TRUE(out.empty());
2749 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002750
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002751 ASSERT_EQ(1u, out.size());
2752 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002753 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002754 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2755 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002756 args.switchMask);
2757 ASSERT_EQ(uint32_t(0), args.policyFlags);
2758}
2759
Chris Ye87143712020-11-10 05:05:58 +00002760// --- VibratorInputMapperTest ---
2761class VibratorInputMapperTest : public InputMapperTest {
2762protected:
2763 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2764};
2765
2766TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singhc68f85b2023-04-26 16:23:13 +00002767 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002768
2769 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2770}
2771
2772TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singhc68f85b2023-04-26 16:23:13 +00002773 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002774
2775 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2776}
2777
2778TEST_F(VibratorInputMapperTest, Vibrate) {
2779 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002780 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singhc68f85b2023-04-26 16:23:13 +00002781 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002782
2783 VibrationElement pattern(2);
2784 VibrationSequence sequence(2);
2785 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002786 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2787 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002788 sequence.addElement(pattern);
2789 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002790 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2791 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002792 sequence.addElement(pattern);
2793
2794 std::vector<int64_t> timings = {0, 1};
2795 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2796
2797 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002798 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002799 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002800 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002801 // Verify vibrator state listener was notified.
2802 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002803 ASSERT_EQ(1u, out.size());
2804 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2805 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2806 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002807 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002808 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002809 ASSERT_FALSE(mapper.isVibrating());
2810 // Verify vibrator state listener was notified.
2811 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002812 ASSERT_EQ(1u, out.size());
2813 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2814 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2815 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002816}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817
Chris Yef59a2f42020-10-16 12:55:26 -07002818// --- SensorInputMapperTest ---
2819
2820class SensorInputMapperTest : public InputMapperTest {
2821protected:
2822 static const int32_t ACCEL_RAW_MIN;
2823 static const int32_t ACCEL_RAW_MAX;
2824 static const int32_t ACCEL_RAW_FUZZ;
2825 static const int32_t ACCEL_RAW_FLAT;
2826 static const int32_t ACCEL_RAW_RESOLUTION;
2827
2828 static const int32_t GYRO_RAW_MIN;
2829 static const int32_t GYRO_RAW_MAX;
2830 static const int32_t GYRO_RAW_FUZZ;
2831 static const int32_t GYRO_RAW_FLAT;
2832 static const int32_t GYRO_RAW_RESOLUTION;
2833
2834 static const float GRAVITY_MS2_UNIT;
2835 static const float DEGREE_RADIAN_UNIT;
2836
2837 void prepareAccelAxes();
2838 void prepareGyroAxes();
2839 void setAccelProperties();
2840 void setGyroProperties();
2841 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2842};
2843
2844const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2845const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2846const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2847const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2848const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2849
2850const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2851const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2852const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2853const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2854const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2855
2856const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2857const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2858
2859void SensorInputMapperTest::prepareAccelAxes() {
2860 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2861 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2862 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2863 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2864 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2865 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2866}
2867
2868void SensorInputMapperTest::prepareGyroAxes() {
2869 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2870 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2871 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2872 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2873 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2874 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2875}
2876
2877void SensorInputMapperTest::setAccelProperties() {
2878 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2879 /* sensorDataIndex */ 0);
2880 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2881 /* sensorDataIndex */ 1);
2882 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2883 /* sensorDataIndex */ 2);
2884 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2885 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2886 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2887 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2888 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2889}
2890
2891void SensorInputMapperTest::setGyroProperties() {
2892 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2893 /* sensorDataIndex */ 0);
2894 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2895 /* sensorDataIndex */ 1);
2896 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2897 /* sensorDataIndex */ 2);
2898 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2899 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2900 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2901 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2902 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2903}
2904
2905TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singh61dd33e2023-04-26 15:07:55 +00002906 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002907
2908 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2909}
2910
2911TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2912 setAccelProperties();
2913 prepareAccelAxes();
Arpit Singh61dd33e2023-04-26 15:07:55 +00002914 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002915
2916 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2917 std::chrono::microseconds(10000),
2918 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002919 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2922 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2923 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2924 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002925
2926 NotifySensorArgs args;
2927 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2928 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2929 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2930
2931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2932 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2933 ASSERT_EQ(args.deviceId, DEVICE_ID);
2934 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2935 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2936 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2937 ASSERT_EQ(args.values, values);
2938 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2939}
2940
2941TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2942 setGyroProperties();
2943 prepareGyroAxes();
Arpit Singh61dd33e2023-04-26 15:07:55 +00002944 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002945
2946 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2947 std::chrono::microseconds(10000),
2948 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002949 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002955
2956 NotifySensorArgs args;
2957 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2958 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2959 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2960
2961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2962 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2963 ASSERT_EQ(args.deviceId, DEVICE_ID);
2964 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2965 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2966 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2967 ASSERT_EQ(args.values, values);
2968 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2969}
2970
Michael Wrightd02c5b62014-02-10 15:10:22 -08002971// --- KeyboardInputMapperTest ---
2972
2973class KeyboardInputMapperTest : public InputMapperTest {
2974protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002975 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002976 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002977 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002978
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002979 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002980 int32_t originalKeyCode, int32_t rotatedKeyCode,
2981 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002982};
2983
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002984/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2985 * orientation.
2986 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002987void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002988 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2989 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002990}
2991
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002992void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002993 int32_t originalScanCode, int32_t originalKeyCode,
2994 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002995 NotifyKeyArgs args;
2996
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2999 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3000 ASSERT_EQ(originalScanCode, args.scanCode);
3001 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003002 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003003
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3006 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3007 ASSERT_EQ(originalScanCode, args.scanCode);
3008 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003009 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010}
3011
Michael Wrightd02c5b62014-02-10 15:10:22 -08003012TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003013 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003014 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003015 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003016
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003017 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003018}
3019
3020TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3021 const int32_t USAGE_A = 0x070004;
3022 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003023 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3024 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003025 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3026 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3027 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003028
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003029 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003030 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003031 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003032 // Initial metastate is AMETA_NONE.
3033 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034
3035 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003037 NotifyKeyArgs args;
3038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3039 ASSERT_EQ(DEVICE_ID, args.deviceId);
3040 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3041 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3042 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3043 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3044 ASSERT_EQ(KEY_HOME, args.scanCode);
3045 ASSERT_EQ(AMETA_NONE, args.metaState);
3046 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3047 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3048 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3049
3050 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003051 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3053 ASSERT_EQ(DEVICE_ID, args.deviceId);
3054 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3055 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3056 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3057 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3058 ASSERT_EQ(KEY_HOME, args.scanCode);
3059 ASSERT_EQ(AMETA_NONE, args.metaState);
3060 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3061 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3062 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3063
3064 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3066 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3068 ASSERT_EQ(DEVICE_ID, args.deviceId);
3069 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3070 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3071 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3072 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3073 ASSERT_EQ(0, args.scanCode);
3074 ASSERT_EQ(AMETA_NONE, args.metaState);
3075 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3076 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3077 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3078
3079 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3081 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3083 ASSERT_EQ(DEVICE_ID, args.deviceId);
3084 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3085 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3086 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3087 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3088 ASSERT_EQ(0, args.scanCode);
3089 ASSERT_EQ(AMETA_NONE, args.metaState);
3090 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3091 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3092 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3093
3094 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3098 ASSERT_EQ(DEVICE_ID, args.deviceId);
3099 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3100 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3101 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3102 ASSERT_EQ(0, args.keyCode);
3103 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3104 ASSERT_EQ(AMETA_NONE, args.metaState);
3105 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3106 ASSERT_EQ(0U, args.policyFlags);
3107 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3108
3109 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3111 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3113 ASSERT_EQ(DEVICE_ID, args.deviceId);
3114 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3115 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3116 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3117 ASSERT_EQ(0, args.keyCode);
3118 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3119 ASSERT_EQ(AMETA_NONE, args.metaState);
3120 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3121 ASSERT_EQ(0U, args.policyFlags);
3122 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3123}
3124
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003125TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3126 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3127 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3128 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3129
3130 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003131 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003132 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3133
3134 // Key down by scan code.
3135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3136 NotifyKeyArgs args;
3137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3138 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3139
3140 // Key up by scan code.
3141 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3143 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3144}
3145
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003146/**
3147 * Ensure that the readTime is set to the time when the EV_KEY is received.
3148 */
3149TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3150 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3151
3152 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003153 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003154 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3155 NotifyKeyArgs args;
3156
3157 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003158 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3160 ASSERT_EQ(12, args.readTime);
3161
3162 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003163 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3165 ASSERT_EQ(15, args.readTime);
3166}
3167
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003169 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3170 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003171 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3172 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3173 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003174
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003175 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003176 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003177 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003178
Arthur Hung95f68612022-04-07 14:08:22 +08003179 // Initial metastate is AMETA_NONE.
3180 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003181
3182 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003184 NotifyKeyArgs args;
3185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3186 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003187 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003188 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003189
3190 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003191 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3193 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003194 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003195
3196 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003197 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3199 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003200 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003201
3202 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003203 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3205 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003206 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003207 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003208}
3209
3210TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003211 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3212 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3213 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3214 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003215
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003216 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003217 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003218 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003219
Michael Wrighta9cf4192022-12-01 23:46:39 +00003220 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3222 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3223 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3224 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3225 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3226 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3227 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3228 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3229}
3230
3231TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003232 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3233 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3234 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3235 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003236
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003238 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003239 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003240 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241
Michael Wrighta9cf4192022-12-01 23:46:39 +00003242 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003243 ASSERT_NO_FATAL_FAILURE(
3244 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3245 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3246 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3247 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3248 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3249 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3250 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003252 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003253 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003254 ASSERT_NO_FATAL_FAILURE(
3255 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3256 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3257 AKEYCODE_DPAD_UP, DISPLAY_ID));
3258 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3259 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3260 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3261 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003263 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003264 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003265 ASSERT_NO_FATAL_FAILURE(
3266 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3267 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3268 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3269 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3270 AKEYCODE_DPAD_UP, DISPLAY_ID));
3271 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3272 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003274 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003275 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003276 ASSERT_NO_FATAL_FAILURE(
3277 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3278 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3279 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3280 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3281 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3282 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3283 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284
3285 // Special case: if orientation changes while key is down, we still emit the same keycode
3286 // in the key up as we did in the key down.
3287 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003288 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003289 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3292 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3293 ASSERT_EQ(KEY_UP, args.scanCode);
3294 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3295
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003296 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003297 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3300 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3301 ASSERT_EQ(KEY_UP, args.scanCode);
3302 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3303}
3304
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003305TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3306 // If the keyboard is not orientation aware,
3307 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003308 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003309
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);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003313 NotifyKeyArgs args;
3314
3315 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3320 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3321
Michael Wrighta9cf4192022-12-01 23:46:39 +00003322 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3327 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3328}
3329
3330TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3331 // If the keyboard is orientation aware,
3332 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003333 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003334
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003335 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003336 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003337 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003338 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003339 NotifyKeyArgs args;
3340
3341 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3342 // ^--- already checked by the previous test
3343
Michael Wrighta9cf4192022-12-01 23:46:39 +00003344 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003345 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3350 ASSERT_EQ(DISPLAY_ID, args.displayId);
3351
3352 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003353 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003354 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003355 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003356 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3360 ASSERT_EQ(newDisplayId, args.displayId);
3361}
3362
Michael Wrightd02c5b62014-02-10 15:10:22 -08003363TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003364 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003365 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003366 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003368 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003369 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003370
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003371 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003372 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373}
3374
Philip Junker4af3b3d2021-12-14 10:36:55 +01003375TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3376 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003377 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003378 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3379
3380 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3381 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3382 << "If a mapping is available, the result is equal to the mapping";
3383
3384 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3385 << "If no mapping is available, the result is the key location";
3386}
3387
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003389 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003390 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003391 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003392
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003393 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003394 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003395
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003396 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003397 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398}
3399
3400TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003401 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003402 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003403 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003404
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003405 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003406
Michael Wrightd02c5b62014-02-10 15:10:22 -08003407 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003408 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409 ASSERT_TRUE(flags[0]);
3410 ASSERT_FALSE(flags[1]);
3411}
3412
3413TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003414 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3415 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3416 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3417 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3418 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3419 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003421 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003422 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003423 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003424 // Initial metastate is AMETA_NONE.
3425 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003426
3427 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003428 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3429 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3430 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003431
3432 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003435 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3436 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3437 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003438 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003439
3440 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003443 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3444 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3445 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003446 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003447
3448 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003451 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3452 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3453 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003454 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455
3456 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003459 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3460 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3461 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003462 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003463
3464 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003465 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003467 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3468 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3469 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003470 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471
3472 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003475 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3476 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3477 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003478 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003479}
3480
Chris Yea52ade12020-08-27 16:49:20 -07003481TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3482 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3483 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3484 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3485 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3486
3487 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003488 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003489 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3490
Chris Yea52ade12020-08-27 16:49:20 -07003491 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003492 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003493 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3494 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3495 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3496 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3497
3498 NotifyKeyArgs args;
3499 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3502 ASSERT_EQ(AMETA_NONE, args.metaState);
3503 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3504 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3505 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3506
3507 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003508 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3510 ASSERT_EQ(AMETA_NONE, args.metaState);
3511 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3512 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3513 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3514}
3515
Arthur Hung2c9a3342019-07-23 14:18:59 +08003516TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3517 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003518 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3519 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3520 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3521 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003522
3523 // keyboard 2.
3524 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003525 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003526 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003527 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003528 std::shared_ptr<InputDevice> device2 =
3529 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003530 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003531
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003532 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3533 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3534 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3535 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003536
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003537 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003538 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003539 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003540
Arpit Singh033e3ec2023-04-26 14:43:16 +00003541 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003542 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003543 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3544 mFakePolicy
3545 ->getReaderConfiguration(),
3546 AINPUT_SOURCE_KEYBOARD,
3547 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003548 std::list<NotifyArgs> unused =
3549 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003550 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003551 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003552
3553 // Prepared displays and associated info.
3554 constexpr uint8_t hdmi1 = 0;
3555 constexpr uint8_t hdmi2 = 1;
3556 const std::string SECONDARY_UNIQUE_ID = "local:1";
3557
3558 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3559 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3560
3561 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003562 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003563 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003564 ASSERT_FALSE(device2->isEnabled());
3565
3566 // Prepare second display.
3567 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003568 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003569 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003570 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003571 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003572 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003573 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003574 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003575
3576 // Device should be enabled after the associated display is found.
3577 ASSERT_TRUE(mDevice->isEnabled());
3578 ASSERT_TRUE(device2->isEnabled());
3579
3580 // Test pad key events
3581 ASSERT_NO_FATAL_FAILURE(
3582 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3583 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3584 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3586 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3587 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3588 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3589
3590 ASSERT_NO_FATAL_FAILURE(
3591 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3592 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3593 AKEYCODE_DPAD_RIGHT, newDisplayId));
3594 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3595 AKEYCODE_DPAD_DOWN, newDisplayId));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3597 AKEYCODE_DPAD_LEFT, newDisplayId));
3598}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003599
arthurhungc903df12020-08-11 15:08:42 +08003600TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3601 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3602 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3603 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3604 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3605 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3606 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3607
3608 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003609 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003610 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003611 // Initial metastate is AMETA_NONE.
3612 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003613
3614 // Initialization should have turned all of the lights off.
3615 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3616 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3617 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3618
3619 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3621 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003622 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3623 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3624
3625 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003628 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3629 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3630
3631 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003632 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3633 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003634 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3635 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3636
3637 mFakeEventHub->removeDevice(EVENTHUB_ID);
3638 mReader->loopOnce();
3639
3640 // keyboard 2 should default toggle keys.
3641 const std::string USB2 = "USB2";
3642 const std::string DEVICE_NAME2 = "KEYBOARD2";
3643 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3644 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3645 std::shared_ptr<InputDevice> device2 =
3646 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003647 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003648 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3649 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3650 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3651 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3652 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3653 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3654
Arpit Singh033e3ec2023-04-26 14:43:16 +00003655 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003656 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003657 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3658 mFakePolicy
3659 ->getReaderConfiguration(),
3660 AINPUT_SOURCE_KEYBOARD,
3661 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003662 std::list<NotifyArgs> unused =
3663 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003664 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003665 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003666
3667 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3668 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3669 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003670 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3671 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003672}
3673
Arthur Hungcb40a002021-08-03 14:31:01 +00003674TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3675 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3676 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3677 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3678
3679 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh033e3ec2023-04-26 14:43:16 +00003680 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003681 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3682 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003683 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003684 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003685 // Initial metastate is AMETA_NONE.
3686 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003687
3688 mReader->toggleCapsLockState(DEVICE_ID);
3689 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3690}
3691
Arthur Hungfb3cc112022-04-13 07:39:50 +00003692TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3693 // keyboard 1.
3694 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3695 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3696 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3697 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3698 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3699 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3700
3701 KeyboardInputMapper& mapper1 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003702 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003703 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3704
3705 // keyboard 2.
3706 const std::string USB2 = "USB2";
3707 const std::string DEVICE_NAME2 = "KEYBOARD2";
3708 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3709 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3710 std::shared_ptr<InputDevice> device2 =
3711 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3712 ftl::Flags<InputDeviceClass>(0));
3713 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3714 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3715 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3716 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3717 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3718 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3719
Arpit Singh033e3ec2023-04-26 14:43:16 +00003720 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003721 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003722 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3723 mFakePolicy
3724 ->getReaderConfiguration(),
3725 AINPUT_SOURCE_KEYBOARD,
3726 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003727 std::list<NotifyArgs> unused =
3728 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003729 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003730 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003731
Arthur Hung95f68612022-04-07 14:08:22 +08003732 // Initial metastate is AMETA_NONE.
3733 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3734 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3735
3736 // Toggle num lock on and off.
3737 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3738 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003739 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3740 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3741 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3742
3743 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3744 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3745 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3746 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3747 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3748
3749 // Toggle caps lock on and off.
3750 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3751 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3752 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3753 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3754 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3755
3756 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3757 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3758 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3759 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3760 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3761
3762 // Toggle scroll lock on and off.
3763 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3764 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3765 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3766 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3767 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3768
3769 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3770 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3771 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3772 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3773 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3774}
3775
Arthur Hung2141d542022-08-23 07:45:21 +00003776TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3777 const int32_t USAGE_A = 0x070004;
3778 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3779 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3780
3781 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003782 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003783 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3784 // Key down by scan code.
3785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3786 NotifyKeyArgs args;
3787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3788 ASSERT_EQ(DEVICE_ID, args.deviceId);
3789 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3790 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3791 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3792 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3793 ASSERT_EQ(KEY_HOME, args.scanCode);
3794 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3795
3796 // Disable device, it should synthesize cancellation events for down events.
3797 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003798 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003799
3800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3801 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3802 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3803 ASSERT_EQ(KEY_HOME, args.scanCode);
3804 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3805}
3806
Zixuan Qufecb6062022-11-12 04:44:31 +00003807TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh033e3ec2023-04-26 14:43:16 +00003808 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3809 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003810 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003811 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3812 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003813
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003814 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003815 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3816
3817 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003818 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003819
3820 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3821 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3822 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3823 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3824 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003825 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3826
3827 // Call change layout association with the same values: Generation shouldn't change
3828 generation = mReader->getContext()->getGeneration();
3829 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3830 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3831 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3832 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003833}
3834
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003835TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3836 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3837 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3838
3839 // Configuration
Arpit Singh033e3ec2023-04-26 14:43:16 +00003840 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003841 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3842 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003843 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003844
3845 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3846 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3847}
3848
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003849// --- KeyboardInputMapperTest_ExternalDevice ---
3850
3851class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3852protected:
Chris Yea52ade12020-08-27 16:49:20 -07003853 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003854};
3855
3856TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003857 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3858 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003859
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003860 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3861 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3862 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3863 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003864
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003865 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003866 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003867 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003868
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003870 NotifyKeyArgs args;
3871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3872 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3873
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003874 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3876 ASSERT_EQ(uint32_t(0), args.policyFlags);
3877
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003880 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003881
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003882 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3884 ASSERT_EQ(uint32_t(0), args.policyFlags);
3885
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3888 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3889
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003890 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3892 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3893}
3894
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003895TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003896 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003897
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003898 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3899 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3900 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003901
Powei Fengd041c5d2019-05-03 17:11:33 -07003902 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003903 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003904 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003905 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003906
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003908 NotifyKeyArgs args;
3909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3910 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3911
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003912 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3914 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3915
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003916 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3918 ASSERT_EQ(uint32_t(0), args.policyFlags);
3919
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003920 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3922 ASSERT_EQ(uint32_t(0), args.policyFlags);
3923
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003924 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3926 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3927
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003928 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3930 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3931}
3932
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933// --- CursorInputMapperTest ---
3934
3935class CursorInputMapperTest : public InputMapperTest {
3936protected:
3937 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3938
Michael Wright17db18e2020-06-26 20:51:44 +01003939 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003940
Chris Yea52ade12020-08-27 16:49:20 -07003941 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942 InputMapperTest::SetUp();
3943
Michael Wright17db18e2020-06-26 20:51:44 +01003944 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003945 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003946 }
3947
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003948 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3949 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003950
Michael Wrighta9cf4192022-12-01 23:46:39 +00003951 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003952 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3953 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3954 }
3955
3956 void prepareSecondaryDisplay() {
3957 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003958 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003959 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003960 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003961
3962 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3963 float pressure) {
3964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3965 0.0f, 0.0f, 0.0f, EPSILON));
3966 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003967};
3968
3969const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3970
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003971void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3972 int32_t originalY, int32_t rotatedX,
3973 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003974 NotifyMotionArgs args;
3975
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003981 ASSERT_NO_FATAL_FAILURE(
3982 assertCursorPointerCoords(args.pointerCoords[0],
3983 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3984 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003985}
3986
3987TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003988 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003989 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003991 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003992}
3993
3994TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003995 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003996 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003997
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003998 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999}
4000
4001TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004002 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004003 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004004
4005 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004006 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007
4008 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004009 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4010 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4012 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4013
4014 // When the bounds are set, then there should be a valid motion range.
4015 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4016
4017 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004018 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019
4020 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4021 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4022 1, 800 - 1, 0.0f, 0.0f));
4023 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4024 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4025 2, 480 - 1, 0.0f, 0.0f));
4026 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4027 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4028 0.0f, 1.0f, 0.0f, 0.0f));
4029}
4030
4031TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004032 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004033 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034
4035 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004036 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037
4038 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4039 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4040 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4041 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4042 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4043 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4044 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4045 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4046 0.0f, 1.0f, 0.0f, 0.0f));
4047}
4048
4049TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004050 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004051 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052
arthurhungdcef2dc2020-08-11 14:47:50 +08004053 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054
4055 NotifyMotionArgs args;
4056
4057 // Button press.
4058 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004059 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4062 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4063 ASSERT_EQ(DEVICE_ID, args.deviceId);
4064 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4065 ASSERT_EQ(uint32_t(0), args.policyFlags);
4066 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4067 ASSERT_EQ(0, args.flags);
4068 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4069 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4070 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004071 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004073 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004074 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4076 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4077 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4078
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4080 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4081 ASSERT_EQ(DEVICE_ID, args.deviceId);
4082 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4083 ASSERT_EQ(uint32_t(0), args.policyFlags);
4084 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4085 ASSERT_EQ(0, args.flags);
4086 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4087 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4088 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004089 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004090 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004091 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004092 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004093 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4094 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4095 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4096
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004098 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4099 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4101 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4102 ASSERT_EQ(DEVICE_ID, args.deviceId);
4103 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4104 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004105 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4106 ASSERT_EQ(0, args.flags);
4107 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4108 ASSERT_EQ(0, args.buttonState);
4109 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004110 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004111 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004112 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004113 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004114 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4115 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4116 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4117
4118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4119 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4120 ASSERT_EQ(DEVICE_ID, args.deviceId);
4121 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4122 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004123 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4124 ASSERT_EQ(0, args.flags);
4125 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4126 ASSERT_EQ(0, args.buttonState);
4127 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004128 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004129 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004130 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004131 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4133 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4134 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4135}
4136
4137TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004139 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140
4141 NotifyMotionArgs args;
4142
4143 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004148 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4149 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4150 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151
4152 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004157 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4158 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159}
4160
4161TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004163 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164
4165 NotifyMotionArgs args;
4166
4167 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4171 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004172 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4175 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004176 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004177
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004182 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004183 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004184
4185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004186 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004187 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004188}
4189
4190TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004192 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193
4194 NotifyMotionArgs args;
4195
4196 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4198 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4202 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004203 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4204 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4205 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4208 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004209 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4210 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4211 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004212
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004219 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4220 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4221 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222
4223 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004227 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004228 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004229
4230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004232 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233}
4234
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004235TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004236 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004238 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4239 // need to be rotated.
4240 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004241 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242
Michael Wrighta9cf4192022-12-01 23:46:39 +00004243 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4245 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4246 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4247 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4248 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4249 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4250 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4251 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4252}
4253
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004254TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004255 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004257 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4258 // orientation-aware are affected by display rotation.
Arpit Singhd8510bd2023-04-27 12:48:15 +00004259 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004261 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004262 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4264 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4265 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4266 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4267 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4268 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4269 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4270 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4271
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004272 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004273 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004274 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4275 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4276 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4277 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4278 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4279 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4280 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4281 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004283 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004284 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4286 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4287 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4288 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4289 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4290 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4291 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4292 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4293
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004294 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004295 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004296 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4297 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4298 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4299 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4300 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4301 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4302 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4303 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304}
4305
4306TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004308 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309
4310 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4311 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312
4313 NotifyMotionArgs motionArgs;
4314 NotifyKeyArgs keyArgs;
4315
4316 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4320 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4321 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004322 ASSERT_NO_FATAL_FAILURE(
4323 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4326 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4327 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004328 ASSERT_NO_FATAL_FAILURE(
4329 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004330
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004334 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004336 ASSERT_NO_FATAL_FAILURE(
4337 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338
4339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004340 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341 ASSERT_EQ(0, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004347 ASSERT_EQ(0, 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
4351 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4356 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4357 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004358 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004359 ASSERT_NO_FATAL_FAILURE(
4360 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4363 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4364 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004365 ASSERT_NO_FATAL_FAILURE(
4366 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004367
4368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4369 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4370 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004371 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004372 ASSERT_NO_FATAL_FAILURE(
4373 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004374
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004378 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004380 ASSERT_NO_FATAL_FAILURE(
4381 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004382
4383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004385 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004386 ASSERT_NO_FATAL_FAILURE(
4387 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004392 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4393 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004394 ASSERT_NO_FATAL_FAILURE(
4395 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004398
4399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004402 ASSERT_NO_FATAL_FAILURE(
4403 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004404
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4406 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004408 ASSERT_NO_FATAL_FAILURE(
4409 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410
4411 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4415 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4416 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004417
Michael Wrightd02c5b62014-02-10 15:10:22 -08004418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004419 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004421 ASSERT_NO_FATAL_FAILURE(
4422 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004423
4424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4425 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4426 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004427 ASSERT_NO_FATAL_FAILURE(
4428 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004435 ASSERT_NO_FATAL_FAILURE(
4436 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004437
4438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004440 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004441
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004442 ASSERT_NO_FATAL_FAILURE(
4443 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4445 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4446 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4447
4448 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4452 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4453 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004454
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004456 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004458 ASSERT_NO_FATAL_FAILURE(
4459 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004460
4461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4462 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4463 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004464 ASSERT_NO_FATAL_FAILURE(
4465 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004470 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004472 ASSERT_NO_FATAL_FAILURE(
4473 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004474
4475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4476 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4477 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004478 ASSERT_NO_FATAL_FAILURE(
4479 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004480
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4482 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4483 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4484
4485 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004486 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4487 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4489 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4490 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004491
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004493 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004495 ASSERT_NO_FATAL_FAILURE(
4496 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004497
4498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4499 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4500 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004501 ASSERT_NO_FATAL_FAILURE(
4502 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004504 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004507 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004509 ASSERT_NO_FATAL_FAILURE(
4510 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004511
4512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4513 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4514 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004515 ASSERT_NO_FATAL_FAILURE(
4516 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004517
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4519 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4520 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4521
4522 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004523 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4524 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4526 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4527 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004528
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004530 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004532 ASSERT_NO_FATAL_FAILURE(
4533 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004534
4535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4536 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4537 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004538 ASSERT_NO_FATAL_FAILURE(
4539 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004544 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004546 ASSERT_NO_FATAL_FAILURE(
4547 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004548
4549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4550 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4551 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004552 ASSERT_NO_FATAL_FAILURE(
4553 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004554
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4556 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4557 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4558}
4559
4560TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004561 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004562 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004563
4564 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4565 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566
4567 NotifyMotionArgs args;
4568
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004573 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4574 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4576 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 +00004577 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004578}
4579
4580TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004581 addConfigurationProperty("cursor.mode", "pointer");
4582 mFakePolicy->setPointerCapture(true);
Arpit Singhd8510bd2023-04-27 12:48:15 +00004583 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004584
4585 NotifyDeviceResetArgs resetArgs;
4586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4587 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4588 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4589
4590 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4591 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004592
4593 NotifyMotionArgs args;
4594
4595 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4600 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4601 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4603 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 +00004604 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004605
4606 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004607 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4608 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4610 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4611 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4613 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4615 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4616 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4618 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4619
4620 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004621 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4622 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4624 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4625 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4627 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4629 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4630 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4632 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4633
4634 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4637 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4639 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4642 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 +00004643 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004644
4645 // Disable pointer capture and check that the device generation got bumped
4646 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004647 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004648 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004649 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004650 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004651
4652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004653 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4654
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4659 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4662 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 +00004663 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664}
4665
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004666/**
4667 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4668 * pointer acceleration or speed processing should not be applied.
4669 */
4670TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4671 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004672 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4673 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004674 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhd8510bd2023-04-27 12:48:15 +00004675 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004676
4677 NotifyDeviceResetArgs resetArgs;
4678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4679 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4680 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4681
4682 NotifyMotionArgs args;
4683
4684 // Move and verify scale is applied.
4685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4689 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4690 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4691 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4692 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4693 ASSERT_GT(relX, 10);
4694 ASSERT_GT(relY, 20);
4695
4696 // Enable Pointer Capture
4697 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004698 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004699 NotifyPointerCaptureChangedArgs captureArgs;
4700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4701 ASSERT_TRUE(captureArgs.request.enable);
4702
4703 // Move and verify scale is not applied.
4704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4708 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4709 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4710 ASSERT_EQ(10, args.pointerCoords[0].getX());
4711 ASSERT_EQ(20, args.pointerCoords[0].getY());
4712}
4713
Prabir Pradhan208360b2022-06-24 18:37:04 +00004714TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4715 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004716 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004717
4718 NotifyDeviceResetArgs resetArgs;
4719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4720 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4721 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4722
4723 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004724 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004725
4726 NotifyMotionArgs args;
4727
4728 // Verify that the coordinates are rotated.
4729 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4733 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4734 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4735 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4736 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4737
4738 // Enable Pointer Capture.
4739 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004740 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004741 NotifyPointerCaptureChangedArgs captureArgs;
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4743 ASSERT_TRUE(captureArgs.request.enable);
4744
4745 // Move and verify rotation is not applied.
4746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4750 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4752 ASSERT_EQ(10, args.pointerCoords[0].getX());
4753 ASSERT_EQ(20, args.pointerCoords[0].getY());
4754}
4755
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004756TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004757 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004758
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004759 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004760 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004761
4762 // Set up the secondary display as the display on which the pointer should be shown.
4763 // The InputDevice is not associated with any display.
4764 prepareSecondaryDisplay();
4765 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004766 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004767
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004768 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004769 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004770
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004771 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004776 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4777 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4778 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004779 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004780}
4781
4782TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004783 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004784
4785 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004786 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004787
4788 // Set up the secondary display as the display on which the pointer should be shown,
4789 // and associate the InputDevice with the secondary display.
4790 prepareSecondaryDisplay();
4791 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4792 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004793 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004794
4795 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4796 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004797
4798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004802 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4803 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4804 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004805 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004806}
4807
4808TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004809 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004810
4811 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004812 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004813 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4814
4815 // Associate the InputDevice with the secondary display.
4816 prepareSecondaryDisplay();
4817 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004818 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004819
4820 // The mapper should not generate any events because it is associated with a display that is
4821 // different from the pointer display.
4822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004826}
4827
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004828// --- BluetoothCursorInputMapperTest ---
4829
4830class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4831protected:
4832 void SetUp() override {
4833 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4834
4835 mFakePointerController = std::make_shared<FakePointerController>();
4836 mFakePolicy->setPointerController(mFakePointerController);
4837 }
4838};
4839
4840TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4841 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004842 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004843
4844 nsecs_t kernelEventTime = ARBITRARY_TIME;
4845 nsecs_t expectedEventTime = ARBITRARY_TIME;
4846 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4847 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4849 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4850 WithEventTime(expectedEventTime))));
4851
4852 // Process several events that come in quick succession, according to their timestamps.
4853 for (int i = 0; i < 3; i++) {
4854 constexpr static nsecs_t delta = ms2ns(1);
4855 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4856 kernelEventTime += delta;
4857 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4858
4859 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4860 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4862 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4863 WithEventTime(expectedEventTime))));
4864 }
4865}
4866
4867TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4868 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004869 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004870
4871 nsecs_t expectedEventTime = ARBITRARY_TIME;
4872 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4873 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4875 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4876 WithEventTime(expectedEventTime))));
4877
4878 // Process several events with the same timestamp from the kernel.
4879 // Ensure that we do not generate events too far into the future.
4880 constexpr static int32_t numEvents =
4881 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4882 for (int i = 0; i < numEvents; i++) {
4883 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4884
4885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4888 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4889 WithEventTime(expectedEventTime))));
4890 }
4891
4892 // By processing more events with the same timestamp, we should not generate events with a
4893 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4894 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4895 for (int i = 0; i < 3; i++) {
4896 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4899 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4900 WithEventTime(cappedEventTime))));
4901 }
4902}
4903
4904TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4905 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004906 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004907
4908 nsecs_t kernelEventTime = ARBITRARY_TIME;
4909 nsecs_t expectedEventTime = ARBITRARY_TIME;
4910 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4911 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4913 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4914 WithEventTime(expectedEventTime))));
4915
4916 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4917 // smoothening is not needed, its timestamp is not affected.
4918 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4919 expectedEventTime = kernelEventTime;
4920
4921 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4922 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4924 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4925 WithEventTime(expectedEventTime))));
4926}
4927
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928// --- TouchInputMapperTest ---
4929
4930class TouchInputMapperTest : public InputMapperTest {
4931protected:
4932 static const int32_t RAW_X_MIN;
4933 static const int32_t RAW_X_MAX;
4934 static const int32_t RAW_Y_MIN;
4935 static const int32_t RAW_Y_MAX;
4936 static const int32_t RAW_TOUCH_MIN;
4937 static const int32_t RAW_TOUCH_MAX;
4938 static const int32_t RAW_TOOL_MIN;
4939 static const int32_t RAW_TOOL_MAX;
4940 static const int32_t RAW_PRESSURE_MIN;
4941 static const int32_t RAW_PRESSURE_MAX;
4942 static const int32_t RAW_ORIENTATION_MIN;
4943 static const int32_t RAW_ORIENTATION_MAX;
4944 static const int32_t RAW_DISTANCE_MIN;
4945 static const int32_t RAW_DISTANCE_MAX;
4946 static const int32_t RAW_TILT_MIN;
4947 static const int32_t RAW_TILT_MAX;
4948 static const int32_t RAW_ID_MIN;
4949 static const int32_t RAW_ID_MAX;
4950 static const int32_t RAW_SLOT_MIN;
4951 static const int32_t RAW_SLOT_MAX;
4952 static const float X_PRECISION;
4953 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004954 static const float X_PRECISION_VIRTUAL;
4955 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956
4957 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004958 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004959
4960 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4961
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004962 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004963 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004964
Michael Wrightd02c5b62014-02-10 15:10:22 -08004965 enum Axes {
4966 POSITION = 1 << 0,
4967 TOUCH = 1 << 1,
4968 TOOL = 1 << 2,
4969 PRESSURE = 1 << 3,
4970 ORIENTATION = 1 << 4,
4971 MINOR = 1 << 5,
4972 ID = 1 << 6,
4973 DISTANCE = 1 << 7,
4974 TILT = 1 << 8,
4975 SLOT = 1 << 9,
4976 TOOL_TYPE = 1 << 10,
4977 };
4978
Michael Wrighta9cf4192022-12-01 23:46:39 +00004979 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004980 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004981 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004983 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984 int32_t toRawX(float displayX);
4985 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004986 int32_t toRotatedRawX(float displayX);
4987 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004988 float toCookedX(float rawX, float rawY);
4989 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004990 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004991 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004993 float toDisplayY(int32_t rawY, int32_t displayHeight);
4994
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995};
4996
4997const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4998const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4999const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5000const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5001const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5002const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5003const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5004const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005005const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5006const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005007const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5008const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5009const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5010const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5011const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5012const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5013const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5014const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5015const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5016const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5017const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5018const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005019const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5020 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5021const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5022 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005023const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5024 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005025
5026const float TouchInputMapperTest::GEOMETRIC_SCALE =
5027 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5028 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5029
5030const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5031 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5032 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5033};
5034
Michael Wrighta9cf4192022-12-01 23:46:39 +00005035void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005036 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5037 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005038}
5039
5040void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5041 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00005042 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043}
5044
Michael Wrighta9cf4192022-12-01 23:46:39 +00005045void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005046 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5047 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5048 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005049}
5050
Michael Wrightd02c5b62014-02-10 15:10:22 -08005051void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005052 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5053 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5054 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5055 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005056}
5057
Jason Gerecke489fda82012-09-07 17:19:40 -07005058void TouchInputMapperTest::prepareLocationCalibration() {
5059 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5060}
5061
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062int32_t TouchInputMapperTest::toRawX(float displayX) {
5063 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5064}
5065
5066int32_t TouchInputMapperTest::toRawY(float displayY) {
5067 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5068}
5069
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005070int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5071 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5072}
5073
5074int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5075 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5076}
5077
Jason Gerecke489fda82012-09-07 17:19:40 -07005078float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5079 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5080 return rawX;
5081}
5082
5083float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5084 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5085 return rawY;
5086}
5087
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005089 return toDisplayX(rawX, DISPLAY_WIDTH);
5090}
5091
5092float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5093 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005094}
5095
5096float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005097 return toDisplayY(rawY, DISPLAY_HEIGHT);
5098}
5099
5100float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5101 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102}
5103
5104
5105// --- SingleTouchInputMapperTest ---
5106
5107class SingleTouchInputMapperTest : public TouchInputMapperTest {
5108protected:
5109 void prepareButtons();
5110 void prepareAxes(int axes);
5111
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005112 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5113 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5114 void processUp(SingleTouchInputMapper& mappery);
5115 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5116 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5117 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5118 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5119 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5120 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005121};
5122
5123void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005124 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125}
5126
5127void SingleTouchInputMapperTest::prepareAxes(int axes) {
5128 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005129 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5130 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005131 }
5132 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005133 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5134 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005135 }
5136 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005137 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5138 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005139 }
5140 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005141 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5142 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143 }
5144 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005145 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5146 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005147 }
5148}
5149
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005150void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005154}
5155
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005156void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005159}
5160
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005161void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005163}
5164
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005165void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005167}
5168
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005169void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5170 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172}
5173
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005174void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176}
5177
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005178void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5179 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182}
5183
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005184void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5185 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005187}
5188
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005189void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005191}
5192
Michael Wrightd02c5b62014-02-10 15:10:22 -08005193TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194 prepareButtons();
5195 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00005196 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005197
Josep del Río2d8c79a2023-01-23 19:33:50 +00005198 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005199}
5200
Michael Wrightd02c5b62014-02-10 15:10:22 -08005201TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005202 prepareButtons();
5203 prepareAxes(POSITION);
5204 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00005205 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005206
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005207 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005208}
5209
5210TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005211 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005212 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005213 prepareButtons();
5214 prepareAxes(POSITION);
5215 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005216 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217
5218 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005219 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220
5221 // Virtual key is down.
5222 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5223 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5224 processDown(mapper, x, y);
5225 processSync(mapper);
5226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5227
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005228 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229
5230 // Virtual key is up.
5231 processUp(mapper);
5232 processSync(mapper);
5233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5234
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005235 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005236}
5237
5238TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005239 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005240 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005241 prepareButtons();
5242 prepareAxes(POSITION);
5243 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005244 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005245
5246 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005247 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248
5249 // Virtual key is down.
5250 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5251 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5252 processDown(mapper, x, y);
5253 processSync(mapper);
5254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5255
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005256 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005257
5258 // Virtual key is up.
5259 processUp(mapper);
5260 processSync(mapper);
5261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5262
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005263 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264}
5265
5266TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005267 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005268 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005269 prepareButtons();
5270 prepareAxes(POSITION);
5271 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005272 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005273
Michael Wrightd02c5b62014-02-10 15:10:22 -08005274 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005275 ASSERT_TRUE(
5276 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005277 ASSERT_TRUE(flags[0]);
5278 ASSERT_FALSE(flags[1]);
5279}
5280
5281TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005283 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005284 prepareButtons();
5285 prepareAxes(POSITION);
5286 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005287 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005288
arthurhungdcef2dc2020-08-11 14:47:50 +08005289 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290
5291 NotifyKeyArgs args;
5292
5293 // Press virtual key.
5294 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5295 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5296 processDown(mapper, x, y);
5297 processSync(mapper);
5298
5299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5300 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5301 ASSERT_EQ(DEVICE_ID, args.deviceId);
5302 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5303 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5304 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5305 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5306 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5307 ASSERT_EQ(KEY_HOME, args.scanCode);
5308 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5309 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5310
5311 // Release virtual key.
5312 processUp(mapper);
5313 processSync(mapper);
5314
5315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5316 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5317 ASSERT_EQ(DEVICE_ID, args.deviceId);
5318 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5319 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5320 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5321 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5322 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5323 ASSERT_EQ(KEY_HOME, args.scanCode);
5324 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5325 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5326
5327 // Should not have sent any motions.
5328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5329}
5330
5331TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005332 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005333 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005334 prepareButtons();
5335 prepareAxes(POSITION);
5336 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005337 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005338
arthurhungdcef2dc2020-08-11 14:47:50 +08005339 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340
5341 NotifyKeyArgs keyArgs;
5342
5343 // Press virtual key.
5344 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5345 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5346 processDown(mapper, x, y);
5347 processSync(mapper);
5348
5349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5350 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5351 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5352 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5353 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5354 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5355 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5356 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5357 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5358 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5359 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5360
5361 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5362 // into the display area.
5363 y -= 100;
5364 processMove(mapper, x, y);
5365 processSync(mapper);
5366
5367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5368 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5369 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5370 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5371 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5372 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5373 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5374 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5375 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5376 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5377 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5378 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5379
5380 NotifyMotionArgs motionArgs;
5381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5382 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5383 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5384 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5385 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5386 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5387 ASSERT_EQ(0, motionArgs.flags);
5388 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5389 ASSERT_EQ(0, motionArgs.buttonState);
5390 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005391 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005392 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005393 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5395 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5396 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5397 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5398 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5399
5400 // Keep moving out of bounds. Should generate a pointer move.
5401 y -= 50;
5402 processMove(mapper, x, y);
5403 processSync(mapper);
5404
5405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5406 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5407 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5408 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5409 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5411 ASSERT_EQ(0, motionArgs.flags);
5412 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5413 ASSERT_EQ(0, motionArgs.buttonState);
5414 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005415 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005417 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5419 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5420 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5421 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5422 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5423
5424 // Release out of bounds. Should generate a pointer up.
5425 processUp(mapper);
5426 processSync(mapper);
5427
5428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5429 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5430 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5431 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5432 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5433 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5434 ASSERT_EQ(0, motionArgs.flags);
5435 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5436 ASSERT_EQ(0, motionArgs.buttonState);
5437 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005438 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005439 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005440 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5442 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5443 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5444 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5445 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5446
5447 // Should not have sent any more keys or motions.
5448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5450}
5451
5452TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005453 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005454 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455 prepareButtons();
5456 prepareAxes(POSITION);
5457 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005458 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005459
arthurhungdcef2dc2020-08-11 14:47:50 +08005460 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461
5462 NotifyMotionArgs motionArgs;
5463
5464 // Initially go down out of bounds.
5465 int32_t x = -10;
5466 int32_t y = -10;
5467 processDown(mapper, x, y);
5468 processSync(mapper);
5469
5470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5471
5472 // Move into the display area. Should generate a pointer down.
5473 x = 50;
5474 y = 75;
5475 processMove(mapper, x, y);
5476 processSync(mapper);
5477
5478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5479 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5480 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5481 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5482 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5483 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5484 ASSERT_EQ(0, motionArgs.flags);
5485 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5486 ASSERT_EQ(0, motionArgs.buttonState);
5487 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005488 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005489 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005490 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5492 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5493 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5494 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5495 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5496
5497 // Release. Should generate a pointer up.
5498 processUp(mapper);
5499 processSync(mapper);
5500
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5502 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5503 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5504 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5505 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5506 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5507 ASSERT_EQ(0, motionArgs.flags);
5508 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5509 ASSERT_EQ(0, motionArgs.buttonState);
5510 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005511 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005513 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5515 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5516 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5517 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5518 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5519
5520 // Should not have sent any more keys or motions.
5521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5523}
5524
Santos Cordonfa5cf462017-04-05 10:37:00 -07005525TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005526 addConfigurationProperty("touch.deviceType", "touchScreen");
5527 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5528
Michael Wrighta9cf4192022-12-01 23:46:39 +00005529 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005530 prepareButtons();
5531 prepareAxes(POSITION);
5532 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005533 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005534
arthurhungdcef2dc2020-08-11 14:47:50 +08005535 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005536
5537 NotifyMotionArgs motionArgs;
5538
5539 // Down.
5540 int32_t x = 100;
5541 int32_t y = 125;
5542 processDown(mapper, x, y);
5543 processSync(mapper);
5544
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5546 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5547 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5548 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5549 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5550 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5551 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5552 ASSERT_EQ(0, motionArgs.flags);
5553 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5554 ASSERT_EQ(0, motionArgs.buttonState);
5555 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005556 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005557 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005558 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5560 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5561 1, 0, 0, 0, 0, 0, 0, 0));
5562 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5563 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5564 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5565
5566 // Move.
5567 x += 50;
5568 y += 75;
5569 processMove(mapper, x, y);
5570 processSync(mapper);
5571
5572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5573 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5574 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5575 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5576 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5577 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5579 ASSERT_EQ(0, motionArgs.flags);
5580 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5581 ASSERT_EQ(0, motionArgs.buttonState);
5582 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005583 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005584 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005585 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5587 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5588 1, 0, 0, 0, 0, 0, 0, 0));
5589 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5590 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5591 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5592
5593 // Up.
5594 processUp(mapper);
5595 processSync(mapper);
5596
5597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5598 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5599 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5600 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5601 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5602 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5603 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5604 ASSERT_EQ(0, motionArgs.flags);
5605 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5606 ASSERT_EQ(0, motionArgs.buttonState);
5607 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005608 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005609 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005610 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5612 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5613 1, 0, 0, 0, 0, 0, 0, 0));
5614 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5615 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5616 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5617
5618 // Should not have sent any more keys or motions.
5619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5621}
5622
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005624 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005625 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005626 prepareButtons();
5627 prepareAxes(POSITION);
5628 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005629 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005630
arthurhungdcef2dc2020-08-11 14:47:50 +08005631 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005632
5633 NotifyMotionArgs motionArgs;
5634
5635 // Down.
5636 int32_t x = 100;
5637 int32_t y = 125;
5638 processDown(mapper, x, y);
5639 processSync(mapper);
5640
5641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5642 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5643 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5644 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5645 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5646 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5647 ASSERT_EQ(0, motionArgs.flags);
5648 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5649 ASSERT_EQ(0, motionArgs.buttonState);
5650 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005651 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005652 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005653 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5655 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5656 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5657 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5658 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5659
5660 // Move.
5661 x += 50;
5662 y += 75;
5663 processMove(mapper, x, y);
5664 processSync(mapper);
5665
5666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5667 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5668 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5669 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5670 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5671 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5672 ASSERT_EQ(0, motionArgs.flags);
5673 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5674 ASSERT_EQ(0, motionArgs.buttonState);
5675 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005676 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005677 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005678 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5680 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5681 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5682 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5683 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5684
5685 // Up.
5686 processUp(mapper);
5687 processSync(mapper);
5688
5689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5690 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5691 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5692 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5693 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5694 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5695 ASSERT_EQ(0, motionArgs.flags);
5696 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5697 ASSERT_EQ(0, motionArgs.buttonState);
5698 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005699 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005700 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005701 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5703 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5704 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5705 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5706 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5707
5708 // Should not have sent any more keys or motions.
5709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5711}
5712
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005713TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005714 addConfigurationProperty("touch.deviceType", "touchScreen");
5715 prepareButtons();
5716 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005717 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5718 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singh56adebc2023-04-25 13:56:05 +00005719 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005720
5721 NotifyMotionArgs args;
5722
5723 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005724 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005725 processDown(mapper, toRawX(50), toRawY(75));
5726 processSync(mapper);
5727
5728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5729 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5730 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5731
5732 processUp(mapper);
5733 processSync(mapper);
5734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5735}
5736
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005737TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005738 addConfigurationProperty("touch.deviceType", "touchScreen");
5739 prepareButtons();
5740 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005741 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5742 // orientation-aware are affected by display rotation.
5743 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00005744 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005745
5746 NotifyMotionArgs args;
5747
5748 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005749 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005750 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005751 processDown(mapper, toRawX(50), toRawY(75));
5752 processSync(mapper);
5753
5754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5755 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5756 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5757
5758 processUp(mapper);
5759 processSync(mapper);
5760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5761
5762 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005763 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005764 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005765 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005766 processSync(mapper);
5767
5768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5769 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5770 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5771
5772 processUp(mapper);
5773 processSync(mapper);
5774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5775
5776 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005777 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005778 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005779 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5780 processSync(mapper);
5781
5782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5783 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5784 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5785
5786 processUp(mapper);
5787 processSync(mapper);
5788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5789
5790 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005791 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005792 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005793 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005794 processSync(mapper);
5795
5796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5797 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5798 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5799
5800 processUp(mapper);
5801 processSync(mapper);
5802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5803}
5804
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005805TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5806 addConfigurationProperty("touch.deviceType", "touchScreen");
5807 prepareButtons();
5808 prepareAxes(POSITION);
5809 addConfigurationProperty("touch.orientationAware", "1");
5810 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5811 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005812 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005813 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005814 NotifyMotionArgs args;
5815
5816 // Orientation 0.
5817 processDown(mapper, toRawX(50), toRawY(75));
5818 processSync(mapper);
5819
5820 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5821 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5822 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5823
5824 processUp(mapper);
5825 processSync(mapper);
5826 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5827}
5828
5829TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5830 addConfigurationProperty("touch.deviceType", "touchScreen");
5831 prepareButtons();
5832 prepareAxes(POSITION);
5833 addConfigurationProperty("touch.orientationAware", "1");
5834 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5835 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005836 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005837 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005838 NotifyMotionArgs args;
5839
5840 // Orientation 90.
5841 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5842 processSync(mapper);
5843
5844 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5845 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5846 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5847
5848 processUp(mapper);
5849 processSync(mapper);
5850 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5851}
5852
5853TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5854 addConfigurationProperty("touch.deviceType", "touchScreen");
5855 prepareButtons();
5856 prepareAxes(POSITION);
5857 addConfigurationProperty("touch.orientationAware", "1");
5858 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5859 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005860 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005861 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005862 NotifyMotionArgs args;
5863
5864 // Orientation 180.
5865 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + 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
5877TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5878 addConfigurationProperty("touch.deviceType", "touchScreen");
5879 prepareButtons();
5880 prepareAxes(POSITION);
5881 addConfigurationProperty("touch.orientationAware", "1");
5882 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5883 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005884 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005885 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005886 NotifyMotionArgs args;
5887
5888 // Orientation 270.
5889 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5890 processSync(mapper);
5891
5892 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5893 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5894 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5895
5896 processUp(mapper);
5897 processSync(mapper);
5898 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5899}
5900
5901TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5902 addConfigurationProperty("touch.deviceType", "touchScreen");
5903 prepareButtons();
5904 prepareAxes(POSITION);
5905 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5906 // orientation-aware are affected by display rotation.
5907 addConfigurationProperty("touch.orientationAware", "0");
5908 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singh56adebc2023-04-25 13:56:05 +00005909 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005910
5911 NotifyMotionArgs args;
5912
5913 // Orientation 90, Rotation 0.
5914 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005915 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005916 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5917 processSync(mapper);
5918
5919 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5920 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5921 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5922
5923 processUp(mapper);
5924 processSync(mapper);
5925 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5926
5927 // Orientation 90, Rotation 90.
5928 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005929 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005930 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005931 processSync(mapper);
5932
5933 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5934 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5935 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5936
5937 processUp(mapper);
5938 processSync(mapper);
5939 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5940
5941 // Orientation 90, Rotation 180.
5942 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005943 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005944 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5945 processSync(mapper);
5946
5947 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5948 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5949 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5950
5951 processUp(mapper);
5952 processSync(mapper);
5953 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5954
5955 // Orientation 90, Rotation 270.
5956 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005957 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005958 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 -07005959 processSync(mapper);
5960
5961 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5962 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5963 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5964
5965 processUp(mapper);
5966 processSync(mapper);
5967 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5968}
5969
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005970TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5971 addConfigurationProperty("touch.deviceType", "touchScreen");
5972 prepareButtons();
5973 prepareAxes(POSITION);
5974 addConfigurationProperty("touch.orientationAware", "1");
5975 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005976 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005977
5978 // Set a physical frame in the display viewport.
5979 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5980 viewport->physicalLeft = 20;
5981 viewport->physicalTop = 600;
5982 viewport->physicalRight = 30;
5983 viewport->physicalBottom = 610;
5984 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005985 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005986
5987 // Start the touch.
5988 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5989 processSync(mapper);
5990
5991 // Expect all input starting outside the physical frame to be ignored.
5992 const std::array<Point, 6> outsidePoints = {
5993 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5994 for (const auto& p : outsidePoints) {
5995 processMove(mapper, toRawX(p.x), toRawY(p.y));
5996 processSync(mapper);
5997 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5998 }
5999
6000 // Move the touch into the physical frame.
6001 processMove(mapper, toRawX(25), toRawY(605));
6002 processSync(mapper);
6003 NotifyMotionArgs args;
6004 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6005 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6006 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6007 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6008
6009 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
6010 for (const auto& p : outsidePoints) {
6011 processMove(mapper, toRawX(p.x), toRawY(p.y));
6012 processSync(mapper);
6013 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6014 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
6015 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6016 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6017 }
6018
6019 processUp(mapper);
6020 processSync(mapper);
6021 EXPECT_NO_FATAL_FAILURE(
6022 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
6023}
6024
Harry Cutts1db43992023-06-19 17:05:07 +00006025TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
6026 std::shared_ptr<FakePointerController> fakePointerController =
6027 std::make_shared<FakePointerController>();
6028 mFakePolicy->setPointerController(fakePointerController);
6029
6030 addConfigurationProperty("touch.deviceType", "pointer");
6031 prepareAxes(POSITION);
6032 prepareDisplay(ui::ROTATION_0);
6033 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
6034
6035 // Set a physical frame in the display viewport.
6036 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6037 viewport->physicalLeft = 20;
6038 viewport->physicalTop = 600;
6039 viewport->physicalRight = 30;
6040 viewport->physicalBottom = 610;
6041 mFakePolicy->updateViewport(*viewport);
6042 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
6043
6044 // Start the touch.
6045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6046 processSync(mapper);
6047
6048 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
6049 // produced.
6050 const std::array<Point, 6> outsidePoints = {
6051 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6052 for (const auto& p : outsidePoints) {
6053 processMove(mapper, toRawX(p.x), toRawY(p.y));
6054 processSync(mapper);
6055 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6056 }
6057}
6058
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006060 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006061 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006062 prepareButtons();
6063 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singh56adebc2023-04-25 13:56:05 +00006064 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065
6066 // These calculations are based on the input device calibration documentation.
6067 int32_t rawX = 100;
6068 int32_t rawY = 200;
6069 int32_t rawPressure = 10;
6070 int32_t rawToolMajor = 12;
6071 int32_t rawDistance = 2;
6072 int32_t rawTiltX = 30;
6073 int32_t rawTiltY = 110;
6074
6075 float x = toDisplayX(rawX);
6076 float y = toDisplayY(rawY);
6077 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6078 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6079 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6080 float distance = float(rawDistance);
6081
6082 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6083 float tiltScale = M_PI / 180;
6084 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6085 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6086 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6087 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6088
6089 processDown(mapper, rawX, rawY);
6090 processPressure(mapper, rawPressure);
6091 processToolMajor(mapper, rawToolMajor);
6092 processDistance(mapper, rawDistance);
6093 processTilt(mapper, rawTiltX, rawTiltY);
6094 processSync(mapper);
6095
6096 NotifyMotionArgs args;
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6099 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6100 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6101}
6102
Jason Gerecke489fda82012-09-07 17:19:40 -07006103TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006104 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006105 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006106 prepareLocationCalibration();
6107 prepareButtons();
6108 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006109 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006110
6111 int32_t rawX = 100;
6112 int32_t rawY = 200;
6113
6114 float x = toDisplayX(toCookedX(rawX, rawY));
6115 float y = toDisplayY(toCookedY(rawX, rawY));
6116
6117 processDown(mapper, rawX, rawY);
6118 processSync(mapper);
6119
6120 NotifyMotionArgs args;
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6122 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6123 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6124}
6125
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006127 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006128 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129 prepareButtons();
6130 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006131 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132
6133 NotifyMotionArgs motionArgs;
6134 NotifyKeyArgs keyArgs;
6135
6136 processDown(mapper, 100, 200);
6137 processSync(mapper);
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6139 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6140 ASSERT_EQ(0, motionArgs.buttonState);
6141
6142 // press BTN_LEFT, release BTN_LEFT
6143 processKey(mapper, BTN_LEFT, 1);
6144 processSync(mapper);
6145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6147 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6148
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6150 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6151 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6152
Michael Wrightd02c5b62014-02-10 15:10:22 -08006153 processKey(mapper, BTN_LEFT, 0);
6154 processSync(mapper);
6155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006156 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006157 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006158
6159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006161 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006162
6163 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6164 processKey(mapper, BTN_RIGHT, 1);
6165 processKey(mapper, BTN_MIDDLE, 1);
6166 processSync(mapper);
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6169 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6170 motionArgs.buttonState);
6171
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6173 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6174 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6175
6176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6177 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6178 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6179 motionArgs.buttonState);
6180
Michael Wrightd02c5b62014-02-10 15:10:22 -08006181 processKey(mapper, BTN_RIGHT, 0);
6182 processSync(mapper);
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006184 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006186
6187 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_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190
6191 processKey(mapper, BTN_MIDDLE, 0);
6192 processSync(mapper);
6193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006194 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006195 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006196
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006199 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006200
6201 // press BTN_BACK, release BTN_BACK
6202 processKey(mapper, BTN_BACK, 1);
6203 processSync(mapper);
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6205 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6206 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006207
Michael Wrightd02c5b62014-02-10 15:10:22 -08006208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006210 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6211
6212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6213 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6214 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006215
6216 processKey(mapper, BTN_BACK, 0);
6217 processSync(mapper);
6218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006219 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006221
6222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006224 ASSERT_EQ(0, motionArgs.buttonState);
6225
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6227 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6228 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6229
6230 // press BTN_SIDE, release BTN_SIDE
6231 processKey(mapper, BTN_SIDE, 1);
6232 processSync(mapper);
6233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6234 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6235 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006236
Michael Wrightd02c5b62014-02-10 15:10:22 -08006237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006239 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6240
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6242 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6243 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244
6245 processKey(mapper, BTN_SIDE, 0);
6246 processSync(mapper);
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006248 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006250
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006253 ASSERT_EQ(0, motionArgs.buttonState);
6254
Michael Wrightd02c5b62014-02-10 15:10:22 -08006255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6256 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6257 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6258
6259 // press BTN_FORWARD, release BTN_FORWARD
6260 processKey(mapper, BTN_FORWARD, 1);
6261 processSync(mapper);
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6263 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6264 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006265
Michael Wrightd02c5b62014-02-10 15:10:22 -08006266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006268 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6269
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6272 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273
6274 processKey(mapper, BTN_FORWARD, 0);
6275 processSync(mapper);
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006279
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006282 ASSERT_EQ(0, motionArgs.buttonState);
6283
Michael Wrightd02c5b62014-02-10 15:10:22 -08006284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6285 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6286 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6287
6288 // press BTN_EXTRA, release BTN_EXTRA
6289 processKey(mapper, BTN_EXTRA, 1);
6290 processSync(mapper);
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6292 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6293 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294
Michael Wrightd02c5b62014-02-10 15:10:22 -08006295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006297 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6298
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6301 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006302
6303 processKey(mapper, BTN_EXTRA, 0);
6304 processSync(mapper);
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006306 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006308
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006311 ASSERT_EQ(0, motionArgs.buttonState);
6312
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6314 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6315 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6316
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6318
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 // press BTN_STYLUS, release BTN_STYLUS
6320 processKey(mapper, BTN_STYLUS, 1);
6321 processSync(mapper);
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006324 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6325
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6327 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6328 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329
6330 processKey(mapper, BTN_STYLUS, 0);
6331 processSync(mapper);
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006333 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006334 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006335
6336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006338 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339
6340 // press BTN_STYLUS2, release BTN_STYLUS2
6341 processKey(mapper, BTN_STYLUS2, 1);
6342 processSync(mapper);
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006345 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6346
6347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6348 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6349 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006350
6351 processKey(mapper, BTN_STYLUS2, 0);
6352 processSync(mapper);
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006355 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006356
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006359 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360
6361 // release touch
6362 processUp(mapper);
6363 processSync(mapper);
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6365 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6366 ASSERT_EQ(0, motionArgs.buttonState);
6367}
6368
6369TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006371 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372 prepareButtons();
6373 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006374 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006375
6376 NotifyMotionArgs motionArgs;
6377
6378 // default tool type is finger
6379 processDown(mapper, 100, 200);
6380 processSync(mapper);
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, 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 // eraser
6386 processKey(mapper, BTN_TOOL_RUBBER, 1);
6387 processSync(mapper);
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006390 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006391
6392 // stylus
6393 processKey(mapper, BTN_TOOL_RUBBER, 0);
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 // brush
6401 processKey(mapper, BTN_TOOL_PEN, 0);
6402 processKey(mapper, BTN_TOOL_BRUSH, 1);
6403 processSync(mapper);
6404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006406 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006407
6408 // pencil
6409 processKey(mapper, BTN_TOOL_BRUSH, 0);
6410 processKey(mapper, BTN_TOOL_PENCIL, 1);
6411 processSync(mapper);
6412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6413 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006414 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006415
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006416 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417 processKey(mapper, BTN_TOOL_PENCIL, 0);
6418 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006423
6424 // mouse
6425 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6426 processKey(mapper, BTN_TOOL_MOUSE, 1);
6427 processSync(mapper);
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006430 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006431
6432 // lens
6433 processKey(mapper, BTN_TOOL_MOUSE, 0);
6434 processKey(mapper, BTN_TOOL_LENS, 1);
6435 processSync(mapper);
6436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006438 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006439
6440 // double-tap
6441 processKey(mapper, BTN_TOOL_LENS, 0);
6442 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6443 processSync(mapper);
6444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006446 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006447
6448 // triple-tap
6449 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6450 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006454 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006455
6456 // quad-tap
6457 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6458 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6459 processSync(mapper);
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006462 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006463
6464 // finger
6465 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6466 processKey(mapper, BTN_TOOL_FINGER, 1);
6467 processSync(mapper);
6468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006470 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006471
6472 // stylus trumps finger
6473 processKey(mapper, BTN_TOOL_PEN, 1);
6474 processSync(mapper);
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6476 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006477 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478
6479 // eraser trumps stylus
6480 processKey(mapper, BTN_TOOL_RUBBER, 1);
6481 processSync(mapper);
6482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006484 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006485
6486 // mouse trumps eraser
6487 processKey(mapper, BTN_TOOL_MOUSE, 1);
6488 processSync(mapper);
6489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006491 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006492
6493 // back to default tool type
6494 processKey(mapper, BTN_TOOL_MOUSE, 0);
6495 processKey(mapper, BTN_TOOL_RUBBER, 0);
6496 processKey(mapper, BTN_TOOL_PEN, 0);
6497 processKey(mapper, BTN_TOOL_FINGER, 0);
6498 processSync(mapper);
6499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6500 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006501 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006502}
6503
6504TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006505 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006506 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006507 prepareButtons();
6508 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006509 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +00006510 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511
6512 NotifyMotionArgs motionArgs;
6513
6514 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6515 processKey(mapper, BTN_TOOL_FINGER, 1);
6516 processMove(mapper, 100, 200);
6517 processSync(mapper);
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6519 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6521 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6522
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(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6527
6528 // move a little
6529 processMove(mapper, 150, 250);
6530 processSync(mapper);
6531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6532 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // down when BTN_TOUCH is pressed, pressure defaults to 1
6537 processKey(mapper, BTN_TOUCH, 1);
6538 processSync(mapper);
6539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6540 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6542 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6543
6544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6545 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, 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 // up when BTN_TOUCH is released, hover restored
6550 processKey(mapper, BTN_TOUCH, 0);
6551 processSync(mapper);
6552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6553 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6555 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6556
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6558 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6560 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6561
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6563 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // exit hover when pointer goes away
6568 processKey(mapper, BTN_TOOL_FINGER, 0);
6569 processSync(mapper);
6570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6571 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6573 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6574}
6575
6576TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006577 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006578 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006579 prepareButtons();
6580 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006581 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006582
6583 NotifyMotionArgs motionArgs;
6584
6585 // initially hovering because pressure is 0
6586 processDown(mapper, 100, 200);
6587 processPressure(mapper, 0);
6588 processSync(mapper);
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6590 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6592 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6593
6594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6595 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6597 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6598
6599 // move a little
6600 processMove(mapper, 150, 250);
6601 processSync(mapper);
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6603 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6605 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6606
6607 // down when pressure is non-zero
6608 processPressure(mapper, RAW_PRESSURE_MAX);
6609 processSync(mapper);
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6611 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6613 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6614
6615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6616 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6618 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6619
6620 // up when pressure becomes 0, hover restored
6621 processPressure(mapper, 0);
6622 processSync(mapper);
6623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6624 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6626 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6627
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6629 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6631 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6632
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6634 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6636 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6637
6638 // exit hover when pointer goes away
6639 processUp(mapper);
6640 processSync(mapper);
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6642 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6644 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6645}
6646
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006647TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6648 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006649 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006650 prepareButtons();
6651 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006652 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006653
6654 // Touch down.
6655 processDown(mapper, 100, 200);
6656 processPressure(mapper, 1);
6657 processSync(mapper);
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6659 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6660
6661 // Reset the mapper. This should cancel the ongoing gesture.
6662 resetMapper(mapper, ARBITRARY_TIME);
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6664 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6665
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6667}
6668
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006669TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6670 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006671 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006672 prepareButtons();
6673 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006674 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006675
6676 // Set the initial state for the touch pointer.
6677 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6678 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6679 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6680 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6681
6682 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006683 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6684 // does not generate any events.
6685 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006686
6687 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6688 // the recreated touch state to generate a down event.
6689 processSync(mapper);
6690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6691 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6692
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6694}
6695
lilinnan687e58f2022-07-19 16:00:50 +08006696TEST_F(SingleTouchInputMapperTest,
6697 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6698 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006699 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006700 prepareButtons();
6701 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006702 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006703 NotifyMotionArgs motionArgs;
6704
6705 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006706 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006707 processSync(mapper);
6708
6709 // We should receive a down event
6710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6711 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6712
6713 // Change display id
6714 clearViewports();
6715 prepareSecondaryDisplay(ViewportType::INTERNAL);
6716
6717 // We should receive a cancel event
6718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6719 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6720 // Then receive reset called
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6722}
6723
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006724TEST_F(SingleTouchInputMapperTest,
6725 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6726 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006727 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006728 prepareButtons();
6729 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006730 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6732 NotifyMotionArgs motionArgs;
6733
6734 // Start a new gesture.
6735 processDown(mapper, 100, 200);
6736 processSync(mapper);
6737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6738 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6739
6740 // Make the viewport inactive. This will put the device in disabled mode.
6741 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6742 viewport->isActive = false;
6743 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006744 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006745
6746 // We should receive a cancel event for the ongoing gesture.
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6748 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6749 // Then we should be notified that the device was reset.
6750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6751
6752 // No events are generated while the viewport is inactive.
6753 processMove(mapper, 101, 201);
6754 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006755 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006756 processSync(mapper);
6757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6758
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006759 // Start a new gesture while the viewport is still inactive.
6760 processDown(mapper, 300, 400);
6761 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6762 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6763 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6764 processSync(mapper);
6765
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006766 // Make the viewport active again. The device should resume processing events.
6767 viewport->isActive = true;
6768 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006769 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006770
6771 // The device is reset because it changes back to direct mode, without generating any events.
6772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6774
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006775 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006776 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6778 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006779
6780 // No more events.
6781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6783}
6784
Prabir Pradhan211ba622022-10-31 21:09:21 +00006785TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6786 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006787 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006788 prepareButtons();
6789 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006790 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6792
6793 // Press a stylus button.
6794 processKey(mapper, BTN_STYLUS, 1);
6795 processSync(mapper);
6796
6797 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6798 processDown(mapper, 100, 200);
6799 processSync(mapper);
6800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6801 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6802 WithCoords(toDisplayX(100), toDisplayY(200)),
6803 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6805 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6806 WithCoords(toDisplayX(100), toDisplayY(200)),
6807 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6808
6809 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6810 // the button has not actually been released, since there will be no pointers through which the
6811 // button state can be reported. The event is generated at the location of the pointer before
6812 // it went up.
6813 processUp(mapper);
6814 processSync(mapper);
6815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6816 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6817 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6819 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6820 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6821}
6822
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006823TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6824 addConfigurationProperty("touch.deviceType", "touchScreen");
6825 prepareDisplay(ui::ROTATION_0);
6826 prepareButtons();
6827 prepareAxes(POSITION);
6828
6829 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6830
Arpit Singh56adebc2023-04-25 13:56:05 +00006831 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6833
6834 // Press a stylus button.
6835 processKey(mapper, BTN_STYLUS, 1);
6836 processSync(mapper);
6837
6838 // Start a touch gesture and ensure that the stylus button is not reported.
6839 processDown(mapper, 100, 200);
6840 processSync(mapper);
6841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6842 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6843
6844 // Release and press the stylus button again.
6845 processKey(mapper, BTN_STYLUS, 0);
6846 processSync(mapper);
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6848 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6849 processKey(mapper, BTN_STYLUS, 1);
6850 processSync(mapper);
6851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6852 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6853
6854 // Release the touch gesture.
6855 processUp(mapper);
6856 processSync(mapper);
6857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6858 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6859
6860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6861}
6862
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006863TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6864 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6865 prepareDisplay(ui::ROTATION_0);
6866 prepareButtons();
6867 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006868 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6870
6871 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6872}
6873
Seunghwan Choi356026c2023-02-01 14:37:25 +09006874TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6875 std::shared_ptr<FakePointerController> fakePointerController =
6876 std::make_shared<FakePointerController>();
6877 addConfigurationProperty("touch.deviceType", "touchScreen");
6878 prepareDisplay(ui::ROTATION_0);
6879 prepareButtons();
6880 prepareAxes(POSITION);
6881 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6882 mFakePolicy->setPointerController(fakePointerController);
6883 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singh56adebc2023-04-25 13:56:05 +00006884 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006885
6886 processKey(mapper, BTN_TOOL_PEN, 1);
6887 processMove(mapper, 100, 200);
6888 processSync(mapper);
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6890 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006891 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006892 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6893 ASSERT_TRUE(fakePointerController->isPointerShown());
6894 ASSERT_NO_FATAL_FAILURE(
6895 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6896}
6897
6898TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6899 std::shared_ptr<FakePointerController> fakePointerController =
6900 std::make_shared<FakePointerController>();
6901 addConfigurationProperty("touch.deviceType", "touchScreen");
6902 prepareDisplay(ui::ROTATION_0);
6903 prepareButtons();
6904 prepareAxes(POSITION);
6905 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6906 mFakePolicy->setPointerController(fakePointerController);
6907 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singh56adebc2023-04-25 13:56:05 +00006908 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006909
6910 processKey(mapper, BTN_TOOL_PEN, 1);
6911 processMove(mapper, 100, 200);
6912 processSync(mapper);
6913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6914 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006915 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006916 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6917 ASSERT_FALSE(fakePointerController->isPointerShown());
6918}
6919
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006920TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6921 // Initialize the device without setting device source to touch navigation.
6922 addConfigurationProperty("touch.deviceType", "touchScreen");
6923 prepareDisplay(ui::ROTATION_0);
6924 prepareButtons();
6925 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006926 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006927
6928 // Ensure that the device is created as a touchscreen, not touch navigation.
6929 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6930
6931 // Add device type association after the device was created.
6932 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6933
6934 // Send update to the mapper.
6935 std::list<NotifyArgs> unused2 =
6936 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006937 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006938
6939 // Check whether device type update was successful.
6940 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6941}
6942
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006943TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6944 // Initialize the device without setting device source to touch navigation.
6945 addConfigurationProperty("touch.deviceType", "touchScreen");
6946 prepareDisplay(ui::ROTATION_0);
6947 prepareButtons();
6948 prepareAxes(POSITION);
6949 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6950
6951 // Set a physical frame in the display viewport.
6952 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6953 viewport->physicalLeft = 0;
6954 viewport->physicalTop = 0;
6955 viewport->physicalRight = DISPLAY_WIDTH / 2;
6956 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6957 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006958 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006959
Arpit Singh56adebc2023-04-25 13:56:05 +00006960 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006961
6962 // Hovering inside the physical frame produces events.
6963 processKey(mapper, BTN_TOOL_PEN, 1);
6964 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6965 processSync(mapper);
6966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6967 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6969 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6970
6971 // Leaving the physical frame ends the hovering gesture.
6972 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6973 processSync(mapper);
6974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6975 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6976
6977 // Moving outside the physical frame does not produce events.
6978 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6979 processSync(mapper);
6980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6981
6982 // Re-entering the physical frame produces events.
6983 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6984 processSync(mapper);
6985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6986 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6988 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6989}
6990
Prabir Pradhan5632d622021-09-06 07:57:20 -07006991// --- TouchDisplayProjectionTest ---
6992
6993class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6994public:
6995 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6996 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6997 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006998 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6999 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
7000 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007001 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007002 auto rotatedWidth = naturalDisplayWidth;
7003 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007004 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00007005 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007006 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007007 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007008 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007009 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007010 inverseRotationFlags = ui::Transform::ROT_180;
7011 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007012 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007013 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007014 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007015 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007016 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007017 inverseRotationFlags = ui::Transform::ROT_0;
7018 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007019 }
7020
Prabir Pradhana9df3162022-12-05 23:57:27 +00007021 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007022 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7023
7024 std::optional<DisplayViewport> internalViewport =
7025 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7026 DisplayViewport& v = *internalViewport;
7027 v.displayId = DISPLAY_ID;
7028 v.orientation = orientation;
7029
7030 v.logicalLeft = 0;
7031 v.logicalTop = 0;
7032 v.logicalRight = 100;
7033 v.logicalBottom = 100;
7034
7035 v.physicalLeft = rotatedPhysicalDisplay.left;
7036 v.physicalTop = rotatedPhysicalDisplay.top;
7037 v.physicalRight = rotatedPhysicalDisplay.right;
7038 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7039
Prabir Pradhana9df3162022-12-05 23:57:27 +00007040 v.deviceWidth = rotatedWidth;
7041 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007042
7043 v.isActive = true;
7044 v.uniqueId = UNIQUE_ID;
7045 v.type = ViewportType::INTERNAL;
7046 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007047 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007048 }
7049
7050 void assertReceivedMove(const Point& point) {
7051 NotifyMotionArgs motionArgs;
7052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007054 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7056 1, 0, 0, 0, 0, 0, 0, 0));
7057 }
7058};
7059
7060TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7061 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007062 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007063
7064 prepareButtons();
7065 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00007066 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007067
7068 NotifyMotionArgs motionArgs;
7069
7070 // Configure the DisplayViewport such that the logical display maps to a subsection of
7071 // the display panel called the physical display. Here, the physical display is bounded by the
7072 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7073 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7074 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7075 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7076
Michael Wrighta9cf4192022-12-01 23:46:39 +00007077 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007078 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7079
7080 // Touches outside the physical display should be ignored, and should not generate any
7081 // events. Ensure touches at the following points that lie outside of the physical display
7082 // area do not generate any events.
7083 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7084 processDown(mapper, toRawX(point.x), toRawY(point.y));
7085 processSync(mapper);
7086 processUp(mapper);
7087 processSync(mapper);
7088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7089 << "Unexpected event generated for touch outside physical display at point: "
7090 << point.x << ", " << point.y;
7091 }
7092 }
7093}
7094
7095TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7096 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007097 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007098
7099 prepareButtons();
7100 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00007101 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007102
7103 NotifyMotionArgs motionArgs;
7104
7105 // Configure the DisplayViewport such that the logical display maps to a subsection of
7106 // the display panel called the physical display. Here, the physical display is bounded by the
7107 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7108 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7109
Michael Wrighta9cf4192022-12-01 23:46:39 +00007110 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007111 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7112
7113 // Touches that start outside the physical display should be ignored until it enters the
7114 // physical display bounds, at which point it should generate a down event. Start a touch at
7115 // the point (5, 100), which is outside the physical display bounds.
7116 static const Point kOutsidePoint{5, 100};
7117 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7118 processSync(mapper);
7119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7120
7121 // Move the touch into the physical display area. This should generate a pointer down.
7122 processMove(mapper, toRawX(11), toRawY(21));
7123 processSync(mapper);
7124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7125 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007126 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007127 ASSERT_NO_FATAL_FAILURE(
7128 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7129
7130 // Move the touch inside the physical display area. This should generate a pointer move.
7131 processMove(mapper, toRawX(69), toRawY(159));
7132 processSync(mapper);
7133 assertReceivedMove({69, 159});
7134
7135 // Move outside the physical display area. Since the pointer is already down, this should
7136 // now continue generating events.
7137 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7138 processSync(mapper);
7139 assertReceivedMove(kOutsidePoint);
7140
7141 // Release. This should generate a pointer up.
7142 processUp(mapper);
7143 processSync(mapper);
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7145 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7147 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7148
7149 // Ensure no more events were generated.
7150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7152 }
7153}
7154
Prabir Pradhana9df3162022-12-05 23:57:27 +00007155// --- TouchscreenPrecisionTests ---
7156
7157// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7158// in various orientations and with different display rotations. We configure the touchscreen to
7159// have a higher resolution than that of the display by an integer scale factor in each axis so that
7160// we can enforce that coordinates match precisely as expected.
7161class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7162 public ::testing::WithParamInterface<ui::Rotation> {
7163public:
7164 void SetUp() override {
7165 SingleTouchInputMapperTest::SetUp();
7166
7167 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7168 // four times the resolution of the display in the Y axis.
7169 prepareButtons();
7170 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007171 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7172 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007173 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007174 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7175 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007176 }
7177
7178 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7179 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7180 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7181 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7182
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007183 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7184 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7185
7186 static const int32_t PRECISION_RAW_X_FLAT = 16;
7187 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7188
7189 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7190 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7191
Prabir Pradhana9df3162022-12-05 23:57:27 +00007192 static const std::array<Point, 4> kRawCorners;
7193};
7194
7195const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7196 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7197 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7198 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7199 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7200}};
7201
7202// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7203// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7204// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7205TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7206 enum class Orientation {
7207 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7208 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7209 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7210 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7211 ftl_last = ORIENTATION_270,
7212 };
7213 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7214 Orientation::ORIENTATION_270;
7215 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7216 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7217 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7218 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7219 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7220 };
7221
7222 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7223
7224 // Configure the touchscreen as being installed in the one of the four different orientations
7225 // relative to the display.
7226 addConfigurationProperty("touch.deviceType", "touchScreen");
7227 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7228 prepareDisplay(ui::ROTATION_0);
7229
Arpit Singh56adebc2023-04-25 13:56:05 +00007230 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007231
7232 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7233 // orientations of either 90 or 270) this means the display's natural resolution will be
7234 // flipped.
7235 const bool displayRotated =
7236 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7237 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7238 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7239 const Rect physicalFrame{0, 0, width, height};
7240 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7241
7242 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7243 const float expectedPrecisionX = displayRotated ? 4 : 2;
7244 const float expectedPrecisionY = displayRotated ? 2 : 4;
7245
7246 // Test all four corners.
7247 for (int i = 0; i < 4; i++) {
7248 const auto& raw = kRawCorners[i];
7249 processDown(mapper, raw.x, raw.y);
7250 processSync(mapper);
7251 const auto& expected = expectedPoints[i];
7252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7253 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7254 WithCoords(expected.x, expected.y),
7255 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7256 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7257 << "with touchscreen orientation "
7258 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7259 << expected.x << ", " << expected.y << ").";
7260 processUp(mapper);
7261 processSync(mapper);
7262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7263 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7264 WithCoords(expected.x, expected.y))));
7265 }
7266}
7267
Prabir Pradhan82687402022-12-06 01:32:53 +00007268TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7269 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7270 kMappedCorners = {
7271 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7272 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7273 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7274 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7275 };
7276
7277 const ui::Rotation displayRotation = GetParam();
7278
7279 addConfigurationProperty("touch.deviceType", "touchScreen");
7280 prepareDisplay(displayRotation);
7281
Arpit Singh56adebc2023-04-25 13:56:05 +00007282 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007283
7284 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7285
7286 // Test all four corners.
7287 for (int i = 0; i < 4; i++) {
7288 const auto& expected = expectedPoints[i];
7289 const auto& raw = kRawCorners[i];
7290 processDown(mapper, raw.x, raw.y);
7291 processSync(mapper);
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7293 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7294 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7295 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7296 << "with display rotation " << ui::toCString(displayRotation)
7297 << ", expected point (" << expected.x << ", " << expected.y << ").";
7298 processUp(mapper);
7299 processSync(mapper);
7300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7301 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7302 WithCoords(expected.x, expected.y))));
7303 }
7304}
7305
Prabir Pradhan3e798762022-12-02 21:02:11 +00007306TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7307 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7308 kMappedCorners = {
7309 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7310 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7311 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7312 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7313 };
7314
7315 const ui::Rotation displayRotation = GetParam();
7316
7317 addConfigurationProperty("touch.deviceType", "touchScreen");
7318 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7319
Arpit Singh56adebc2023-04-25 13:56:05 +00007320 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007321
7322 // Ori 270, so width and height swapped
7323 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7324 prepareDisplay(displayRotation);
7325 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7326
7327 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7328
7329 // Test all four corners.
7330 for (int i = 0; i < 4; i++) {
7331 const auto& expected = expectedPoints[i];
7332 const auto& raw = kRawCorners[i];
7333 processDown(mapper, raw.x, raw.y);
7334 processSync(mapper);
7335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7336 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7337 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7338 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7339 << "with display rotation " << ui::toCString(displayRotation)
7340 << ", expected point (" << expected.x << ", " << expected.y << ").";
7341 processUp(mapper);
7342 processSync(mapper);
7343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7344 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7345 WithCoords(expected.x, expected.y))));
7346 }
7347}
7348
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007349TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7350 const ui::Rotation displayRotation = GetParam();
7351
7352 addConfigurationProperty("touch.deviceType", "touchScreen");
7353 prepareDisplay(displayRotation);
7354
7355 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singh56adebc2023-04-25 13:56:05 +00007356 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007357
7358 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7359 // MotionRanges use display pixels as their units
7360 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7361 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7362
7363 // The MotionRanges should be oriented in the rotated display's coordinate space
7364 const bool displayRotated =
7365 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7366
7367 constexpr float MAX_X = 479.5;
7368 constexpr float MAX_Y = 799.75;
7369 EXPECT_EQ(xRange->min, 0.f);
7370 EXPECT_EQ(yRange->min, 0.f);
7371 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7372 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7373
7374 EXPECT_EQ(xRange->flat, 8.f);
7375 EXPECT_EQ(yRange->flat, 8.f);
7376
7377 EXPECT_EQ(xRange->fuzz, 2.f);
7378 EXPECT_EQ(yRange->fuzz, 2.f);
7379
7380 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7381 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7382}
7383
Prabir Pradhana9df3162022-12-05 23:57:27 +00007384// Run the precision tests for all rotations.
7385INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7386 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7387 ui::ROTATION_270),
7388 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7389 return ftl::enum_string(testParamInfo.param);
7390 });
7391
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007392// --- ExternalStylusFusionTest ---
7393
7394class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7395public:
7396 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7397 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007398 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007399 prepareButtons();
7400 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00007401 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007402
7403 mStylusState.when = ARBITRARY_TIME;
7404 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007405 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007406 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007407 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007408 processExternalStylusState(mapper);
7409 return mapper;
7410 }
7411
7412 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7413 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7414 for (const NotifyArgs& args : generatedArgs) {
7415 mFakeListener->notify(args);
7416 }
7417 // Loop the reader to flush the input listener queue.
7418 mReader->loopOnce();
7419 return generatedArgs;
7420 }
7421
7422protected:
7423 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007424
7425 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7426 auto toolTypeSource =
Prabir Pradhanf1198582023-09-14 22:28:32 +00007427 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007428
7429 // The first pointer is withheld.
7430 processDown(mapper, 100, 200);
7431 processSync(mapper);
7432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7433 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7434 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7435
7436 // The external stylus reports pressure. The withheld finger pointer is released as a
7437 // stylus.
7438 mStylusState.pressure = 1.f;
7439 processExternalStylusState(mapper);
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7441 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7442 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7443
7444 // Subsequent pointer events are not withheld.
7445 processMove(mapper, 101, 201);
7446 processSync(mapper);
7447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7448 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7449
7450 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7452 }
7453
7454 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7455 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7456
7457 // Releasing the touch pointer ends the gesture.
7458 processUp(mapper);
7459 processSync(mapper);
7460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanf1198582023-09-14 22:28:32 +00007461 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007462 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007463
7464 mStylusState.pressure = 0.f;
7465 processExternalStylusState(mapper);
7466 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7468 }
7469
7470 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanf1198582023-09-14 22:28:32 +00007471 // When stylus fusion is not successful, events should be reported with the original source.
7472 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007473 auto toolTypeSource =
Prabir Pradhanf1198582023-09-14 22:28:32 +00007474 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007475
7476 // The first pointer is withheld when an external stylus is connected,
7477 // and a timeout is requested.
7478 processDown(mapper, 100, 200);
7479 processSync(mapper);
7480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7481 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7482 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7483
7484 // If the timeout expires early, it is requested again.
7485 handleTimeout(mapper, ARBITRARY_TIME + 1);
7486 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7487 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7488
7489 // When the timeout expires, the withheld touch is released as a finger pointer.
7490 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7492 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7493
7494 // Subsequent pointer events are not withheld.
7495 processMove(mapper, 101, 201);
7496 processSync(mapper);
7497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7498 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7499 processUp(mapper);
7500 processSync(mapper);
7501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7502 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7503
7504 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7506 }
7507
7508private:
7509 InputDeviceInfo mExternalStylusDeviceInfo{};
7510};
7511
7512TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7513 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanf1198582023-09-14 22:28:32 +00007514 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007515}
7516
7517TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7518 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7519 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7520}
7521
7522TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7523 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7524 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7525}
7526
7527// Test a successful stylus fusion gesture where the pressure is reported by the external
7528// before the touch is reported by the touchscreen.
7529TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7530 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanf1198582023-09-14 22:28:32 +00007531 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007532
7533 // The external stylus reports pressure first. It is ignored for now.
7534 mStylusState.pressure = 1.f;
7535 processExternalStylusState(mapper);
7536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7537 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7538
7539 // When the touch goes down afterwards, it is reported as a stylus pointer.
7540 processDown(mapper, 100, 200);
7541 processSync(mapper);
7542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7543 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7544 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7545
7546 processMove(mapper, 101, 201);
7547 processSync(mapper);
7548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7549 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7550 processUp(mapper);
7551 processSync(mapper);
7552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7553 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7554
7555 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7557}
7558
7559TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7560 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7561
7562 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7563 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7564
7565 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7566 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7567 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7568 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7569}
7570
7571TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7572 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanf1198582023-09-14 22:28:32 +00007573 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007574
7575 mStylusState.pressure = 0.8f;
7576 processExternalStylusState(mapper);
7577 processDown(mapper, 100, 200);
7578 processSync(mapper);
7579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7580 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7581 WithPressure(0.8f))));
7582 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7583
7584 // The external stylus reports a pressure change. We wait for some time for a touch event.
7585 mStylusState.pressure = 0.6f;
7586 processExternalStylusState(mapper);
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7588 ASSERT_NO_FATAL_FAILURE(
7589 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7590
7591 // If a touch is reported within the timeout, it reports the updated pressure.
7592 processMove(mapper, 101, 201);
7593 processSync(mapper);
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7595 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7596 WithPressure(0.6f))));
7597 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7598
7599 // There is another pressure change.
7600 mStylusState.pressure = 0.5f;
7601 processExternalStylusState(mapper);
7602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7603 ASSERT_NO_FATAL_FAILURE(
7604 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7605
7606 // If a touch is not reported within the timeout, a move event is generated to report
7607 // the new pressure.
7608 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7610 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7611 WithPressure(0.5f))));
7612
7613 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7614 // repeated indefinitely.
7615 mStylusState.pressure = 0.0f;
7616 processExternalStylusState(mapper);
7617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7618 ASSERT_NO_FATAL_FAILURE(
7619 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7620 processMove(mapper, 102, 202);
7621 processSync(mapper);
7622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7623 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7624 WithPressure(0.5f))));
7625 processMove(mapper, 103, 203);
7626 processSync(mapper);
7627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7628 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7629 WithPressure(0.5f))));
7630
7631 processUp(mapper);
7632 processSync(mapper);
7633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanf1198582023-09-14 22:28:32 +00007634 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007635 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007636
7637 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7639}
7640
7641TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7642 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanf1198582023-09-14 22:28:32 +00007643 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007644
7645 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007646 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007647 processExternalStylusState(mapper);
7648 processDown(mapper, 100, 200);
7649 processSync(mapper);
7650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7651 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007652 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007653 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7654
7655 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007656 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007657 processExternalStylusState(mapper);
7658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7659 ASSERT_NO_FATAL_FAILURE(
7660 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7661
7662 // If a touch is reported within the timeout, it reports the updated pressure.
7663 processMove(mapper, 101, 201);
7664 processSync(mapper);
7665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7666 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007667 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007668 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7669
7670 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007671 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007672 processExternalStylusState(mapper);
7673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7674 ASSERT_NO_FATAL_FAILURE(
7675 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7676
7677 // If a touch is not reported within the timeout, a move event is generated to report
7678 // the new tool type.
7679 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7681 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007682 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007683
7684 processUp(mapper);
7685 processSync(mapper);
7686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7687 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007688 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007689
7690 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7692}
7693
7694TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7695 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanf1198582023-09-14 22:28:32 +00007696 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007697
7698 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7699
7700 // The external stylus reports a button change. We wait for some time for a touch event.
7701 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7702 processExternalStylusState(mapper);
7703 ASSERT_NO_FATAL_FAILURE(
7704 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7705
7706 // If a touch is reported within the timeout, it reports the updated button state.
7707 processMove(mapper, 101, 201);
7708 processSync(mapper);
7709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7710 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7711 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7713 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7714 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7715 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7716
7717 // The button is now released.
7718 mStylusState.buttons = 0;
7719 processExternalStylusState(mapper);
7720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7721 ASSERT_NO_FATAL_FAILURE(
7722 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7723
7724 // If a touch is not reported within the timeout, a move event is generated to report
7725 // the new button state.
7726 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7728 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7729 WithButtonState(0))));
7730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007731 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7732 WithButtonState(0))));
7733
7734 processUp(mapper);
7735 processSync(mapper);
7736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007737 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7738
7739 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7741}
7742
Michael Wrightd02c5b62014-02-10 15:10:22 -08007743// --- MultiTouchInputMapperTest ---
7744
7745class MultiTouchInputMapperTest : public TouchInputMapperTest {
7746protected:
7747 void prepareAxes(int axes);
7748
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007749 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7750 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7751 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7752 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7753 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7754 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7755 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7756 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7757 void processId(MultiTouchInputMapper& mapper, int32_t id);
7758 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7759 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7760 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007761 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007762 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007763 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7764 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007765};
7766
7767void MultiTouchInputMapperTest::prepareAxes(int axes) {
7768 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007769 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7770 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007771 }
7772 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007773 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7774 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007775 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007776 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7777 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007778 }
7779 }
7780 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007781 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7782 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007783 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007784 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007785 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007786 }
7787 }
7788 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007789 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7790 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007791 }
7792 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007793 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7794 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007795 }
7796 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007797 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7798 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007799 }
7800 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007801 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7802 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007803 }
7804 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007805 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7806 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007807 }
7808 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007809 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007810 }
7811}
7812
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007813void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7814 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007817}
7818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007819void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7820 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007822}
7823
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007824void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7825 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007827}
7828
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007829void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007831}
7832
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007833void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007835}
7836
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007837void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7838 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007840}
7841
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007842void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007844}
7845
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007846void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007848}
7849
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007850void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007852}
7853
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007854void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007856}
7857
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007858void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007860}
7861
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007862void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7863 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007865}
7866
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007867void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7868 int32_t value) {
7869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7871}
7872
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007873void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007874 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007875}
7876
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007877void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7878 nsecs_t readTime) {
7879 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007880}
7881
Michael Wrightd02c5b62014-02-10 15:10:22 -08007882TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007883 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007884 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007885 prepareAxes(POSITION);
7886 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00007887 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007888
arthurhungdcef2dc2020-08-11 14:47:50 +08007889 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007890
7891 NotifyMotionArgs motionArgs;
7892
7893 // Two fingers down at once.
7894 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7895 processPosition(mapper, x1, y1);
7896 processMTSync(mapper);
7897 processPosition(mapper, x2, y2);
7898 processMTSync(mapper);
7899 processSync(mapper);
7900
7901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7902 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7903 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7904 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7905 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7906 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7907 ASSERT_EQ(0, motionArgs.flags);
7908 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7909 ASSERT_EQ(0, motionArgs.buttonState);
7910 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007911 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007912 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007913 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7915 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7916 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7917 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7918 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7919
7920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7921 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7922 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7923 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7924 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007925 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007926 ASSERT_EQ(0, motionArgs.flags);
7927 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7928 ASSERT_EQ(0, motionArgs.buttonState);
7929 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007930 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007931 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007932 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007933 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007934 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7936 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7938 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7939 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7940 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7941 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7942
7943 // Move.
7944 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7945 processPosition(mapper, x1, y1);
7946 processMTSync(mapper);
7947 processPosition(mapper, x2, y2);
7948 processMTSync(mapper);
7949 processSync(mapper);
7950
7951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7952 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7953 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7954 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7955 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7957 ASSERT_EQ(0, motionArgs.flags);
7958 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7959 ASSERT_EQ(0, motionArgs.buttonState);
7960 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007961 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007962 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007963 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007964 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007965 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7967 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7969 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7970 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7971 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7972 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7973
7974 // First finger up.
7975 x2 += 15; y2 -= 20;
7976 processPosition(mapper, x2, y2);
7977 processMTSync(mapper);
7978 processSync(mapper);
7979
7980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7981 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7982 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7983 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7984 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007985 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007986 ASSERT_EQ(0, motionArgs.flags);
7987 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7988 ASSERT_EQ(0, motionArgs.buttonState);
7989 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007990 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007991 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007992 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007994 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7996 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7998 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7999 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8000 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8001 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8002
8003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8004 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8005 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8006 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8007 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8009 ASSERT_EQ(0, motionArgs.flags);
8010 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8011 ASSERT_EQ(0, motionArgs.buttonState);
8012 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008013 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008014 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008015 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8017 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8018 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8019 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8020 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8021
8022 // Move.
8023 x2 += 20; y2 -= 25;
8024 processPosition(mapper, x2, y2);
8025 processMTSync(mapper);
8026 processSync(mapper);
8027
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8029 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8030 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8031 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8032 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8034 ASSERT_EQ(0, motionArgs.flags);
8035 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8036 ASSERT_EQ(0, motionArgs.buttonState);
8037 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008038 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008040 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8042 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8043 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8044 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8045 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8046
8047 // New finger down.
8048 int32_t x3 = 700, y3 = 300;
8049 processPosition(mapper, x2, y2);
8050 processMTSync(mapper);
8051 processPosition(mapper, x3, y3);
8052 processMTSync(mapper);
8053 processSync(mapper);
8054
8055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8056 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8057 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8058 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8059 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008060 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008061 ASSERT_EQ(0, motionArgs.flags);
8062 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8063 ASSERT_EQ(0, motionArgs.buttonState);
8064 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008065 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008066 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008067 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008068 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008069 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8071 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8073 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8074 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8075 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8076 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8077
8078 // Second finger up.
8079 x3 += 30; y3 -= 20;
8080 processPosition(mapper, x3, y3);
8081 processMTSync(mapper);
8082 processSync(mapper);
8083
8084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8085 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8086 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8087 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8088 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008089 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090 ASSERT_EQ(0, motionArgs.flags);
8091 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8092 ASSERT_EQ(0, motionArgs.buttonState);
8093 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008094 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008096 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008098 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008099 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8100 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8102 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8103 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8104 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8105 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8106
8107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8108 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8109 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8110 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8111 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8113 ASSERT_EQ(0, motionArgs.flags);
8114 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8115 ASSERT_EQ(0, motionArgs.buttonState);
8116 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008117 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008118 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008119 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8121 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8122 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8123 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8124 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8125
8126 // Last finger up.
8127 processMTSync(mapper);
8128 processSync(mapper);
8129
8130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8131 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8132 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8133 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8134 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8135 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8136 ASSERT_EQ(0, motionArgs.flags);
8137 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8138 ASSERT_EQ(0, motionArgs.buttonState);
8139 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008140 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008141 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008142 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008143 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8144 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8145 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8146 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8147 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8148
8149 // Should not have sent any more keys or motions.
8150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8152}
8153
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008154TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8155 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008156 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008157
8158 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8159 /*fuzz*/ 0, /*resolution*/ 10);
8160 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8161 /*fuzz*/ 0, /*resolution*/ 11);
8162 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8163 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8164 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8165 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8166 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8167 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8168 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8169 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8170
Arpit Singh56adebc2023-04-25 13:56:05 +00008171 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008172
8173 // X and Y axes
8174 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8175 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8176 // Touch major and minor
8177 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8178 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8179 // Tool major and minor
8180 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8181 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8182}
8183
8184TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8185 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008186 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008187
8188 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8189 /*fuzz*/ 0, /*resolution*/ 10);
8190 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8191 /*fuzz*/ 0, /*resolution*/ 11);
8192
8193 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8194
Arpit Singh56adebc2023-04-25 13:56:05 +00008195 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008196
8197 // Touch major and minor
8198 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8199 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8200 // Tool major and minor
8201 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8202 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8203}
8204
Michael Wrightd02c5b62014-02-10 15:10:22 -08008205TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008206 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008207 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008208 prepareAxes(POSITION | ID);
8209 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00008210 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008211
arthurhungdcef2dc2020-08-11 14:47:50 +08008212 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008213
8214 NotifyMotionArgs motionArgs;
8215
8216 // Two fingers down at once.
8217 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8218 processPosition(mapper, x1, y1);
8219 processId(mapper, 1);
8220 processMTSync(mapper);
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_DOWN, 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(0, 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(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8233
8234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008235 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008236 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008237 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008238 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008240 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8242 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8244 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8245
8246 // Move.
8247 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8248 processPosition(mapper, x1, y1);
8249 processId(mapper, 1);
8250 processMTSync(mapper);
8251 processPosition(mapper, x2, y2);
8252 processId(mapper, 2);
8253 processMTSync(mapper);
8254 processSync(mapper);
8255
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008258 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008259 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008260 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008261 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008262 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8264 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8266 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8267
8268 // First finger up.
8269 x2 += 15; y2 -= 20;
8270 processPosition(mapper, x2, y2);
8271 processId(mapper, 2);
8272 processMTSync(mapper);
8273 processSync(mapper);
8274
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008276 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008277 ASSERT_EQ(size_t(2), 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_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008281 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8283 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8285 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8286
8287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, 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(1, 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(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8294
8295 // Move.
8296 x2 += 20; y2 -= 25;
8297 processPosition(mapper, x2, y2);
8298 processId(mapper, 2);
8299 processMTSync(mapper);
8300 processSync(mapper);
8301
8302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008304 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008305 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008306 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8308 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8309
8310 // New finger down.
8311 int32_t x3 = 700, y3 = 300;
8312 processPosition(mapper, x2, y2);
8313 processId(mapper, 2);
8314 processMTSync(mapper);
8315 processPosition(mapper, x3, y3);
8316 processId(mapper, 3);
8317 processMTSync(mapper);
8318 processSync(mapper);
8319
8320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008321 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008322 ASSERT_EQ(size_t(2), 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_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008326 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8328 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8329 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8330 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8331
8332 // Second finger up.
8333 x3 += 30; y3 -= 20;
8334 processPosition(mapper, x3, y3);
8335 processId(mapper, 3);
8336 processMTSync(mapper);
8337 processSync(mapper);
8338
8339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008340 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008341 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008342 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008343 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008344 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008345 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8347 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8349 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8350
8351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008353 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008354 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008355 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8357 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8358
8359 // Last finger up.
8360 processMTSync(mapper);
8361 processSync(mapper);
8362
8363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8364 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008365 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008366 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008367 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8369 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8370
8371 // Should not have sent any more keys or motions.
8372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8374}
8375
8376TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008377 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008378 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008379 prepareAxes(POSITION | ID | SLOT);
8380 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00008381 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008382
arthurhungdcef2dc2020-08-11 14:47:50 +08008383 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008384
8385 NotifyMotionArgs motionArgs;
8386
8387 // Two fingers down at once.
8388 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8389 processPosition(mapper, x1, y1);
8390 processId(mapper, 1);
8391 processSlot(mapper, 1);
8392 processPosition(mapper, x2, y2);
8393 processId(mapper, 2);
8394 processSync(mapper);
8395
8396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8397 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008398 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008399 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008400 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8402 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8403
8404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008405 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008406 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008407 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008408 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008409 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008410 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8412 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8414 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8415
8416 // Move.
8417 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8418 processSlot(mapper, 0);
8419 processPosition(mapper, x1, y1);
8420 processSlot(mapper, 1);
8421 processPosition(mapper, x2, y2);
8422 processSync(mapper);
8423
8424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8425 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008426 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008427 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008428 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008429 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008430 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8432 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8434 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8435
8436 // First finger up.
8437 x2 += 15; y2 -= 20;
8438 processSlot(mapper, 0);
8439 processId(mapper, -1);
8440 processSlot(mapper, 1);
8441 processPosition(mapper, x2, y2);
8442 processSync(mapper);
8443
8444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008445 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008446 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008447 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008448 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008449 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008450 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8452 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8454 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8455
8456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008458 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008459 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008460 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8462 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8463
8464 // Move.
8465 x2 += 20; y2 -= 25;
8466 processPosition(mapper, x2, y2);
8467 processSync(mapper);
8468
8469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008471 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008472 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008473 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8475 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8476
8477 // New finger down.
8478 int32_t x3 = 700, y3 = 300;
8479 processPosition(mapper, x2, y2);
8480 processSlot(mapper, 0);
8481 processId(mapper, 3);
8482 processPosition(mapper, x3, y3);
8483 processSync(mapper);
8484
8485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008486 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008487 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008488 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008489 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008490 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008491 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8493 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8495 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8496
8497 // Second finger up.
8498 x3 += 30; y3 -= 20;
8499 processSlot(mapper, 1);
8500 processId(mapper, -1);
8501 processSlot(mapper, 0);
8502 processPosition(mapper, x3, y3);
8503 processSync(mapper);
8504
8505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008506 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008507 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008508 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008509 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008510 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008511 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8513 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8515 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8516
8517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8518 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008519 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008520 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008521 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8523 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8524
8525 // Last finger up.
8526 processId(mapper, -1);
8527 processSync(mapper);
8528
8529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8530 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008531 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008532 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008533 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8535 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8536
8537 // Should not have sent any more keys or motions.
8538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8540}
8541
8542TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008543 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008544 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008545 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singh56adebc2023-04-25 13:56:05 +00008546 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008547
8548 // These calculations are based on the input device calibration documentation.
8549 int32_t rawX = 100;
8550 int32_t rawY = 200;
8551 int32_t rawTouchMajor = 7;
8552 int32_t rawTouchMinor = 6;
8553 int32_t rawToolMajor = 9;
8554 int32_t rawToolMinor = 8;
8555 int32_t rawPressure = 11;
8556 int32_t rawDistance = 0;
8557 int32_t rawOrientation = 3;
8558 int32_t id = 5;
8559
8560 float x = toDisplayX(rawX);
8561 float y = toDisplayY(rawY);
8562 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8563 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8564 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8565 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8566 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8567 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8568 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8569 float distance = float(rawDistance);
8570
8571 processPosition(mapper, rawX, rawY);
8572 processTouchMajor(mapper, rawTouchMajor);
8573 processTouchMinor(mapper, rawTouchMinor);
8574 processToolMajor(mapper, rawToolMajor);
8575 processToolMinor(mapper, rawToolMinor);
8576 processPressure(mapper, rawPressure);
8577 processOrientation(mapper, rawOrientation);
8578 processDistance(mapper, rawDistance);
8579 processId(mapper, id);
8580 processMTSync(mapper);
8581 processSync(mapper);
8582
8583 NotifyMotionArgs args;
8584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8585 ASSERT_EQ(0, args.pointerProperties[0].id);
8586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8587 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8588 orientation, distance));
8589}
8590
8591TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008592 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008593 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008594 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8595 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singh56adebc2023-04-25 13:56:05 +00008596 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008597
8598 // These calculations are based on the input device calibration documentation.
8599 int32_t rawX = 100;
8600 int32_t rawY = 200;
8601 int32_t rawTouchMajor = 140;
8602 int32_t rawTouchMinor = 120;
8603 int32_t rawToolMajor = 180;
8604 int32_t rawToolMinor = 160;
8605
8606 float x = toDisplayX(rawX);
8607 float y = toDisplayY(rawY);
8608 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8609 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8610 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8611 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8612 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8613
8614 processPosition(mapper, rawX, rawY);
8615 processTouchMajor(mapper, rawTouchMajor);
8616 processTouchMinor(mapper, rawTouchMinor);
8617 processToolMajor(mapper, rawToolMajor);
8618 processToolMinor(mapper, rawToolMinor);
8619 processMTSync(mapper);
8620 processSync(mapper);
8621
8622 NotifyMotionArgs args;
8623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8625 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8626}
8627
8628TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008629 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008630 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008631 prepareAxes(POSITION | TOUCH | TOOL);
8632 addConfigurationProperty("touch.size.calibration", "diameter");
8633 addConfigurationProperty("touch.size.scale", "10");
8634 addConfigurationProperty("touch.size.bias", "160");
8635 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singh56adebc2023-04-25 13:56:05 +00008636 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008637
8638 // These calculations are based on the input device calibration documentation.
8639 // Note: We only provide a single common touch/tool value because the device is assumed
8640 // not to emit separate values for each pointer (isSummed = 1).
8641 int32_t rawX = 100;
8642 int32_t rawY = 200;
8643 int32_t rawX2 = 150;
8644 int32_t rawY2 = 250;
8645 int32_t rawTouchMajor = 5;
8646 int32_t rawToolMajor = 8;
8647
8648 float x = toDisplayX(rawX);
8649 float y = toDisplayY(rawY);
8650 float x2 = toDisplayX(rawX2);
8651 float y2 = toDisplayY(rawY2);
8652 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8653 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8654 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8655
8656 processPosition(mapper, rawX, rawY);
8657 processTouchMajor(mapper, rawTouchMajor);
8658 processToolMajor(mapper, rawToolMajor);
8659 processMTSync(mapper);
8660 processPosition(mapper, rawX2, rawY2);
8661 processTouchMajor(mapper, rawTouchMajor);
8662 processToolMajor(mapper, rawToolMajor);
8663 processMTSync(mapper);
8664 processSync(mapper);
8665
8666 NotifyMotionArgs args;
8667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8668 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8669
8670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008671 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008672 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8674 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8676 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8677}
8678
8679TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008680 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008681 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008682 prepareAxes(POSITION | TOUCH | TOOL);
8683 addConfigurationProperty("touch.size.calibration", "area");
8684 addConfigurationProperty("touch.size.scale", "43");
8685 addConfigurationProperty("touch.size.bias", "3");
Arpit Singh56adebc2023-04-25 13:56:05 +00008686 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008687
8688 // These calculations are based on the input device calibration documentation.
8689 int32_t rawX = 100;
8690 int32_t rawY = 200;
8691 int32_t rawTouchMajor = 5;
8692 int32_t rawToolMajor = 8;
8693
8694 float x = toDisplayX(rawX);
8695 float y = toDisplayY(rawY);
8696 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8697 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8698 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8699
8700 processPosition(mapper, rawX, rawY);
8701 processTouchMajor(mapper, rawTouchMajor);
8702 processToolMajor(mapper, rawToolMajor);
8703 processMTSync(mapper);
8704 processSync(mapper);
8705
8706 NotifyMotionArgs args;
8707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8708 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8709 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8710}
8711
8712TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008713 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008714 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008715 prepareAxes(POSITION | PRESSURE);
8716 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8717 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singh56adebc2023-04-25 13:56:05 +00008718 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008719
Michael Wrightaa449c92017-12-13 21:21:43 +00008720 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008721 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008722 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8723 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8724 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8725
Michael Wrightd02c5b62014-02-10 15:10:22 -08008726 // These calculations are based on the input device calibration documentation.
8727 int32_t rawX = 100;
8728 int32_t rawY = 200;
8729 int32_t rawPressure = 60;
8730
8731 float x = toDisplayX(rawX);
8732 float y = toDisplayY(rawY);
8733 float pressure = float(rawPressure) * 0.01f;
8734
8735 processPosition(mapper, rawX, rawY);
8736 processPressure(mapper, rawPressure);
8737 processMTSync(mapper);
8738 processSync(mapper);
8739
8740 NotifyMotionArgs args;
8741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8743 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8744}
8745
8746TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008747 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008748 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008749 prepareAxes(POSITION | ID | SLOT);
Arpit Singh56adebc2023-04-25 13:56:05 +00008750 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008751
8752 NotifyMotionArgs motionArgs;
8753 NotifyKeyArgs keyArgs;
8754
8755 processId(mapper, 1);
8756 processPosition(mapper, 100, 200);
8757 processSync(mapper);
8758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8759 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8760 ASSERT_EQ(0, motionArgs.buttonState);
8761
8762 // press BTN_LEFT, release BTN_LEFT
8763 processKey(mapper, BTN_LEFT, 1);
8764 processSync(mapper);
8765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8767 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8768
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8770 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8771 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8772
Michael Wrightd02c5b62014-02-10 15:10:22 -08008773 processKey(mapper, BTN_LEFT, 0);
8774 processSync(mapper);
8775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008776 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008777 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008778
8779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008781 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008782
8783 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8784 processKey(mapper, BTN_RIGHT, 1);
8785 processKey(mapper, BTN_MIDDLE, 1);
8786 processSync(mapper);
8787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8788 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8789 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8790 motionArgs.buttonState);
8791
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8793 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8794 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8795
8796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8797 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8798 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8799 motionArgs.buttonState);
8800
Michael Wrightd02c5b62014-02-10 15:10:22 -08008801 processKey(mapper, BTN_RIGHT, 0);
8802 processSync(mapper);
8803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008804 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008805 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008806
8807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008808 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008809 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008810
8811 processKey(mapper, BTN_MIDDLE, 0);
8812 processSync(mapper);
8813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008814 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008815 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008816
8817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008819 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008820
8821 // press BTN_BACK, release BTN_BACK
8822 processKey(mapper, BTN_BACK, 1);
8823 processSync(mapper);
8824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8825 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8826 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008827
Michael Wrightd02c5b62014-02-10 15:10:22 -08008828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008829 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008830 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8831
8832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8833 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8834 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008835
8836 processKey(mapper, BTN_BACK, 0);
8837 processSync(mapper);
8838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008839 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008840 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008841
8842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008844 ASSERT_EQ(0, motionArgs.buttonState);
8845
Michael Wrightd02c5b62014-02-10 15:10:22 -08008846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8847 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8848 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8849
8850 // press BTN_SIDE, release BTN_SIDE
8851 processKey(mapper, BTN_SIDE, 1);
8852 processSync(mapper);
8853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8854 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8855 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008856
Michael Wrightd02c5b62014-02-10 15:10:22 -08008857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008859 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8860
8861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8863 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008864
8865 processKey(mapper, BTN_SIDE, 0);
8866 processSync(mapper);
8867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008868 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008869 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008870
8871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008873 ASSERT_EQ(0, motionArgs.buttonState);
8874
Michael Wrightd02c5b62014-02-10 15:10:22 -08008875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8876 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8877 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8878
8879 // press BTN_FORWARD, release BTN_FORWARD
8880 processKey(mapper, BTN_FORWARD, 1);
8881 processSync(mapper);
8882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8883 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8884 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008885
Michael Wrightd02c5b62014-02-10 15:10:22 -08008886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008888 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8889
8890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8891 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8892 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008893
8894 processKey(mapper, BTN_FORWARD, 0);
8895 processSync(mapper);
8896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008897 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008898 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008899
8900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008901 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008902 ASSERT_EQ(0, motionArgs.buttonState);
8903
Michael Wrightd02c5b62014-02-10 15:10:22 -08008904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8905 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8906 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8907
8908 // press BTN_EXTRA, release BTN_EXTRA
8909 processKey(mapper, BTN_EXTRA, 1);
8910 processSync(mapper);
8911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8912 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8913 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008914
Michael Wrightd02c5b62014-02-10 15:10:22 -08008915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008917 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8918
8919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8920 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8921 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008922
8923 processKey(mapper, BTN_EXTRA, 0);
8924 processSync(mapper);
8925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008926 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008927 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008928
8929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008931 ASSERT_EQ(0, motionArgs.buttonState);
8932
Michael Wrightd02c5b62014-02-10 15:10:22 -08008933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8934 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8935 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8936
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8938
Michael Wrightd02c5b62014-02-10 15:10:22 -08008939 // press BTN_STYLUS, release BTN_STYLUS
8940 processKey(mapper, BTN_STYLUS, 1);
8941 processSync(mapper);
8942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008944 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8945
8946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8948 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008949
8950 processKey(mapper, BTN_STYLUS, 0);
8951 processSync(mapper);
8952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008953 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008954 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008955
8956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008958 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008959
8960 // press BTN_STYLUS2, release BTN_STYLUS2
8961 processKey(mapper, BTN_STYLUS2, 1);
8962 processSync(mapper);
8963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008965 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8966
8967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8968 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8969 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008970
8971 processKey(mapper, BTN_STYLUS2, 0);
8972 processSync(mapper);
8973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008974 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008975 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008976
8977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008978 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008979 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008980
8981 // release touch
8982 processId(mapper, -1);
8983 processSync(mapper);
8984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8985 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8986 ASSERT_EQ(0, motionArgs.buttonState);
8987}
8988
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008989TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8990 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008991 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008992 prepareAxes(POSITION | ID | SLOT);
Arpit Singh56adebc2023-04-25 13:56:05 +00008993 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008994
8995 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8996 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8997
8998 // Touch down.
8999 processId(mapper, 1);
9000 processPosition(mapper, 100, 200);
9001 processSync(mapper);
9002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9003 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9004
9005 // Press and release button mapped to the primary stylus button.
9006 processKey(mapper, BTN_A, 1);
9007 processSync(mapper);
9008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9009 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9010 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9012 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9013 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9014
9015 processKey(mapper, BTN_A, 0);
9016 processSync(mapper);
9017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9018 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9020 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9021
9022 // Press and release the HID usage mapped to the secondary stylus button.
9023 processHidUsage(mapper, 0xabcd, 1);
9024 processSync(mapper);
9025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9026 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9027 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9029 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9030 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9031
9032 processHidUsage(mapper, 0xabcd, 0);
9033 processSync(mapper);
9034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9035 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9037 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9038
9039 // Release touch.
9040 processId(mapper, -1);
9041 processSync(mapper);
9042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9043 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9044}
9045
Michael Wrightd02c5b62014-02-10 15:10:22 -08009046TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009047 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009048 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009049 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009050 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009051
9052 NotifyMotionArgs motionArgs;
9053
9054 // default tool type is finger
9055 processId(mapper, 1);
9056 processPosition(mapper, 100, 200);
9057 processSync(mapper);
9058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9059 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009060 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009061
9062 // eraser
9063 processKey(mapper, BTN_TOOL_RUBBER, 1);
9064 processSync(mapper);
9065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009067 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009068
9069 // stylus
9070 processKey(mapper, BTN_TOOL_RUBBER, 0);
9071 processKey(mapper, BTN_TOOL_PEN, 1);
9072 processSync(mapper);
9073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009075 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009076
9077 // brush
9078 processKey(mapper, BTN_TOOL_PEN, 0);
9079 processKey(mapper, BTN_TOOL_BRUSH, 1);
9080 processSync(mapper);
9081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009083 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009084
9085 // pencil
9086 processKey(mapper, BTN_TOOL_BRUSH, 0);
9087 processKey(mapper, BTN_TOOL_PENCIL, 1);
9088 processSync(mapper);
9089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009091 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009092
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009093 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009094 processKey(mapper, BTN_TOOL_PENCIL, 0);
9095 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009100
9101 // mouse
9102 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9103 processKey(mapper, BTN_TOOL_MOUSE, 1);
9104 processSync(mapper);
9105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009107 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009108
9109 // lens
9110 processKey(mapper, BTN_TOOL_MOUSE, 0);
9111 processKey(mapper, BTN_TOOL_LENS, 1);
9112 processSync(mapper);
9113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009115 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009116
9117 // double-tap
9118 processKey(mapper, BTN_TOOL_LENS, 0);
9119 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9120 processSync(mapper);
9121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009123 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009124
9125 // triple-tap
9126 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9127 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9128 processSync(mapper);
9129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009131 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009132
9133 // quad-tap
9134 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9135 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9136 processSync(mapper);
9137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009139 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009140
9141 // finger
9142 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9143 processKey(mapper, BTN_TOOL_FINGER, 1);
9144 processSync(mapper);
9145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009147 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009148
9149 // stylus trumps finger
9150 processKey(mapper, BTN_TOOL_PEN, 1);
9151 processSync(mapper);
9152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009154 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009155
9156 // eraser trumps stylus
9157 processKey(mapper, BTN_TOOL_RUBBER, 1);
9158 processSync(mapper);
9159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9160 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009161 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009162
9163 // mouse trumps eraser
9164 processKey(mapper, BTN_TOOL_MOUSE, 1);
9165 processSync(mapper);
9166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9167 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009168 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009169
9170 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9171 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9172 processSync(mapper);
9173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009175 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009176
9177 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9178 processToolType(mapper, MT_TOOL_PEN);
9179 processSync(mapper);
9180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009182 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009183
9184 // back to default tool type
9185 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9186 processKey(mapper, BTN_TOOL_MOUSE, 0);
9187 processKey(mapper, BTN_TOOL_RUBBER, 0);
9188 processKey(mapper, BTN_TOOL_PEN, 0);
9189 processKey(mapper, BTN_TOOL_FINGER, 0);
9190 processSync(mapper);
9191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009193 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009194}
9195
9196TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009197 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009198 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009199 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009200 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +00009201 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009202
9203 NotifyMotionArgs motionArgs;
9204
9205 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9206 processId(mapper, 1);
9207 processPosition(mapper, 100, 200);
9208 processSync(mapper);
9209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9210 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9215 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9217 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9218
9219 // move a little
9220 processPosition(mapper, 150, 250);
9221 processSync(mapper);
9222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9223 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9225 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9226
9227 // down when BTN_TOUCH is pressed, pressure defaults to 1
9228 processKey(mapper, BTN_TOUCH, 1);
9229 processSync(mapper);
9230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9231 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9233 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9234
9235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9236 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9238 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9239
9240 // up when BTN_TOUCH is released, hover restored
9241 processKey(mapper, BTN_TOUCH, 0);
9242 processSync(mapper);
9243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9244 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9246 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9247
9248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9249 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9254 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9256 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9257
9258 // exit hover when pointer goes away
9259 processId(mapper, -1);
9260 processSync(mapper);
9261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9262 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9264 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9265}
9266
9267TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009268 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009269 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009270 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009271 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009272
9273 NotifyMotionArgs motionArgs;
9274
9275 // initially hovering because pressure is 0
9276 processId(mapper, 1);
9277 processPosition(mapper, 100, 200);
9278 processPressure(mapper, 0);
9279 processSync(mapper);
9280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9281 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9283 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9284
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9286 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9288 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9289
9290 // move a little
9291 processPosition(mapper, 150, 250);
9292 processSync(mapper);
9293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9294 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9296 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9297
9298 // down when pressure becomes non-zero
9299 processPressure(mapper, RAW_PRESSURE_MAX);
9300 processSync(mapper);
9301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9302 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9304 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9305
9306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9307 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9309 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9310
9311 // up when pressure becomes 0, hover restored
9312 processPressure(mapper, 0);
9313 processSync(mapper);
9314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9315 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9317 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9318
9319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9320 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9322 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9323
9324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9325 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9327 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9328
9329 // exit hover when pointer goes away
9330 processId(mapper, -1);
9331 processSync(mapper);
9332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9333 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9335 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9336}
9337
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009338/**
9339 * Set the input device port <--> display port associations, and check that the
9340 * events are routed to the display that matches the display port.
9341 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9342 */
9343TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009344 const std::string usb2 = "USB2";
9345 const uint8_t hdmi1 = 0;
9346 const uint8_t hdmi2 = 1;
9347 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009348 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009349
9350 addConfigurationProperty("touch.deviceType", "touchScreen");
9351 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009352 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009353
9354 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9355 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9356
9357 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9358 // for this input device is specified, and the matching viewport is not present,
9359 // the input device should be disabled (at the mapper level).
9360
9361 // Add viewport for display 2 on hdmi2
9362 prepareSecondaryDisplay(type, hdmi2);
9363 // Send a touch event
9364 processPosition(mapper, 100, 100);
9365 processSync(mapper);
9366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9367
9368 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009369 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009370 // Send a touch event again
9371 processPosition(mapper, 100, 100);
9372 processSync(mapper);
9373
9374 NotifyMotionArgs args;
9375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9376 ASSERT_EQ(DISPLAY_ID, args.displayId);
9377}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009378
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009379TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9380 addConfigurationProperty("touch.deviceType", "touchScreen");
9381 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009382 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009383
9384 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9385
Michael Wrighta9cf4192022-12-01 23:46:39 +00009386 prepareDisplay(ui::ROTATION_0);
9387 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009388
9389 // Send a touch event
9390 processPosition(mapper, 100, 100);
9391 processSync(mapper);
9392
9393 NotifyMotionArgs args;
9394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9395 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9396}
9397
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009398TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009399 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009400 std::shared_ptr<FakePointerController> fakePointerController =
9401 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009402 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009403 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009404 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009405
Garfield Tan888a6a42020-01-09 11:39:16 -08009406 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009407 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009408
Michael Wrighta9cf4192022-12-01 23:46:39 +00009409 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009410 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009411 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009412
Josep del Río2d8c79a2023-01-23 19:33:50 +00009413 // Check source is mouse that would obtain the PointerController.
9414 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009415
9416 NotifyMotionArgs motionArgs;
9417 processPosition(mapper, 100, 100);
9418 processSync(mapper);
9419
9420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9421 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9422 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9423}
9424
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009425/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009426 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9427 */
9428TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9429 addConfigurationProperty("touch.deviceType", "touchScreen");
9430 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009431 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009432
Michael Wrighta9cf4192022-12-01 23:46:39 +00009433 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009434 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9435 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9436 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9437 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009438
9439 NotifyMotionArgs args;
9440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9441 ASSERT_EQ(26, args.readTime);
9442
Harry Cutts33476232023-01-30 19:57:29 +00009443 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9444 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9445 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009446
9447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9448 ASSERT_EQ(33, args.readTime);
9449}
9450
9451/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009452 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9453 * events should not be delivered to the listener.
9454 */
9455TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9456 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009457 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009458 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009459 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009460 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009461 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009462 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009463
9464 NotifyMotionArgs motionArgs;
9465 processPosition(mapper, 100, 100);
9466 processSync(mapper);
9467
9468 mFakeListener->assertNotifyMotionWasNotCalled();
9469}
9470
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009471/**
9472 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9473 * the touch mapper can process the events and the events can be delivered to the listener.
9474 */
9475TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9476 addConfigurationProperty("touch.deviceType", "touchScreen");
9477 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009478 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009479 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009480 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009481 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009482 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009483
9484 NotifyMotionArgs motionArgs;
9485 processPosition(mapper, 100, 100);
9486 processSync(mapper);
9487
9488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9489 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9490}
9491
Garfield Tanc734e4f2021-01-15 20:01:39 -08009492TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9493 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009494 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009495 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009496 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009497 std::optional<DisplayViewport> optionalDisplayViewport =
9498 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9499 ASSERT_TRUE(optionalDisplayViewport.has_value());
9500 DisplayViewport displayViewport = *optionalDisplayViewport;
9501
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009502 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009503 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009504 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009505
9506 // Finger down
9507 int32_t x = 100, y = 100;
9508 processPosition(mapper, x, y);
9509 processSync(mapper);
9510
9511 NotifyMotionArgs motionArgs;
9512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9513 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9514
9515 // Deactivate display viewport
9516 displayViewport.isActive = false;
9517 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009518 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009519
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009520 // The ongoing touch should be canceled immediately
9521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9522 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9523
9524 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009525 x += 10, y += 10;
9526 processPosition(mapper, x, y);
9527 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009529
9530 // Reactivate display viewport
9531 displayViewport.isActive = true;
9532 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009533 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009534
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009535 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009536 x += 10, y += 10;
9537 processPosition(mapper, x, y);
9538 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9540 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009541}
9542
Arthur Hung7c645402019-01-25 17:45:42 +08009543TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9544 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009545 prepareAxes(POSITION | ID | SLOT);
9546 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009547 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009548
9549 // Create the second touch screen device, and enable multi fingers.
9550 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009551 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009552 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009553 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009554 std::shared_ptr<InputDevice> device2 =
9555 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009556 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009557
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009558 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009559 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009560 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009561 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009562 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009563 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009564 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009565 /*flat=*/0, /*fuzz=*/0);
9566 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009567 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9568 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009569
9570 // Setup the second touch screen device.
Arpit Singh56adebc2023-04-25 13:56:05 +00009571 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9572 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9573 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009574 std::list<NotifyArgs> unused =
9575 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009576 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009577 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009578
9579 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009580 std::shared_ptr<FakePointerController> fakePointerController =
9581 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009582 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009583
9584 // Setup policy for associated displays and show touches.
9585 const uint8_t hdmi1 = 0;
9586 const uint8_t hdmi2 = 1;
9587 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9588 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9589 mFakePolicy->setShowTouches(true);
9590
9591 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009592 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009593 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009594
9595 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009596 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009597 InputReaderConfiguration::Change::DISPLAY_INFO |
9598 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009599
9600 // Two fingers down at default display.
9601 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9602 processPosition(mapper, x1, y1);
9603 processId(mapper, 1);
9604 processSlot(mapper, 1);
9605 processPosition(mapper, x2, y2);
9606 processId(mapper, 2);
9607 processSync(mapper);
9608
9609 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9610 fakePointerController->getSpots().find(DISPLAY_ID);
9611 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9612 ASSERT_EQ(size_t(2), iter->second.size());
9613
9614 // Two fingers down at second display.
9615 processPosition(mapper2, x1, y1);
9616 processId(mapper2, 1);
9617 processSlot(mapper2, 1);
9618 processPosition(mapper2, x2, y2);
9619 processId(mapper2, 2);
9620 processSync(mapper2);
9621
9622 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9623 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9624 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009625
9626 // Disable the show touches configuration and ensure the spots are cleared.
9627 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009628 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009629 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009630
9631 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009632}
9633
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009634TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009635 prepareAxes(POSITION);
9636 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009637 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00009638 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009639
9640 NotifyMotionArgs motionArgs;
9641 // Unrotated video frame
9642 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9643 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009644 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009645 processPosition(mapper, 100, 200);
9646 processSync(mapper);
9647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9648 ASSERT_EQ(frames, motionArgs.videoFrames);
9649
9650 // Subsequent touch events should not have any videoframes
9651 // This is implemented separately in FakeEventHub,
9652 // but that should match the behaviour of TouchVideoDevice.
9653 processPosition(mapper, 200, 200);
9654 processSync(mapper);
9655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9656 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9657}
9658
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009659TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009660 prepareAxes(POSITION);
9661 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009662 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009663 // Unrotated video frame
9664 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9665 NotifyMotionArgs motionArgs;
9666
9667 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009668 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009669 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9670 clearViewports();
9671 prepareDisplay(orientation);
9672 std::vector<TouchVideoFrame> frames{frame};
9673 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9674 processPosition(mapper, 100, 200);
9675 processSync(mapper);
9676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9677 ASSERT_EQ(frames, motionArgs.videoFrames);
9678 }
9679}
9680
9681TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9682 prepareAxes(POSITION);
9683 addConfigurationProperty("touch.deviceType", "touchScreen");
9684 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9685 // orientation-aware are affected by display rotation.
9686 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00009687 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009688 // Unrotated video frame
9689 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9690 NotifyMotionArgs motionArgs;
9691
9692 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009693 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009694 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9695 clearViewports();
9696 prepareDisplay(orientation);
9697 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009698 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009699 processPosition(mapper, 100, 200);
9700 processSync(mapper);
9701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009702 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9703 // compared to the display. This is so that when the window transform (which contains the
9704 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9705 // window's coordinate space.
9706 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009707 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009708
9709 // Release finger.
9710 processSync(mapper);
9711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009712 }
9713}
9714
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009715TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009716 prepareAxes(POSITION);
9717 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009718 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009719 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9720 // so mix these.
9721 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9722 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9723 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9724 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9725 NotifyMotionArgs motionArgs;
9726
Michael Wrighta9cf4192022-12-01 23:46:39 +00009727 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009728 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009729 processPosition(mapper, 100, 200);
9730 processSync(mapper);
9731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009732 ASSERT_EQ(frames, motionArgs.videoFrames);
9733}
9734
9735TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9736 prepareAxes(POSITION);
9737 addConfigurationProperty("touch.deviceType", "touchScreen");
9738 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9739 // orientation-aware are affected by display rotation.
9740 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00009741 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009742 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9743 // so mix these.
9744 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9745 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9746 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9747 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9748 NotifyMotionArgs motionArgs;
9749
Michael Wrighta9cf4192022-12-01 23:46:39 +00009750 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009751 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9752 processPosition(mapper, 100, 200);
9753 processSync(mapper);
9754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9755 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9756 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9757 // compared to the display. This is so that when the window transform (which contains the
9758 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9759 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009760 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009761 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009762 ASSERT_EQ(frames, motionArgs.videoFrames);
9763}
9764
Arthur Hung9da14732019-09-02 16:16:58 +08009765/**
9766 * If we had defined port associations, but the viewport is not ready, the touch device would be
9767 * expected to be disabled, and it should be enabled after the viewport has found.
9768 */
9769TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009770 constexpr uint8_t hdmi2 = 1;
9771 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009772 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009773
9774 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9775
9776 addConfigurationProperty("touch.deviceType", "touchScreen");
9777 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009778 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009779
9780 ASSERT_EQ(mDevice->isEnabled(), false);
9781
9782 // Add display on hdmi2, the device should be enabled and can receive touch event.
9783 prepareSecondaryDisplay(type, hdmi2);
9784 ASSERT_EQ(mDevice->isEnabled(), true);
9785
9786 // Send a touch event.
9787 processPosition(mapper, 100, 100);
9788 processSync(mapper);
9789
9790 NotifyMotionArgs args;
9791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9792 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9793}
9794
Arthur Hung421eb1c2020-01-16 00:09:42 +08009795TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009796 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009797 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009798 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009799 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009800
9801 NotifyMotionArgs motionArgs;
9802
9803 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9804 // finger down
9805 processId(mapper, 1);
9806 processPosition(mapper, x1, y1);
9807 processSync(mapper);
9808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9809 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009810 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009811
9812 // finger move
9813 processId(mapper, 1);
9814 processPosition(mapper, x2, y2);
9815 processSync(mapper);
9816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009818 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009819
9820 // finger up.
9821 processId(mapper, -1);
9822 processSync(mapper);
9823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9824 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009825 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009826
9827 // new finger down
9828 processId(mapper, 1);
9829 processPosition(mapper, x3, y3);
9830 processSync(mapper);
9831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9832 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009833 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009834}
9835
9836/**
arthurhungcc7f9802020-04-30 17:55:40 +08009837 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9838 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009839 */
arthurhungcc7f9802020-04-30 17:55:40 +08009840TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009841 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009842 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009843 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009844 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009845
9846 NotifyMotionArgs motionArgs;
9847
9848 // default tool type is finger
9849 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009850 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009851 processPosition(mapper, x1, y1);
9852 processSync(mapper);
9853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9854 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009855 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009856
9857 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9858 processToolType(mapper, MT_TOOL_PALM);
9859 processSync(mapper);
9860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9861 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9862
9863 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009864 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009865 processPosition(mapper, x2, y2);
9866 processSync(mapper);
9867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9868
9869 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009870 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009871 processSync(mapper);
9872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9873
9874 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009875 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009876 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009877 processPosition(mapper, x3, y3);
9878 processSync(mapper);
9879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9880 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009881 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009882}
9883
arthurhungbf89a482020-04-17 17:37:55 +08009884/**
arthurhungcc7f9802020-04-30 17:55:40 +08009885 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9886 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009887 */
arthurhungcc7f9802020-04-30 17:55:40 +08009888TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009889 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009890 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009891 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009892 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009893
9894 NotifyMotionArgs motionArgs;
9895
9896 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009897 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9898 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009899 processPosition(mapper, x1, y1);
9900 processSync(mapper);
9901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9902 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009903 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009904
9905 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009906 processSlot(mapper, SECOND_SLOT);
9907 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009908 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009909 processSync(mapper);
9910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009911 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009912 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009913
9914 // If the tool type of the first finger changes to MT_TOOL_PALM,
9915 // we expect to receive ACTION_POINTER_UP with cancel flag.
9916 processSlot(mapper, FIRST_SLOT);
9917 processId(mapper, FIRST_TRACKING_ID);
9918 processToolType(mapper, MT_TOOL_PALM);
9919 processSync(mapper);
9920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009921 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009922 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9923
9924 // The following MOVE events of second finger should be processed.
9925 processSlot(mapper, SECOND_SLOT);
9926 processId(mapper, SECOND_TRACKING_ID);
9927 processPosition(mapper, x2 + 1, y2 + 1);
9928 processSync(mapper);
9929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009931 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009932
9933 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9934 // it. Second finger receive move.
9935 processSlot(mapper, FIRST_SLOT);
9936 processId(mapper, INVALID_TRACKING_ID);
9937 processSync(mapper);
9938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009940 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009941
9942 // Second finger keeps moving.
9943 processSlot(mapper, SECOND_SLOT);
9944 processId(mapper, SECOND_TRACKING_ID);
9945 processPosition(mapper, x2 + 2, y2 + 2);
9946 processSync(mapper);
9947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9948 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009949 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009950
9951 // Second finger up.
9952 processId(mapper, INVALID_TRACKING_ID);
9953 processSync(mapper);
9954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9955 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9956 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9957}
9958
9959/**
9960 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9961 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9962 */
9963TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9964 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009965 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009966 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009967 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009968
9969 NotifyMotionArgs motionArgs;
9970
9971 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9972 // First finger down.
9973 processId(mapper, FIRST_TRACKING_ID);
9974 processPosition(mapper, x1, y1);
9975 processSync(mapper);
9976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9977 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009978 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009979
9980 // Second finger down.
9981 processSlot(mapper, SECOND_SLOT);
9982 processId(mapper, SECOND_TRACKING_ID);
9983 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009984 processSync(mapper);
9985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009986 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009987 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009988
arthurhungcc7f9802020-04-30 17:55:40 +08009989 // If the tool type of the first finger changes to MT_TOOL_PALM,
9990 // we expect to receive ACTION_POINTER_UP with cancel flag.
9991 processSlot(mapper, FIRST_SLOT);
9992 processId(mapper, FIRST_TRACKING_ID);
9993 processToolType(mapper, MT_TOOL_PALM);
9994 processSync(mapper);
9995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009996 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009997 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9998
9999 // Second finger keeps moving.
10000 processSlot(mapper, SECOND_SLOT);
10001 processId(mapper, SECOND_TRACKING_ID);
10002 processPosition(mapper, x2 + 1, y2 + 1);
10003 processSync(mapper);
10004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10006
10007 // second finger becomes palm, receive cancel due to only 1 finger is active.
10008 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010009 processToolType(mapper, MT_TOOL_PALM);
10010 processSync(mapper);
10011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10012 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10013
arthurhungcc7f9802020-04-30 17:55:40 +080010014 // third finger down.
10015 processSlot(mapper, THIRD_SLOT);
10016 processId(mapper, THIRD_TRACKING_ID);
10017 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010018 processPosition(mapper, x3, y3);
10019 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10021 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010022 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010023 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010024
10025 // third finger move
10026 processId(mapper, THIRD_TRACKING_ID);
10027 processPosition(mapper, x3 + 1, y3 + 1);
10028 processSync(mapper);
10029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10030 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10031
10032 // first finger up, third finger receive move.
10033 processSlot(mapper, FIRST_SLOT);
10034 processId(mapper, INVALID_TRACKING_ID);
10035 processSync(mapper);
10036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010038 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010039
10040 // second finger up, third finger receive move.
10041 processSlot(mapper, SECOND_SLOT);
10042 processId(mapper, INVALID_TRACKING_ID);
10043 processSync(mapper);
10044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010046 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010047
10048 // third finger up.
10049 processSlot(mapper, THIRD_SLOT);
10050 processId(mapper, INVALID_TRACKING_ID);
10051 processSync(mapper);
10052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10053 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10054 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10055}
10056
10057/**
10058 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10059 * and the active finger could still be allowed to receive the events
10060 */
10061TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10062 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010063 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010064 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010065 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010066
10067 NotifyMotionArgs motionArgs;
10068
10069 // default tool type is finger
10070 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10071 processId(mapper, FIRST_TRACKING_ID);
10072 processPosition(mapper, x1, y1);
10073 processSync(mapper);
10074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10075 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010076 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010077
10078 // Second finger down.
10079 processSlot(mapper, SECOND_SLOT);
10080 processId(mapper, SECOND_TRACKING_ID);
10081 processPosition(mapper, x2, y2);
10082 processSync(mapper);
10083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010084 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010085 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010086
10087 // If the tool type of the second finger changes to MT_TOOL_PALM,
10088 // we expect to receive ACTION_POINTER_UP with cancel flag.
10089 processId(mapper, SECOND_TRACKING_ID);
10090 processToolType(mapper, MT_TOOL_PALM);
10091 processSync(mapper);
10092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010093 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010094 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10095
10096 // The following MOVE event should be processed.
10097 processSlot(mapper, FIRST_SLOT);
10098 processId(mapper, FIRST_TRACKING_ID);
10099 processPosition(mapper, x1 + 1, y1 + 1);
10100 processSync(mapper);
10101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010103 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010104
10105 // second finger up.
10106 processSlot(mapper, SECOND_SLOT);
10107 processId(mapper, INVALID_TRACKING_ID);
10108 processSync(mapper);
10109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10111
10112 // first finger keep moving
10113 processSlot(mapper, FIRST_SLOT);
10114 processId(mapper, FIRST_TRACKING_ID);
10115 processPosition(mapper, x1 + 2, y1 + 2);
10116 processSync(mapper);
10117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10119
10120 // first finger up.
10121 processId(mapper, INVALID_TRACKING_ID);
10122 processSync(mapper);
10123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10124 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10125 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010126}
10127
Arthur Hung9ad18942021-06-19 02:04:46 +000010128/**
10129 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10130 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10131 * cause slot be valid again.
10132 */
10133TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10134 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010135 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010136 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010137 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010138
10139 NotifyMotionArgs motionArgs;
10140
10141 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10142 // First finger down.
10143 processId(mapper, FIRST_TRACKING_ID);
10144 processPosition(mapper, x1, y1);
10145 processPressure(mapper, RAW_PRESSURE_MAX);
10146 processSync(mapper);
10147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10148 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010149 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010150
10151 // First finger move.
10152 processId(mapper, FIRST_TRACKING_ID);
10153 processPosition(mapper, x1 + 1, y1 + 1);
10154 processPressure(mapper, RAW_PRESSURE_MAX);
10155 processSync(mapper);
10156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010158 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010159
10160 // Second finger down.
10161 processSlot(mapper, SECOND_SLOT);
10162 processId(mapper, SECOND_TRACKING_ID);
10163 processPosition(mapper, x2, y2);
10164 processPressure(mapper, RAW_PRESSURE_MAX);
10165 processSync(mapper);
10166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010167 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010168 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010169
10170 // second finger up with some unexpected data.
10171 processSlot(mapper, SECOND_SLOT);
10172 processId(mapper, INVALID_TRACKING_ID);
10173 processPosition(mapper, x2, y2);
10174 processSync(mapper);
10175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010176 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010177 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010178
10179 // first finger up with some unexpected data.
10180 processSlot(mapper, FIRST_SLOT);
10181 processId(mapper, INVALID_TRACKING_ID);
10182 processPosition(mapper, x2, y2);
10183 processPressure(mapper, RAW_PRESSURE_MAX);
10184 processSync(mapper);
10185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10186 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010187 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010188}
10189
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010190TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10191 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010192 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010193 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010194 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010195
10196 // First finger down.
10197 processId(mapper, FIRST_TRACKING_ID);
10198 processPosition(mapper, 100, 200);
10199 processPressure(mapper, RAW_PRESSURE_MAX);
10200 processSync(mapper);
10201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10202 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10203
10204 // Second finger down.
10205 processSlot(mapper, SECOND_SLOT);
10206 processId(mapper, SECOND_TRACKING_ID);
10207 processPosition(mapper, 300, 400);
10208 processPressure(mapper, RAW_PRESSURE_MAX);
10209 processSync(mapper);
10210 ASSERT_NO_FATAL_FAILURE(
10211 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10212
10213 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010214 // preserved. Resetting should cancel the ongoing gesture.
10215 resetMapper(mapper, ARBITRARY_TIME);
10216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10217 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010218
10219 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10220 // the existing touch state to generate a down event.
10221 processPosition(mapper, 301, 302);
10222 processSync(mapper);
10223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10224 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10226 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10227
10228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10229}
10230
10231TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10232 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010233 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010234 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010235 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010236
10237 // First finger touches down and releases.
10238 processId(mapper, FIRST_TRACKING_ID);
10239 processPosition(mapper, 100, 200);
10240 processPressure(mapper, RAW_PRESSURE_MAX);
10241 processSync(mapper);
10242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10243 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10244 processId(mapper, INVALID_TRACKING_ID);
10245 processSync(mapper);
10246 ASSERT_NO_FATAL_FAILURE(
10247 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10248
10249 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10250 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010251 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10253
10254 // Send an empty sync frame. Since there are no pointers, no events are generated.
10255 processSync(mapper);
10256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10257}
10258
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010259TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010260 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010261 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010262 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010263 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010265
10266 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10267 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10268 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10269 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10270 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10271
10272 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010273 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010274 processId(mapper, FIRST_TRACKING_ID);
10275 processToolType(mapper, MT_TOOL_PEN);
10276 processPosition(mapper, 100, 200);
10277 processPressure(mapper, RAW_PRESSURE_MAX);
10278 processSync(mapper);
10279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10280 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10281 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010282 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010283
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010284 // Now that we know the device supports styluses, ensure that the device is re-configured with
10285 // the stylus source.
10286 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10287 {
10288 const auto& devices = mReader->getInputDevices();
10289 auto deviceInfo =
10290 std::find_if(devices.begin(), devices.end(),
10291 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10292 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10293 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10294 }
10295
10296 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10298
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010299 processId(mapper, INVALID_TRACKING_ID);
10300 processSync(mapper);
10301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10302 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10303 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010304 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010305}
10306
Seunghwan Choi356026c2023-02-01 14:37:25 +090010307TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10308 addConfigurationProperty("touch.deviceType", "touchScreen");
10309 prepareDisplay(ui::ROTATION_0);
10310 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10311 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10312 // indicate stylus presence dynamically.
10313 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10314 std::shared_ptr<FakePointerController> fakePointerController =
10315 std::make_shared<FakePointerController>();
10316 mFakePolicy->setPointerController(fakePointerController);
10317 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singh56adebc2023-04-25 13:56:05 +000010318 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010319
10320 processId(mapper, FIRST_TRACKING_ID);
10321 processPressure(mapper, RAW_PRESSURE_MIN);
10322 processPosition(mapper, 100, 200);
10323 processToolType(mapper, MT_TOOL_PEN);
10324 processSync(mapper);
10325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10326 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010327 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010328 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10329 ASSERT_TRUE(fakePointerController->isPointerShown());
10330 ASSERT_NO_FATAL_FAILURE(
10331 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10332}
10333
10334TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10335 addConfigurationProperty("touch.deviceType", "touchScreen");
10336 prepareDisplay(ui::ROTATION_0);
10337 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10338 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10339 // indicate stylus presence dynamically.
10340 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10341 std::shared_ptr<FakePointerController> fakePointerController =
10342 std::make_shared<FakePointerController>();
10343 mFakePolicy->setPointerController(fakePointerController);
10344 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singh56adebc2023-04-25 13:56:05 +000010345 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010346
10347 processId(mapper, FIRST_TRACKING_ID);
10348 processPressure(mapper, RAW_PRESSURE_MIN);
10349 processPosition(mapper, 100, 200);
10350 processToolType(mapper, MT_TOOL_PEN);
10351 processSync(mapper);
10352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10353 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010354 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010355 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10356 ASSERT_FALSE(fakePointerController->isPointerShown());
10357}
10358
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010359// --- MultiTouchInputMapperTest_ExternalDevice ---
10360
10361class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10362protected:
Chris Yea52ade12020-08-27 16:49:20 -070010363 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010364};
10365
10366/**
10367 * Expect fallback to internal viewport if device is external and external viewport is not present.
10368 */
10369TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10370 prepareAxes(POSITION);
10371 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010372 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +000010373 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010374
10375 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10376
10377 NotifyMotionArgs motionArgs;
10378
10379 // Expect the event to be sent to the internal viewport,
10380 // because an external viewport is not present.
10381 processPosition(mapper, 100, 100);
10382 processSync(mapper);
10383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10384 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10385
10386 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010387 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010388 processPosition(mapper, 100, 100);
10389 processSync(mapper);
10390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10391 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10392}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010393
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010394TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10395 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10396 std::shared_ptr<FakePointerController> fakePointerController =
10397 std::make_shared<FakePointerController>();
10398 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10399 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010400
10401 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010402 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010403 prepareAxes(POSITION | ID | SLOT);
10404 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10405 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10406 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010407 mFakePolicy->setPointerController(fakePointerController);
Arpit Singh56adebc2023-04-25 13:56:05 +000010408 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010409
10410 // captured touchpad should be a touchpad source
10411 NotifyDeviceResetArgs resetArgs;
10412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10413 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10414
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010415 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010416
10417 const InputDeviceInfo::MotionRange* relRangeX =
10418 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10419 ASSERT_NE(relRangeX, nullptr);
10420 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10421 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10422 const InputDeviceInfo::MotionRange* relRangeY =
10423 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10424 ASSERT_NE(relRangeY, nullptr);
10425 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10426 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10427
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010428 // run captured pointer tests - note that this is unscaled, so input listener events should be
10429 // identical to what the hardware sends (accounting for any
10430 // calibration).
10431 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010432 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010433 processId(mapper, 1);
10434 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10435 processKey(mapper, BTN_TOUCH, 1);
10436 processSync(mapper);
10437
10438 // expect coord[0] to contain initial location of touch 0
10439 NotifyMotionArgs args;
10440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10441 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010442 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010443 ASSERT_EQ(0, args.pointerProperties[0].id);
10444 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10445 ASSERT_NO_FATAL_FAILURE(
10446 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10447
10448 // FINGER 1 DOWN
10449 processSlot(mapper, 1);
10450 processId(mapper, 2);
10451 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10452 processSync(mapper);
10453
10454 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010456 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010457 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010458 ASSERT_EQ(0, args.pointerProperties[0].id);
10459 ASSERT_EQ(1, args.pointerProperties[1].id);
10460 ASSERT_NO_FATAL_FAILURE(
10461 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10462 ASSERT_NO_FATAL_FAILURE(
10463 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10464
10465 // FINGER 1 MOVE
10466 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10467 processSync(mapper);
10468
10469 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10470 // from move
10471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10473 ASSERT_NO_FATAL_FAILURE(
10474 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10475 ASSERT_NO_FATAL_FAILURE(
10476 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10477
10478 // FINGER 0 MOVE
10479 processSlot(mapper, 0);
10480 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10481 processSync(mapper);
10482
10483 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10486 ASSERT_NO_FATAL_FAILURE(
10487 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10488 ASSERT_NO_FATAL_FAILURE(
10489 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10490
10491 // BUTTON DOWN
10492 processKey(mapper, BTN_LEFT, 1);
10493 processSync(mapper);
10494
10495 // touchinputmapper design sends a move before button press
10496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10499 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10500
10501 // BUTTON UP
10502 processKey(mapper, BTN_LEFT, 0);
10503 processSync(mapper);
10504
10505 // touchinputmapper design sends a move after button release
10506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10507 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10510
10511 // FINGER 0 UP
10512 processId(mapper, -1);
10513 processSync(mapper);
10514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10515 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10516
10517 // FINGER 1 MOVE
10518 processSlot(mapper, 1);
10519 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10520 processSync(mapper);
10521
10522 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010525 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010526 ASSERT_EQ(1, args.pointerProperties[0].id);
10527 ASSERT_NO_FATAL_FAILURE(
10528 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10529
10530 // FINGER 1 UP
10531 processId(mapper, -1);
10532 processKey(mapper, BTN_TOUCH, 0);
10533 processSync(mapper);
10534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10535 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10536
Josep del Río2d8c79a2023-01-23 19:33:50 +000010537 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010538 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010539 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010541 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010542}
10543
10544TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10545 std::shared_ptr<FakePointerController> fakePointerController =
10546 std::make_shared<FakePointerController>();
10547 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10548 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010549
10550 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010551 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010552 prepareAxes(POSITION | ID | SLOT);
10553 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10554 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010555 mFakePolicy->setPointerController(fakePointerController);
Arpit Singh56adebc2023-04-25 13:56:05 +000010556 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010557 // run uncaptured pointer tests - pushes out generic events
10558 // FINGER 0 DOWN
10559 processId(mapper, 3);
10560 processPosition(mapper, 100, 100);
10561 processKey(mapper, BTN_TOUCH, 1);
10562 processSync(mapper);
10563
10564 // start at (100,100), cursor should be at (0,0) * scale
10565 NotifyMotionArgs args;
10566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10567 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10568 ASSERT_NO_FATAL_FAILURE(
10569 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10570
10571 // FINGER 0 MOVE
10572 processPosition(mapper, 200, 200);
10573 processSync(mapper);
10574
10575 // compute scaling to help with touch position checking
10576 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10577 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10578 float scale =
10579 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10580
10581 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10583 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10585 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010586
10587 // BUTTON DOWN
10588 processKey(mapper, BTN_LEFT, 1);
10589 processSync(mapper);
10590
10591 // touchinputmapper design sends a move before button press
10592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10593 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10595 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10596
10597 // BUTTON UP
10598 processKey(mapper, BTN_LEFT, 0);
10599 processSync(mapper);
10600
10601 // touchinputmapper design sends a move after button release
10602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10603 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10605 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010606}
10607
10608TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10609 std::shared_ptr<FakePointerController> fakePointerController =
10610 std::make_shared<FakePointerController>();
10611
Michael Wrighta9cf4192022-12-01 23:46:39 +000010612 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010613 prepareAxes(POSITION | ID | SLOT);
10614 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010615 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010616 mFakePolicy->setPointerCapture(false);
Arpit Singh56adebc2023-04-25 13:56:05 +000010617 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010618
Josep del Río2d8c79a2023-01-23 19:33:50 +000010619 // uncaptured touchpad should be a pointer device
10620 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010621
Josep del Río2d8c79a2023-01-23 19:33:50 +000010622 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010623 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010624 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010625 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10626}
10627
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010628// --- BluetoothMultiTouchInputMapperTest ---
10629
10630class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10631protected:
10632 void SetUp() override {
10633 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10634 }
10635};
10636
10637TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10638 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010639 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010640 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010641 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010642
10643 nsecs_t kernelEventTime = ARBITRARY_TIME;
10644 nsecs_t expectedEventTime = ARBITRARY_TIME;
10645 // Touch down.
10646 processId(mapper, FIRST_TRACKING_ID);
10647 processPosition(mapper, 100, 200);
10648 processPressure(mapper, RAW_PRESSURE_MAX);
10649 processSync(mapper, ARBITRARY_TIME);
10650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10651 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10652
10653 // Process several events that come in quick succession, according to their timestamps.
10654 for (int i = 0; i < 3; i++) {
10655 constexpr static nsecs_t delta = ms2ns(1);
10656 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10657 kernelEventTime += delta;
10658 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10659
10660 processPosition(mapper, 101 + i, 201 + i);
10661 processSync(mapper, kernelEventTime);
10662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10663 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10664 WithEventTime(expectedEventTime))));
10665 }
10666
10667 // Release the touch.
10668 processId(mapper, INVALID_TRACKING_ID);
10669 processPressure(mapper, RAW_PRESSURE_MIN);
10670 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10672 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10673 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10674}
10675
10676// --- MultiTouchPointerModeTest ---
10677
HQ Liue6983c72022-04-19 22:14:56 +000010678class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10679protected:
10680 float mPointerMovementScale;
10681 float mPointerXZoomScale;
10682 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10683 addConfigurationProperty("touch.deviceType", "pointer");
10684 std::shared_ptr<FakePointerController> fakePointerController =
10685 std::make_shared<FakePointerController>();
10686 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10687 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010688 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010689
10690 prepareAxes(POSITION);
10691 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10692 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10693 // needs to be disabled, and the pointer gesture needs to be enabled.
10694 mFakePolicy->setPointerCapture(false);
10695 mFakePolicy->setPointerGestureEnabled(true);
10696 mFakePolicy->setPointerController(fakePointerController);
10697
10698 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10699 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10700 mPointerMovementScale =
10701 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10702 mPointerXZoomScale =
10703 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10704 }
10705
10706 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10707 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10708 /*flat*/ 0,
10709 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10710 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10711 /*flat*/ 0,
10712 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10713 }
10714};
10715
10716/**
10717 * Two fingers down on a pointer mode touch pad. The width
10718 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10719 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10720 * be greater than the both value to be freeform gesture, so that after two
10721 * fingers start to move downwards, the gesture should be swipe.
10722 */
10723TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10724 // The min freeform gesture width is 25units/mm x 30mm = 750
10725 // which is greater than fraction of the diagnal length of the touchpad (349).
10726 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010727 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010728 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010729 NotifyMotionArgs motionArgs;
10730
10731 // Two fingers down at once.
10732 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10733 // Pointer's initial position is used the [0,0] coordinate.
10734 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10735
10736 processId(mapper, FIRST_TRACKING_ID);
10737 processPosition(mapper, x1, y1);
10738 processMTSync(mapper);
10739 processId(mapper, SECOND_TRACKING_ID);
10740 processPosition(mapper, x2, y2);
10741 processMTSync(mapper);
10742 processSync(mapper);
10743
10744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010745 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010746 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010747 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010748 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010749 ASSERT_NO_FATAL_FAILURE(
10750 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10751
10752 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10753 // that there should be 1 pointer.
10754 int32_t movingDistance = 200;
10755 y1 += movingDistance;
10756 y2 += movingDistance;
10757
10758 processId(mapper, FIRST_TRACKING_ID);
10759 processPosition(mapper, x1, y1);
10760 processMTSync(mapper);
10761 processId(mapper, SECOND_TRACKING_ID);
10762 processPosition(mapper, x2, y2);
10763 processMTSync(mapper);
10764 processSync(mapper);
10765
10766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010767 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010768 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010769 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010770 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10772 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10773 0, 0, 0, 0));
10774}
10775
10776/**
10777 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10778 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10779 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10780 * value to be freeform gesture, so that after two fingers start to move downwards,
10781 * the gesture should be swipe.
10782 */
10783TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10784 // The min freeform gesture width is 5units/mm x 30mm = 150
10785 // which is greater than fraction of the diagnal length of the touchpad (349).
10786 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010787 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singh56adebc2023-04-25 13:56:05 +000010788 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010789 NotifyMotionArgs motionArgs;
10790
10791 // Two fingers down at once.
10792 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10793 // Pointer's initial position is used the [0,0] coordinate.
10794 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10795
10796 processId(mapper, FIRST_TRACKING_ID);
10797 processPosition(mapper, x1, y1);
10798 processMTSync(mapper);
10799 processId(mapper, SECOND_TRACKING_ID);
10800 processPosition(mapper, x2, y2);
10801 processMTSync(mapper);
10802 processSync(mapper);
10803
10804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010805 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010806 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010807 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010808 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010809 ASSERT_NO_FATAL_FAILURE(
10810 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10811
10812 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10813 // and there should be 1 pointer.
10814 int32_t movingDistance = 200;
10815 y1 += movingDistance;
10816 y2 += movingDistance;
10817
10818 processId(mapper, FIRST_TRACKING_ID);
10819 processPosition(mapper, x1, y1);
10820 processMTSync(mapper);
10821 processId(mapper, SECOND_TRACKING_ID);
10822 processPosition(mapper, x2, y2);
10823 processMTSync(mapper);
10824 processSync(mapper);
10825
10826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010827 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010829 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010830 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010831 // New coordinate is the scaled relative coordinate from the initial coordinate.
10832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10833 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10834 0, 0, 0, 0));
10835}
10836
10837/**
10838 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10839 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10840 * freeform gestures after two fingers start to move downwards.
10841 */
10842TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010843 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010844 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010845
10846 NotifyMotionArgs motionArgs;
10847
10848 // Two fingers down at once. Wider than the max swipe width.
10849 // The gesture is expected to be PRESS, then transformed to FREEFORM
10850 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10851
10852 processId(mapper, FIRST_TRACKING_ID);
10853 processPosition(mapper, x1, y1);
10854 processMTSync(mapper);
10855 processId(mapper, SECOND_TRACKING_ID);
10856 processPosition(mapper, x2, y2);
10857 processMTSync(mapper);
10858 processSync(mapper);
10859
10860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010861 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010862 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010863 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010864 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010865 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10866 ASSERT_NO_FATAL_FAILURE(
10867 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10868
10869 int32_t movingDistance = 200;
10870
10871 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10872 // then two down events for two pointers.
10873 y1 += movingDistance;
10874 y2 += movingDistance;
10875
10876 processId(mapper, FIRST_TRACKING_ID);
10877 processPosition(mapper, x1, y1);
10878 processMTSync(mapper);
10879 processId(mapper, SECOND_TRACKING_ID);
10880 processPosition(mapper, x2, y2);
10881 processMTSync(mapper);
10882 processSync(mapper);
10883
10884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10885 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010886 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010887 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010889 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010890 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010891 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010893 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010894 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010895 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010896 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010897 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010898 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010899 // Two pointers' scaled relative coordinates from their initial centroid.
10900 // Initial y coordinates are 0 as y1 and y2 have the same value.
10901 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10902 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10903 // When pointers move, the new coordinates equal to the initial coordinates plus
10904 // scaled moving distance.
10905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10906 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10907 0, 0, 0, 0));
10908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10909 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10910 0, 0, 0, 0));
10911
10912 // Move two fingers down again, expect one MOVE motion event.
10913 y1 += movingDistance;
10914 y2 += movingDistance;
10915
10916 processId(mapper, FIRST_TRACKING_ID);
10917 processPosition(mapper, x1, y1);
10918 processMTSync(mapper);
10919 processId(mapper, SECOND_TRACKING_ID);
10920 processPosition(mapper, x2, y2);
10921 processMTSync(mapper);
10922 processSync(mapper);
10923
10924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010925 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010927 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010928 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10930 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10931 0, 0, 0, 0, 0));
10932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10933 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10934 0, 0, 0, 0, 0));
10935}
10936
Harry Cutts39b7ca22022-10-05 15:55:48 +000010937TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010938 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010939 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010940 NotifyMotionArgs motionArgs;
10941
10942 // Place two fingers down.
10943 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10944
10945 processId(mapper, FIRST_TRACKING_ID);
10946 processPosition(mapper, x1, y1);
10947 processMTSync(mapper);
10948 processId(mapper, SECOND_TRACKING_ID);
10949 processPosition(mapper, x2, y2);
10950 processMTSync(mapper);
10951 processSync(mapper);
10952
10953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010954 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010955 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10956 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10957 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10958 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10959
10960 // Move the two fingers down and to the left.
10961 int32_t movingDistance = 200;
10962 x1 -= movingDistance;
10963 y1 += movingDistance;
10964 x2 -= movingDistance;
10965 y2 += movingDistance;
10966
10967 processId(mapper, FIRST_TRACKING_ID);
10968 processPosition(mapper, x1, y1);
10969 processMTSync(mapper);
10970 processId(mapper, SECOND_TRACKING_ID);
10971 processPosition(mapper, x2, y2);
10972 processMTSync(mapper);
10973 processSync(mapper);
10974
10975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010976 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010977 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10978 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10979 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10980 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10981}
10982
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010983TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010984 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010985 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +000010986 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10988
10989 // Start a stylus gesture.
10990 processKey(mapper, BTN_TOOL_PEN, 1);
10991 processId(mapper, FIRST_TRACKING_ID);
10992 processPosition(mapper, 100, 200);
10993 processSync(mapper);
10994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10995 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10996 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010997 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010998 // TODO(b/257078296): Pointer mode generates extra event.
10999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11000 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11001 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011002 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11004
11005 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11006 // gesture should be disabled.
11007 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11008 viewport->isActive = false;
11009 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011010 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11012 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11013 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011014 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011015 // TODO(b/257078296): Pointer mode generates extra event.
11016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11017 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11018 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011019 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11021}
11022
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011023// --- JoystickInputMapperTest ---
11024
11025class JoystickInputMapperTest : public InputMapperTest {
11026protected:
11027 static const int32_t RAW_X_MIN;
11028 static const int32_t RAW_X_MAX;
11029 static const int32_t RAW_Y_MIN;
11030 static const int32_t RAW_Y_MAX;
11031
11032 void SetUp() override {
11033 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11034 }
11035 void prepareAxes() {
11036 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11037 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11038 }
11039
11040 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11042 }
11043
11044 void processSync(JoystickInputMapper& mapper) {
11045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11046 }
11047
Michael Wrighta9cf4192022-12-01 23:46:39 +000011048 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011049 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11050 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11051 NO_PORT, ViewportType::VIRTUAL);
11052 }
11053};
11054
11055const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11056const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11057const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11058const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11059
11060TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11061 prepareAxes();
Arpit Singh2be4a362023-04-26 14:16:50 +000011062 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011063
11064 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11065
Michael Wrighta9cf4192022-12-01 23:46:39 +000011066 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011067
11068 // Send an axis event
11069 processAxis(mapper, ABS_X, 100);
11070 processSync(mapper);
11071
11072 NotifyMotionArgs args;
11073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11074 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11075
11076 // Send another axis event
11077 processAxis(mapper, ABS_Y, 100);
11078 processSync(mapper);
11079
11080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11081 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11082}
11083
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011084// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011085
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011086class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011087protected:
11088 static const char* DEVICE_NAME;
11089 static const char* DEVICE_LOCATION;
11090 static const int32_t DEVICE_ID;
11091 static const int32_t DEVICE_GENERATION;
11092 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011093 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011094 static const int32_t EVENTHUB_ID;
11095
11096 std::shared_ptr<FakeEventHub> mFakeEventHub;
11097 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011098 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011099 std::unique_ptr<InstrumentedInputReader> mReader;
11100 std::shared_ptr<InputDevice> mDevice;
11101
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011102 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011103 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011104 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011105 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011106 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011107 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011108 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11109 }
11110
11111 void SetUp() override { SetUp(DEVICE_CLASSES); }
11112
11113 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011114 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011115 mFakePolicy.clear();
11116 }
11117
Chris Yee2b1e5c2021-03-10 22:45:12 -080011118 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11119 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011120 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011121 InputDeviceIdentifier identifier;
11122 identifier.name = name;
11123 identifier.location = location;
11124 std::shared_ptr<InputDevice> device =
11125 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11126 identifier);
11127 mReader->pushNextDevice(device);
11128 mFakeEventHub->addDevice(eventHubId, name, classes);
11129 mReader->loopOnce();
11130 return device;
11131 }
11132
11133 template <class T, typename... Args>
11134 T& addControllerAndConfigure(Args... args) {
11135 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11136
11137 return controller;
11138 }
11139};
11140
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011141const char* PeripheralControllerTest::DEVICE_NAME = "device";
11142const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11143const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11144const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11145const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011146const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11147 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011148const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011149
11150// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011151class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011152protected:
11153 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011154 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011155 }
11156};
11157
11158TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011159 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011160
Harry Cuttsa5b71292022-11-28 12:56:17 +000011161 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11162 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11163 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011164}
11165
11166TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011167 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011168
Harry Cuttsa5b71292022-11-28 12:56:17 +000011169 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11170 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11171 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011172}
11173
11174// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011175class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011176protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011177 void SetUp() override {
11178 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11179 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011180};
11181
Chris Ye85758332021-05-16 23:05:17 -070011182TEST_F(LightControllerTest, MonoLight) {
11183 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011184 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011185 .maxBrightness = 255,
11186 .flags = InputLightClass::BRIGHTNESS,
11187 .path = ""};
11188 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011189
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011190 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011191 InputDeviceInfo info;
11192 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011193 std::vector<InputDeviceLightInfo> lights = info.getLights();
11194 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011195 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11196 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11197
11198 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11199 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11200}
11201
11202TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11203 RawLightInfo infoMono = {.id = 1,
11204 .name = "mono_keyboard_backlight",
11205 .maxBrightness = 255,
11206 .flags = InputLightClass::BRIGHTNESS |
11207 InputLightClass::KEYBOARD_BACKLIGHT,
11208 .path = ""};
11209 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11210
11211 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11212 InputDeviceInfo info;
11213 controller.populateDeviceInfo(&info);
11214 std::vector<InputDeviceLightInfo> lights = info.getLights();
11215 ASSERT_EQ(1U, lights.size());
11216 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11217 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011218
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011219 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11220 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011221}
11222
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011223TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11224 RawLightInfo infoMono = {.id = 1,
11225 .name = "mono_light",
11226 .maxBrightness = 255,
11227 .flags = InputLightClass::BRIGHTNESS,
11228 .path = ""};
11229 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11230 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11231 "0,100,200");
11232
11233 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11234 std::list<NotifyArgs> unused =
11235 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11236 /*changes=*/{});
11237
11238 InputDeviceInfo info;
11239 controller.populateDeviceInfo(&info);
11240 std::vector<InputDeviceLightInfo> lights = info.getLights();
11241 ASSERT_EQ(1U, lights.size());
11242 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11243}
11244
11245TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11246 RawLightInfo infoMono = {.id = 1,
11247 .name = "mono_keyboard_backlight",
11248 .maxBrightness = 255,
11249 .flags = InputLightClass::BRIGHTNESS |
11250 InputLightClass::KEYBOARD_BACKLIGHT,
11251 .path = ""};
11252 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11253
11254 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11255 std::list<NotifyArgs> unused =
11256 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11257 /*changes=*/{});
11258
11259 InputDeviceInfo info;
11260 controller.populateDeviceInfo(&info);
11261 std::vector<InputDeviceLightInfo> lights = info.getLights();
11262 ASSERT_EQ(1U, lights.size());
11263 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11264}
11265
11266TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11267 RawLightInfo infoMono = {.id = 1,
11268 .name = "mono_keyboard_backlight",
11269 .maxBrightness = 255,
11270 .flags = InputLightClass::BRIGHTNESS |
11271 InputLightClass::KEYBOARD_BACKLIGHT,
11272 .path = ""};
11273 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11274 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11275 "0,100,200");
11276
11277 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11278 std::list<NotifyArgs> unused =
11279 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11280 /*changes=*/{});
11281
11282 InputDeviceInfo info;
11283 controller.populateDeviceInfo(&info);
11284 std::vector<InputDeviceLightInfo> lights = info.getLights();
11285 ASSERT_EQ(1U, lights.size());
11286 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
11287 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
11288 ASSERT_EQ(BrightnessLevel(0), *it);
11289 std::advance(it, 1);
11290 ASSERT_EQ(BrightnessLevel(100), *it);
11291 std::advance(it, 1);
11292 ASSERT_EQ(BrightnessLevel(200), *it);
11293}
11294
11295TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
11296 RawLightInfo infoMono = {.id = 1,
11297 .name = "mono_keyboard_backlight",
11298 .maxBrightness = 255,
11299 .flags = InputLightClass::BRIGHTNESS |
11300 InputLightClass::KEYBOARD_BACKLIGHT,
11301 .path = ""};
11302 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11303 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11304 "0,100,200,300,400,500");
11305
11306 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11307 std::list<NotifyArgs> unused =
11308 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11309 /*changes=*/{});
11310
11311 InputDeviceInfo info;
11312 controller.populateDeviceInfo(&info);
11313 std::vector<InputDeviceLightInfo> lights = info.getLights();
11314 ASSERT_EQ(1U, lights.size());
11315 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11316}
11317
Chris Yee2b1e5c2021-03-10 22:45:12 -080011318TEST_F(LightControllerTest, RGBLight) {
11319 RawLightInfo infoRed = {.id = 1,
11320 .name = "red",
11321 .maxBrightness = 255,
11322 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11323 .path = ""};
11324 RawLightInfo infoGreen = {.id = 2,
11325 .name = "green",
11326 .maxBrightness = 255,
11327 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11328 .path = ""};
11329 RawLightInfo infoBlue = {.id = 3,
11330 .name = "blue",
11331 .maxBrightness = 255,
11332 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11333 .path = ""};
11334 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11335 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11336 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11337
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011338 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011339 InputDeviceInfo info;
11340 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011341 std::vector<InputDeviceLightInfo> lights = info.getLights();
11342 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011343 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));
11346
11347 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11348 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11349}
11350
11351TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11352 RawLightInfo infoRed = {.id = 1,
11353 .name = "red_keyboard_backlight",
11354 .maxBrightness = 255,
11355 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11356 InputLightClass::KEYBOARD_BACKLIGHT,
11357 .path = ""};
11358 RawLightInfo infoGreen = {.id = 2,
11359 .name = "green_keyboard_backlight",
11360 .maxBrightness = 255,
11361 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11362 InputLightClass::KEYBOARD_BACKLIGHT,
11363 .path = ""};
11364 RawLightInfo infoBlue = {.id = 3,
11365 .name = "blue_keyboard_backlight",
11366 .maxBrightness = 255,
11367 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11368 InputLightClass::KEYBOARD_BACKLIGHT,
11369 .path = ""};
11370 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11371 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11372 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11373
11374 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11375 InputDeviceInfo info;
11376 controller.populateDeviceInfo(&info);
11377 std::vector<InputDeviceLightInfo> lights = info.getLights();
11378 ASSERT_EQ(1U, lights.size());
11379 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11380 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11381 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11382
11383 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11384 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11385}
11386
11387TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11388 RawLightInfo infoRed = {.id = 1,
11389 .name = "red",
11390 .maxBrightness = 255,
11391 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11392 .path = ""};
11393 RawLightInfo infoGreen = {.id = 2,
11394 .name = "green",
11395 .maxBrightness = 255,
11396 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11397 .path = ""};
11398 RawLightInfo infoBlue = {.id = 3,
11399 .name = "blue",
11400 .maxBrightness = 255,
11401 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11402 .path = ""};
11403 RawLightInfo infoGlobal = {.id = 3,
11404 .name = "global_keyboard_backlight",
11405 .maxBrightness = 255,
11406 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11407 InputLightClass::KEYBOARD_BACKLIGHT,
11408 .path = ""};
11409 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11410 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11411 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11412 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11413
11414 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11415 InputDeviceInfo info;
11416 controller.populateDeviceInfo(&info);
11417 std::vector<InputDeviceLightInfo> lights = info.getLights();
11418 ASSERT_EQ(1U, lights.size());
11419 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11420 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11421 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011422
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011423 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11424 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011425}
11426
11427TEST_F(LightControllerTest, MultiColorRGBLight) {
11428 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011429 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011430 .maxBrightness = 255,
11431 .flags = InputLightClass::BRIGHTNESS |
11432 InputLightClass::MULTI_INTENSITY |
11433 InputLightClass::MULTI_INDEX,
11434 .path = ""};
11435
11436 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11437
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011438 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011439 InputDeviceInfo info;
11440 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011441 std::vector<InputDeviceLightInfo> lights = info.getLights();
11442 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011443 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11444 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11445 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11446
11447 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11448 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11449}
11450
11451TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11452 RawLightInfo infoColor = {.id = 1,
11453 .name = "multi_color_keyboard_backlight",
11454 .maxBrightness = 255,
11455 .flags = InputLightClass::BRIGHTNESS |
11456 InputLightClass::MULTI_INTENSITY |
11457 InputLightClass::MULTI_INDEX |
11458 InputLightClass::KEYBOARD_BACKLIGHT,
11459 .path = ""};
11460
11461 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11462
11463 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11464 InputDeviceInfo info;
11465 controller.populateDeviceInfo(&info);
11466 std::vector<InputDeviceLightInfo> lights = info.getLights();
11467 ASSERT_EQ(1U, lights.size());
11468 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11469 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11470 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011471
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011472 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11473 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011474}
11475
11476TEST_F(LightControllerTest, PlayerIdLight) {
11477 RawLightInfo info1 = {.id = 1,
11478 .name = "player1",
11479 .maxBrightness = 255,
11480 .flags = InputLightClass::BRIGHTNESS,
11481 .path = ""};
11482 RawLightInfo info2 = {.id = 2,
11483 .name = "player2",
11484 .maxBrightness = 255,
11485 .flags = InputLightClass::BRIGHTNESS,
11486 .path = ""};
11487 RawLightInfo info3 = {.id = 3,
11488 .name = "player3",
11489 .maxBrightness = 255,
11490 .flags = InputLightClass::BRIGHTNESS,
11491 .path = ""};
11492 RawLightInfo info4 = {.id = 4,
11493 .name = "player4",
11494 .maxBrightness = 255,
11495 .flags = InputLightClass::BRIGHTNESS,
11496 .path = ""};
11497 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11498 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11499 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11500 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11501
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011502 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011503 InputDeviceInfo info;
11504 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011505 std::vector<InputDeviceLightInfo> lights = info.getLights();
11506 ASSERT_EQ(1U, lights.size());
11507 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011508 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11509 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011510
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011511 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11512 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11513 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011514}
11515
Michael Wrightd02c5b62014-02-10 15:10:22 -080011516} // namespace android