blob: c045e15f199f10a12de40ce3ab944f39b93a0c72 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
Harry Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020
Prabir Pradhan2770d242019-09-02 18:07:11 -070021#include <CursorInputMapper.h>
22#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080025#include <InputReaderBase.h>
26#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000027#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <KeyboardInputMapper.h>
29#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070031#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070032#include <SingleTouchInputMapper.h>
33#include <SwitchInputMapper.h>
34#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000035#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070036#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080037#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000038#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039#include <android-base/thread_annotations.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000040#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050042#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000043#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080044
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070045#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000046#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000047#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000048#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000049#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000050#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000051#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000052#include "input/DisplayViewport.h"
53#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010054
Michael Wrightd02c5b62014-02-10 15:10:22 -080055namespace android {
56
Dominik Laskowski2f01d772022-03-23 16:01:29 -070057using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000058using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070059using std::chrono_literals::operator""ms;
60
Michael Wrightd02c5b62014-02-10 15:10:22 -080061// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000063static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t DISPLAY_WIDTH = 480;
67static constexpr int32_t DISPLAY_HEIGHT = 800;
68static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
69static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
70static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070071static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070072static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080073
arthurhungcc7f9802020-04-30 17:55:40 +080074static constexpr int32_t FIRST_SLOT = 0;
75static constexpr int32_t SECOND_SLOT = 1;
76static constexpr int32_t THIRD_SLOT = 2;
77static constexpr int32_t INVALID_TRACKING_ID = -1;
78static constexpr int32_t FIRST_TRACKING_ID = 0;
79static constexpr int32_t SECOND_TRACKING_ID = 1;
80static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080081static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
82static constexpr int32_t LIGHT_COLOR = 0x7F448866;
83static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080084
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080085static constexpr int32_t ACTION_POINTER_0_DOWN =
86 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
87static constexpr int32_t ACTION_POINTER_0_UP =
88 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
89static constexpr int32_t ACTION_POINTER_1_DOWN =
90 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_1_UP =
92 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000094// Minimum timestamp separation between subsequent input events from a Bluetooth device.
95static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
96// Maximum smoothing time delta so that we don't generate events too far into the future.
97constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
98
Michael Wrightd02c5b62014-02-10 15:10:22 -080099template<typename T>
100static inline T min(T a, T b) {
101 return a < b ? a : b;
102}
103
104static inline float avg(float x, float y) {
105 return (x + y) / 2;
106}
107
Chris Ye3fdbfef2021-01-06 18:45:18 -0800108// Mapping for light color name and the light color
109const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
110 {"green", LightColor::GREEN},
111 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112
Michael Wrighta9cf4192022-12-01 23:46:39 +0000113static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700114 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000115 case ui::ROTATION_90:
116 return ui::ROTATION_270;
117 case ui::ROTATION_270:
118 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700119 default:
120 return orientation;
121 }
122}
123
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800124static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
125 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000126 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800127
128 const InputDeviceInfo::MotionRange* motionRange =
129 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
130 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
131}
132
133static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
134 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000135 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800136
137 const InputDeviceInfo::MotionRange* motionRange =
138 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
139 ASSERT_EQ(nullptr, motionRange);
140}
141
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700142[[maybe_unused]] static void dumpReader(InputReader& reader) {
143 std::string dump;
144 reader.dump(dump);
145 std::istringstream iss(dump);
146 for (std::string line; std::getline(iss, line);) {
147 ALOGE("%s", line.c_str());
148 std::this_thread::sleep_for(std::chrono::milliseconds(1));
149 }
150}
151
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152// --- FakeInputMapper ---
153
154class FakeInputMapper : public InputMapper {
155 uint32_t mSources;
156 int32_t mKeyboardType;
157 int32_t mMetaState;
158 KeyedVector<int32_t, int32_t> mKeyCodeStates;
159 KeyedVector<int32_t, int32_t> mScanCodeStates;
160 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100161 // fake mapping which would normally come from keyCharacterMap
162 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800163 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700165 std::mutex mLock;
166 std::condition_variable mStateChangedCondition;
167 bool mConfigureWasCalled GUARDED_BY(mLock);
168 bool mResetWasCalled GUARDED_BY(mLock);
169 bool mProcessWasCalled GUARDED_BY(mLock);
170 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171
Arthur Hungc23540e2018-11-29 20:42:11 +0800172 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000174 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
175 uint32_t sources)
176 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800177 mSources(sources),
178 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800180 mConfigureWasCalled(false),
181 mResetWasCalled(false),
182 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
Chris Yea52ade12020-08-27 16:49:20 -0700184 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185
186 void setKeyboardType(int32_t keyboardType) {
187 mKeyboardType = keyboardType;
188 }
189
190 void setMetaState(int32_t metaState) {
191 mMetaState = metaState;
192 }
193
194 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700195 std::unique_lock<std::mutex> lock(mLock);
196 base::ScopedLockAssertion assumeLocked(mLock);
197 const bool configureCalled =
198 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
199 return mConfigureWasCalled;
200 });
201 if (!configureCalled) {
202 FAIL() << "Expected configure() to have been called.";
203 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204 mConfigureWasCalled = false;
205 }
206
207 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208 std::unique_lock<std::mutex> lock(mLock);
209 base::ScopedLockAssertion assumeLocked(mLock);
210 const bool resetCalled =
211 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
212 return mResetWasCalled;
213 });
214 if (!resetCalled) {
215 FAIL() << "Expected reset() to have been called.";
216 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 mResetWasCalled = false;
218 }
219
Yi Kong9b14ac62018-07-17 13:48:38 -0700220 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 std::unique_lock<std::mutex> lock(mLock);
222 base::ScopedLockAssertion assumeLocked(mLock);
223 const bool processCalled =
224 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
225 return mProcessWasCalled;
226 });
227 if (!processCalled) {
228 FAIL() << "Expected process() to have been called.";
229 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 if (outLastEvent) {
231 *outLastEvent = mLastEvent;
232 }
233 mProcessWasCalled = false;
234 }
235
236 void setKeyCodeState(int32_t keyCode, int32_t state) {
237 mKeyCodeStates.replaceValueFor(keyCode, state);
238 }
239
240 void setScanCodeState(int32_t scanCode, int32_t state) {
241 mScanCodeStates.replaceValueFor(scanCode, state);
242 }
243
244 void setSwitchState(int32_t switchCode, int32_t state) {
245 mSwitchStates.replaceValueFor(switchCode, state);
246 }
247
248 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800249 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250 }
251
Philip Junker4af3b3d2021-12-14 10:36:55 +0100252 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
253 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
254 }
255
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100257 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258
Harry Cuttsd02ea102023-03-17 18:21:30 +0000259 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 InputMapper::populateDeviceInfo(deviceInfo);
261
262 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000263 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264 }
265 }
266
Arpit Singhed6c3de2023-04-05 19:24:37 +0000267 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000268 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700269 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800271
272 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800273 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000274 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000275 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800276 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700277
278 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700279 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280 }
281
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700282 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700283 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700285 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700286 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 }
288
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700289 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700290 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291 mLastEvent = *rawEvent;
292 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700293 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700294 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295 }
296
Chris Yea52ade12020-08-27 16:49:20 -0700297 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
299 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
300 }
301
Philip Junker4af3b3d2021-12-14 10:36:55 +0100302 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
303 auto it = mKeyCodeMapping.find(locationKeyCode);
304 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
305 }
306
Chris Yea52ade12020-08-27 16:49:20 -0700307 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
309 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
310 }
311
Chris Yea52ade12020-08-27 16:49:20 -0700312 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 ssize_t index = mSwitchStates.indexOfKey(switchCode);
314 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
315 }
316
Chris Yea52ade12020-08-27 16:49:20 -0700317 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700318 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700319 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700320 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
322 if (keyCodes[i] == mSupportedKeyCodes[j]) {
323 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800324 }
325 }
326 }
Chris Yea52ade12020-08-27 16:49:20 -0700327 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328 return result;
329 }
330
331 virtual int32_t getMetaState() {
332 return mMetaState;
333 }
334
335 virtual void fadePointer() {
336 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800337
338 virtual std::optional<int32_t> getAssociatedDisplay() {
339 if (mViewport) {
340 return std::make_optional(mViewport->displayId);
341 }
342 return std::nullopt;
343 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344};
345
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700346// --- InputReaderPolicyTest ---
347class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700348protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700349 sp<FakeInputReaderPolicy> mFakePolicy;
350
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700351 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700352 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700353};
354
355/**
356 * Check that empty set of viewports is an acceptable configuration.
357 * Also try to get internal viewport two different ways - by type and by uniqueId.
358 *
359 * There will be confusion if two viewports with empty uniqueId and identical type are present.
360 * Such configuration is not currently allowed.
361 */
362TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700363 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700364
365 // We didn't add any viewports yet, so there shouldn't be any.
366 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100367 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700368 ASSERT_FALSE(internalViewport);
369
370 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000371 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000372 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700373
374 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700375 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700376 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100377 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700378
379 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100380 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700381 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700382 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700383
384 mFakePolicy->clearViewports();
385 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700386 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700387 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100388 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700389 ASSERT_FALSE(internalViewport);
390}
391
392TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
393 const std::string internalUniqueId = "local:0";
394 const std::string externalUniqueId = "local:1";
395 const std::string virtualUniqueId1 = "virtual:2";
396 const std::string virtualUniqueId2 = "virtual:3";
397 constexpr int32_t virtualDisplayId1 = 2;
398 constexpr int32_t virtualDisplayId2 = 3;
399
400 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000401 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000402 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000403 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700404 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000405 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000406 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000407 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700408 // Add an virtual viewport
409 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000410 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000411 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700412 // Add another virtual viewport
413 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000414 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000415 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700416
417 // Check matching by type for internal
418 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100419 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700420 ASSERT_TRUE(internalViewport);
421 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
422
423 // Check matching by type for external
424 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100425 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700426 ASSERT_TRUE(externalViewport);
427 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
428
429 // Check matching by uniqueId for virtual viewport #1
430 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700431 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700432 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100433 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700434 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
435 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
436
437 // Check matching by uniqueId for virtual viewport #2
438 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700439 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700440 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100441 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700442 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
443 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
444}
445
446
447/**
448 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
449 * that lookup works by checking display id.
450 * Check that 2 viewports of each kind is possible, for all existing viewport types.
451 */
452TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
453 const std::string uniqueId1 = "uniqueId1";
454 const std::string uniqueId2 = "uniqueId2";
455 constexpr int32_t displayId1 = 2;
456 constexpr int32_t displayId2 = 3;
457
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100458 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
459 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700460 for (const ViewportType& type : types) {
461 mFakePolicy->clearViewports();
462 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000463 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000464 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700465 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000466 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000467 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700468
469 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700470 std::optional<DisplayViewport> viewport1 =
471 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700472 ASSERT_TRUE(viewport1);
473 ASSERT_EQ(displayId1, viewport1->displayId);
474 ASSERT_EQ(type, viewport1->type);
475
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700476 std::optional<DisplayViewport> viewport2 =
477 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700478 ASSERT_TRUE(viewport2);
479 ASSERT_EQ(displayId2, viewport2->displayId);
480 ASSERT_EQ(type, viewport2->type);
481
482 // When there are multiple viewports of the same kind, and uniqueId is not specified
483 // in the call to getDisplayViewport, then that situation is not supported.
484 // The viewports can be stored in any order, so we cannot rely on the order, since that
485 // is just implementation detail.
486 // However, we can check that it still returns *a* viewport, we just cannot assert
487 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700488 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700489 ASSERT_TRUE(someViewport);
490 }
491}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700493/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000494 * When we have multiple internal displays make sure we always return the default display when
495 * querying by type.
496 */
497TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
498 const std::string uniqueId1 = "uniqueId1";
499 const std::string uniqueId2 = "uniqueId2";
500 constexpr int32_t nonDefaultDisplayId = 2;
501 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
502 "Test display ID should not be ADISPLAY_ID_DEFAULT");
503
504 // Add the default display first and ensure it gets returned.
505 mFakePolicy->clearViewports();
506 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000507 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000508 ViewportType::INTERNAL);
509 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000510 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000511 ViewportType::INTERNAL);
512
513 std::optional<DisplayViewport> viewport =
514 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
515 ASSERT_TRUE(viewport);
516 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
517 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
518
519 // Add the default display second to make sure order doesn't matter.
520 mFakePolicy->clearViewports();
521 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000522 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000523 ViewportType::INTERNAL);
524 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000525 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000526 ViewportType::INTERNAL);
527
528 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
529 ASSERT_TRUE(viewport);
530 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
531 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
532}
533
534/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700535 * Check getDisplayViewportByPort
536 */
537TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100538 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700539 const std::string uniqueId1 = "uniqueId1";
540 const std::string uniqueId2 = "uniqueId2";
541 constexpr int32_t displayId1 = 1;
542 constexpr int32_t displayId2 = 2;
543 const uint8_t hdmi1 = 0;
544 const uint8_t hdmi2 = 1;
545 const uint8_t hdmi3 = 2;
546
547 mFakePolicy->clearViewports();
548 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000549 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000550 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700551 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000552 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000553 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700554
555 // Check that correct display viewport was returned by comparing the display ports.
556 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
557 ASSERT_TRUE(hdmi1Viewport);
558 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
559 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
560
561 // Check that we can still get the same viewport using the uniqueId
562 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
563 ASSERT_TRUE(hdmi1Viewport);
564 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
565 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
566 ASSERT_EQ(type, hdmi1Viewport->type);
567
568 // Check that we cannot find a port with "HDMI2", because we never added one
569 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
570 ASSERT_FALSE(hdmi2Viewport);
571}
572
Michael Wrightd02c5b62014-02-10 15:10:22 -0800573// --- InputReaderTest ---
574
575class InputReaderTest : public testing::Test {
576protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700577 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700579 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000580 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581
Chris Yea52ade12020-08-27 16:49:20 -0700582 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700583 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700584 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700585 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586
Prabir Pradhan28efc192019-11-05 01:10:04 +0000587 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700588 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589 }
590
Chris Yea52ade12020-08-27 16:49:20 -0700591 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700592 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594 }
595
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700596 void addDevice(int32_t eventHubId, const std::string& name,
597 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800598 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599
600 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800601 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 }
603 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000604 mReader->loopOnce();
605 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700606 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700608 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 }
610
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800611 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700612 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000613 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700614 }
615
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800616 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700617 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000618 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700619 }
620
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700622 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700623 ftl::Flags<InputDeviceClass> classes,
624 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800625 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800626 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000627 FakeInputMapper& mapper =
628 device->addMapper<FakeInputMapper>(eventHubId,
629 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800630 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800631 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 return mapper;
633 }
634};
635
Chris Ye98d3f532020-10-01 21:48:59 -0700636TEST_F(InputReaderTest, PolicyGetInputDevices) {
637 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700638 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700639 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640
641 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700642 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800644 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100645 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800646 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
647 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000648 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649}
650
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000651TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
652 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
653 mFakeEventHub->setSysfsRootPath(1, "xyz");
654
655 // Should also have received a notification describing the new input device.
656 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
657 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
658 ASSERT_EQ(0U, inputDevice.getLights().size());
659
660 RawLightInfo infoMonolight = {.id = 123,
661 .name = "mono_keyboard_backlight",
662 .maxBrightness = 255,
663 .flags = InputLightClass::BRIGHTNESS,
664 .path = ""};
665 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
666 mReader->sysfsNodeChanged("xyz");
667 mReader->loopOnce();
668
669 // Should also have received a notification describing the new recreated input device.
670 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
671 inputDevice = mFakePolicy->getInputDevices()[0];
672 ASSERT_EQ(1U, inputDevice.getLights().size());
673}
674
Chris Yee7310032020-09-22 15:36:28 -0700675TEST_F(InputReaderTest, GetMergedInputDevices) {
676 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
677 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
678 // Add two subdevices to device
679 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
680 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000681 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
682 AINPUT_SOURCE_KEYBOARD);
683 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
684 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700685
686 // Push same device instance for next device to be added, so they'll have same identifier.
687 mReader->pushNextDevice(device);
688 mReader->pushNextDevice(device);
689 ASSERT_NO_FATAL_FAILURE(
690 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
691 ASSERT_NO_FATAL_FAILURE(
692 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
693
694 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000695 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700696}
697
Chris Yee14523a2020-12-19 13:46:00 -0800698TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
700 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
701 // Add two subdevices to device
702 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
703 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000704 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
705 AINPUT_SOURCE_KEYBOARD);
706 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
707 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800708
709 // Push same device instance for next device to be added, so they'll have same identifier.
710 mReader->pushNextDevice(device);
711 mReader->pushNextDevice(device);
712 // Sensor device is initially disabled
713 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
714 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
715 nullptr));
716 // Device is disabled because the only sub device is a sensor device and disabled initially.
717 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
718 ASSERT_FALSE(device->isEnabled());
719 ASSERT_NO_FATAL_FAILURE(
720 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
721 // The merged device is enabled if any sub device is enabled
722 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
723 ASSERT_TRUE(device->isEnabled());
724}
725
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700726TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800727 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700728 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800729 constexpr int32_t eventHubId = 1;
730 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700731 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000732 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
733 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800734 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800735 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700736
Yi Kong9b14ac62018-07-17 13:48:38 -0700737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700738
739 NotifyDeviceResetArgs resetArgs;
740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700741 ASSERT_EQ(deviceId, resetArgs.deviceId);
742
743 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800744 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000745 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700746
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700748 ASSERT_EQ(deviceId, resetArgs.deviceId);
749 ASSERT_EQ(device->isEnabled(), false);
750
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800751 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000752 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700755 ASSERT_EQ(device->isEnabled(), false);
756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800757 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000758 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700760 ASSERT_EQ(deviceId, resetArgs.deviceId);
761 ASSERT_EQ(device->isEnabled(), true);
762}
763
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800765 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700766 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800767 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800768 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800769 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800770 AINPUT_SOURCE_KEYBOARD, nullptr);
771 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800772
773 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
774 AINPUT_SOURCE_ANY, AKEYCODE_A))
775 << "Should return unknown when the device id is >= 0 but unknown.";
776
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800777 ASSERT_EQ(AKEY_STATE_UNKNOWN,
778 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
779 << "Should return unknown when the device id is valid but the sources are not "
780 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800781
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800782 ASSERT_EQ(AKEY_STATE_DOWN,
783 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
784 AKEYCODE_A))
785 << "Should return value provided by mapper when device id is valid and the device "
786 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787
788 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
789 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
790 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
791
792 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
793 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
794 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
795}
796
Philip Junker4af3b3d2021-12-14 10:36:55 +0100797TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
798 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
799 constexpr int32_t eventHubId = 1;
800 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
801 InputDeviceClass::KEYBOARD,
802 AINPUT_SOURCE_KEYBOARD, nullptr);
803 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
804
805 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
806 << "Should return unknown when the device with the specified id is not found.";
807
808 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
809 << "Should return correct mapping when device id is valid and mapping exists.";
810
811 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
812 << "Should return the location key code when device id is valid and there's no "
813 "mapping.";
814}
815
816TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
817 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
818 constexpr int32_t eventHubId = 1;
819 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
820 InputDeviceClass::JOYSTICK,
821 AINPUT_SOURCE_GAMEPAD, nullptr);
822 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
823
824 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
825 << "Should return unknown when the device id is valid but there is no keyboard mapper";
826}
827
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800829 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700830 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800831 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800832 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800833 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800834 AINPUT_SOURCE_KEYBOARD, nullptr);
835 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836
837 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
838 AINPUT_SOURCE_ANY, KEY_A))
839 << "Should return unknown when the device id is >= 0 but unknown.";
840
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800841 ASSERT_EQ(AKEY_STATE_UNKNOWN,
842 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
843 << "Should return unknown when the device id is valid but the sources are not "
844 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800846 ASSERT_EQ(AKEY_STATE_DOWN,
847 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
848 KEY_A))
849 << "Should return value provided by mapper when device id is valid and the device "
850 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851
852 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
853 AINPUT_SOURCE_TRACKBALL, KEY_A))
854 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
855
856 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
857 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
858 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
859}
860
861TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800862 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700863 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800865 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800866 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800867 AINPUT_SOURCE_KEYBOARD, nullptr);
868 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869
870 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
871 AINPUT_SOURCE_ANY, SW_LID))
872 << "Should return unknown when the device id is >= 0 but unknown.";
873
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800874 ASSERT_EQ(AKEY_STATE_UNKNOWN,
875 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
876 << "Should return unknown when the device id is valid but the sources are not "
877 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800879 ASSERT_EQ(AKEY_STATE_DOWN,
880 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
881 SW_LID))
882 << "Should return value provided by mapper when device id is valid and the device "
883 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800884
885 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
886 AINPUT_SOURCE_TRACKBALL, SW_LID))
887 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
888
889 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
890 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
891 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
892}
893
894TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800895 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700896 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800897 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800898 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800899 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800900 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100901
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800902 mapper.addSupportedKeyCode(AKEYCODE_A);
903 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700905 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800906 uint8_t flags[4] = { 0, 0, 0, 1 };
907
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700908 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 << "Should return false when device id is >= 0 but unknown.";
910 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
911
912 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700913 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800914 << "Should return false when device id is valid but the sources are not supported by "
915 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
917
918 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700919 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800920 keyCodes, flags))
921 << "Should return value provided by mapper when device id is valid and the device "
922 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
924
925 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700926 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
927 << "Should return false when the device id is < 0 but the sources are not supported by "
928 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
930
931 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700932 ASSERT_TRUE(
933 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
934 << "Should return value provided by mapper when device id is < 0 and one of the "
935 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800936 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
937}
938
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000939TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800940 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700941 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942
943 NotifyConfigurationChangedArgs args;
944
945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
946 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
947}
948
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000949TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800950 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700951 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000952 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800953 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000954 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800955 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800956 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800957 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000959 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000960 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
962
963 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800964 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000965 ASSERT_EQ(when, event.when);
966 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800967 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 ASSERT_EQ(EV_KEY, event.type);
969 ASSERT_EQ(KEY_A, event.code);
970 ASSERT_EQ(1, event.value);
971}
972
Garfield Tan1c7bc862020-01-28 13:24:04 -0800973TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800974 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700975 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800976 constexpr int32_t eventHubId = 1;
977 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800978 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000979 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
980 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800981 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800982 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800983
984 NotifyDeviceResetArgs resetArgs;
985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800986 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800987
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800988 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000989 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800991 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800992 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800994 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000995 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800997 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800998 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001000 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001001 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001003 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001004 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001005}
1006
Garfield Tan1c7bc862020-01-28 13:24:04 -08001007TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1008 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001009 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001010 constexpr int32_t eventHubId = 1;
1011 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1012 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001013 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1014 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001015 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001016 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1017
1018 NotifyDeviceResetArgs resetArgs;
1019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1020 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1021}
1022
Arthur Hungc23540e2018-11-29 20:42:11 +08001023TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001024 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001025 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001026 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001027 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001028 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1029 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001030 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1031 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001032 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001033
1034 const uint8_t hdmi1 = 1;
1035
1036 // Associated touch screen with second display.
1037 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1038
1039 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001040 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001041 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001042 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001043 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001044 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001045 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001046 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001047 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001048
1049 // Add the device, and make sure all of the callbacks are triggered.
1050 // The device is added after the input port associations are processed since
1051 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001052 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001055 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001056
Arthur Hung2c9a3342019-07-23 14:18:59 +08001057 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001058 ASSERT_EQ(deviceId, device->getId());
1059 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1060 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001061
1062 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001063 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001064 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001065 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001066}
1067
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001068TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1069 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001070 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001071 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1072 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1073 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001074 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1075 AINPUT_SOURCE_KEYBOARD);
1076 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1077 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001078 mReader->pushNextDevice(device);
1079 mReader->pushNextDevice(device);
1080 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1081 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1082
1083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1084
1085 NotifyDeviceResetArgs resetArgs;
1086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1087 ASSERT_EQ(deviceId, resetArgs.deviceId);
1088 ASSERT_TRUE(device->isEnabled());
1089 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1090 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1091
1092 disableDevice(deviceId);
1093 mReader->loopOnce();
1094
1095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1096 ASSERT_EQ(deviceId, resetArgs.deviceId);
1097 ASSERT_FALSE(device->isEnabled());
1098 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1099 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1100
1101 enableDevice(deviceId);
1102 mReader->loopOnce();
1103
1104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1105 ASSERT_EQ(deviceId, resetArgs.deviceId);
1106 ASSERT_TRUE(device->isEnabled());
1107 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1108 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1109}
1110
1111TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1112 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001113 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001114 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1115 // Add two subdevices to device
1116 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1117 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001118 device->addMapper<FakeInputMapper>(eventHubIds[0],
1119 mFakePolicy->getReaderConfiguration(),
1120 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001121 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001122 device->addMapper<FakeInputMapper>(eventHubIds[1],
1123 mFakePolicy->getReaderConfiguration(),
1124 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001125 mReader->pushNextDevice(device);
1126 mReader->pushNextDevice(device);
1127 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1128 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1129
1130 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1131 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1132
1133 ASSERT_EQ(AKEY_STATE_DOWN,
1134 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1135 ASSERT_EQ(AKEY_STATE_DOWN,
1136 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1137 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1138 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1139}
1140
Prabir Pradhan7e186182020-11-10 13:56:45 -08001141TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1142 NotifyPointerCaptureChangedArgs args;
1143
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001144 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001145 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001146 mReader->loopOnce();
1147 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001148 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1149 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001150
1151 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001152 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001153 mReader->loopOnce();
1154 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001155 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001156
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001157 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001158 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001159 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001160 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001161 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001162}
1163
Chris Ye87143712020-11-10 05:05:58 +00001164class FakeVibratorInputMapper : public FakeInputMapper {
1165public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001166 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1167 const InputReaderConfiguration& readerConfig, uint32_t sources)
1168 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001169
1170 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1171};
1172
1173TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1174 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001175 ftl::Flags<InputDeviceClass> deviceClass =
1176 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001177 constexpr int32_t eventHubId = 1;
1178 const char* DEVICE_LOCATION = "BLUETOOTH";
1179 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1180 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001181 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1182 mFakePolicy->getReaderConfiguration(),
1183 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001184 mReader->pushNextDevice(device);
1185
1186 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1187 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1188
1189 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1190 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1191}
1192
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001193// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001194
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001195class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001196public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001197 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001198
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001199 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001200
Andy Chenf9f1a022022-08-29 20:07:10 -04001201 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1202
Chris Yee2b1e5c2021-03-10 22:45:12 -08001203 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1204
1205 void dump(std::string& dump) override {}
1206
1207 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1208 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001209 }
1210
Chris Yee2b1e5c2021-03-10 22:45:12 -08001211 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1212 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001213 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001214
1215 bool setLightColor(int32_t lightId, int32_t color) override {
1216 getDeviceContext().setLightBrightness(lightId, color >> 24);
1217 return true;
1218 }
1219
1220 std::optional<int32_t> getLightColor(int32_t lightId) override {
1221 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1222 if (!result.has_value()) {
1223 return std::nullopt;
1224 }
1225 return result.value() << 24;
1226 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001227
1228 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1229
1230 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1231
1232private:
1233 InputDeviceContext& mDeviceContext;
1234 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1235 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001236 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001237};
1238
Chris Yee2b1e5c2021-03-10 22:45:12 -08001239TEST_F(InputReaderTest, BatteryGetCapacity) {
1240 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001241 ftl::Flags<InputDeviceClass> deviceClass =
1242 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001243 constexpr int32_t eventHubId = 1;
1244 const char* DEVICE_LOCATION = "BLUETOOTH";
1245 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001246 FakePeripheralController& controller =
1247 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001248 mReader->pushNextDevice(device);
1249
1250 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1251
Harry Cuttsa5b71292022-11-28 12:56:17 +00001252 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1253 FakeEventHub::BATTERY_CAPACITY);
1254 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001255}
1256
1257TEST_F(InputReaderTest, BatteryGetStatus) {
1258 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001259 ftl::Flags<InputDeviceClass> deviceClass =
1260 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001261 constexpr int32_t eventHubId = 1;
1262 const char* DEVICE_LOCATION = "BLUETOOTH";
1263 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001264 FakePeripheralController& controller =
1265 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001266 mReader->pushNextDevice(device);
1267
1268 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1269
Harry Cuttsa5b71292022-11-28 12:56:17 +00001270 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1271 FakeEventHub::BATTERY_STATUS);
1272 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001273}
1274
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001275TEST_F(InputReaderTest, BatteryGetDevicePath) {
1276 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1277 ftl::Flags<InputDeviceClass> deviceClass =
1278 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1279 constexpr int32_t eventHubId = 1;
1280 const char* DEVICE_LOCATION = "BLUETOOTH";
1281 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1282 device->addController<FakePeripheralController>(eventHubId);
1283 mReader->pushNextDevice(device);
1284
1285 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1286
Harry Cuttsa5b71292022-11-28 12:56:17 +00001287 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001288}
1289
Chris Ye3fdbfef2021-01-06 18:45:18 -08001290TEST_F(InputReaderTest, LightGetColor) {
1291 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001292 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001293 constexpr int32_t eventHubId = 1;
1294 const char* DEVICE_LOCATION = "BLUETOOTH";
1295 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001296 FakePeripheralController& controller =
1297 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001298 mReader->pushNextDevice(device);
1299 RawLightInfo info = {.id = 1,
1300 .name = "Mono",
1301 .maxBrightness = 255,
1302 .flags = InputLightClass::BRIGHTNESS,
1303 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001304 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1305 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001306
1307 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001308
Harry Cutts33476232023-01-30 19:57:29 +00001309 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1310 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1311 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1312 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001313}
1314
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001315// --- InputReaderIntegrationTest ---
1316
1317// These tests create and interact with the InputReader only through its interface.
1318// The InputReader is started during SetUp(), which starts its processing in its own
1319// thread. The tests use linux uinput to emulate input devices.
1320// NOTE: Interacting with the physical device while these tests are running may cause
1321// the tests to fail.
1322class InputReaderIntegrationTest : public testing::Test {
1323protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001324 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001325 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001326 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001327
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001328 std::shared_ptr<FakePointerController> mFakePointerController;
1329
Chris Yea52ade12020-08-27 16:49:20 -07001330 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001331#if !defined(__ANDROID__)
1332 GTEST_SKIP();
1333#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001334 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001335 mFakePointerController = std::make_shared<FakePointerController>();
1336 mFakePolicy->setPointerController(mFakePointerController);
Harry Cutts33476232023-01-30 19:57:29 +00001337 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1338 /*eventDidNotHappenTimeout=*/30ms);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001339
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001340 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1341 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001342 ASSERT_EQ(mReader->start(), OK);
1343
1344 // Since this test is run on a real device, all the input devices connected
1345 // to the test device will show up in mReader. We wait for those input devices to
1346 // show up before beginning the tests.
1347 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +00001348 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1350 }
1351
Chris Yea52ade12020-08-27 16:49:20 -07001352 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001353#if !defined(__ANDROID__)
1354 return;
1355#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001356 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001357 mReader.reset();
1358 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001359 mFakePolicy.clear();
1360 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001361
1362 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1363 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1364 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1365 [&name](const InputDeviceInfo& info) {
1366 return info.getIdentifier().name == name;
1367 });
1368 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1369 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001370};
1371
1372TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1373 // An invalid input device that is only used for this test.
1374 class InvalidUinputDevice : public UinputDevice {
1375 public:
Harry Cutts33476232023-01-30 19:57:29 +00001376 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001377
1378 private:
1379 void configureDevice(int fd, uinput_user_dev* device) override {}
1380 };
1381
1382 const size_t numDevices = mFakePolicy->getInputDevices().size();
1383
1384 // UinputDevice does not set any event or key bits, so InputReader should not
1385 // consider it as a valid device.
1386 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1387 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1388 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1389 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1390
1391 invalidDevice.reset();
1392 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1394 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1395}
1396
1397TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1398 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1399
1400 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1401 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1402 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1403 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1404
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001405 const auto device = findDeviceByName(keyboard->getName());
1406 ASSERT_TRUE(device.has_value());
1407 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1408 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1409 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001410
1411 keyboard.reset();
1412 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1414 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1415}
1416
1417TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1418 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1419 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1420
1421 NotifyConfigurationChangedArgs configChangedArgs;
1422 ASSERT_NO_FATAL_FAILURE(
1423 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001424 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001425 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1426
1427 NotifyKeyArgs keyArgs;
1428 keyboard->pressAndReleaseHomeKey();
1429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1430 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001431 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001432 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001433 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001434 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001435 prevTimestamp = keyArgs.eventTime;
1436
1437 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1438 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001439 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001440 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001441 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001442}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001443
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001444TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1445 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1446 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1447
1448 const auto device = findDeviceByName(stylus->getName());
1449 ASSERT_TRUE(device.has_value());
1450
Prabir Pradhana3621852022-10-14 18:57:23 +00001451 // An external stylus with buttons should also be recognized as a keyboard.
1452 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001453 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1454 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1455
1456 const auto DOWN =
1457 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1458 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1459
1460 stylus->pressAndReleaseKey(BTN_STYLUS);
1461 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1462 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1463 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1464 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1465
1466 stylus->pressAndReleaseKey(BTN_STYLUS2);
1467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1468 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1469 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1470 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1471
1472 stylus->pressAndReleaseKey(BTN_STYLUS3);
1473 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1474 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1475 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1476 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1477}
1478
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001479/**
1480 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1481 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1482 * are passed to the listener.
1483 */
1484static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1485TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1486 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1487 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1488 NotifyKeyArgs keyArgs;
1489
1490 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1491 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1492 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1493 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1494
1495 controller->pressAndReleaseKey(BTN_GEAR_UP);
1496 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1497 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1498 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1499}
1500
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001501// --- TouchIntegrationTest ---
1502
Arthur Hungaab25622020-01-16 11:22:11 +08001503class TouchIntegrationTest : public InputReaderIntegrationTest {
1504protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001505 const std::string UNIQUE_ID = "local:0";
1506
Chris Yea52ade12020-08-27 16:49:20 -07001507 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001508#if !defined(__ANDROID__)
1509 GTEST_SKIP();
1510#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001511 InputReaderIntegrationTest::SetUp();
1512 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001513 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1514 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001515
1516 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1517 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1518 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001519 const auto info = findDeviceByName(mDevice->getName());
1520 ASSERT_TRUE(info);
1521 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001522 }
1523
1524 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001525 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001526 std::optional<uint8_t> physicalPort,
1527 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001528 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001529 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001530 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001531 }
1532
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001533 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1534 NotifyMotionArgs args;
1535 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1536 EXPECT_EQ(action, args.action);
1537 ASSERT_EQ(points.size(), args.pointerCount);
1538 for (size_t i = 0; i < args.pointerCount; i++) {
1539 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1540 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1541 }
1542 }
1543
Arthur Hungaab25622020-01-16 11:22:11 +08001544 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001545 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001546};
1547
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001548TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1549 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1550 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1551 // presses).
1552 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1553 mDeviceInfo.getSources());
1554}
1555
Arthur Hungaab25622020-01-16 11:22:11 +08001556TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1557 NotifyMotionArgs args;
1558 const Point centerPoint = mDevice->getCenterPoint();
1559
1560 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001561 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001562 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001563 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001564 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1565 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1566
1567 // ACTION_MOVE
1568 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001569 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001570 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1572
1573 // ACTION_UP
1574 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001575 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001576 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1577 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1578}
1579
1580TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1581 NotifyMotionArgs args;
1582 const Point centerPoint = mDevice->getCenterPoint();
1583
1584 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001585 mDevice->sendSlot(FIRST_SLOT);
1586 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001587 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001588 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001589 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1590 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1591
1592 // ACTION_POINTER_DOWN (Second slot)
1593 const Point secondPoint = centerPoint + Point(100, 100);
1594 mDevice->sendSlot(SECOND_SLOT);
1595 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001596 mDevice->sendDown(secondPoint);
1597 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001598 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001599 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001600
1601 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001602 mDevice->sendMove(secondPoint + Point(1, 1));
1603 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001604 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1606
1607 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001608 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001609 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001610 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001611 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001612
1613 // ACTION_UP
1614 mDevice->sendSlot(FIRST_SLOT);
1615 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001616 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001617 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1618 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1619}
1620
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001621/**
1622 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1623 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1624 * data?
1625 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1626 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1627 * for Pointer 0 only is generated after.
1628 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1629 * events, we will not miss any information.
1630 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1631 * event generated afterwards that contains the newest movement of pointer 0.
1632 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1633 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1634 * losing information about non-palm pointers.
1635 */
1636TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1637 NotifyMotionArgs args;
1638 const Point centerPoint = mDevice->getCenterPoint();
1639
1640 // ACTION_DOWN
1641 mDevice->sendSlot(FIRST_SLOT);
1642 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1643 mDevice->sendDown(centerPoint);
1644 mDevice->sendSync();
1645 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1646
1647 // ACTION_POINTER_DOWN (Second slot)
1648 const Point secondPoint = centerPoint + Point(100, 100);
1649 mDevice->sendSlot(SECOND_SLOT);
1650 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1651 mDevice->sendDown(secondPoint);
1652 mDevice->sendSync();
1653 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1654
1655 // ACTION_MOVE (First slot)
1656 mDevice->sendSlot(FIRST_SLOT);
1657 mDevice->sendMove(centerPoint + Point(5, 5));
1658 // ACTION_POINTER_UP (Second slot)
1659 mDevice->sendSlot(SECOND_SLOT);
1660 mDevice->sendPointerUp();
1661 // Send a single sync for the above 2 pointer updates
1662 mDevice->sendSync();
1663
1664 // First, we should get POINTER_UP for the second pointer
1665 assertReceivedMotion(ACTION_POINTER_1_UP,
1666 {/*first pointer */ centerPoint + Point(5, 5),
1667 /*second pointer*/ secondPoint});
1668
1669 // Next, the MOVE event for the first pointer
1670 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1671}
1672
1673/**
1674 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1675 * move, and then it will go up, all in the same frame.
1676 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1677 * gets sent to the listener.
1678 */
1679TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1680 NotifyMotionArgs args;
1681 const Point centerPoint = mDevice->getCenterPoint();
1682
1683 // ACTION_DOWN
1684 mDevice->sendSlot(FIRST_SLOT);
1685 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1686 mDevice->sendDown(centerPoint);
1687 mDevice->sendSync();
1688 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1689
1690 // ACTION_POINTER_DOWN (Second slot)
1691 const Point secondPoint = centerPoint + Point(100, 100);
1692 mDevice->sendSlot(SECOND_SLOT);
1693 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1694 mDevice->sendDown(secondPoint);
1695 mDevice->sendSync();
1696 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1697
1698 // ACTION_MOVE (First slot)
1699 mDevice->sendSlot(FIRST_SLOT);
1700 mDevice->sendMove(centerPoint + Point(5, 5));
1701 // ACTION_POINTER_UP (Second slot)
1702 mDevice->sendSlot(SECOND_SLOT);
1703 mDevice->sendMove(secondPoint + Point(6, 6));
1704 mDevice->sendPointerUp();
1705 // Send a single sync for the above 2 pointer updates
1706 mDevice->sendSync();
1707
1708 // First, we should get POINTER_UP for the second pointer
1709 // The movement of the second pointer during the liftoff frame is ignored.
1710 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1711 assertReceivedMotion(ACTION_POINTER_1_UP,
1712 {/*first pointer */ centerPoint + Point(5, 5),
1713 /*second pointer*/ secondPoint});
1714
1715 // Next, the MOVE event for the first pointer
1716 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1717}
1718
Arthur Hungaab25622020-01-16 11:22:11 +08001719TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1720 NotifyMotionArgs args;
1721 const Point centerPoint = mDevice->getCenterPoint();
1722
1723 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001724 mDevice->sendSlot(FIRST_SLOT);
1725 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001726 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001727 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001728 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1729 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1730
arthurhungcc7f9802020-04-30 17:55:40 +08001731 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001732 const Point secondPoint = centerPoint + Point(100, 100);
1733 mDevice->sendSlot(SECOND_SLOT);
1734 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1735 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001736 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001737 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001738 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001739
arthurhungcc7f9802020-04-30 17:55:40 +08001740 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001741 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001742 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001743 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1745
arthurhungcc7f9802020-04-30 17:55:40 +08001746 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1747 // a palm event.
1748 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001749 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001750 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001751 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001752 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001753 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001754
arthurhungcc7f9802020-04-30 17:55:40 +08001755 // Send up to second slot, expect first slot send moving.
1756 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001757 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001758 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001760
arthurhungcc7f9802020-04-30 17:55:40 +08001761 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001762 mDevice->sendSlot(FIRST_SLOT);
1763 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001764 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001765
arthurhungcc7f9802020-04-30 17:55:40 +08001766 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1767 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001768}
1769
Prabir Pradhanda20b172022-09-26 17:01:18 +00001770TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1771 const Point centerPoint = mDevice->getCenterPoint();
1772
1773 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1774 mDevice->sendSlot(FIRST_SLOT);
1775 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1776 mDevice->sendToolType(MT_TOOL_PEN);
1777 mDevice->sendDown(centerPoint);
1778 mDevice->sendSync();
1779 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1780 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001781 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001782
1783 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1784
1785 // Release the stylus touch.
1786 mDevice->sendUp();
1787 mDevice->sendSync();
1788 ASSERT_NO_FATAL_FAILURE(
1789 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1790
1791 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1792
1793 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1794 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1795 mDevice->sendToolType(MT_TOOL_FINGER);
1796 mDevice->sendDown(centerPoint);
1797 mDevice->sendSync();
1798 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1799 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001800 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001801
1802 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1803
1804 mDevice->sendUp();
1805 mDevice->sendSync();
1806 ASSERT_NO_FATAL_FAILURE(
1807 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1808
1809 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1810 // The policy should be notified of the stylus presence.
1811 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1812 mDevice->sendToolType(MT_TOOL_PEN);
1813 mDevice->sendMove(centerPoint);
1814 mDevice->sendSync();
1815 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1816 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001817 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001818
1819 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1820}
1821
Prabir Pradhan124ea442022-10-28 20:27:44 +00001822// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001823
Prabir Pradhan124ea442022-10-28 20:27:44 +00001824// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1825// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1826// stylus.
1827template <typename UinputStylusDevice>
1828class StylusButtonIntegrationTest : public TouchIntegrationTest {
1829protected:
1830 void SetUp() override {
1831#if !defined(__ANDROID__)
1832 GTEST_SKIP();
1833#endif
1834 TouchIntegrationTest::SetUp();
1835 mTouchscreen = mDevice.get();
1836 mTouchscreenInfo = mDeviceInfo;
1837
1838 setUpStylusDevice();
1839 }
1840
1841 UinputStylusDevice* mStylus{nullptr};
1842 InputDeviceInfo mStylusInfo{};
1843
1844 UinputTouchScreen* mTouchscreen{nullptr};
1845 InputDeviceInfo mTouchscreenInfo{};
1846
1847private:
1848 // When we are attempting to test stylus button events that are sent from the touchscreen,
1849 // use the same Uinput device for the touchscreen and the stylus.
1850 template <typename T = UinputStylusDevice>
1851 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1852 mStylus = mDevice.get();
1853 mStylusInfo = mDeviceInfo;
1854 }
1855
1856 // When we are attempting to stylus buttons from an external stylus being merged with touches
1857 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1858 template <typename T = UinputStylusDevice>
1859 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1860 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1861 mStylus = mStylusDeviceLifecycleTracker.get();
1862 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1863 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1864 const auto info = findDeviceByName(mStylus->getName());
1865 ASSERT_TRUE(info);
1866 mStylusInfo = *info;
1867 }
1868
1869 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1870
1871 // Hide the base class's device to expose it with a different name for readability.
1872 using TouchIntegrationTest::mDevice;
1873 using TouchIntegrationTest::mDeviceInfo;
1874};
1875
1876using StylusButtonIntegrationTestTypes =
1877 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1878TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1879
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001880TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001881 const auto stylusId = TestFixture::mStylusInfo.getId();
1882
1883 TestFixture::mStylus->pressKey(BTN_STYLUS);
1884 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1885 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1886 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1887
1888 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1889 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001890 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001891 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001892}
1893
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001894TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001895 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1896 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1897 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001898
1899 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001900 TestFixture::mStylus->pressKey(BTN_STYLUS);
1901 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001902 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001903 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001904
1905 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001906 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1907 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1908 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1909 TestFixture::mTouchscreen->sendDown(centerPoint);
1910 TestFixture::mTouchscreen->sendSync();
1911 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001912 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001913 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001914 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1915 WithDeviceId(touchscreenId))));
1916 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001917 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001918 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001919 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1920 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001921
Prabir Pradhan124ea442022-10-28 20:27:44 +00001922 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1923 TestFixture::mTouchscreen->sendSync();
1924 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001925 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001926 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001927 WithDeviceId(touchscreenId))));
1928 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001929 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001930 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001931 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001932
1933 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001934 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1935 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001936 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001937 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001938}
1939
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001940TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001941 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1942 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1943 const auto stylusId = TestFixture::mStylusInfo.getId();
1944 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001945 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001946
1947 // Press the stylus button.
1948 TestFixture::mStylus->pressKey(BTN_STYLUS);
1949 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1950 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1951 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1952
1953 // Start hovering with the stylus.
1954 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1955 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1956 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1957 TestFixture::mTouchscreen->sendMove(centerPoint);
1958 TestFixture::mTouchscreen->sendSync();
1959 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1960 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1961 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1962 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1963 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1964 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1965 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1966 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1967 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1968
1969 // Touch down with the stylus.
1970 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1971 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1972 TestFixture::mTouchscreen->sendDown(centerPoint);
1973 TestFixture::mTouchscreen->sendSync();
1974 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1975 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1976 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1977
1978 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1979 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1980 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1981
1982 // Stop touching with the stylus, and start hovering.
1983 TestFixture::mTouchscreen->sendUp();
1984 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1985 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1986 TestFixture::mTouchscreen->sendMove(centerPoint);
1987 TestFixture::mTouchscreen->sendSync();
1988 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1989 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
1990 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1991 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1992 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1993 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1994 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1995 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1996 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1997
1998 // Stop hovering.
1999 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2000 TestFixture::mTouchscreen->sendSync();
2001 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2002 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2003 WithButtonState(0))));
2004 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2005 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2006 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2007 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2008
2009 // Release the stylus button.
2010 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2011 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2012 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2013 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2014}
2015
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002016TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002017 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2018 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2019 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002020
2021 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002022 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2023 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2024 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2025 TestFixture::mTouchscreen->sendDown(centerPoint);
2026 TestFixture::mTouchscreen->sendSync();
2027 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002028 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002029 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002030 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002031
2032 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002033 TestFixture::mStylus->pressKey(BTN_STYLUS);
2034 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002035 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002036 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2037 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002038 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002039 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002040 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2041 WithDeviceId(touchscreenId))));
2042 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002043 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002044 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002045 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2046 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002047
Prabir Pradhan124ea442022-10-28 20:27:44 +00002048 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2049 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002050 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002051 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2052 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002053 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002054 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002055 WithDeviceId(touchscreenId))));
2056 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002057 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002058 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002059 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002060
2061 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002062 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2063 TestFixture::mTouchscreen->sendSync();
2064 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002065 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002066 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002067 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002068}
2069
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002070TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002071 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2072 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002073 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002074
2075 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2076 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2077 const auto stylusId = TestFixture::mStylusInfo.getId();
2078
2079 // Start a stylus gesture. By the time this event is processed, the configuration change that
2080 // was requested is guaranteed to be completed.
2081 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2082 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2083 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2084 TestFixture::mTouchscreen->sendDown(centerPoint);
2085 TestFixture::mTouchscreen->sendSync();
2086 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2087 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002088 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002089 WithDeviceId(touchscreenId))));
2090
2091 // Press and release a stylus button. Each change only generates a MOVE motion event.
2092 // Key events are unaffected.
2093 TestFixture::mStylus->pressKey(BTN_STYLUS);
2094 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2095 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2096 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2097 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2098 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002099 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002100 WithDeviceId(touchscreenId))));
2101
2102 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2103 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2104 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2105 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2106 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2107 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002108 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002109 WithDeviceId(touchscreenId))));
2110
2111 // Finish the stylus gesture.
2112 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2113 TestFixture::mTouchscreen->sendSync();
2114 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2115 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002116 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002117 WithDeviceId(touchscreenId))));
2118}
2119
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002120// --- ExternalStylusIntegrationTest ---
2121
2122// Verify the behavior of an external stylus. An external stylus can report pressure or button
2123// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2124// ongoing stylus gesture that is being emitted by the touchscreen.
2125using ExternalStylusIntegrationTest = TouchIntegrationTest;
2126
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002127TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002128 const Point centerPoint = mDevice->getCenterPoint();
2129
2130 // Create an external stylus capable of reporting pressure data that
2131 // should be fused with a touch pointer.
2132 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2133 createUinputDevice<UinputExternalStylusWithPressure>();
2134 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2135 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2136 const auto stylusInfo = findDeviceByName(stylus->getName());
2137 ASSERT_TRUE(stylusInfo);
2138
2139 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2140
2141 const auto touchscreenId = mDeviceInfo.getId();
2142
2143 // Set a pressure value on the stylus. It doesn't generate any events.
2144 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2145 stylus->setPressure(100);
2146 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2147
2148 // Start a finger gesture, and ensure it shows up as stylus gesture
2149 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002150 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002151 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002152 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002153 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002154 mDevice->sendSync();
2155 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2156 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002157 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002158 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002159
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002160 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2161 // event with the updated pressure.
2162 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002163 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2164 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002165 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002166 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002167
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002168 // The external stylus did not generate any events.
2169 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2170 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2171}
2172
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002173TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002174 const Point centerPoint = mDevice->getCenterPoint();
2175
2176 // Create an external stylus capable of reporting pressure data that
2177 // should be fused with a touch pointer.
2178 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2179 createUinputDevice<UinputExternalStylusWithPressure>();
2180 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2181 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2182 const auto stylusInfo = findDeviceByName(stylus->getName());
2183 ASSERT_TRUE(stylusInfo);
2184
2185 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2186
2187 const auto touchscreenId = mDeviceInfo.getId();
2188
2189 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2190 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002191 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2192 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002193 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002194 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002195
2196 // Start a finger gesture. The touch device will withhold generating any touches for
2197 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2198 mDevice->sendSlot(FIRST_SLOT);
2199 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2200 mDevice->sendToolType(MT_TOOL_FINGER);
2201 mDevice->sendDown(centerPoint);
2202 auto waitUntil = std::chrono::system_clock::now() +
2203 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002204 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002205 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002206
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002207 // Since the external stylus did not report a pressure value within the timeout,
2208 // it shows up as a finger pointer.
2209 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2210 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002211 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002212 WithPressure(1.f))));
2213
2214 // Change the pressure on the external stylus. Since the pressure was not present at the start
2215 // of the gesture, it is ignored for now.
2216 stylus->setPressure(200);
2217 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2218
2219 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002220 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2221 mDevice->sendSync();
2222 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2223 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002224 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002225
2226 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2227 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2228 mDevice->sendToolType(MT_TOOL_FINGER);
2229 mDevice->sendDown(centerPoint);
2230 mDevice->sendSync();
2231 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2232 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002233 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002234 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2235
2236 // The external stylus did not generate any events.
2237 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2238 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002239}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002240
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002241TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002242 const Point centerPoint = mDevice->getCenterPoint();
2243
2244 // Create an external stylus device that does not support pressure. It should not affect any
2245 // touch pointers.
2246 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2247 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2248 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2249 const auto stylusInfo = findDeviceByName(stylus->getName());
2250 ASSERT_TRUE(stylusInfo);
2251
2252 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2253
2254 const auto touchscreenId = mDeviceInfo.getId();
2255
2256 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2257 // pressure data from the external stylus.
2258 mDevice->sendSlot(FIRST_SLOT);
2259 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2260 mDevice->sendToolType(MT_TOOL_FINGER);
2261 mDevice->sendDown(centerPoint);
2262 auto waitUntil = std::chrono::system_clock::now() +
2263 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2264 mDevice->sendSync();
2265 ASSERT_NO_FATAL_FAILURE(
2266 mTestListener
2267 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2268 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002269 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002270 WithButtonState(0),
2271 WithDeviceId(touchscreenId),
2272 WithPressure(1.f)),
2273 waitUntil));
2274
2275 // The external stylus did not generate any events.
2276 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2277 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2278}
2279
Michael Wrightd02c5b62014-02-10 15:10:22 -08002280// --- InputDeviceTest ---
2281class InputDeviceTest : public testing::Test {
2282protected:
2283 static const char* DEVICE_NAME;
2284 static const char* DEVICE_LOCATION;
2285 static const int32_t DEVICE_ID;
2286 static const int32_t DEVICE_GENERATION;
2287 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002288 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002289 static const int32_t EVENTHUB_ID;
2290 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2291
2292 std::shared_ptr<FakeEventHub> mFakeEventHub;
2293 sp<FakeInputReaderPolicy> mFakePolicy;
2294 std::unique_ptr<TestInputListener> mFakeListener;
2295 std::unique_ptr<InstrumentedInputReader> mReader;
2296 std::shared_ptr<InputDevice> mDevice;
2297
2298 void SetUp() override {
2299 mFakeEventHub = std::make_unique<FakeEventHub>();
2300 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2301 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002302 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002303 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002304 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002305 identifier.name = DEVICE_NAME;
2306 identifier.location = DEVICE_LOCATION;
2307 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2308 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2309 identifier);
2310 mReader->pushNextDevice(mDevice);
2311 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002312 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002313 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002314
2315 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002316 mFakeListener.reset();
2317 mFakePolicy.clear();
2318 }
2319};
2320
2321const char* InputDeviceTest::DEVICE_NAME = "device";
2322const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2323const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2324const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002325const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002326const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2327 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002328const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002329const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2330
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002331TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002332 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002333 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2334 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002335}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002336
Michael Wrightd02c5b62014-02-10 15:10:22 -08002337TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2338 ASSERT_EQ(mDevice->isEnabled(), false);
2339}
2340
2341TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2342 // Configuration.
2343 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002344 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002345
2346 // Reset.
2347 unused += mDevice->reset(ARBITRARY_TIME);
2348
2349 NotifyDeviceResetArgs resetArgs;
2350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2351 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2352 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2353
2354 // Metadata.
2355 ASSERT_TRUE(mDevice->isIgnored());
2356 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2357
2358 InputDeviceInfo info = mDevice->getDeviceInfo();
2359 ASSERT_EQ(DEVICE_ID, info.getId());
2360 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2361 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2362 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2363
2364 // State queries.
2365 ASSERT_EQ(0, mDevice->getMetaState());
2366
2367 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2368 << "Ignored device should return unknown key code state.";
2369 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2370 << "Ignored device should return unknown scan code state.";
2371 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2372 << "Ignored device should return unknown switch state.";
2373
2374 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2375 uint8_t flags[2] = { 0, 1 };
2376 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2377 << "Ignored device should never mark any key codes.";
2378 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2379 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2380}
2381
2382TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2383 // Configuration.
2384 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2385
2386 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002387 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2388 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2390 mapper1.setMetaState(AMETA_ALT_ON);
2391 mapper1.addSupportedKeyCode(AKEYCODE_A);
2392 mapper1.addSupportedKeyCode(AKEYCODE_B);
2393 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2394 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2395 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2396 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2397 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2398
2399 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002400 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2401 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402 mapper2.setMetaState(AMETA_SHIFT_ON);
2403
2404 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002405 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002406
Harry Cuttsf13161a2023-03-08 14:15:49 +00002407 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2408 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002410 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002411
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002412 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2413 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414
2415 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002416 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002417 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2418 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419
2420 NotifyDeviceResetArgs resetArgs;
2421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2422 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2423 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2424
2425 // Metadata.
2426 ASSERT_FALSE(mDevice->isIgnored());
2427 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2428
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002429 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002431 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2433 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2434
2435 // State queries.
2436 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2437 << "Should query mappers and combine meta states.";
2438
2439 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2440 << "Should return unknown key code state when source not supported.";
2441 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2442 << "Should return unknown scan code state when source not supported.";
2443 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2444 << "Should return unknown switch state when source not supported.";
2445
2446 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2447 << "Should query mapper when source is supported.";
2448 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2449 << "Should query mapper when source is supported.";
2450 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2451 << "Should query mapper when source is supported.";
2452
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002453 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002455 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456 << "Should do nothing when source is unsupported.";
2457 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2458 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2459 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2460 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2461
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002462 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463 << "Should query mapper when source is supported.";
2464 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2465 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2466 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2467 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2468
2469 // Event handling.
2470 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002471 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002472 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002474 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2475 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002476}
2477
Arthur Hung2c9a3342019-07-23 14:18:59 +08002478// A single input device is associated with a specific display. Check that:
2479// 1. Device is disabled if the viewport corresponding to the associated display is not found
2480// 2. Device is disabled when setEnabled API is called
2481TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002482 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2483 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002484
2485 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002486 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002487 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2488 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002489
2490 // Device should be enabled by default.
2491 ASSERT_TRUE(mDevice->isEnabled());
2492
2493 // Prepare associated info.
2494 constexpr uint8_t hdmi = 1;
2495 const std::string UNIQUE_ID = "local:1";
2496
2497 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002498 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002499 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002500 // Device should be disabled because it is associated with a specific display via
2501 // input port <-> display port association, but the corresponding display is not found
2502 ASSERT_FALSE(mDevice->isEnabled());
2503
2504 // Prepare displays.
2505 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002506 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002507 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002508 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002509 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002510 ASSERT_TRUE(mDevice->isEnabled());
2511
2512 // Device should be disabled after set disable.
2513 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002514 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002515 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002516 ASSERT_FALSE(mDevice->isEnabled());
2517
2518 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002519 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002520 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002521 ASSERT_FALSE(mDevice->isEnabled());
2522}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002523
Christine Franks1ba71cc2021-04-07 14:37:42 -07002524TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2525 // Device should be enabled by default.
2526 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002527 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2528 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002529 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002530 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2531 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002532 ASSERT_TRUE(mDevice->isEnabled());
2533
2534 // Device should be disabled because it is associated with a specific display, but the
2535 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002536 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002537 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002538 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002539 ASSERT_FALSE(mDevice->isEnabled());
2540
2541 // Device should be enabled when a display is found.
2542 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002543 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002544 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002545 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002546 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002547 ASSERT_TRUE(mDevice->isEnabled());
2548
2549 // Device should be disabled after set disable.
2550 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002551 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002552 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002553 ASSERT_FALSE(mDevice->isEnabled());
2554
2555 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002556 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002557 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002558 ASSERT_FALSE(mDevice->isEnabled());
2559}
2560
Christine Franks2a2293c2022-01-18 11:51:16 -08002561TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2562 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002563 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2564 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002565 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002566 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2567 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002568
Christine Franks2a2293c2022-01-18 11:51:16 -08002569 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2570 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002571 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002572 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002573 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002574 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002575 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2576}
2577
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002578/**
2579 * This test reproduces a crash caused by a dangling reference that remains after device is added
2580 * and removed. The reference is accessed in InputDevice::dump(..);
2581 */
2582TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2583 constexpr int32_t TEST_EVENTHUB_ID = 10;
2584 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2585
Harry Cutts33476232023-01-30 19:57:29 +00002586 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh7f4dd512023-05-18 13:22:12 +00002587 device.addEmptyEventHubDevice(TEST_EVENTHUB_ID);
2588 auto unused = device.configure(systemTime(SYSTEM_TIME_MONOTONIC),
2589 mFakePolicy->getReaderConfiguration(), /*changes=*/{});
2590 device.populateMappers(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002591 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2592 std::string dumpStr, eventHubDevStr;
2593 device.dump(dumpStr, eventHubDevStr);
2594}
2595
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002596TEST_F(InputDeviceTest, GetBluetoothAddress) {
2597 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2598 ASSERT_TRUE(address);
2599 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2600}
2601
Michael Wrightd02c5b62014-02-10 15:10:22 -08002602// --- SwitchInputMapperTest ---
2603
2604class SwitchInputMapperTest : public InputMapperTest {
2605protected:
2606};
2607
2608TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002609 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002610
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002611 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002612}
2613
2614TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002615 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002617 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002618 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002619
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002620 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002621 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002622}
2623
2624TEST_F(SwitchInputMapperTest, Process) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002625 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002626 std::list<NotifyArgs> out;
2627 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2628 ASSERT_TRUE(out.empty());
2629 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2630 ASSERT_TRUE(out.empty());
2631 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2632 ASSERT_TRUE(out.empty());
2633 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002634
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002635 ASSERT_EQ(1u, out.size());
2636 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002638 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2639 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640 args.switchMask);
2641 ASSERT_EQ(uint32_t(0), args.policyFlags);
2642}
2643
Chris Ye87143712020-11-10 05:05:58 +00002644// --- VibratorInputMapperTest ---
2645class VibratorInputMapperTest : public InputMapperTest {
2646protected:
2647 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2648};
2649
2650TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singhc68f85b2023-04-26 16:23:13 +00002651 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002652
2653 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2654}
2655
2656TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singhc68f85b2023-04-26 16:23:13 +00002657 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002658
2659 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2660}
2661
2662TEST_F(VibratorInputMapperTest, Vibrate) {
2663 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002664 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singhc68f85b2023-04-26 16:23:13 +00002665 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002666
2667 VibrationElement pattern(2);
2668 VibrationSequence sequence(2);
2669 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002670 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2671 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002672 sequence.addElement(pattern);
2673 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002674 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2675 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002676 sequence.addElement(pattern);
2677
2678 std::vector<int64_t> timings = {0, 1};
2679 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2680
2681 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002682 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002683 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002684 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002685 // Verify vibrator state listener was notified.
2686 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002687 ASSERT_EQ(1u, out.size());
2688 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2689 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2690 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002691 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002692 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002693 ASSERT_FALSE(mapper.isVibrating());
2694 // Verify vibrator state listener was notified.
2695 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002696 ASSERT_EQ(1u, out.size());
2697 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2698 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2699 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002700}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002701
Chris Yef59a2f42020-10-16 12:55:26 -07002702// --- SensorInputMapperTest ---
2703
2704class SensorInputMapperTest : public InputMapperTest {
2705protected:
2706 static const int32_t ACCEL_RAW_MIN;
2707 static const int32_t ACCEL_RAW_MAX;
2708 static const int32_t ACCEL_RAW_FUZZ;
2709 static const int32_t ACCEL_RAW_FLAT;
2710 static const int32_t ACCEL_RAW_RESOLUTION;
2711
2712 static const int32_t GYRO_RAW_MIN;
2713 static const int32_t GYRO_RAW_MAX;
2714 static const int32_t GYRO_RAW_FUZZ;
2715 static const int32_t GYRO_RAW_FLAT;
2716 static const int32_t GYRO_RAW_RESOLUTION;
2717
2718 static const float GRAVITY_MS2_UNIT;
2719 static const float DEGREE_RADIAN_UNIT;
2720
2721 void prepareAccelAxes();
2722 void prepareGyroAxes();
2723 void setAccelProperties();
2724 void setGyroProperties();
2725 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2726};
2727
2728const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2729const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2730const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2731const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2732const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2733
2734const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2735const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2736const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2737const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2738const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2739
2740const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2741const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2742
2743void SensorInputMapperTest::prepareAccelAxes() {
2744 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2745 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2746 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2747 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2748 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2749 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2750}
2751
2752void SensorInputMapperTest::prepareGyroAxes() {
2753 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2754 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2755 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2756 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2757 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2758 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2759}
2760
2761void SensorInputMapperTest::setAccelProperties() {
2762 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2763 /* sensorDataIndex */ 0);
2764 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2765 /* sensorDataIndex */ 1);
2766 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2767 /* sensorDataIndex */ 2);
2768 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2769 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2770 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2771 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2772 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2773}
2774
2775void SensorInputMapperTest::setGyroProperties() {
2776 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2777 /* sensorDataIndex */ 0);
2778 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2779 /* sensorDataIndex */ 1);
2780 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2781 /* sensorDataIndex */ 2);
2782 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2783 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2784 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2785 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2786 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2787}
2788
2789TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singh61dd33e2023-04-26 15:07:55 +00002790 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002791
2792 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2793}
2794
2795TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2796 setAccelProperties();
2797 prepareAccelAxes();
Arpit Singh61dd33e2023-04-26 15:07:55 +00002798 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002799
2800 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2801 std::chrono::microseconds(10000),
2802 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002803 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2805 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002809
2810 NotifySensorArgs args;
2811 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2812 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2813 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2814
2815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2816 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2817 ASSERT_EQ(args.deviceId, DEVICE_ID);
2818 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2819 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2820 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2821 ASSERT_EQ(args.values, values);
2822 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2823}
2824
2825TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2826 setGyroProperties();
2827 prepareGyroAxes();
Arpit Singh61dd33e2023-04-26 15:07:55 +00002828 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002829
2830 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2831 std::chrono::microseconds(10000),
2832 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002833 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002839
2840 NotifySensorArgs args;
2841 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2842 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2843 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2844
2845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2846 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2847 ASSERT_EQ(args.deviceId, DEVICE_ID);
2848 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2849 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2850 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2851 ASSERT_EQ(args.values, values);
2852 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2853}
2854
Michael Wrightd02c5b62014-02-10 15:10:22 -08002855// --- KeyboardInputMapperTest ---
2856
2857class KeyboardInputMapperTest : public InputMapperTest {
2858protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002859 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002860 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002861 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002862
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002863 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002864 int32_t originalKeyCode, int32_t rotatedKeyCode,
2865 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002866};
2867
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002868/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2869 * orientation.
2870 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002871void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002872 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2873 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002874}
2875
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002876void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002877 int32_t originalScanCode, int32_t originalKeyCode,
2878 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879 NotifyKeyArgs args;
2880
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2883 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2884 ASSERT_EQ(originalScanCode, args.scanCode);
2885 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002886 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002887
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2890 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2891 ASSERT_EQ(originalScanCode, args.scanCode);
2892 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002893 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002894}
2895
Michael Wrightd02c5b62014-02-10 15:10:22 -08002896TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002897 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00002898 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002899 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002900
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002901 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002902}
2903
2904TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2905 const int32_t USAGE_A = 0x070004;
2906 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002907 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2908 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002909 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2910 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2911 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002912
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002913 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00002914 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002915 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002916 // Initial metastate is AMETA_NONE.
2917 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002918
2919 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002921 NotifyKeyArgs args;
2922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2923 ASSERT_EQ(DEVICE_ID, args.deviceId);
2924 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2925 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2926 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2927 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2928 ASSERT_EQ(KEY_HOME, args.scanCode);
2929 ASSERT_EQ(AMETA_NONE, args.metaState);
2930 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2931 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2932 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2933
2934 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002935 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2937 ASSERT_EQ(DEVICE_ID, args.deviceId);
2938 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2939 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2940 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2941 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2942 ASSERT_EQ(KEY_HOME, args.scanCode);
2943 ASSERT_EQ(AMETA_NONE, args.metaState);
2944 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2945 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2946 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2947
2948 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2952 ASSERT_EQ(DEVICE_ID, args.deviceId);
2953 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2954 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2955 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2956 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2957 ASSERT_EQ(0, args.scanCode);
2958 ASSERT_EQ(AMETA_NONE, args.metaState);
2959 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2960 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2961 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2962
2963 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2965 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2967 ASSERT_EQ(DEVICE_ID, args.deviceId);
2968 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2969 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2970 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2971 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2972 ASSERT_EQ(0, args.scanCode);
2973 ASSERT_EQ(AMETA_NONE, args.metaState);
2974 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2975 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2976 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2977
2978 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002979 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2982 ASSERT_EQ(DEVICE_ID, args.deviceId);
2983 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2984 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2985 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2986 ASSERT_EQ(0, args.keyCode);
2987 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2988 ASSERT_EQ(AMETA_NONE, args.metaState);
2989 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2990 ASSERT_EQ(0U, args.policyFlags);
2991 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2992
2993 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2995 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2997 ASSERT_EQ(DEVICE_ID, args.deviceId);
2998 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2999 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3000 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3001 ASSERT_EQ(0, args.keyCode);
3002 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3003 ASSERT_EQ(AMETA_NONE, args.metaState);
3004 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3005 ASSERT_EQ(0U, args.policyFlags);
3006 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3007}
3008
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003009TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3010 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3011 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3012 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3013
3014 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003015 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003016 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3017
3018 // Key down by scan code.
3019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3020 NotifyKeyArgs args;
3021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3022 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3023
3024 // Key up by scan code.
3025 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3027 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3028}
3029
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003030/**
3031 * Ensure that the readTime is set to the time when the EV_KEY is received.
3032 */
3033TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3034 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3035
3036 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003037 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003038 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3039 NotifyKeyArgs args;
3040
3041 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003042 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3044 ASSERT_EQ(12, args.readTime);
3045
3046 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003047 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3049 ASSERT_EQ(15, args.readTime);
3050}
3051
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003053 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3054 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003055 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3056 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3057 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003059 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003060 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003061 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062
Arthur Hung95f68612022-04-07 14:08:22 +08003063 // Initial metastate is AMETA_NONE.
3064 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065
3066 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068 NotifyKeyArgs args;
3069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3070 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003071 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003072 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073
3074 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003075 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3077 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003078 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079
3080 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003081 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3083 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003084 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085
3086 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003087 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3089 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003090 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003091 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092}
3093
3094TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003095 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3096 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3097 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3098 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003100 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003101 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003102 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103
Michael Wrighta9cf4192022-12-01 23:46:39 +00003104 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3106 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3107 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3108 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3109 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3110 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3111 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3112 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3113}
3114
3115TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003116 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3117 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3118 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3119 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120
Michael Wrightd02c5b62014-02-10 15:10:22 -08003121 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003122 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003123 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003124 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125
Michael Wrighta9cf4192022-12-01 23:46:39 +00003126 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003127 ASSERT_NO_FATAL_FAILURE(
3128 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3129 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3130 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3131 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3132 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3133 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3134 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003136 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003137 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003138 ASSERT_NO_FATAL_FAILURE(
3139 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3140 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3141 AKEYCODE_DPAD_UP, DISPLAY_ID));
3142 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3143 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3144 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3145 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003146
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003147 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003148 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003149 ASSERT_NO_FATAL_FAILURE(
3150 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3151 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3152 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3153 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3154 AKEYCODE_DPAD_UP, DISPLAY_ID));
3155 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3156 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003158 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003159 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003160 ASSERT_NO_FATAL_FAILURE(
3161 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3162 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3163 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3164 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3165 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3166 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3167 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168
3169 // Special case: if orientation changes while key is down, we still emit the same keycode
3170 // in the key up as we did in the key down.
3171 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003172 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003173 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3176 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3177 ASSERT_EQ(KEY_UP, args.scanCode);
3178 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3179
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003180 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003181 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3184 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3185 ASSERT_EQ(KEY_UP, args.scanCode);
3186 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3187}
3188
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003189TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3190 // If the keyboard is not orientation aware,
3191 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003192 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003194 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003195 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003196 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003197 NotifyKeyArgs args;
3198
3199 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3204 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3205
Michael Wrighta9cf4192022-12-01 23:46:39 +00003206 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3211 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3212}
3213
3214TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3215 // If the keyboard is orientation aware,
3216 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003217 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003218
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003219 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003220 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003221 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003222 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003223 NotifyKeyArgs args;
3224
3225 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3226 // ^--- already checked by the previous test
3227
Michael Wrighta9cf4192022-12-01 23:46:39 +00003228 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003229 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003230 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3234 ASSERT_EQ(DISPLAY_ID, args.displayId);
3235
3236 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003237 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003238 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003239 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003242 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3244 ASSERT_EQ(newDisplayId, args.displayId);
3245}
3246
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003248 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003249 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003250 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003252 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003253 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003255 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003256 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257}
3258
Philip Junker4af3b3d2021-12-14 10:36:55 +01003259TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3260 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003261 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003262 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3263
3264 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3265 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3266 << "If a mapping is available, the result is equal to the mapping";
3267
3268 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3269 << "If no mapping is available, the result is the key location";
3270}
3271
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003273 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003274 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003275 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003277 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003278 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003280 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003281 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282}
3283
3284TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003285 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003286 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003287 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003289 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003292 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293 ASSERT_TRUE(flags[0]);
3294 ASSERT_FALSE(flags[1]);
3295}
3296
3297TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003298 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3299 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3300 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3301 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3302 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3303 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003304
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003305 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003306 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003307 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003308 // Initial metastate is AMETA_NONE.
3309 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003310
3311 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003312 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3313 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3314 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315
3316 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003319 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3320 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3321 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003322 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003323
3324 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003327 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3328 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3329 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003330 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003331
3332 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003335 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3336 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3337 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003338 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339
3340 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003343 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3344 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3345 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003346 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347
3348 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003351 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3352 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3353 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003354 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355
3356 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003359 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3360 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3361 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003362 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003363}
3364
Chris Yea52ade12020-08-27 16:49:20 -07003365TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3366 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3367 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3368 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3369 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3370
3371 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003372 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003373 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3374
Chris Yea52ade12020-08-27 16:49:20 -07003375 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003376 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003377 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3378 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3379 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3380 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3381
3382 NotifyKeyArgs args;
3383 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3386 ASSERT_EQ(AMETA_NONE, args.metaState);
3387 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3388 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3389 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3390
3391 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003392 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3394 ASSERT_EQ(AMETA_NONE, args.metaState);
3395 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3396 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3397 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3398}
3399
Arthur Hung2c9a3342019-07-23 14:18:59 +08003400TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3401 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003402 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3403 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3404 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3405 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003406
3407 // keyboard 2.
3408 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003409 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003410 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003411 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003412 std::shared_ptr<InputDevice> device2 =
3413 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003414 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003415
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003416 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3417 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3418 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3419 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +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 Hung2c9a3342019-07-23 14:18:59 +08003424
Arpit Singh033e3ec2023-04-26 14:43:16 +00003425 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003426 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003427 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3428 mFakePolicy
3429 ->getReaderConfiguration(),
3430 AINPUT_SOURCE_KEYBOARD,
3431 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003432 std::list<NotifyArgs> unused =
3433 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003434 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003435 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003436
3437 // Prepared displays and associated info.
3438 constexpr uint8_t hdmi1 = 0;
3439 constexpr uint8_t hdmi2 = 1;
3440 const std::string SECONDARY_UNIQUE_ID = "local:1";
3441
3442 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3443 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3444
3445 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003446 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003447 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003448 ASSERT_FALSE(device2->isEnabled());
3449
3450 // Prepare second display.
3451 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003452 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003453 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003454 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003455 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003456 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003457 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003458 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003459
3460 // Device should be enabled after the associated display is found.
3461 ASSERT_TRUE(mDevice->isEnabled());
3462 ASSERT_TRUE(device2->isEnabled());
3463
3464 // Test pad key events
3465 ASSERT_NO_FATAL_FAILURE(
3466 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3467 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3468 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3469 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3470 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3471 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3472 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3473
3474 ASSERT_NO_FATAL_FAILURE(
3475 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3476 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3477 AKEYCODE_DPAD_RIGHT, newDisplayId));
3478 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3479 AKEYCODE_DPAD_DOWN, newDisplayId));
3480 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3481 AKEYCODE_DPAD_LEFT, newDisplayId));
3482}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483
arthurhungc903df12020-08-11 15:08:42 +08003484TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3485 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3486 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3487 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3488 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3489 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3490 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3491
3492 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003493 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003494 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003495 // Initial metastate is AMETA_NONE.
3496 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003497
3498 // Initialization should have turned all of the lights off.
3499 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3500 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3501 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3502
3503 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003504 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003506 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3507 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3508
3509 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003512 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3513 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3514
3515 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003518 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3519 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3520
3521 mFakeEventHub->removeDevice(EVENTHUB_ID);
3522 mReader->loopOnce();
3523
3524 // keyboard 2 should default toggle keys.
3525 const std::string USB2 = "USB2";
3526 const std::string DEVICE_NAME2 = "KEYBOARD2";
3527 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3528 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3529 std::shared_ptr<InputDevice> device2 =
3530 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003531 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003532 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3533 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3534 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3535 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3536 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3537 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3538
Arpit Singh033e3ec2023-04-26 14:43:16 +00003539 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003540 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003541 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3542 mFakePolicy
3543 ->getReaderConfiguration(),
3544 AINPUT_SOURCE_KEYBOARD,
3545 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003546 std::list<NotifyArgs> unused =
3547 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003548 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003549 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003550
3551 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3552 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3553 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003554 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3555 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003556}
3557
Arthur Hungcb40a002021-08-03 14:31:01 +00003558TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3559 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3560 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3561 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3562
3563 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh033e3ec2023-04-26 14:43:16 +00003564 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003565 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3566 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003567 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003568 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003569 // Initial metastate is AMETA_NONE.
3570 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003571
3572 mReader->toggleCapsLockState(DEVICE_ID);
3573 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3574}
3575
Arthur Hungfb3cc112022-04-13 07:39:50 +00003576TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3577 // keyboard 1.
3578 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3579 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3580 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3581 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3582 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3583 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3584
3585 KeyboardInputMapper& mapper1 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003586 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003587 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3588
3589 // keyboard 2.
3590 const std::string USB2 = "USB2";
3591 const std::string DEVICE_NAME2 = "KEYBOARD2";
3592 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3593 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3594 std::shared_ptr<InputDevice> device2 =
3595 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3596 ftl::Flags<InputDeviceClass>(0));
3597 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3598 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3599 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3600 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3601 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3602 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3603
Arpit Singh033e3ec2023-04-26 14:43:16 +00003604 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003605 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003606 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3607 mFakePolicy
3608 ->getReaderConfiguration(),
3609 AINPUT_SOURCE_KEYBOARD,
3610 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003611 std::list<NotifyArgs> unused =
3612 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003613 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003614 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003615
Arthur Hung95f68612022-04-07 14:08:22 +08003616 // Initial metastate is AMETA_NONE.
3617 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3618 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3619
3620 // Toggle num lock on and off.
3621 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3622 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003623 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3624 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3625 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3626
3627 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3628 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3629 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3630 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3631 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3632
3633 // Toggle caps lock on and off.
3634 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3635 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3636 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3637 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3638 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3639
3640 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3641 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3642 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3643 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3644 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3645
3646 // Toggle scroll lock on and off.
3647 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3648 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3649 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3650 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3651 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3652
3653 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3654 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3655 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3656 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3657 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3658}
3659
Arthur Hung2141d542022-08-23 07:45:21 +00003660TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3661 const int32_t USAGE_A = 0x070004;
3662 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3663 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3664
3665 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003666 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003667 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3668 // Key down by scan code.
3669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3670 NotifyKeyArgs args;
3671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3672 ASSERT_EQ(DEVICE_ID, args.deviceId);
3673 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3674 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3675 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3676 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3677 ASSERT_EQ(KEY_HOME, args.scanCode);
3678 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3679
3680 // Disable device, it should synthesize cancellation events for down events.
3681 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003682 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003683
3684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3685 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3686 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3687 ASSERT_EQ(KEY_HOME, args.scanCode);
3688 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3689}
3690
Zixuan Qufecb6062022-11-12 04:44:31 +00003691TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh033e3ec2023-04-26 14:43:16 +00003692 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3693 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003694 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003695 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3696 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003697
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003698 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003699 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3700
3701 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003702 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003703
3704 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3705 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3706 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3707 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3708 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003709 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3710
3711 // Call change layout association with the same values: Generation shouldn't change
3712 generation = mReader->getContext()->getGeneration();
3713 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3714 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3715 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3716 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003717}
3718
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003719TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3720 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3721 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3722
3723 // Configuration
Arpit Singh033e3ec2023-04-26 14:43:16 +00003724 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003725 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3726 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003727 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003728
3729 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3730 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3731}
3732
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003733// --- KeyboardInputMapperTest_ExternalDevice ---
3734
3735class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3736protected:
Chris Yea52ade12020-08-27 16:49:20 -07003737 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003738};
3739
3740TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003741 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3742 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003743
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003744 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3745 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3746 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3747 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003748
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003749 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003750 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003751 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003752
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003754 NotifyKeyArgs args;
3755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3756 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3757
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003758 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3760 ASSERT_EQ(uint32_t(0), args.policyFlags);
3761
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003764 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003765
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003766 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3768 ASSERT_EQ(uint32_t(0), args.policyFlags);
3769
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3772 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3773
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003774 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3776 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3777}
3778
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003779TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003780 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003781
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003782 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3783 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3784 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003785
Powei Fengd041c5d2019-05-03 17:11:33 -07003786 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003787 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003788 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003789 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003790
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003792 NotifyKeyArgs args;
3793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3794 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3795
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003796 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3798 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3799
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3802 ASSERT_EQ(uint32_t(0), args.policyFlags);
3803
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003804 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3806 ASSERT_EQ(uint32_t(0), args.policyFlags);
3807
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3810 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3811
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003812 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3814 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3815}
3816
Michael Wrightd02c5b62014-02-10 15:10:22 -08003817// --- CursorInputMapperTest ---
3818
3819class CursorInputMapperTest : public InputMapperTest {
3820protected:
3821 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3822
Michael Wright17db18e2020-06-26 20:51:44 +01003823 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003824
Chris Yea52ade12020-08-27 16:49:20 -07003825 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003826 InputMapperTest::SetUp();
3827
Michael Wright17db18e2020-06-26 20:51:44 +01003828 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003829 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003830 }
3831
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003832 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3833 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003834
Michael Wrighta9cf4192022-12-01 23:46:39 +00003835 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003836 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3837 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3838 }
3839
3840 void prepareSecondaryDisplay() {
3841 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003842 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003843 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003844 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003845
3846 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3847 float pressure) {
3848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3849 0.0f, 0.0f, 0.0f, EPSILON));
3850 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003851};
3852
3853const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003855void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3856 int32_t originalY, int32_t rotatedX,
3857 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858 NotifyMotionArgs args;
3859
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003865 ASSERT_NO_FATAL_FAILURE(
3866 assertCursorPointerCoords(args.pointerCoords[0],
3867 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3868 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003869}
3870
3871TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003873 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003875 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876}
3877
3878TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003880 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003882 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003883}
3884
3885TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003887 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003888
3889 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003890 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003891
3892 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003893 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3894 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3896 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3897
3898 // When the bounds are set, then there should be a valid motion range.
3899 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3900
3901 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003902 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003903
3904 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3905 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3906 1, 800 - 1, 0.0f, 0.0f));
3907 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3908 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3909 2, 480 - 1, 0.0f, 0.0f));
3910 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3911 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3912 0.0f, 1.0f, 0.0f, 0.0f));
3913}
3914
3915TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003917 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918
3919 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003920 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921
3922 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3923 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3924 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3925 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3926 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3927 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3928 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3929 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3930 0.0f, 1.0f, 0.0f, 0.0f));
3931}
3932
3933TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003934 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003935 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936
arthurhungdcef2dc2020-08-11 14:47:50 +08003937 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003938
3939 NotifyMotionArgs args;
3940
3941 // Button press.
3942 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3946 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3947 ASSERT_EQ(DEVICE_ID, args.deviceId);
3948 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3949 ASSERT_EQ(uint32_t(0), args.policyFlags);
3950 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3951 ASSERT_EQ(0, args.flags);
3952 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3953 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3954 ASSERT_EQ(0, args.edgeFlags);
3955 ASSERT_EQ(uint32_t(1), args.pointerCount);
3956 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003957 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003958 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003959 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3960 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3961 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3962
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3964 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3965 ASSERT_EQ(DEVICE_ID, args.deviceId);
3966 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3967 ASSERT_EQ(uint32_t(0), args.policyFlags);
3968 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3969 ASSERT_EQ(0, args.flags);
3970 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3971 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3972 ASSERT_EQ(0, args.edgeFlags);
3973 ASSERT_EQ(uint32_t(1), args.pointerCount);
3974 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003975 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003976 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003977 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3978 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3979 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3980
Michael Wrightd02c5b62014-02-10 15:10:22 -08003981 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003982 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3983 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3985 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3986 ASSERT_EQ(DEVICE_ID, args.deviceId);
3987 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3988 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003989 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3990 ASSERT_EQ(0, args.flags);
3991 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3992 ASSERT_EQ(0, args.buttonState);
3993 ASSERT_EQ(0, args.edgeFlags);
3994 ASSERT_EQ(uint32_t(1), args.pointerCount);
3995 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003996 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003997 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003998 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3999 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4000 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4001
4002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4003 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4004 ASSERT_EQ(DEVICE_ID, args.deviceId);
4005 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4006 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4008 ASSERT_EQ(0, args.flags);
4009 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4010 ASSERT_EQ(0, args.buttonState);
4011 ASSERT_EQ(0, args.edgeFlags);
4012 ASSERT_EQ(uint32_t(1), args.pointerCount);
4013 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004014 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004015 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4017 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4018 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4019}
4020
4021TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004023 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004024
4025 NotifyMotionArgs args;
4026
4027 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4031 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004032 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4033 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4034 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004035
4036 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004041 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4042 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043}
4044
4045TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004047 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048
4049 NotifyMotionArgs args;
4050
4051 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4055 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004056 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4059 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004060 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004061
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004066 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004067 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004068
4069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004071 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072}
4073
4074TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004076 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004077
4078 NotifyMotionArgs args;
4079
4080 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4084 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4086 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004087 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4088 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4089 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4092 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004093 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4094 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4095 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004096
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4100 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004103 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4104 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4105 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004106
4107 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004112 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004113
4114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004116 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117}
4118
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004119TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004120 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004122 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4123 // need to be rotated.
4124 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004125 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004126
Michael Wrighta9cf4192022-12-01 23:46:39 +00004127 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4130 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4133 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4134 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4135 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4136}
4137
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004138TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004139 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004141 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4142 // orientation-aware are affected by display rotation.
Arpit Singhd8510bd2023-04-27 12:48:15 +00004143 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004145 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004146 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4148 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4150 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4151 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4152 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4153 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4154 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4155
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004156 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004157 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004158 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4160 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4161 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4162 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4163 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4164 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4165 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004167 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004168 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004169 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4170 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4171 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4172 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4173 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4174 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4175 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4176 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4177
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004178 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004179 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004180 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4181 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4182 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4183 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4187 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004188}
4189
4190TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004192 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193
4194 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4195 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196
4197 NotifyMotionArgs motionArgs;
4198 NotifyKeyArgs keyArgs;
4199
4200 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4204 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4205 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004206 ASSERT_NO_FATAL_FAILURE(
4207 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4210 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4211 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004212 ASSERT_NO_FATAL_FAILURE(
4213 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004214
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
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(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004218 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004220 ASSERT_NO_FATAL_FAILURE(
4221 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222
4223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004224 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004226 ASSERT_NO_FATAL_FAILURE(
4227 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004228
4229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004231 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004232 ASSERT_NO_FATAL_FAILURE(
4233 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234
4235 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4240 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4241 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004242 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004243 ASSERT_NO_FATAL_FAILURE(
4244 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4248 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004249 ASSERT_NO_FATAL_FAILURE(
4250 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004251
4252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4253 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4254 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004255 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004256 ASSERT_NO_FATAL_FAILURE(
4257 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004258
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004262 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004264 ASSERT_NO_FATAL_FAILURE(
4265 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004266
4267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004269 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004270 ASSERT_NO_FATAL_FAILURE(
4271 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004276 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4277 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004278 ASSERT_NO_FATAL_FAILURE(
4279 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004282
4283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004286 ASSERT_NO_FATAL_FAILURE(
4287 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004288
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4290 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004292 ASSERT_NO_FATAL_FAILURE(
4293 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294
4295 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4299 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4300 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004301
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004303 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004305 ASSERT_NO_FATAL_FAILURE(
4306 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004307
4308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4309 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4310 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004311 ASSERT_NO_FATAL_FAILURE(
4312 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004317 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004319 ASSERT_NO_FATAL_FAILURE(
4320 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004321
4322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004324 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004325
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004326 ASSERT_NO_FATAL_FAILURE(
4327 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4329 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4330 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4331
4332 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4336 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4337 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004338
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004340 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004342 ASSERT_NO_FATAL_FAILURE(
4343 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004344
4345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4346 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4347 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004348 ASSERT_NO_FATAL_FAILURE(
4349 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004356 ASSERT_NO_FATAL_FAILURE(
4357 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004358
4359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4360 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4361 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004362 ASSERT_NO_FATAL_FAILURE(
4363 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004364
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4366 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4367 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4368
4369 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4373 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4374 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004375
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004377 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004379 ASSERT_NO_FATAL_FAILURE(
4380 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004381
4382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4383 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4384 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004385 ASSERT_NO_FATAL_FAILURE(
4386 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004391 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004393 ASSERT_NO_FATAL_FAILURE(
4394 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004395
4396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4397 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4398 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004399 ASSERT_NO_FATAL_FAILURE(
4400 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004401
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4403 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4404 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4405
4406 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4410 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4411 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004414 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004416 ASSERT_NO_FATAL_FAILURE(
4417 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004418
4419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4420 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4421 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004422 ASSERT_NO_FATAL_FAILURE(
4423 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004428 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004430 ASSERT_NO_FATAL_FAILURE(
4431 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004432
4433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4434 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4435 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004436 ASSERT_NO_FATAL_FAILURE(
4437 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004438
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4440 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4441 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4442}
4443
4444TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004446 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447
4448 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4449 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450
4451 NotifyMotionArgs args;
4452
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004457 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4458 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4460 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 +00004461 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004462}
4463
4464TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004465 addConfigurationProperty("cursor.mode", "pointer");
4466 mFakePolicy->setPointerCapture(true);
Arpit Singhd8510bd2023-04-27 12:48:15 +00004467 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004468
4469 NotifyDeviceResetArgs resetArgs;
4470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4471 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4472 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4473
4474 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4475 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004476
4477 NotifyMotionArgs args;
4478
4479 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4484 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4487 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 +00004488 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004489
4490 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4492 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4494 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4495 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4497 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4499 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4500 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4502 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4503
4504 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004505 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4506 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4508 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4509 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4511 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4513 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4514 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4516 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4517
4518 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4523 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4526 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 +00004527 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004528
4529 // Disable pointer capture and check that the device generation got bumped
4530 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004531 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004532 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004533 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004534 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004535
4536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004537 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4538
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004539 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4543 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4546 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 +00004547 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548}
4549
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004550/**
4551 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4552 * pointer acceleration or speed processing should not be applied.
4553 */
4554TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4555 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004556 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4557 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004558 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhd8510bd2023-04-27 12:48:15 +00004559 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004560
4561 NotifyDeviceResetArgs resetArgs;
4562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4563 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4564 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4565
4566 NotifyMotionArgs args;
4567
4568 // Move and verify scale is applied.
4569 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);
4572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4573 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4574 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4575 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4576 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4577 ASSERT_GT(relX, 10);
4578 ASSERT_GT(relY, 20);
4579
4580 // Enable Pointer Capture
4581 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004582 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004583 NotifyPointerCaptureChangedArgs captureArgs;
4584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4585 ASSERT_TRUE(captureArgs.request.enable);
4586
4587 // Move and verify scale is not applied.
4588 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4590 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4592 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4594 ASSERT_EQ(10, args.pointerCoords[0].getX());
4595 ASSERT_EQ(20, args.pointerCoords[0].getY());
4596}
4597
Prabir Pradhan208360b2022-06-24 18:37:04 +00004598TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4599 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004600 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004601
4602 NotifyDeviceResetArgs resetArgs;
4603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4604 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4605 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4606
4607 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004608 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004609
4610 NotifyMotionArgs args;
4611
4612 // Verify that the coordinates are rotated.
4613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4617 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4618 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4619 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4620 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4621
4622 // Enable Pointer Capture.
4623 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004624 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004625 NotifyPointerCaptureChangedArgs captureArgs;
4626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4627 ASSERT_TRUE(captureArgs.request.enable);
4628
4629 // Move and verify rotation is not applied.
4630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4632 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4634 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4636 ASSERT_EQ(10, args.pointerCoords[0].getX());
4637 ASSERT_EQ(20, args.pointerCoords[0].getY());
4638}
4639
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004640TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004641 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004642
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004643 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004644 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004645
4646 // Set up the secondary display as the display on which the pointer should be shown.
4647 // The InputDevice is not associated with any display.
4648 prepareSecondaryDisplay();
4649 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004650 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004651
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004652 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004653 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004654
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004655 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004660 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4661 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4662 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004663 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004664}
4665
4666TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004667 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004668
4669 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004670 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004671
4672 // Set up the secondary display as the display on which the pointer should be shown,
4673 // and associate the InputDevice with the secondary display.
4674 prepareSecondaryDisplay();
4675 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4676 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004677 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004678
4679 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4680 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004681
4682 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004686 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4687 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4688 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004689 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004690}
4691
4692TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004693 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004694
4695 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004696 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004697 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4698
4699 // Associate the InputDevice with the secondary display.
4700 prepareSecondaryDisplay();
4701 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004702 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004703
4704 // The mapper should not generate any events because it is associated with a display that is
4705 // different from the pointer display.
4706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004710}
4711
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004712// --- BluetoothCursorInputMapperTest ---
4713
4714class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4715protected:
4716 void SetUp() override {
4717 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4718
4719 mFakePointerController = std::make_shared<FakePointerController>();
4720 mFakePolicy->setPointerController(mFakePointerController);
4721 }
4722};
4723
4724TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4725 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004726 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004727
4728 nsecs_t kernelEventTime = ARBITRARY_TIME;
4729 nsecs_t expectedEventTime = ARBITRARY_TIME;
4730 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4731 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4733 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4734 WithEventTime(expectedEventTime))));
4735
4736 // Process several events that come in quick succession, according to their timestamps.
4737 for (int i = 0; i < 3; i++) {
4738 constexpr static nsecs_t delta = ms2ns(1);
4739 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4740 kernelEventTime += delta;
4741 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4742
4743 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4744 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4746 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4747 WithEventTime(expectedEventTime))));
4748 }
4749}
4750
4751TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4752 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004753 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004754
4755 nsecs_t expectedEventTime = ARBITRARY_TIME;
4756 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4759 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4760 WithEventTime(expectedEventTime))));
4761
4762 // Process several events with the same timestamp from the kernel.
4763 // Ensure that we do not generate events too far into the future.
4764 constexpr static int32_t numEvents =
4765 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4766 for (int i = 0; i < numEvents; i++) {
4767 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4768
4769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4772 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4773 WithEventTime(expectedEventTime))));
4774 }
4775
4776 // By processing more events with the same timestamp, we should not generate events with a
4777 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4778 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4779 for (int i = 0; i < 3; i++) {
4780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4783 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4784 WithEventTime(cappedEventTime))));
4785 }
4786}
4787
4788TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4789 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004790 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004791
4792 nsecs_t kernelEventTime = ARBITRARY_TIME;
4793 nsecs_t expectedEventTime = ARBITRARY_TIME;
4794 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4795 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4797 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4798 WithEventTime(expectedEventTime))));
4799
4800 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4801 // smoothening is not needed, its timestamp is not affected.
4802 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4803 expectedEventTime = kernelEventTime;
4804
4805 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4806 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4808 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4809 WithEventTime(expectedEventTime))));
4810}
4811
Michael Wrightd02c5b62014-02-10 15:10:22 -08004812// --- TouchInputMapperTest ---
4813
4814class TouchInputMapperTest : public InputMapperTest {
4815protected:
4816 static const int32_t RAW_X_MIN;
4817 static const int32_t RAW_X_MAX;
4818 static const int32_t RAW_Y_MIN;
4819 static const int32_t RAW_Y_MAX;
4820 static const int32_t RAW_TOUCH_MIN;
4821 static const int32_t RAW_TOUCH_MAX;
4822 static const int32_t RAW_TOOL_MIN;
4823 static const int32_t RAW_TOOL_MAX;
4824 static const int32_t RAW_PRESSURE_MIN;
4825 static const int32_t RAW_PRESSURE_MAX;
4826 static const int32_t RAW_ORIENTATION_MIN;
4827 static const int32_t RAW_ORIENTATION_MAX;
4828 static const int32_t RAW_DISTANCE_MIN;
4829 static const int32_t RAW_DISTANCE_MAX;
4830 static const int32_t RAW_TILT_MIN;
4831 static const int32_t RAW_TILT_MAX;
4832 static const int32_t RAW_ID_MIN;
4833 static const int32_t RAW_ID_MAX;
4834 static const int32_t RAW_SLOT_MIN;
4835 static const int32_t RAW_SLOT_MAX;
4836 static const float X_PRECISION;
4837 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004838 static const float X_PRECISION_VIRTUAL;
4839 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004840
4841 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004842 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843
4844 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4845
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004846 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004847 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004848
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849 enum Axes {
4850 POSITION = 1 << 0,
4851 TOUCH = 1 << 1,
4852 TOOL = 1 << 2,
4853 PRESSURE = 1 << 3,
4854 ORIENTATION = 1 << 4,
4855 MINOR = 1 << 5,
4856 ID = 1 << 6,
4857 DISTANCE = 1 << 7,
4858 TILT = 1 << 8,
4859 SLOT = 1 << 9,
4860 TOOL_TYPE = 1 << 10,
4861 };
4862
Michael Wrighta9cf4192022-12-01 23:46:39 +00004863 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004864 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004865 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004867 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868 int32_t toRawX(float displayX);
4869 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004870 int32_t toRotatedRawX(float displayX);
4871 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004872 float toCookedX(float rawX, float rawY);
4873 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004875 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004876 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004877 float toDisplayY(int32_t rawY, int32_t displayHeight);
4878
Michael Wrightd02c5b62014-02-10 15:10:22 -08004879};
4880
4881const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4882const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4883const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4884const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4885const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4886const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4887const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4888const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004889const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4890const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004891const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4892const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4893const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4894const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4895const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4896const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4897const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4898const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4899const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4900const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4901const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4902const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004903const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4904 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4905const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4906 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004907const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4908 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004909
4910const float TouchInputMapperTest::GEOMETRIC_SCALE =
4911 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4912 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4913
4914const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4915 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4916 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4917};
4918
Michael Wrighta9cf4192022-12-01 23:46:39 +00004919void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004920 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4921 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004922}
4923
4924void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4925 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004926 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927}
4928
Michael Wrighta9cf4192022-12-01 23:46:39 +00004929void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004930 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4931 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4932 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004933}
4934
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004936 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4937 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4938 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4939 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940}
4941
Jason Gerecke489fda82012-09-07 17:19:40 -07004942void TouchInputMapperTest::prepareLocationCalibration() {
4943 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4944}
4945
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946int32_t TouchInputMapperTest::toRawX(float displayX) {
4947 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4948}
4949
4950int32_t TouchInputMapperTest::toRawY(float displayY) {
4951 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4952}
4953
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004954int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4955 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4956}
4957
4958int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4959 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4960}
4961
Jason Gerecke489fda82012-09-07 17:19:40 -07004962float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4963 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4964 return rawX;
4965}
4966
4967float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4968 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4969 return rawY;
4970}
4971
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004973 return toDisplayX(rawX, DISPLAY_WIDTH);
4974}
4975
4976float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4977 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978}
4979
4980float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004981 return toDisplayY(rawY, DISPLAY_HEIGHT);
4982}
4983
4984float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4985 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986}
4987
4988
4989// --- SingleTouchInputMapperTest ---
4990
4991class SingleTouchInputMapperTest : public TouchInputMapperTest {
4992protected:
4993 void prepareButtons();
4994 void prepareAxes(int axes);
4995
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004996 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4997 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4998 void processUp(SingleTouchInputMapper& mappery);
4999 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5000 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5001 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5002 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5003 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5004 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005005};
5006
5007void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005008 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009}
5010
5011void SingleTouchInputMapperTest::prepareAxes(int axes) {
5012 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005013 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5014 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015 }
5016 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005017 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5018 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005019 }
5020 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005021 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5022 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023 }
5024 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005025 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5026 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005027 }
5028 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005029 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5030 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005031 }
5032}
5033
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005034void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038}
5039
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005040void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043}
5044
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005045void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005046 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047}
5048
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005049void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005051}
5052
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005053void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5054 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005055 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005056}
5057
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005058void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005059 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060}
5061
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005062void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5063 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066}
5067
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005068void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5069 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005071}
5072
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005073void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075}
5076
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078 prepareButtons();
5079 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00005080 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005081
Josep del Río2d8c79a2023-01-23 19:33:50 +00005082 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083}
5084
Michael Wrightd02c5b62014-02-10 15:10:22 -08005085TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086 prepareButtons();
5087 prepareAxes(POSITION);
5088 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00005089 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005090
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005091 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005092}
5093
5094TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005096 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005097 prepareButtons();
5098 prepareAxes(POSITION);
5099 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005100 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005101
5102 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005103 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005104
5105 // Virtual key is down.
5106 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5107 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5108 processDown(mapper, x, y);
5109 processSync(mapper);
5110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5111
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005112 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113
5114 // Virtual key is up.
5115 processUp(mapper);
5116 processSync(mapper);
5117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5118
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005119 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005120}
5121
5122TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005124 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125 prepareButtons();
5126 prepareAxes(POSITION);
5127 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005128 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129
5130 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005131 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005132
5133 // Virtual key is down.
5134 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5135 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5136 processDown(mapper, x, y);
5137 processSync(mapper);
5138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005140 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005141
5142 // Virtual key is up.
5143 processUp(mapper);
5144 processSync(mapper);
5145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5146
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005147 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148}
5149
5150TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005152 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153 prepareButtons();
5154 prepareAxes(POSITION);
5155 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005156 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157
Michael Wrightd02c5b62014-02-10 15:10:22 -08005158 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005159 ASSERT_TRUE(
5160 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161 ASSERT_TRUE(flags[0]);
5162 ASSERT_FALSE(flags[1]);
5163}
5164
5165TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005167 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005168 prepareButtons();
5169 prepareAxes(POSITION);
5170 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005171 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172
arthurhungdcef2dc2020-08-11 14:47:50 +08005173 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005174
5175 NotifyKeyArgs args;
5176
5177 // Press virtual key.
5178 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5179 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5180 processDown(mapper, x, y);
5181 processSync(mapper);
5182
5183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5184 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5185 ASSERT_EQ(DEVICE_ID, args.deviceId);
5186 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5187 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5188 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5189 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5190 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5191 ASSERT_EQ(KEY_HOME, args.scanCode);
5192 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5193 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5194
5195 // Release virtual key.
5196 processUp(mapper);
5197 processSync(mapper);
5198
5199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5200 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5201 ASSERT_EQ(DEVICE_ID, args.deviceId);
5202 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5203 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5204 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5205 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5206 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5207 ASSERT_EQ(KEY_HOME, args.scanCode);
5208 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5209 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5210
5211 // Should not have sent any motions.
5212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5213}
5214
5215TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005217 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005218 prepareButtons();
5219 prepareAxes(POSITION);
5220 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005221 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005222
arthurhungdcef2dc2020-08-11 14:47:50 +08005223 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005224
5225 NotifyKeyArgs keyArgs;
5226
5227 // Press virtual key.
5228 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5229 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5230 processDown(mapper, x, y);
5231 processSync(mapper);
5232
5233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5234 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5235 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5236 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5237 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5238 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5239 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5240 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5241 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5242 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5243 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5244
5245 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5246 // into the display area.
5247 y -= 100;
5248 processMove(mapper, x, y);
5249 processSync(mapper);
5250
5251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5252 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5253 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5254 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5255 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5256 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5257 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5258 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5259 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5260 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5261 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5262 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5263
5264 NotifyMotionArgs motionArgs;
5265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5266 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5267 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5268 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5269 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5270 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5271 ASSERT_EQ(0, motionArgs.flags);
5272 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5273 ASSERT_EQ(0, motionArgs.buttonState);
5274 ASSERT_EQ(0, motionArgs.edgeFlags);
5275 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5276 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005277 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5279 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5280 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5281 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5282 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5283
5284 // Keep moving out of bounds. Should generate a pointer move.
5285 y -= 50;
5286 processMove(mapper, x, y);
5287 processSync(mapper);
5288
5289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5290 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5291 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5292 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5293 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5294 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5295 ASSERT_EQ(0, motionArgs.flags);
5296 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5297 ASSERT_EQ(0, motionArgs.buttonState);
5298 ASSERT_EQ(0, motionArgs.edgeFlags);
5299 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5300 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005301 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5303 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5304 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5305 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5306 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5307
5308 // Release out of bounds. Should generate a pointer up.
5309 processUp(mapper);
5310 processSync(mapper);
5311
5312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5314 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5315 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5316 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5317 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5318 ASSERT_EQ(0, motionArgs.flags);
5319 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5320 ASSERT_EQ(0, motionArgs.buttonState);
5321 ASSERT_EQ(0, motionArgs.edgeFlags);
5322 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5323 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005324 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5326 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5327 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5328 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5329 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5330
5331 // Should not have sent any more keys or motions.
5332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5334}
5335
5336TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005338 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339 prepareButtons();
5340 prepareAxes(POSITION);
5341 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005342 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005343
arthurhungdcef2dc2020-08-11 14:47:50 +08005344 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345
5346 NotifyMotionArgs motionArgs;
5347
5348 // Initially go down out of bounds.
5349 int32_t x = -10;
5350 int32_t y = -10;
5351 processDown(mapper, x, y);
5352 processSync(mapper);
5353
5354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5355
5356 // Move into the display area. Should generate a pointer down.
5357 x = 50;
5358 y = 75;
5359 processMove(mapper, x, y);
5360 processSync(mapper);
5361
5362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5363 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5364 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5365 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5366 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5367 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5368 ASSERT_EQ(0, motionArgs.flags);
5369 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5370 ASSERT_EQ(0, motionArgs.buttonState);
5371 ASSERT_EQ(0, motionArgs.edgeFlags);
5372 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5373 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005374 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5376 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5377 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5378 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5379 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5380
5381 // Release. Should generate a pointer up.
5382 processUp(mapper);
5383 processSync(mapper);
5384
5385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5386 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5387 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5388 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5389 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5390 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5391 ASSERT_EQ(0, motionArgs.flags);
5392 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5393 ASSERT_EQ(0, motionArgs.buttonState);
5394 ASSERT_EQ(0, motionArgs.edgeFlags);
5395 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5396 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005397 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5399 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5400 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5401 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5402 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5403
5404 // Should not have sent any more keys or motions.
5405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5407}
5408
Santos Cordonfa5cf462017-04-05 10:37:00 -07005409TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005410 addConfigurationProperty("touch.deviceType", "touchScreen");
5411 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5412
Michael Wrighta9cf4192022-12-01 23:46:39 +00005413 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005414 prepareButtons();
5415 prepareAxes(POSITION);
5416 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005417 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005418
arthurhungdcef2dc2020-08-11 14:47:50 +08005419 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005420
5421 NotifyMotionArgs motionArgs;
5422
5423 // Down.
5424 int32_t x = 100;
5425 int32_t y = 125;
5426 processDown(mapper, x, y);
5427 processSync(mapper);
5428
5429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5430 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5431 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5432 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5433 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5434 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5435 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5436 ASSERT_EQ(0, motionArgs.flags);
5437 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5438 ASSERT_EQ(0, motionArgs.buttonState);
5439 ASSERT_EQ(0, motionArgs.edgeFlags);
5440 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5441 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005442 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5444 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5445 1, 0, 0, 0, 0, 0, 0, 0));
5446 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5447 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5448 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5449
5450 // Move.
5451 x += 50;
5452 y += 75;
5453 processMove(mapper, x, y);
5454 processSync(mapper);
5455
5456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5457 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5458 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5459 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5460 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5461 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5463 ASSERT_EQ(0, motionArgs.flags);
5464 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5465 ASSERT_EQ(0, motionArgs.buttonState);
5466 ASSERT_EQ(0, motionArgs.edgeFlags);
5467 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5468 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005469 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5471 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5472 1, 0, 0, 0, 0, 0, 0, 0));
5473 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5474 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5475 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5476
5477 // Up.
5478 processUp(mapper);
5479 processSync(mapper);
5480
5481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5482 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5483 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5484 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5485 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5486 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5487 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5488 ASSERT_EQ(0, motionArgs.flags);
5489 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5490 ASSERT_EQ(0, motionArgs.buttonState);
5491 ASSERT_EQ(0, motionArgs.edgeFlags);
5492 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5493 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005494 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5496 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5497 1, 0, 0, 0, 0, 0, 0, 0));
5498 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5499 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5500 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5501
5502 // Should not have sent any more keys or motions.
5503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5505}
5506
Michael Wrightd02c5b62014-02-10 15:10:22 -08005507TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005508 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005509 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005510 prepareButtons();
5511 prepareAxes(POSITION);
5512 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005513 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005514
arthurhungdcef2dc2020-08-11 14:47:50 +08005515 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005516
5517 NotifyMotionArgs motionArgs;
5518
5519 // Down.
5520 int32_t x = 100;
5521 int32_t y = 125;
5522 processDown(mapper, x, y);
5523 processSync(mapper);
5524
5525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5526 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5527 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5528 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5529 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5530 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5531 ASSERT_EQ(0, motionArgs.flags);
5532 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5533 ASSERT_EQ(0, motionArgs.buttonState);
5534 ASSERT_EQ(0, motionArgs.edgeFlags);
5535 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5536 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005537 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5539 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5540 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5541 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5542 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5543
5544 // Move.
5545 x += 50;
5546 y += 75;
5547 processMove(mapper, x, y);
5548 processSync(mapper);
5549
5550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5551 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5552 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5553 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5554 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5556 ASSERT_EQ(0, motionArgs.flags);
5557 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5558 ASSERT_EQ(0, motionArgs.buttonState);
5559 ASSERT_EQ(0, motionArgs.edgeFlags);
5560 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5561 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005562 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5564 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5565 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5566 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5567 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5568
5569 // Up.
5570 processUp(mapper);
5571 processSync(mapper);
5572
5573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5574 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5575 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5576 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5577 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5578 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, 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);
5583 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5584 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005585 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5587 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5588 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5589 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5590 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5591
5592 // Should not have sent any more keys or motions.
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5595}
5596
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005597TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005598 addConfigurationProperty("touch.deviceType", "touchScreen");
5599 prepareButtons();
5600 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005601 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5602 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singh56adebc2023-04-25 13:56:05 +00005603 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005604
5605 NotifyMotionArgs args;
5606
5607 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005608 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005609 processDown(mapper, toRawX(50), toRawY(75));
5610 processSync(mapper);
5611
5612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5613 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5614 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5615
5616 processUp(mapper);
5617 processSync(mapper);
5618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5619}
5620
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005621TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005622 addConfigurationProperty("touch.deviceType", "touchScreen");
5623 prepareButtons();
5624 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005625 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5626 // orientation-aware are affected by display rotation.
5627 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00005628 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005629
5630 NotifyMotionArgs args;
5631
5632 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005633 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005634 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005635 processDown(mapper, toRawX(50), toRawY(75));
5636 processSync(mapper);
5637
5638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5639 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5640 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5641
5642 processUp(mapper);
5643 processSync(mapper);
5644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5645
5646 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005647 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005648 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005649 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005650 processSync(mapper);
5651
5652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5653 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5654 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5655
5656 processUp(mapper);
5657 processSync(mapper);
5658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5659
5660 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005661 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005662 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005663 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5664 processSync(mapper);
5665
5666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5667 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5668 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5669
5670 processUp(mapper);
5671 processSync(mapper);
5672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5673
5674 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005675 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005676 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005677 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005678 processSync(mapper);
5679
5680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5681 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5682 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5683
5684 processUp(mapper);
5685 processSync(mapper);
5686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5687}
5688
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005689TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5690 addConfigurationProperty("touch.deviceType", "touchScreen");
5691 prepareButtons();
5692 prepareAxes(POSITION);
5693 addConfigurationProperty("touch.orientationAware", "1");
5694 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5695 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005696 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005697 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005698 NotifyMotionArgs args;
5699
5700 // Orientation 0.
5701 processDown(mapper, toRawX(50), toRawY(75));
5702 processSync(mapper);
5703
5704 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5705 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5706 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5707
5708 processUp(mapper);
5709 processSync(mapper);
5710 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5711}
5712
5713TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5714 addConfigurationProperty("touch.deviceType", "touchScreen");
5715 prepareButtons();
5716 prepareAxes(POSITION);
5717 addConfigurationProperty("touch.orientationAware", "1");
5718 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5719 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005720 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005721 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005722 NotifyMotionArgs args;
5723
5724 // Orientation 90.
5725 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5726 processSync(mapper);
5727
5728 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5729 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5730 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5731
5732 processUp(mapper);
5733 processSync(mapper);
5734 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5735}
5736
5737TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5738 addConfigurationProperty("touch.deviceType", "touchScreen");
5739 prepareButtons();
5740 prepareAxes(POSITION);
5741 addConfigurationProperty("touch.orientationAware", "1");
5742 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5743 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005744 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005745 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005746 NotifyMotionArgs args;
5747
5748 // Orientation 180.
5749 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5750 processSync(mapper);
5751
5752 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5753 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5754 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5755
5756 processUp(mapper);
5757 processSync(mapper);
5758 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5759}
5760
5761TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5762 addConfigurationProperty("touch.deviceType", "touchScreen");
5763 prepareButtons();
5764 prepareAxes(POSITION);
5765 addConfigurationProperty("touch.orientationAware", "1");
5766 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5767 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005768 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005769 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005770 NotifyMotionArgs args;
5771
5772 // Orientation 270.
5773 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5774 processSync(mapper);
5775
5776 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5777 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5778 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5779
5780 processUp(mapper);
5781 processSync(mapper);
5782 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5783}
5784
5785TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5786 addConfigurationProperty("touch.deviceType", "touchScreen");
5787 prepareButtons();
5788 prepareAxes(POSITION);
5789 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5790 // orientation-aware are affected by display rotation.
5791 addConfigurationProperty("touch.orientationAware", "0");
5792 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singh56adebc2023-04-25 13:56:05 +00005793 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005794
5795 NotifyMotionArgs args;
5796
5797 // Orientation 90, Rotation 0.
5798 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005799 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005800 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5801 processSync(mapper);
5802
5803 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5804 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5805 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5806
5807 processUp(mapper);
5808 processSync(mapper);
5809 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5810
5811 // Orientation 90, Rotation 90.
5812 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005813 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005814 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005815 processSync(mapper);
5816
5817 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5818 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5819 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5820
5821 processUp(mapper);
5822 processSync(mapper);
5823 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5824
5825 // Orientation 90, Rotation 180.
5826 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005827 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005828 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5829 processSync(mapper);
5830
5831 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5832 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5833 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5834
5835 processUp(mapper);
5836 processSync(mapper);
5837 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5838
5839 // Orientation 90, Rotation 270.
5840 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005841 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005842 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 -07005843 processSync(mapper);
5844
5845 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5846 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5847 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5848
5849 processUp(mapper);
5850 processSync(mapper);
5851 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5852}
5853
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005854TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5855 addConfigurationProperty("touch.deviceType", "touchScreen");
5856 prepareButtons();
5857 prepareAxes(POSITION);
5858 addConfigurationProperty("touch.orientationAware", "1");
5859 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005860 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005861
5862 // Set a physical frame in the display viewport.
5863 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5864 viewport->physicalLeft = 20;
5865 viewport->physicalTop = 600;
5866 viewport->physicalRight = 30;
5867 viewport->physicalBottom = 610;
5868 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005869 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005870
5871 // Start the touch.
5872 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5873 processSync(mapper);
5874
5875 // Expect all input starting outside the physical frame to be ignored.
5876 const std::array<Point, 6> outsidePoints = {
5877 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5878 for (const auto& p : outsidePoints) {
5879 processMove(mapper, toRawX(p.x), toRawY(p.y));
5880 processSync(mapper);
5881 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5882 }
5883
5884 // Move the touch into the physical frame.
5885 processMove(mapper, toRawX(25), toRawY(605));
5886 processSync(mapper);
5887 NotifyMotionArgs args;
5888 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5889 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5890 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5891 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5892
5893 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5894 for (const auto& p : outsidePoints) {
5895 processMove(mapper, toRawX(p.x), toRawY(p.y));
5896 processSync(mapper);
5897 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5898 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5899 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5900 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5901 }
5902
5903 processUp(mapper);
5904 processSync(mapper);
5905 EXPECT_NO_FATAL_FAILURE(
5906 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5907}
5908
Michael Wrightd02c5b62014-02-10 15:10:22 -08005909TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005910 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005911 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005912 prepareButtons();
5913 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singh56adebc2023-04-25 13:56:05 +00005914 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005915
5916 // These calculations are based on the input device calibration documentation.
5917 int32_t rawX = 100;
5918 int32_t rawY = 200;
5919 int32_t rawPressure = 10;
5920 int32_t rawToolMajor = 12;
5921 int32_t rawDistance = 2;
5922 int32_t rawTiltX = 30;
5923 int32_t rawTiltY = 110;
5924
5925 float x = toDisplayX(rawX);
5926 float y = toDisplayY(rawY);
5927 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5928 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5929 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5930 float distance = float(rawDistance);
5931
5932 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5933 float tiltScale = M_PI / 180;
5934 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5935 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5936 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5937 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5938
5939 processDown(mapper, rawX, rawY);
5940 processPressure(mapper, rawPressure);
5941 processToolMajor(mapper, rawToolMajor);
5942 processDistance(mapper, rawDistance);
5943 processTilt(mapper, rawTiltX, rawTiltY);
5944 processSync(mapper);
5945
5946 NotifyMotionArgs args;
5947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5949 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5950 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5951}
5952
Jason Gerecke489fda82012-09-07 17:19:40 -07005953TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005954 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005955 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005956 prepareLocationCalibration();
5957 prepareButtons();
5958 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00005959 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005960
5961 int32_t rawX = 100;
5962 int32_t rawY = 200;
5963
5964 float x = toDisplayX(toCookedX(rawX, rawY));
5965 float y = toDisplayY(toCookedY(rawX, rawY));
5966
5967 processDown(mapper, rawX, rawY);
5968 processSync(mapper);
5969
5970 NotifyMotionArgs args;
5971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5973 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5974}
5975
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005977 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005978 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979 prepareButtons();
5980 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00005981 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982
5983 NotifyMotionArgs motionArgs;
5984 NotifyKeyArgs keyArgs;
5985
5986 processDown(mapper, 100, 200);
5987 processSync(mapper);
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5989 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5990 ASSERT_EQ(0, motionArgs.buttonState);
5991
5992 // press BTN_LEFT, release BTN_LEFT
5993 processKey(mapper, BTN_LEFT, 1);
5994 processSync(mapper);
5995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5997 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5998
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6000 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6001 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6002
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003 processKey(mapper, BTN_LEFT, 0);
6004 processSync(mapper);
6005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006006 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006008
6009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006011 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006012
6013 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6014 processKey(mapper, BTN_RIGHT, 1);
6015 processKey(mapper, BTN_MIDDLE, 1);
6016 processSync(mapper);
6017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6018 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6019 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6020 motionArgs.buttonState);
6021
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6023 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6024 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6025
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6027 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6028 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6029 motionArgs.buttonState);
6030
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031 processKey(mapper, BTN_RIGHT, 0);
6032 processSync(mapper);
6033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006034 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006035 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006036
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006039 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006040
6041 processKey(mapper, BTN_MIDDLE, 0);
6042 processSync(mapper);
6043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006044 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006045 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006046
6047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006049 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006050
6051 // press BTN_BACK, release BTN_BACK
6052 processKey(mapper, BTN_BACK, 1);
6053 processSync(mapper);
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6055 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6056 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006057
Michael Wrightd02c5b62014-02-10 15:10:22 -08006058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006060 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6061
6062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6064 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065
6066 processKey(mapper, BTN_BACK, 0);
6067 processSync(mapper);
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006070 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006071
6072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006074 ASSERT_EQ(0, motionArgs.buttonState);
6075
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6077 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6078 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6079
6080 // press BTN_SIDE, release BTN_SIDE
6081 processKey(mapper, BTN_SIDE, 1);
6082 processSync(mapper);
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6084 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6085 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006086
Michael Wrightd02c5b62014-02-10 15:10:22 -08006087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006089 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6090
6091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6092 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6093 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006094
6095 processKey(mapper, BTN_SIDE, 0);
6096 processSync(mapper);
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006099 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006100
6101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006103 ASSERT_EQ(0, motionArgs.buttonState);
6104
Michael Wrightd02c5b62014-02-10 15:10:22 -08006105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6106 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6107 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6108
6109 // press BTN_FORWARD, release BTN_FORWARD
6110 processKey(mapper, BTN_FORWARD, 1);
6111 processSync(mapper);
6112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6113 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6114 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006115
Michael Wrightd02c5b62014-02-10 15:10:22 -08006116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006118 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6119
6120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6122 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123
6124 processKey(mapper, BTN_FORWARD, 0);
6125 processSync(mapper);
6126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006127 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006128 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006129
6130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006132 ASSERT_EQ(0, motionArgs.buttonState);
6133
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6135 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6136 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6137
6138 // press BTN_EXTRA, release BTN_EXTRA
6139 processKey(mapper, BTN_EXTRA, 1);
6140 processSync(mapper);
6141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6142 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6143 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006144
Michael Wrightd02c5b62014-02-10 15:10:22 -08006145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006147 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6148
6149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6150 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6151 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006152
6153 processKey(mapper, BTN_EXTRA, 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);
6162
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6164 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6165 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6166
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6168
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169 // press BTN_STYLUS, release BTN_STYLUS
6170 processKey(mapper, BTN_STYLUS, 1);
6171 processSync(mapper);
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006174 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, 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_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179
6180 processKey(mapper, BTN_STYLUS, 0);
6181 processSync(mapper);
6182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006185
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006188 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189
6190 // press BTN_STYLUS2, release BTN_STYLUS2
6191 processKey(mapper, BTN_STYLUS2, 1);
6192 processSync(mapper);
6193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006195 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6196
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6198 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6199 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006200
6201 processKey(mapper, BTN_STYLUS2, 0);
6202 processSync(mapper);
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006206
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006209 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210
6211 // release touch
6212 processUp(mapper);
6213 processSync(mapper);
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6215 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6216 ASSERT_EQ(0, motionArgs.buttonState);
6217}
6218
6219TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006221 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006222 prepareButtons();
6223 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006224 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006225
6226 NotifyMotionArgs motionArgs;
6227
6228 // default tool type is finger
6229 processDown(mapper, 100, 200);
6230 processSync(mapper);
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6232 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006233 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234
6235 // eraser
6236 processKey(mapper, BTN_TOOL_RUBBER, 1);
6237 processSync(mapper);
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006240 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006241
6242 // stylus
6243 processKey(mapper, BTN_TOOL_RUBBER, 0);
6244 processKey(mapper, BTN_TOOL_PEN, 1);
6245 processSync(mapper);
6246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006248 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249
6250 // brush
6251 processKey(mapper, BTN_TOOL_PEN, 0);
6252 processKey(mapper, BTN_TOOL_BRUSH, 1);
6253 processSync(mapper);
6254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006256 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006257
6258 // pencil
6259 processKey(mapper, BTN_TOOL_BRUSH, 0);
6260 processKey(mapper, BTN_TOOL_PENCIL, 1);
6261 processSync(mapper);
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006264 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006265
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006266 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267 processKey(mapper, BTN_TOOL_PENCIL, 0);
6268 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6269 processSync(mapper);
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006272 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273
6274 // mouse
6275 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6276 processKey(mapper, BTN_TOOL_MOUSE, 1);
6277 processSync(mapper);
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006280 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281
6282 // lens
6283 processKey(mapper, BTN_TOOL_MOUSE, 0);
6284 processKey(mapper, BTN_TOOL_LENS, 1);
6285 processSync(mapper);
6286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006288 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289
6290 // double-tap
6291 processKey(mapper, BTN_TOOL_LENS, 0);
6292 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6293 processSync(mapper);
6294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6295 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006296 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006297
6298 // triple-tap
6299 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6300 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6301 processSync(mapper);
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006304 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305
6306 // quad-tap
6307 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6308 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6309 processSync(mapper);
6310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006312 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313
6314 // finger
6315 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6316 processKey(mapper, BTN_TOOL_FINGER, 1);
6317 processSync(mapper);
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006320 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006321
6322 // stylus trumps finger
6323 processKey(mapper, BTN_TOOL_PEN, 1);
6324 processSync(mapper);
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006327 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006328
6329 // eraser trumps stylus
6330 processKey(mapper, BTN_TOOL_RUBBER, 1);
6331 processSync(mapper);
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006334 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006335
6336 // mouse trumps eraser
6337 processKey(mapper, BTN_TOOL_MOUSE, 1);
6338 processSync(mapper);
6339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006341 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006342
6343 // back to default tool type
6344 processKey(mapper, BTN_TOOL_MOUSE, 0);
6345 processKey(mapper, BTN_TOOL_RUBBER, 0);
6346 processKey(mapper, BTN_TOOL_PEN, 0);
6347 processKey(mapper, BTN_TOOL_FINGER, 0);
6348 processSync(mapper);
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006351 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006352}
6353
6354TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006355 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006356 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006357 prepareButtons();
6358 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006359 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +00006360 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361
6362 NotifyMotionArgs motionArgs;
6363
6364 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6365 processKey(mapper, BTN_TOOL_FINGER, 1);
6366 processMove(mapper, 100, 200);
6367 processSync(mapper);
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6369 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6371 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6372
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6374 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6376 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6377
6378 // move a little
6379 processMove(mapper, 150, 250);
6380 processSync(mapper);
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6384 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6385
6386 // down when BTN_TOUCH is pressed, pressure defaults to 1
6387 processKey(mapper, BTN_TOUCH, 1);
6388 processSync(mapper);
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6390 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6392 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6393
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6395 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6397 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6398
6399 // up when BTN_TOUCH is released, hover restored
6400 processKey(mapper, BTN_TOUCH, 0);
6401 processSync(mapper);
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6403 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6405 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6406
6407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6408 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6410 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6411
6412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6413 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6415 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6416
6417 // exit hover when pointer goes away
6418 processKey(mapper, BTN_TOOL_FINGER, 0);
6419 processSync(mapper);
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6421 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6423 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6424}
6425
6426TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006427 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006428 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006429 prepareButtons();
6430 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006431 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006432
6433 NotifyMotionArgs motionArgs;
6434
6435 // initially hovering because pressure is 0
6436 processDown(mapper, 100, 200);
6437 processPressure(mapper, 0);
6438 processSync(mapper);
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6440 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6442 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6443
6444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6445 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6447 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6448
6449 // move a little
6450 processMove(mapper, 150, 250);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6455 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6456
6457 // down when pressure is non-zero
6458 processPressure(mapper, RAW_PRESSURE_MAX);
6459 processSync(mapper);
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6461 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6463 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6464
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6466 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6468 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6469
6470 // up when pressure becomes 0, hover restored
6471 processPressure(mapper, 0);
6472 processSync(mapper);
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6474 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6476 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6477
6478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6479 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6481 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6482
6483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6484 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6486 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6487
6488 // exit hover when pointer goes away
6489 processUp(mapper);
6490 processSync(mapper);
6491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6492 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6494 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6495}
6496
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006497TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6498 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006499 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006500 prepareButtons();
6501 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006502 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006503
6504 // Touch down.
6505 processDown(mapper, 100, 200);
6506 processPressure(mapper, 1);
6507 processSync(mapper);
6508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6509 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6510
6511 // Reset the mapper. This should cancel the ongoing gesture.
6512 resetMapper(mapper, ARBITRARY_TIME);
6513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6514 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6515
6516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6517}
6518
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006519TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6520 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006521 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006522 prepareButtons();
6523 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006524 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006525
6526 // Set the initial state for the touch pointer.
6527 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6528 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6529 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6530 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6531
6532 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006533 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6534 // does not generate any events.
6535 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006536
6537 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6538 // the recreated touch state to generate a down event.
6539 processSync(mapper);
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6541 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6542
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6544}
6545
lilinnan687e58f2022-07-19 16:00:50 +08006546TEST_F(SingleTouchInputMapperTest,
6547 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6548 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006549 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006550 prepareButtons();
6551 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006552 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006553 NotifyMotionArgs motionArgs;
6554
6555 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006556 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006557 processSync(mapper);
6558
6559 // We should receive a down event
6560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6561 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6562
6563 // Change display id
6564 clearViewports();
6565 prepareSecondaryDisplay(ViewportType::INTERNAL);
6566
6567 // We should receive a cancel event
6568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6569 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6570 // Then receive reset called
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6572}
6573
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006574TEST_F(SingleTouchInputMapperTest,
6575 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6576 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006577 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006578 prepareButtons();
6579 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006580 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6582 NotifyMotionArgs motionArgs;
6583
6584 // Start a new gesture.
6585 processDown(mapper, 100, 200);
6586 processSync(mapper);
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6588 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6589
6590 // Make the viewport inactive. This will put the device in disabled mode.
6591 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6592 viewport->isActive = false;
6593 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006594 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006595
6596 // We should receive a cancel event for the ongoing gesture.
6597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6598 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6599 // Then we should be notified that the device was reset.
6600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6601
6602 // No events are generated while the viewport is inactive.
6603 processMove(mapper, 101, 201);
6604 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006605 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006606 processSync(mapper);
6607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6608
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006609 // Start a new gesture while the viewport is still inactive.
6610 processDown(mapper, 300, 400);
6611 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6612 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6613 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6614 processSync(mapper);
6615
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006616 // Make the viewport active again. The device should resume processing events.
6617 viewport->isActive = true;
6618 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006619 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006620
6621 // The device is reset because it changes back to direct mode, without generating any events.
6622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6624
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006625 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006626 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6628 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006629
6630 // No more events.
6631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6633}
6634
Prabir Pradhan211ba622022-10-31 21:09:21 +00006635TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6636 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006637 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006638 prepareButtons();
6639 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006640 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6642
6643 // Press a stylus button.
6644 processKey(mapper, BTN_STYLUS, 1);
6645 processSync(mapper);
6646
6647 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6648 processDown(mapper, 100, 200);
6649 processSync(mapper);
6650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6651 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6652 WithCoords(toDisplayX(100), toDisplayY(200)),
6653 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6655 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6656 WithCoords(toDisplayX(100), toDisplayY(200)),
6657 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6658
6659 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6660 // the button has not actually been released, since there will be no pointers through which the
6661 // button state can be reported. The event is generated at the location of the pointer before
6662 // it went up.
6663 processUp(mapper);
6664 processSync(mapper);
6665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6666 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6667 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6669 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6670 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6671}
6672
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006673TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6674 addConfigurationProperty("touch.deviceType", "touchScreen");
6675 prepareDisplay(ui::ROTATION_0);
6676 prepareButtons();
6677 prepareAxes(POSITION);
6678
6679 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6680
Arpit Singh56adebc2023-04-25 13:56:05 +00006681 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6683
6684 // Press a stylus button.
6685 processKey(mapper, BTN_STYLUS, 1);
6686 processSync(mapper);
6687
6688 // Start a touch gesture and ensure that the stylus button is not reported.
6689 processDown(mapper, 100, 200);
6690 processSync(mapper);
6691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6692 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6693
6694 // Release and press the stylus button again.
6695 processKey(mapper, BTN_STYLUS, 0);
6696 processSync(mapper);
6697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6698 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6699 processKey(mapper, BTN_STYLUS, 1);
6700 processSync(mapper);
6701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6702 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6703
6704 // Release the touch gesture.
6705 processUp(mapper);
6706 processSync(mapper);
6707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6708 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6709
6710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6711}
6712
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006713TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6714 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6715 prepareDisplay(ui::ROTATION_0);
6716 prepareButtons();
6717 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006718 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6720
6721 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6722}
6723
Seunghwan Choi356026c2023-02-01 14:37:25 +09006724TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6725 std::shared_ptr<FakePointerController> fakePointerController =
6726 std::make_shared<FakePointerController>();
6727 addConfigurationProperty("touch.deviceType", "touchScreen");
6728 prepareDisplay(ui::ROTATION_0);
6729 prepareButtons();
6730 prepareAxes(POSITION);
6731 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6732 mFakePolicy->setPointerController(fakePointerController);
6733 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singh56adebc2023-04-25 13:56:05 +00006734 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006735
6736 processKey(mapper, BTN_TOOL_PEN, 1);
6737 processMove(mapper, 100, 200);
6738 processSync(mapper);
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6740 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006741 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006742 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6743 ASSERT_TRUE(fakePointerController->isPointerShown());
6744 ASSERT_NO_FATAL_FAILURE(
6745 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6746}
6747
6748TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6749 std::shared_ptr<FakePointerController> fakePointerController =
6750 std::make_shared<FakePointerController>();
6751 addConfigurationProperty("touch.deviceType", "touchScreen");
6752 prepareDisplay(ui::ROTATION_0);
6753 prepareButtons();
6754 prepareAxes(POSITION);
6755 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6756 mFakePolicy->setPointerController(fakePointerController);
6757 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singh56adebc2023-04-25 13:56:05 +00006758 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006759
6760 processKey(mapper, BTN_TOOL_PEN, 1);
6761 processMove(mapper, 100, 200);
6762 processSync(mapper);
6763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6764 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006765 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006766 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6767 ASSERT_FALSE(fakePointerController->isPointerShown());
6768}
6769
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006770TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6771 // Initialize the device without setting device source to touch navigation.
6772 addConfigurationProperty("touch.deviceType", "touchScreen");
6773 prepareDisplay(ui::ROTATION_0);
6774 prepareButtons();
6775 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006776 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006777
6778 // Ensure that the device is created as a touchscreen, not touch navigation.
6779 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6780
6781 // Add device type association after the device was created.
6782 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6783
6784 // Send update to the mapper.
6785 std::list<NotifyArgs> unused2 =
6786 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006787 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006788
6789 // Check whether device type update was successful.
6790 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6791}
6792
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006793TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6794 // Initialize the device without setting device source to touch navigation.
6795 addConfigurationProperty("touch.deviceType", "touchScreen");
6796 prepareDisplay(ui::ROTATION_0);
6797 prepareButtons();
6798 prepareAxes(POSITION);
6799 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6800
6801 // Set a physical frame in the display viewport.
6802 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6803 viewport->physicalLeft = 0;
6804 viewport->physicalTop = 0;
6805 viewport->physicalRight = DISPLAY_WIDTH / 2;
6806 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6807 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006808 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006809
Arpit Singh56adebc2023-04-25 13:56:05 +00006810 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006811
6812 // Hovering inside the physical frame produces events.
6813 processKey(mapper, BTN_TOOL_PEN, 1);
6814 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6815 processSync(mapper);
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6817 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6819 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6820
6821 // Leaving the physical frame ends the hovering gesture.
6822 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6823 processSync(mapper);
6824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6825 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6826
6827 // Moving outside the physical frame does not produce events.
6828 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6829 processSync(mapper);
6830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6831
6832 // Re-entering the physical frame produces events.
6833 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6834 processSync(mapper);
6835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6836 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6838 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6839}
6840
Prabir Pradhan5632d622021-09-06 07:57:20 -07006841// --- TouchDisplayProjectionTest ---
6842
6843class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6844public:
6845 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6846 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6847 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006848 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6849 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6850 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006851 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006852 auto rotatedWidth = naturalDisplayWidth;
6853 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006854 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006855 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006856 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006857 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006858 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006859 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006860 inverseRotationFlags = ui::Transform::ROT_180;
6861 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006862 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006863 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006864 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006865 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006866 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006867 inverseRotationFlags = ui::Transform::ROT_0;
6868 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006869 }
6870
Prabir Pradhana9df3162022-12-05 23:57:27 +00006871 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006872 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6873
6874 std::optional<DisplayViewport> internalViewport =
6875 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6876 DisplayViewport& v = *internalViewport;
6877 v.displayId = DISPLAY_ID;
6878 v.orientation = orientation;
6879
6880 v.logicalLeft = 0;
6881 v.logicalTop = 0;
6882 v.logicalRight = 100;
6883 v.logicalBottom = 100;
6884
6885 v.physicalLeft = rotatedPhysicalDisplay.left;
6886 v.physicalTop = rotatedPhysicalDisplay.top;
6887 v.physicalRight = rotatedPhysicalDisplay.right;
6888 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6889
Prabir Pradhana9df3162022-12-05 23:57:27 +00006890 v.deviceWidth = rotatedWidth;
6891 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006892
6893 v.isActive = true;
6894 v.uniqueId = UNIQUE_ID;
6895 v.type = ViewportType::INTERNAL;
6896 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006897 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006898 }
6899
6900 void assertReceivedMove(const Point& point) {
6901 NotifyMotionArgs motionArgs;
6902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6904 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6906 1, 0, 0, 0, 0, 0, 0, 0));
6907 }
6908};
6909
6910TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6911 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006912 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006913
6914 prepareButtons();
6915 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006916 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006917
6918 NotifyMotionArgs motionArgs;
6919
6920 // Configure the DisplayViewport such that the logical display maps to a subsection of
6921 // the display panel called the physical display. Here, the physical display is bounded by the
6922 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6923 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6924 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6925 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6926
Michael Wrighta9cf4192022-12-01 23:46:39 +00006927 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006928 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6929
6930 // Touches outside the physical display should be ignored, and should not generate any
6931 // events. Ensure touches at the following points that lie outside of the physical display
6932 // area do not generate any events.
6933 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6934 processDown(mapper, toRawX(point.x), toRawY(point.y));
6935 processSync(mapper);
6936 processUp(mapper);
6937 processSync(mapper);
6938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6939 << "Unexpected event generated for touch outside physical display at point: "
6940 << point.x << ", " << point.y;
6941 }
6942 }
6943}
6944
6945TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6946 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006947 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006948
6949 prepareButtons();
6950 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006951 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006952
6953 NotifyMotionArgs motionArgs;
6954
6955 // Configure the DisplayViewport such that the logical display maps to a subsection of
6956 // the display panel called the physical display. Here, the physical display is bounded by the
6957 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6958 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6959
Michael Wrighta9cf4192022-12-01 23:46:39 +00006960 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006961 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6962
6963 // Touches that start outside the physical display should be ignored until it enters the
6964 // physical display bounds, at which point it should generate a down event. Start a touch at
6965 // the point (5, 100), which is outside the physical display bounds.
6966 static const Point kOutsidePoint{5, 100};
6967 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6968 processSync(mapper);
6969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6970
6971 // Move the touch into the physical display area. This should generate a pointer down.
6972 processMove(mapper, toRawX(11), toRawY(21));
6973 processSync(mapper);
6974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6975 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6976 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6977 ASSERT_NO_FATAL_FAILURE(
6978 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6979
6980 // Move the touch inside the physical display area. This should generate a pointer move.
6981 processMove(mapper, toRawX(69), toRawY(159));
6982 processSync(mapper);
6983 assertReceivedMove({69, 159});
6984
6985 // Move outside the physical display area. Since the pointer is already down, this should
6986 // now continue generating events.
6987 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6988 processSync(mapper);
6989 assertReceivedMove(kOutsidePoint);
6990
6991 // Release. This should generate a pointer up.
6992 processUp(mapper);
6993 processSync(mapper);
6994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6995 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6997 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6998
6999 // Ensure no more events were generated.
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7002 }
7003}
7004
Prabir Pradhana9df3162022-12-05 23:57:27 +00007005// --- TouchscreenPrecisionTests ---
7006
7007// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7008// in various orientations and with different display rotations. We configure the touchscreen to
7009// have a higher resolution than that of the display by an integer scale factor in each axis so that
7010// we can enforce that coordinates match precisely as expected.
7011class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7012 public ::testing::WithParamInterface<ui::Rotation> {
7013public:
7014 void SetUp() override {
7015 SingleTouchInputMapperTest::SetUp();
7016
7017 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7018 // four times the resolution of the display in the Y axis.
7019 prepareButtons();
7020 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007021 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7022 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007023 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007024 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7025 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007026 }
7027
7028 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7029 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7030 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7031 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7032
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007033 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7034 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7035
7036 static const int32_t PRECISION_RAW_X_FLAT = 16;
7037 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7038
7039 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7040 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7041
Prabir Pradhana9df3162022-12-05 23:57:27 +00007042 static const std::array<Point, 4> kRawCorners;
7043};
7044
7045const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7046 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7047 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7048 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7049 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7050}};
7051
7052// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7053// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7054// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7055TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7056 enum class Orientation {
7057 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7058 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7059 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7060 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7061 ftl_last = ORIENTATION_270,
7062 };
7063 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7064 Orientation::ORIENTATION_270;
7065 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7066 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7067 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7068 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7069 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7070 };
7071
7072 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7073
7074 // Configure the touchscreen as being installed in the one of the four different orientations
7075 // relative to the display.
7076 addConfigurationProperty("touch.deviceType", "touchScreen");
7077 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7078 prepareDisplay(ui::ROTATION_0);
7079
Arpit Singh56adebc2023-04-25 13:56:05 +00007080 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007081
7082 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7083 // orientations of either 90 or 270) this means the display's natural resolution will be
7084 // flipped.
7085 const bool displayRotated =
7086 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7087 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7088 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7089 const Rect physicalFrame{0, 0, width, height};
7090 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7091
7092 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7093 const float expectedPrecisionX = displayRotated ? 4 : 2;
7094 const float expectedPrecisionY = displayRotated ? 2 : 4;
7095
7096 // Test all four corners.
7097 for (int i = 0; i < 4; i++) {
7098 const auto& raw = kRawCorners[i];
7099 processDown(mapper, raw.x, raw.y);
7100 processSync(mapper);
7101 const auto& expected = expectedPoints[i];
7102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7103 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7104 WithCoords(expected.x, expected.y),
7105 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7106 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7107 << "with touchscreen orientation "
7108 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7109 << expected.x << ", " << expected.y << ").";
7110 processUp(mapper);
7111 processSync(mapper);
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7113 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7114 WithCoords(expected.x, expected.y))));
7115 }
7116}
7117
Prabir Pradhan82687402022-12-06 01:32:53 +00007118TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7119 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7120 kMappedCorners = {
7121 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7122 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7123 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7124 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7125 };
7126
7127 const ui::Rotation displayRotation = GetParam();
7128
7129 addConfigurationProperty("touch.deviceType", "touchScreen");
7130 prepareDisplay(displayRotation);
7131
Arpit Singh56adebc2023-04-25 13:56:05 +00007132 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007133
7134 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7135
7136 // Test all four corners.
7137 for (int i = 0; i < 4; i++) {
7138 const auto& expected = expectedPoints[i];
7139 const auto& raw = kRawCorners[i];
7140 processDown(mapper, raw.x, raw.y);
7141 processSync(mapper);
7142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7143 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7144 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7145 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7146 << "with display rotation " << ui::toCString(displayRotation)
7147 << ", expected point (" << expected.x << ", " << expected.y << ").";
7148 processUp(mapper);
7149 processSync(mapper);
7150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7151 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7152 WithCoords(expected.x, expected.y))));
7153 }
7154}
7155
Prabir Pradhan3e798762022-12-02 21:02:11 +00007156TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7157 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7158 kMappedCorners = {
7159 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7160 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7161 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7162 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7163 };
7164
7165 const ui::Rotation displayRotation = GetParam();
7166
7167 addConfigurationProperty("touch.deviceType", "touchScreen");
7168 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7169
Arpit Singh56adebc2023-04-25 13:56:05 +00007170 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007171
7172 // Ori 270, so width and height swapped
7173 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7174 prepareDisplay(displayRotation);
7175 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7176
7177 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7178
7179 // Test all four corners.
7180 for (int i = 0; i < 4; i++) {
7181 const auto& expected = expectedPoints[i];
7182 const auto& raw = kRawCorners[i];
7183 processDown(mapper, raw.x, raw.y);
7184 processSync(mapper);
7185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7186 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7187 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7188 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7189 << "with display rotation " << ui::toCString(displayRotation)
7190 << ", expected point (" << expected.x << ", " << expected.y << ").";
7191 processUp(mapper);
7192 processSync(mapper);
7193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7194 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7195 WithCoords(expected.x, expected.y))));
7196 }
7197}
7198
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007199TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7200 const ui::Rotation displayRotation = GetParam();
7201
7202 addConfigurationProperty("touch.deviceType", "touchScreen");
7203 prepareDisplay(displayRotation);
7204
7205 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singh56adebc2023-04-25 13:56:05 +00007206 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007207
7208 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7209 // MotionRanges use display pixels as their units
7210 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7211 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7212
7213 // The MotionRanges should be oriented in the rotated display's coordinate space
7214 const bool displayRotated =
7215 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7216
7217 constexpr float MAX_X = 479.5;
7218 constexpr float MAX_Y = 799.75;
7219 EXPECT_EQ(xRange->min, 0.f);
7220 EXPECT_EQ(yRange->min, 0.f);
7221 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7222 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7223
7224 EXPECT_EQ(xRange->flat, 8.f);
7225 EXPECT_EQ(yRange->flat, 8.f);
7226
7227 EXPECT_EQ(xRange->fuzz, 2.f);
7228 EXPECT_EQ(yRange->fuzz, 2.f);
7229
7230 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7231 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7232}
7233
Prabir Pradhana9df3162022-12-05 23:57:27 +00007234// Run the precision tests for all rotations.
7235INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7236 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7237 ui::ROTATION_270),
7238 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7239 return ftl::enum_string(testParamInfo.param);
7240 });
7241
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007242// --- ExternalStylusFusionTest ---
7243
7244class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7245public:
7246 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7247 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007248 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007249 prepareButtons();
7250 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00007251 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007252
7253 mStylusState.when = ARBITRARY_TIME;
7254 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007255 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007256 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007257 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007258 processExternalStylusState(mapper);
7259 return mapper;
7260 }
7261
7262 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7263 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7264 for (const NotifyArgs& args : generatedArgs) {
7265 mFakeListener->notify(args);
7266 }
7267 // Loop the reader to flush the input listener queue.
7268 mReader->loopOnce();
7269 return generatedArgs;
7270 }
7271
7272protected:
7273 StylusState mStylusState{};
7274 static constexpr uint32_t EXPECTED_SOURCE =
7275 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7276
7277 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7278 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007279 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007280
7281 // The first pointer is withheld.
7282 processDown(mapper, 100, 200);
7283 processSync(mapper);
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7285 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7286 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7287
7288 // The external stylus reports pressure. The withheld finger pointer is released as a
7289 // stylus.
7290 mStylusState.pressure = 1.f;
7291 processExternalStylusState(mapper);
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7293 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7294 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7295
7296 // Subsequent pointer events are not withheld.
7297 processMove(mapper, 101, 201);
7298 processSync(mapper);
7299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7300 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7301
7302 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7304 }
7305
7306 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7307 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7308
7309 // Releasing the touch pointer ends the gesture.
7310 processUp(mapper);
7311 processSync(mapper);
7312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7313 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007314 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007315
7316 mStylusState.pressure = 0.f;
7317 processExternalStylusState(mapper);
7318 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7320 }
7321
7322 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7323 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007324 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007325
7326 // The first pointer is withheld when an external stylus is connected,
7327 // and a timeout is requested.
7328 processDown(mapper, 100, 200);
7329 processSync(mapper);
7330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7331 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7332 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7333
7334 // If the timeout expires early, it is requested again.
7335 handleTimeout(mapper, ARBITRARY_TIME + 1);
7336 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7337 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7338
7339 // When the timeout expires, the withheld touch is released as a finger pointer.
7340 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7342 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7343
7344 // Subsequent pointer events are not withheld.
7345 processMove(mapper, 101, 201);
7346 processSync(mapper);
7347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7348 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7349 processUp(mapper);
7350 processSync(mapper);
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7352 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7353
7354 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7356 }
7357
7358private:
7359 InputDeviceInfo mExternalStylusDeviceInfo{};
7360};
7361
7362TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7363 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7364 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7365}
7366
7367TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7368 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7369 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7370}
7371
7372TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7373 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7374 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7375}
7376
7377// Test a successful stylus fusion gesture where the pressure is reported by the external
7378// before the touch is reported by the touchscreen.
7379TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7380 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7381 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007382 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007383
7384 // The external stylus reports pressure first. It is ignored for now.
7385 mStylusState.pressure = 1.f;
7386 processExternalStylusState(mapper);
7387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7388 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7389
7390 // When the touch goes down afterwards, it is reported as a stylus pointer.
7391 processDown(mapper, 100, 200);
7392 processSync(mapper);
7393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7394 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7395 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7396
7397 processMove(mapper, 101, 201);
7398 processSync(mapper);
7399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7400 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7401 processUp(mapper);
7402 processSync(mapper);
7403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7404 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7405
7406 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7408}
7409
7410TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7411 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7412
7413 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7414 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7415
7416 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7417 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7418 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7419 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7420}
7421
7422TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7423 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7424 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007425 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007426
7427 mStylusState.pressure = 0.8f;
7428 processExternalStylusState(mapper);
7429 processDown(mapper, 100, 200);
7430 processSync(mapper);
7431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7432 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7433 WithPressure(0.8f))));
7434 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7435
7436 // The external stylus reports a pressure change. We wait for some time for a touch event.
7437 mStylusState.pressure = 0.6f;
7438 processExternalStylusState(mapper);
7439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7440 ASSERT_NO_FATAL_FAILURE(
7441 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7442
7443 // If a touch is reported within the timeout, it reports the updated pressure.
7444 processMove(mapper, 101, 201);
7445 processSync(mapper);
7446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7447 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7448 WithPressure(0.6f))));
7449 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7450
7451 // There is another pressure change.
7452 mStylusState.pressure = 0.5f;
7453 processExternalStylusState(mapper);
7454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7455 ASSERT_NO_FATAL_FAILURE(
7456 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7457
7458 // If a touch is not reported within the timeout, a move event is generated to report
7459 // the new pressure.
7460 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7462 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7463 WithPressure(0.5f))));
7464
7465 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7466 // repeated indefinitely.
7467 mStylusState.pressure = 0.0f;
7468 processExternalStylusState(mapper);
7469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7470 ASSERT_NO_FATAL_FAILURE(
7471 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7472 processMove(mapper, 102, 202);
7473 processSync(mapper);
7474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7475 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7476 WithPressure(0.5f))));
7477 processMove(mapper, 103, 203);
7478 processSync(mapper);
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7480 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7481 WithPressure(0.5f))));
7482
7483 processUp(mapper);
7484 processSync(mapper);
7485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7486 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007487 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007488
7489 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7491}
7492
7493TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7494 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7495 auto source = WithSource(EXPECTED_SOURCE);
7496
7497 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007498 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007499 processExternalStylusState(mapper);
7500 processDown(mapper, 100, 200);
7501 processSync(mapper);
7502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7503 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007504 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007505 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7506
7507 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007508 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007509 processExternalStylusState(mapper);
7510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7511 ASSERT_NO_FATAL_FAILURE(
7512 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7513
7514 // If a touch is reported within the timeout, it reports the updated pressure.
7515 processMove(mapper, 101, 201);
7516 processSync(mapper);
7517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7518 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007519 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007520 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7521
7522 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007523 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007524 processExternalStylusState(mapper);
7525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7526 ASSERT_NO_FATAL_FAILURE(
7527 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7528
7529 // If a touch is not reported within the timeout, a move event is generated to report
7530 // the new tool type.
7531 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7533 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007534 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007535
7536 processUp(mapper);
7537 processSync(mapper);
7538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7539 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007540 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007541
7542 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7544}
7545
7546TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7547 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7548 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007549 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007550
7551 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7552
7553 // The external stylus reports a button change. We wait for some time for a touch event.
7554 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7555 processExternalStylusState(mapper);
7556 ASSERT_NO_FATAL_FAILURE(
7557 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7558
7559 // If a touch is reported within the timeout, it reports the updated button state.
7560 processMove(mapper, 101, 201);
7561 processSync(mapper);
7562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7563 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7564 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7566 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7567 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7568 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7569
7570 // The button is now released.
7571 mStylusState.buttons = 0;
7572 processExternalStylusState(mapper);
7573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7574 ASSERT_NO_FATAL_FAILURE(
7575 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7576
7577 // If a touch is not reported within the timeout, a move event is generated to report
7578 // the new button state.
7579 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7581 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7582 WithButtonState(0))));
7583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007584 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7585 WithButtonState(0))));
7586
7587 processUp(mapper);
7588 processSync(mapper);
7589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007590 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7591
7592 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7594}
7595
Michael Wrightd02c5b62014-02-10 15:10:22 -08007596// --- MultiTouchInputMapperTest ---
7597
7598class MultiTouchInputMapperTest : public TouchInputMapperTest {
7599protected:
7600 void prepareAxes(int axes);
7601
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007602 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7603 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7604 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7605 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7606 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7607 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7608 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7609 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7610 void processId(MultiTouchInputMapper& mapper, int32_t id);
7611 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7612 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7613 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007614 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007615 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007616 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7617 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007618};
7619
7620void MultiTouchInputMapperTest::prepareAxes(int axes) {
7621 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007622 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7623 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007624 }
7625 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007626 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7627 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007628 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007629 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7630 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007631 }
7632 }
7633 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007634 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7635 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007636 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007637 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007638 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007639 }
7640 }
7641 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007642 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7643 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007644 }
7645 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007646 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7647 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007648 }
7649 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007650 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7651 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007652 }
7653 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007654 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7655 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007656 }
7657 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007658 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7659 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007660 }
7661 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007662 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007663 }
7664}
7665
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007666void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7667 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007670}
7671
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007672void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7673 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007675}
7676
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007677void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7678 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007679 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007680}
7681
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007682void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007684}
7685
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007686void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007688}
7689
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007690void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7691 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007693}
7694
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007695void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007697}
7698
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007699void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007701}
7702
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007703void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007705}
7706
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007707void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007709}
7710
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007711void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007713}
7714
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007715void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7716 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007717 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007718}
7719
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007720void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7721 int32_t value) {
7722 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7724}
7725
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007726void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007728}
7729
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007730void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7731 nsecs_t readTime) {
7732 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007733}
7734
Michael Wrightd02c5b62014-02-10 15:10:22 -08007735TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007736 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007737 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007738 prepareAxes(POSITION);
7739 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00007740 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007741
arthurhungdcef2dc2020-08-11 14:47:50 +08007742 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007743
7744 NotifyMotionArgs motionArgs;
7745
7746 // Two fingers down at once.
7747 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7748 processPosition(mapper, x1, y1);
7749 processMTSync(mapper);
7750 processPosition(mapper, x2, y2);
7751 processMTSync(mapper);
7752 processSync(mapper);
7753
7754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7755 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7756 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7757 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7758 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7759 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7760 ASSERT_EQ(0, motionArgs.flags);
7761 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7762 ASSERT_EQ(0, motionArgs.buttonState);
7763 ASSERT_EQ(0, motionArgs.edgeFlags);
7764 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7765 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007766 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7768 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7769 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7770 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7771 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7772
7773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7774 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7775 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7776 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7777 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007778 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007779 ASSERT_EQ(0, motionArgs.flags);
7780 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7781 ASSERT_EQ(0, motionArgs.buttonState);
7782 ASSERT_EQ(0, motionArgs.edgeFlags);
7783 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7784 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007785 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007786 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007787 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7789 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7791 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7792 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7793 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7794 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7795
7796 // Move.
7797 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7798 processPosition(mapper, x1, y1);
7799 processMTSync(mapper);
7800 processPosition(mapper, x2, y2);
7801 processMTSync(mapper);
7802 processSync(mapper);
7803
7804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7805 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7806 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7807 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7808 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7809 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7810 ASSERT_EQ(0, motionArgs.flags);
7811 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7812 ASSERT_EQ(0, motionArgs.buttonState);
7813 ASSERT_EQ(0, motionArgs.edgeFlags);
7814 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7815 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007816 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007817 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007818 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7820 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7822 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7823 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7824 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7825 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7826
7827 // First finger up.
7828 x2 += 15; y2 -= 20;
7829 processPosition(mapper, x2, y2);
7830 processMTSync(mapper);
7831 processSync(mapper);
7832
7833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7834 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7835 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7836 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7837 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007838 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007839 ASSERT_EQ(0, motionArgs.flags);
7840 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7841 ASSERT_EQ(0, motionArgs.buttonState);
7842 ASSERT_EQ(0, motionArgs.edgeFlags);
7843 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7844 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007845 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007846 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007847 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7849 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7851 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7852 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7853 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7854 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7855
7856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7857 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7858 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7859 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7860 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7861 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7862 ASSERT_EQ(0, motionArgs.flags);
7863 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7864 ASSERT_EQ(0, motionArgs.buttonState);
7865 ASSERT_EQ(0, motionArgs.edgeFlags);
7866 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7867 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007868 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7870 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7871 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7872 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7873 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7874
7875 // Move.
7876 x2 += 20; y2 -= 25;
7877 processPosition(mapper, x2, y2);
7878 processMTSync(mapper);
7879 processSync(mapper);
7880
7881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7882 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7883 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7884 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7885 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7886 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7887 ASSERT_EQ(0, motionArgs.flags);
7888 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7889 ASSERT_EQ(0, motionArgs.buttonState);
7890 ASSERT_EQ(0, motionArgs.edgeFlags);
7891 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7892 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007893 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7895 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7896 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7897 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7898 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7899
7900 // New finger down.
7901 int32_t x3 = 700, y3 = 300;
7902 processPosition(mapper, x2, y2);
7903 processMTSync(mapper);
7904 processPosition(mapper, x3, y3);
7905 processMTSync(mapper);
7906 processSync(mapper);
7907
7908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7909 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7910 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7911 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7912 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007913 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007914 ASSERT_EQ(0, motionArgs.flags);
7915 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7916 ASSERT_EQ(0, motionArgs.buttonState);
7917 ASSERT_EQ(0, motionArgs.edgeFlags);
7918 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7919 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007920 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007921 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007922 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7924 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7926 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7927 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7928 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7929 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7930
7931 // Second finger up.
7932 x3 += 30; y3 -= 20;
7933 processPosition(mapper, x3, y3);
7934 processMTSync(mapper);
7935 processSync(mapper);
7936
7937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7938 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7939 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7940 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7941 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007942 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007943 ASSERT_EQ(0, motionArgs.flags);
7944 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7945 ASSERT_EQ(0, motionArgs.buttonState);
7946 ASSERT_EQ(0, motionArgs.edgeFlags);
7947 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7948 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007949 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007950 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007951 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007952 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7953 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7955 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7956 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7957 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7958 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7959
7960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7961 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7962 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7963 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7964 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7966 ASSERT_EQ(0, motionArgs.flags);
7967 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7968 ASSERT_EQ(0, motionArgs.buttonState);
7969 ASSERT_EQ(0, motionArgs.edgeFlags);
7970 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7971 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007972 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7974 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7975 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7976 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7977 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7978
7979 // Last finger up.
7980 processMTSync(mapper);
7981 processSync(mapper);
7982
7983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7984 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7985 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7986 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7987 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7988 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7989 ASSERT_EQ(0, motionArgs.flags);
7990 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7991 ASSERT_EQ(0, motionArgs.buttonState);
7992 ASSERT_EQ(0, motionArgs.edgeFlags);
7993 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7994 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007995 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7997 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7998 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7999 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8000 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8001
8002 // Should not have sent any more keys or motions.
8003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8005}
8006
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008007TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8008 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008009 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008010
8011 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8012 /*fuzz*/ 0, /*resolution*/ 10);
8013 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8014 /*fuzz*/ 0, /*resolution*/ 11);
8015 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8016 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8017 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8018 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8019 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8020 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8021 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8022 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8023
Arpit Singh56adebc2023-04-25 13:56:05 +00008024 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008025
8026 // X and Y axes
8027 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8028 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8029 // Touch major and minor
8030 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8031 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8032 // Tool major and minor
8033 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8034 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8035}
8036
8037TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8038 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008039 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008040
8041 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8042 /*fuzz*/ 0, /*resolution*/ 10);
8043 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8044 /*fuzz*/ 0, /*resolution*/ 11);
8045
8046 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8047
Arpit Singh56adebc2023-04-25 13:56:05 +00008048 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008049
8050 // Touch major and minor
8051 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8052 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8053 // Tool major and minor
8054 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8055 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8056}
8057
Michael Wrightd02c5b62014-02-10 15:10:22 -08008058TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008059 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008060 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008061 prepareAxes(POSITION | ID);
8062 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00008063 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008064
arthurhungdcef2dc2020-08-11 14:47:50 +08008065 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008066
8067 NotifyMotionArgs motionArgs;
8068
8069 // Two fingers down at once.
8070 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8071 processPosition(mapper, x1, y1);
8072 processId(mapper, 1);
8073 processMTSync(mapper);
8074 processPosition(mapper, x2, y2);
8075 processId(mapper, 2);
8076 processMTSync(mapper);
8077 processSync(mapper);
8078
8079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8080 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8081 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8082 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008083 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8085 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8086
8087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008088 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008089 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8090 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008091 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008093 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8095 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8097 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8098
8099 // Move.
8100 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8101 processPosition(mapper, x1, y1);
8102 processId(mapper, 1);
8103 processMTSync(mapper);
8104 processPosition(mapper, x2, y2);
8105 processId(mapper, 2);
8106 processMTSync(mapper);
8107 processSync(mapper);
8108
8109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8111 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8112 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008113 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008114 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008115 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8117 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8119 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8120
8121 // First finger up.
8122 x2 += 15; y2 -= 20;
8123 processPosition(mapper, x2, y2);
8124 processId(mapper, 2);
8125 processMTSync(mapper);
8126 processSync(mapper);
8127
8128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008129 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008130 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8131 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008132 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008133 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008134 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008135 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8136 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8138 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8139
8140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8142 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8143 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008144 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8146 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8147
8148 // Move.
8149 x2 += 20; y2 -= 25;
8150 processPosition(mapper, x2, y2);
8151 processId(mapper, 2);
8152 processMTSync(mapper);
8153 processSync(mapper);
8154
8155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8157 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8158 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008159 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8161 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8162
8163 // New finger down.
8164 int32_t x3 = 700, y3 = 300;
8165 processPosition(mapper, x2, y2);
8166 processId(mapper, 2);
8167 processMTSync(mapper);
8168 processPosition(mapper, x3, y3);
8169 processId(mapper, 3);
8170 processMTSync(mapper);
8171 processSync(mapper);
8172
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008174 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008175 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8176 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008177 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008179 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8181 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8183 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8184
8185 // Second finger up.
8186 x3 += 30; y3 -= 20;
8187 processPosition(mapper, x3, y3);
8188 processId(mapper, 3);
8189 processMTSync(mapper);
8190 processSync(mapper);
8191
8192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008193 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008194 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8195 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008196 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008197 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008198 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8200 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8202 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8203
8204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8206 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8207 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008208 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8210 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8211
8212 // Last finger up.
8213 processMTSync(mapper);
8214 processSync(mapper);
8215
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8217 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8218 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8219 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008220 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008221 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8222 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8223
8224 // Should not have sent any more keys or motions.
8225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8227}
8228
8229TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008230 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008231 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008232 prepareAxes(POSITION | ID | SLOT);
8233 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00008234 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008235
arthurhungdcef2dc2020-08-11 14:47:50 +08008236 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008237
8238 NotifyMotionArgs motionArgs;
8239
8240 // Two fingers down at once.
8241 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8242 processPosition(mapper, x1, y1);
8243 processId(mapper, 1);
8244 processSlot(mapper, 1);
8245 processPosition(mapper, x2, y2);
8246 processId(mapper, 2);
8247 processSync(mapper);
8248
8249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8250 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8251 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8252 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008253 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8255 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8256
8257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008258 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008259 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8260 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008261 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008262 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008263 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8265 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8267 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8268
8269 // Move.
8270 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8271 processSlot(mapper, 0);
8272 processPosition(mapper, x1, y1);
8273 processSlot(mapper, 1);
8274 processPosition(mapper, x2, y2);
8275 processSync(mapper);
8276
8277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8279 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8280 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008281 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008282 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008283 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8285 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8287 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8288
8289 // First finger up.
8290 x2 += 15; y2 -= 20;
8291 processSlot(mapper, 0);
8292 processId(mapper, -1);
8293 processSlot(mapper, 1);
8294 processPosition(mapper, x2, y2);
8295 processSync(mapper);
8296
8297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008298 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008299 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8300 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008301 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008302 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008303 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8305 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8307 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8308
8309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8311 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8312 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008313 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8315 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8316
8317 // Move.
8318 x2 += 20; y2 -= 25;
8319 processPosition(mapper, x2, y2);
8320 processSync(mapper);
8321
8322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8324 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8325 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008326 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8328 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8329
8330 // New finger down.
8331 int32_t x3 = 700, y3 = 300;
8332 processPosition(mapper, x2, y2);
8333 processSlot(mapper, 0);
8334 processId(mapper, 3);
8335 processPosition(mapper, x3, y3);
8336 processSync(mapper);
8337
8338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008339 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008340 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8341 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008342 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008343 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008344 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8346 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8348 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8349
8350 // Second finger up.
8351 x3 += 30; y3 -= 20;
8352 processSlot(mapper, 1);
8353 processId(mapper, -1);
8354 processSlot(mapper, 0);
8355 processPosition(mapper, x3, y3);
8356 processSync(mapper);
8357
8358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008359 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008360 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8361 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008362 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008363 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008364 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8366 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8368 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8369
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8372 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8373 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008374 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8376 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8377
8378 // Last finger up.
8379 processId(mapper, -1);
8380 processSync(mapper);
8381
8382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8383 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8384 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8385 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008386 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8388 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8389
8390 // Should not have sent any more keys or motions.
8391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8393}
8394
8395TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008396 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008397 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008398 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singh56adebc2023-04-25 13:56:05 +00008399 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008400
8401 // These calculations are based on the input device calibration documentation.
8402 int32_t rawX = 100;
8403 int32_t rawY = 200;
8404 int32_t rawTouchMajor = 7;
8405 int32_t rawTouchMinor = 6;
8406 int32_t rawToolMajor = 9;
8407 int32_t rawToolMinor = 8;
8408 int32_t rawPressure = 11;
8409 int32_t rawDistance = 0;
8410 int32_t rawOrientation = 3;
8411 int32_t id = 5;
8412
8413 float x = toDisplayX(rawX);
8414 float y = toDisplayY(rawY);
8415 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8416 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8417 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8418 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8419 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8420 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8421 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8422 float distance = float(rawDistance);
8423
8424 processPosition(mapper, rawX, rawY);
8425 processTouchMajor(mapper, rawTouchMajor);
8426 processTouchMinor(mapper, rawTouchMinor);
8427 processToolMajor(mapper, rawToolMajor);
8428 processToolMinor(mapper, rawToolMinor);
8429 processPressure(mapper, rawPressure);
8430 processOrientation(mapper, rawOrientation);
8431 processDistance(mapper, rawDistance);
8432 processId(mapper, id);
8433 processMTSync(mapper);
8434 processSync(mapper);
8435
8436 NotifyMotionArgs args;
8437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8438 ASSERT_EQ(0, args.pointerProperties[0].id);
8439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8440 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8441 orientation, distance));
8442}
8443
8444TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008445 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008446 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008447 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8448 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singh56adebc2023-04-25 13:56:05 +00008449 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008450
8451 // These calculations are based on the input device calibration documentation.
8452 int32_t rawX = 100;
8453 int32_t rawY = 200;
8454 int32_t rawTouchMajor = 140;
8455 int32_t rawTouchMinor = 120;
8456 int32_t rawToolMajor = 180;
8457 int32_t rawToolMinor = 160;
8458
8459 float x = toDisplayX(rawX);
8460 float y = toDisplayY(rawY);
8461 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8462 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8463 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8464 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8465 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8466
8467 processPosition(mapper, rawX, rawY);
8468 processTouchMajor(mapper, rawTouchMajor);
8469 processTouchMinor(mapper, rawTouchMinor);
8470 processToolMajor(mapper, rawToolMajor);
8471 processToolMinor(mapper, rawToolMinor);
8472 processMTSync(mapper);
8473 processSync(mapper);
8474
8475 NotifyMotionArgs args;
8476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8478 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8479}
8480
8481TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008482 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008483 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008484 prepareAxes(POSITION | TOUCH | TOOL);
8485 addConfigurationProperty("touch.size.calibration", "diameter");
8486 addConfigurationProperty("touch.size.scale", "10");
8487 addConfigurationProperty("touch.size.bias", "160");
8488 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singh56adebc2023-04-25 13:56:05 +00008489 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008490
8491 // These calculations are based on the input device calibration documentation.
8492 // Note: We only provide a single common touch/tool value because the device is assumed
8493 // not to emit separate values for each pointer (isSummed = 1).
8494 int32_t rawX = 100;
8495 int32_t rawY = 200;
8496 int32_t rawX2 = 150;
8497 int32_t rawY2 = 250;
8498 int32_t rawTouchMajor = 5;
8499 int32_t rawToolMajor = 8;
8500
8501 float x = toDisplayX(rawX);
8502 float y = toDisplayY(rawY);
8503 float x2 = toDisplayX(rawX2);
8504 float y2 = toDisplayY(rawY2);
8505 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8506 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8507 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8508
8509 processPosition(mapper, rawX, rawY);
8510 processTouchMajor(mapper, rawTouchMajor);
8511 processToolMajor(mapper, rawToolMajor);
8512 processMTSync(mapper);
8513 processPosition(mapper, rawX2, rawY2);
8514 processTouchMajor(mapper, rawTouchMajor);
8515 processToolMajor(mapper, rawToolMajor);
8516 processMTSync(mapper);
8517 processSync(mapper);
8518
8519 NotifyMotionArgs args;
8520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8521 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8522
8523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008524 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008525 ASSERT_EQ(size_t(2), args.pointerCount);
8526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8527 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8529 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8530}
8531
8532TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008533 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008534 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008535 prepareAxes(POSITION | TOUCH | TOOL);
8536 addConfigurationProperty("touch.size.calibration", "area");
8537 addConfigurationProperty("touch.size.scale", "43");
8538 addConfigurationProperty("touch.size.bias", "3");
Arpit Singh56adebc2023-04-25 13:56:05 +00008539 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008540
8541 // These calculations are based on the input device calibration documentation.
8542 int32_t rawX = 100;
8543 int32_t rawY = 200;
8544 int32_t rawTouchMajor = 5;
8545 int32_t rawToolMajor = 8;
8546
8547 float x = toDisplayX(rawX);
8548 float y = toDisplayY(rawY);
8549 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8550 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8551 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8552
8553 processPosition(mapper, rawX, rawY);
8554 processTouchMajor(mapper, rawTouchMajor);
8555 processToolMajor(mapper, rawToolMajor);
8556 processMTSync(mapper);
8557 processSync(mapper);
8558
8559 NotifyMotionArgs args;
8560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8562 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8563}
8564
8565TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008566 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008567 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008568 prepareAxes(POSITION | PRESSURE);
8569 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8570 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singh56adebc2023-04-25 13:56:05 +00008571 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008572
Michael Wrightaa449c92017-12-13 21:21:43 +00008573 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008574 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008575 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8576 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8577 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8578
Michael Wrightd02c5b62014-02-10 15:10:22 -08008579 // These calculations are based on the input device calibration documentation.
8580 int32_t rawX = 100;
8581 int32_t rawY = 200;
8582 int32_t rawPressure = 60;
8583
8584 float x = toDisplayX(rawX);
8585 float y = toDisplayY(rawY);
8586 float pressure = float(rawPressure) * 0.01f;
8587
8588 processPosition(mapper, rawX, rawY);
8589 processPressure(mapper, rawPressure);
8590 processMTSync(mapper);
8591 processSync(mapper);
8592
8593 NotifyMotionArgs args;
8594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8596 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8597}
8598
8599TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008600 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008601 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008602 prepareAxes(POSITION | ID | SLOT);
Arpit Singh56adebc2023-04-25 13:56:05 +00008603 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008604
8605 NotifyMotionArgs motionArgs;
8606 NotifyKeyArgs keyArgs;
8607
8608 processId(mapper, 1);
8609 processPosition(mapper, 100, 200);
8610 processSync(mapper);
8611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8612 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8613 ASSERT_EQ(0, motionArgs.buttonState);
8614
8615 // press BTN_LEFT, release BTN_LEFT
8616 processKey(mapper, BTN_LEFT, 1);
8617 processSync(mapper);
8618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8620 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8621
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8623 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8624 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8625
Michael Wrightd02c5b62014-02-10 15:10:22 -08008626 processKey(mapper, BTN_LEFT, 0);
8627 processSync(mapper);
8628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008629 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008630 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008631
8632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008634 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008635
8636 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8637 processKey(mapper, BTN_RIGHT, 1);
8638 processKey(mapper, BTN_MIDDLE, 1);
8639 processSync(mapper);
8640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8642 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8643 motionArgs.buttonState);
8644
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8646 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8647 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8648
8649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8650 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8651 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8652 motionArgs.buttonState);
8653
Michael Wrightd02c5b62014-02-10 15:10:22 -08008654 processKey(mapper, BTN_RIGHT, 0);
8655 processSync(mapper);
8656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008657 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008658 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008659
8660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008661 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008662 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008663
8664 processKey(mapper, BTN_MIDDLE, 0);
8665 processSync(mapper);
8666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008668 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008669
8670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008671 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008672 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008673
8674 // press BTN_BACK, release BTN_BACK
8675 processKey(mapper, BTN_BACK, 1);
8676 processSync(mapper);
8677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8678 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8679 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008680
Michael Wrightd02c5b62014-02-10 15:10:22 -08008681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008682 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008683 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8684
8685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8686 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8687 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008688
8689 processKey(mapper, BTN_BACK, 0);
8690 processSync(mapper);
8691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008692 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008693 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008694
8695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008697 ASSERT_EQ(0, motionArgs.buttonState);
8698
Michael Wrightd02c5b62014-02-10 15:10:22 -08008699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8700 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8701 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8702
8703 // press BTN_SIDE, release BTN_SIDE
8704 processKey(mapper, BTN_SIDE, 1);
8705 processSync(mapper);
8706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8707 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8708 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008709
Michael Wrightd02c5b62014-02-10 15:10:22 -08008710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008712 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8713
8714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8715 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8716 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008717
8718 processKey(mapper, BTN_SIDE, 0);
8719 processSync(mapper);
8720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008721 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008722 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008723
8724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008726 ASSERT_EQ(0, motionArgs.buttonState);
8727
Michael Wrightd02c5b62014-02-10 15:10:22 -08008728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8729 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8730 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8731
8732 // press BTN_FORWARD, release BTN_FORWARD
8733 processKey(mapper, BTN_FORWARD, 1);
8734 processSync(mapper);
8735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8736 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8737 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008738
Michael Wrightd02c5b62014-02-10 15:10:22 -08008739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008741 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8742
8743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8744 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8745 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008746
8747 processKey(mapper, BTN_FORWARD, 0);
8748 processSync(mapper);
8749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008751 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008752
8753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008754 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008755 ASSERT_EQ(0, motionArgs.buttonState);
8756
Michael Wrightd02c5b62014-02-10 15:10:22 -08008757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8758 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8759 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8760
8761 // press BTN_EXTRA, release BTN_EXTRA
8762 processKey(mapper, BTN_EXTRA, 1);
8763 processSync(mapper);
8764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8765 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8766 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008767
Michael Wrightd02c5b62014-02-10 15:10:22 -08008768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008769 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008770 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8771
8772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8773 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8774 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008775
8776 processKey(mapper, BTN_EXTRA, 0);
8777 processSync(mapper);
8778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008780 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008781
8782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008784 ASSERT_EQ(0, motionArgs.buttonState);
8785
Michael Wrightd02c5b62014-02-10 15:10:22 -08008786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8787 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8788 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8789
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8791
Michael Wrightd02c5b62014-02-10 15:10:22 -08008792 // press BTN_STYLUS, release BTN_STYLUS
8793 processKey(mapper, BTN_STYLUS, 1);
8794 processSync(mapper);
8795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008797 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8798
8799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8801 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008802
8803 processKey(mapper, BTN_STYLUS, 0);
8804 processSync(mapper);
8805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008806 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008807 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008808
8809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008811 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008812
8813 // press BTN_STYLUS2, release BTN_STYLUS2
8814 processKey(mapper, BTN_STYLUS2, 1);
8815 processSync(mapper);
8816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008818 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8819
8820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8821 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8822 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008823
8824 processKey(mapper, BTN_STYLUS2, 0);
8825 processSync(mapper);
8826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008827 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008828 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008829
8830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008832 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008833
8834 // release touch
8835 processId(mapper, -1);
8836 processSync(mapper);
8837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8838 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8839 ASSERT_EQ(0, motionArgs.buttonState);
8840}
8841
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008842TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8843 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008844 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008845 prepareAxes(POSITION | ID | SLOT);
Arpit Singh56adebc2023-04-25 13:56:05 +00008846 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008847
8848 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8849 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8850
8851 // Touch down.
8852 processId(mapper, 1);
8853 processPosition(mapper, 100, 200);
8854 processSync(mapper);
8855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8856 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8857
8858 // Press and release button mapped to the primary stylus button.
8859 processKey(mapper, BTN_A, 1);
8860 processSync(mapper);
8861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8862 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8863 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8865 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8866 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8867
8868 processKey(mapper, BTN_A, 0);
8869 processSync(mapper);
8870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8871 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8873 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8874
8875 // Press and release the HID usage mapped to the secondary stylus button.
8876 processHidUsage(mapper, 0xabcd, 1);
8877 processSync(mapper);
8878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8879 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8880 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8882 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8883 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8884
8885 processHidUsage(mapper, 0xabcd, 0);
8886 processSync(mapper);
8887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8888 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8890 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8891
8892 // Release touch.
8893 processId(mapper, -1);
8894 processSync(mapper);
8895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8896 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8897}
8898
Michael Wrightd02c5b62014-02-10 15:10:22 -08008899TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008900 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008901 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008902 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00008903 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008904
8905 NotifyMotionArgs motionArgs;
8906
8907 // default tool type is finger
8908 processId(mapper, 1);
8909 processPosition(mapper, 100, 200);
8910 processSync(mapper);
8911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8912 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008913 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008914
8915 // eraser
8916 processKey(mapper, BTN_TOOL_RUBBER, 1);
8917 processSync(mapper);
8918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008920 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008921
8922 // stylus
8923 processKey(mapper, BTN_TOOL_RUBBER, 0);
8924 processKey(mapper, BTN_TOOL_PEN, 1);
8925 processSync(mapper);
8926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8927 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008928 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008929
8930 // brush
8931 processKey(mapper, BTN_TOOL_PEN, 0);
8932 processKey(mapper, BTN_TOOL_BRUSH, 1);
8933 processSync(mapper);
8934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008936 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008937
8938 // pencil
8939 processKey(mapper, BTN_TOOL_BRUSH, 0);
8940 processKey(mapper, BTN_TOOL_PENCIL, 1);
8941 processSync(mapper);
8942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008944 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008945
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008946 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008947 processKey(mapper, BTN_TOOL_PENCIL, 0);
8948 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8949 processSync(mapper);
8950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008952 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008953
8954 // mouse
8955 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8956 processKey(mapper, BTN_TOOL_MOUSE, 1);
8957 processSync(mapper);
8958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008960 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008961
8962 // lens
8963 processKey(mapper, BTN_TOOL_MOUSE, 0);
8964 processKey(mapper, BTN_TOOL_LENS, 1);
8965 processSync(mapper);
8966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008968 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008969
8970 // double-tap
8971 processKey(mapper, BTN_TOOL_LENS, 0);
8972 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8973 processSync(mapper);
8974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008976 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008977
8978 // triple-tap
8979 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8980 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8981 processSync(mapper);
8982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008984 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008985
8986 // quad-tap
8987 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8988 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8989 processSync(mapper);
8990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008992 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008993
8994 // finger
8995 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8996 processKey(mapper, BTN_TOOL_FINGER, 1);
8997 processSync(mapper);
8998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009000 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009001
9002 // stylus trumps finger
9003 processKey(mapper, BTN_TOOL_PEN, 1);
9004 processSync(mapper);
9005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009007 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009008
9009 // eraser trumps stylus
9010 processKey(mapper, BTN_TOOL_RUBBER, 1);
9011 processSync(mapper);
9012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009014 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009015
9016 // mouse trumps eraser
9017 processKey(mapper, BTN_TOOL_MOUSE, 1);
9018 processSync(mapper);
9019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009021 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009022
9023 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9024 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9025 processSync(mapper);
9026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9027 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009028 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009029
9030 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9031 processToolType(mapper, MT_TOOL_PEN);
9032 processSync(mapper);
9033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009035 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009036
9037 // back to default tool type
9038 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9039 processKey(mapper, BTN_TOOL_MOUSE, 0);
9040 processKey(mapper, BTN_TOOL_RUBBER, 0);
9041 processKey(mapper, BTN_TOOL_PEN, 0);
9042 processKey(mapper, BTN_TOOL_FINGER, 0);
9043 processSync(mapper);
9044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009046 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009047}
9048
9049TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009050 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009051 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009052 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009053 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +00009054 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009055
9056 NotifyMotionArgs motionArgs;
9057
9058 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9059 processId(mapper, 1);
9060 processPosition(mapper, 100, 200);
9061 processSync(mapper);
9062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9063 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9065 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9066
9067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9068 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9070 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9071
9072 // move a little
9073 processPosition(mapper, 150, 250);
9074 processSync(mapper);
9075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9076 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9078 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9079
9080 // down when BTN_TOUCH is pressed, pressure defaults to 1
9081 processKey(mapper, BTN_TOUCH, 1);
9082 processSync(mapper);
9083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9084 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9086 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9087
9088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9089 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9090 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9091 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9092
9093 // up when BTN_TOUCH is released, hover restored
9094 processKey(mapper, BTN_TOUCH, 0);
9095 processSync(mapper);
9096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9097 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9099 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9100
9101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9102 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9104 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9105
9106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9107 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9109 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9110
9111 // exit hover when pointer goes away
9112 processId(mapper, -1);
9113 processSync(mapper);
9114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9115 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9117 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9118}
9119
9120TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009121 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009122 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009123 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009124 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009125
9126 NotifyMotionArgs motionArgs;
9127
9128 // initially hovering because pressure is 0
9129 processId(mapper, 1);
9130 processPosition(mapper, 100, 200);
9131 processPressure(mapper, 0);
9132 processSync(mapper);
9133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9134 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9135 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9136 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9137
9138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9139 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9140 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9141 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9142
9143 // move a little
9144 processPosition(mapper, 150, 250);
9145 processSync(mapper);
9146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9147 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9149 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9150
9151 // down when pressure becomes non-zero
9152 processPressure(mapper, RAW_PRESSURE_MAX);
9153 processSync(mapper);
9154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9155 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9157 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9158
9159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9160 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9161 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9162 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9163
9164 // up when pressure becomes 0, hover restored
9165 processPressure(mapper, 0);
9166 processSync(mapper);
9167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9168 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9170 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9171
9172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9173 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9175 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9176
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9178 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9180 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9181
9182 // exit hover when pointer goes away
9183 processId(mapper, -1);
9184 processSync(mapper);
9185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9186 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9188 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9189}
9190
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009191/**
9192 * Set the input device port <--> display port associations, and check that the
9193 * events are routed to the display that matches the display port.
9194 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9195 */
9196TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009197 const std::string usb2 = "USB2";
9198 const uint8_t hdmi1 = 0;
9199 const uint8_t hdmi2 = 1;
9200 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009201 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009202
9203 addConfigurationProperty("touch.deviceType", "touchScreen");
9204 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009205 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009206
9207 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9208 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9209
9210 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9211 // for this input device is specified, and the matching viewport is not present,
9212 // the input device should be disabled (at the mapper level).
9213
9214 // Add viewport for display 2 on hdmi2
9215 prepareSecondaryDisplay(type, hdmi2);
9216 // Send a touch event
9217 processPosition(mapper, 100, 100);
9218 processSync(mapper);
9219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9220
9221 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009222 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009223 // Send a touch event again
9224 processPosition(mapper, 100, 100);
9225 processSync(mapper);
9226
9227 NotifyMotionArgs args;
9228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9229 ASSERT_EQ(DISPLAY_ID, args.displayId);
9230}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009231
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009232TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9233 addConfigurationProperty("touch.deviceType", "touchScreen");
9234 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009235 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009236
9237 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9238
Michael Wrighta9cf4192022-12-01 23:46:39 +00009239 prepareDisplay(ui::ROTATION_0);
9240 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009241
9242 // Send a touch event
9243 processPosition(mapper, 100, 100);
9244 processSync(mapper);
9245
9246 NotifyMotionArgs args;
9247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9248 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9249}
9250
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009251TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009252 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009253 std::shared_ptr<FakePointerController> fakePointerController =
9254 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009255 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009256 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009257 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009258
Garfield Tan888a6a42020-01-09 11:39:16 -08009259 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009260 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009261
Michael Wrighta9cf4192022-12-01 23:46:39 +00009262 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009263 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009264 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009265
Josep del Río2d8c79a2023-01-23 19:33:50 +00009266 // Check source is mouse that would obtain the PointerController.
9267 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009268
9269 NotifyMotionArgs motionArgs;
9270 processPosition(mapper, 100, 100);
9271 processSync(mapper);
9272
9273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9274 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9275 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9276}
9277
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009278/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009279 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9280 */
9281TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9282 addConfigurationProperty("touch.deviceType", "touchScreen");
9283 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009284 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009285
Michael Wrighta9cf4192022-12-01 23:46:39 +00009286 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009287 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9288 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9289 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9290 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009291
9292 NotifyMotionArgs args;
9293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9294 ASSERT_EQ(26, args.readTime);
9295
Harry Cutts33476232023-01-30 19:57:29 +00009296 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9297 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9298 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009299
9300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9301 ASSERT_EQ(33, args.readTime);
9302}
9303
9304/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009305 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9306 * events should not be delivered to the listener.
9307 */
9308TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9309 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009310 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009311 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009312 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009313 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009314 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009315 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009316
9317 NotifyMotionArgs motionArgs;
9318 processPosition(mapper, 100, 100);
9319 processSync(mapper);
9320
9321 mFakeListener->assertNotifyMotionWasNotCalled();
9322}
9323
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009324/**
9325 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9326 * the touch mapper can process the events and the events can be delivered to the listener.
9327 */
9328TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9329 addConfigurationProperty("touch.deviceType", "touchScreen");
9330 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009331 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009332 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009333 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009334 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009335 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009336
9337 NotifyMotionArgs motionArgs;
9338 processPosition(mapper, 100, 100);
9339 processSync(mapper);
9340
9341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9342 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9343}
9344
Garfield Tanc734e4f2021-01-15 20:01:39 -08009345TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9346 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009347 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009348 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009349 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009350 std::optional<DisplayViewport> optionalDisplayViewport =
9351 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9352 ASSERT_TRUE(optionalDisplayViewport.has_value());
9353 DisplayViewport displayViewport = *optionalDisplayViewport;
9354
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009355 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009356 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009357 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009358
9359 // Finger down
9360 int32_t x = 100, y = 100;
9361 processPosition(mapper, x, y);
9362 processSync(mapper);
9363
9364 NotifyMotionArgs motionArgs;
9365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9366 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9367
9368 // Deactivate display viewport
9369 displayViewport.isActive = false;
9370 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009371 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009372
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009373 // The ongoing touch should be canceled immediately
9374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9375 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9376
9377 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009378 x += 10, y += 10;
9379 processPosition(mapper, x, y);
9380 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009382
9383 // Reactivate display viewport
9384 displayViewport.isActive = true;
9385 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009386 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009387
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009388 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009389 x += 10, y += 10;
9390 processPosition(mapper, x, y);
9391 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9393 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009394}
9395
Arthur Hung7c645402019-01-25 17:45:42 +08009396TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9397 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009398 prepareAxes(POSITION | ID | SLOT);
9399 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009400 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009401
9402 // Create the second touch screen device, and enable multi fingers.
9403 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009404 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009405 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009406 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009407 std::shared_ptr<InputDevice> device2 =
9408 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009409 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009410
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009411 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009412 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009413 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009414 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009415 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009416 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009417 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009418 /*flat=*/0, /*fuzz=*/0);
9419 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009420 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9421 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009422
9423 // Setup the second touch screen device.
Arpit Singh56adebc2023-04-25 13:56:05 +00009424 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9425 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9426 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009427 std::list<NotifyArgs> unused =
9428 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009429 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009430 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009431
9432 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009433 std::shared_ptr<FakePointerController> fakePointerController =
9434 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009435 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009436
9437 // Setup policy for associated displays and show touches.
9438 const uint8_t hdmi1 = 0;
9439 const uint8_t hdmi2 = 1;
9440 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9441 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9442 mFakePolicy->setShowTouches(true);
9443
9444 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009445 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009446 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009447
9448 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009449 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009450 InputReaderConfiguration::Change::DISPLAY_INFO |
9451 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009452
9453 // Two fingers down at default display.
9454 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9455 processPosition(mapper, x1, y1);
9456 processId(mapper, 1);
9457 processSlot(mapper, 1);
9458 processPosition(mapper, x2, y2);
9459 processId(mapper, 2);
9460 processSync(mapper);
9461
9462 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9463 fakePointerController->getSpots().find(DISPLAY_ID);
9464 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9465 ASSERT_EQ(size_t(2), iter->second.size());
9466
9467 // Two fingers down at second display.
9468 processPosition(mapper2, x1, y1);
9469 processId(mapper2, 1);
9470 processSlot(mapper2, 1);
9471 processPosition(mapper2, x2, y2);
9472 processId(mapper2, 2);
9473 processSync(mapper2);
9474
9475 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9476 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9477 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009478
9479 // Disable the show touches configuration and ensure the spots are cleared.
9480 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009481 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009482 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009483
9484 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009485}
9486
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009487TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009488 prepareAxes(POSITION);
9489 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009490 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00009491 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009492
9493 NotifyMotionArgs motionArgs;
9494 // Unrotated video frame
9495 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9496 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009497 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009498 processPosition(mapper, 100, 200);
9499 processSync(mapper);
9500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9501 ASSERT_EQ(frames, motionArgs.videoFrames);
9502
9503 // Subsequent touch events should not have any videoframes
9504 // This is implemented separately in FakeEventHub,
9505 // but that should match the behaviour of TouchVideoDevice.
9506 processPosition(mapper, 200, 200);
9507 processSync(mapper);
9508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9509 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9510}
9511
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009512TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009513 prepareAxes(POSITION);
9514 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009515 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009516 // Unrotated video frame
9517 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9518 NotifyMotionArgs motionArgs;
9519
9520 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009521 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009522 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9523 clearViewports();
9524 prepareDisplay(orientation);
9525 std::vector<TouchVideoFrame> frames{frame};
9526 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9527 processPosition(mapper, 100, 200);
9528 processSync(mapper);
9529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9530 ASSERT_EQ(frames, motionArgs.videoFrames);
9531 }
9532}
9533
9534TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9535 prepareAxes(POSITION);
9536 addConfigurationProperty("touch.deviceType", "touchScreen");
9537 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9538 // orientation-aware are affected by display rotation.
9539 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00009540 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009541 // Unrotated video frame
9542 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9543 NotifyMotionArgs motionArgs;
9544
9545 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009546 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009547 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9548 clearViewports();
9549 prepareDisplay(orientation);
9550 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009551 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009552 processPosition(mapper, 100, 200);
9553 processSync(mapper);
9554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009555 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9556 // compared to the display. This is so that when the window transform (which contains the
9557 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9558 // window's coordinate space.
9559 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009560 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009561
9562 // Release finger.
9563 processSync(mapper);
9564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009565 }
9566}
9567
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009568TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009569 prepareAxes(POSITION);
9570 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009571 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009572 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9573 // so mix these.
9574 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9575 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9576 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9577 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9578 NotifyMotionArgs motionArgs;
9579
Michael Wrighta9cf4192022-12-01 23:46:39 +00009580 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009581 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009582 processPosition(mapper, 100, 200);
9583 processSync(mapper);
9584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009585 ASSERT_EQ(frames, motionArgs.videoFrames);
9586}
9587
9588TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9589 prepareAxes(POSITION);
9590 addConfigurationProperty("touch.deviceType", "touchScreen");
9591 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9592 // orientation-aware are affected by display rotation.
9593 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00009594 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009595 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9596 // so mix these.
9597 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9598 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9599 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9600 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9601 NotifyMotionArgs motionArgs;
9602
Michael Wrighta9cf4192022-12-01 23:46:39 +00009603 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009604 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9605 processPosition(mapper, 100, 200);
9606 processSync(mapper);
9607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9608 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9609 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9610 // compared to the display. This is so that when the window transform (which contains the
9611 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9612 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009613 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009614 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009615 ASSERT_EQ(frames, motionArgs.videoFrames);
9616}
9617
Arthur Hung9da14732019-09-02 16:16:58 +08009618/**
9619 * If we had defined port associations, but the viewport is not ready, the touch device would be
9620 * expected to be disabled, and it should be enabled after the viewport has found.
9621 */
9622TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009623 constexpr uint8_t hdmi2 = 1;
9624 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009625 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009626
9627 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9628
9629 addConfigurationProperty("touch.deviceType", "touchScreen");
9630 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009631 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009632
9633 ASSERT_EQ(mDevice->isEnabled(), false);
9634
9635 // Add display on hdmi2, the device should be enabled and can receive touch event.
9636 prepareSecondaryDisplay(type, hdmi2);
9637 ASSERT_EQ(mDevice->isEnabled(), true);
9638
9639 // Send a touch event.
9640 processPosition(mapper, 100, 100);
9641 processSync(mapper);
9642
9643 NotifyMotionArgs args;
9644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9645 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9646}
9647
Arthur Hung421eb1c2020-01-16 00:09:42 +08009648TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009649 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009650 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009651 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009652 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009653
9654 NotifyMotionArgs motionArgs;
9655
9656 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9657 // finger down
9658 processId(mapper, 1);
9659 processPosition(mapper, x1, y1);
9660 processSync(mapper);
9661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9662 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009663 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009664
9665 // finger move
9666 processId(mapper, 1);
9667 processPosition(mapper, x2, y2);
9668 processSync(mapper);
9669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009671 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009672
9673 // finger up.
9674 processId(mapper, -1);
9675 processSync(mapper);
9676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9677 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009678 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009679
9680 // new finger down
9681 processId(mapper, 1);
9682 processPosition(mapper, x3, y3);
9683 processSync(mapper);
9684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9685 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009686 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009687}
9688
9689/**
arthurhungcc7f9802020-04-30 17:55:40 +08009690 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9691 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009692 */
arthurhungcc7f9802020-04-30 17:55:40 +08009693TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009694 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009695 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009696 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009697 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009698
9699 NotifyMotionArgs motionArgs;
9700
9701 // default tool type is finger
9702 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009703 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009704 processPosition(mapper, x1, y1);
9705 processSync(mapper);
9706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9707 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009708 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009709
9710 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9711 processToolType(mapper, MT_TOOL_PALM);
9712 processSync(mapper);
9713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9714 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9715
9716 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009717 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009718 processPosition(mapper, x2, y2);
9719 processSync(mapper);
9720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9721
9722 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009723 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009724 processSync(mapper);
9725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9726
9727 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009728 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009729 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009730 processPosition(mapper, x3, y3);
9731 processSync(mapper);
9732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9733 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009734 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009735}
9736
arthurhungbf89a482020-04-17 17:37:55 +08009737/**
arthurhungcc7f9802020-04-30 17:55:40 +08009738 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9739 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009740 */
arthurhungcc7f9802020-04-30 17:55:40 +08009741TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009742 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009743 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009744 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009745 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009746
9747 NotifyMotionArgs motionArgs;
9748
9749 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009750 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9751 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009752 processPosition(mapper, x1, y1);
9753 processSync(mapper);
9754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9755 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009756 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009757
9758 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009759 processSlot(mapper, SECOND_SLOT);
9760 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009761 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009762 processSync(mapper);
9763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009764 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009765 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009766
9767 // If the tool type of the first finger changes to MT_TOOL_PALM,
9768 // we expect to receive ACTION_POINTER_UP with cancel flag.
9769 processSlot(mapper, FIRST_SLOT);
9770 processId(mapper, FIRST_TRACKING_ID);
9771 processToolType(mapper, MT_TOOL_PALM);
9772 processSync(mapper);
9773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009774 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009775 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9776
9777 // The following MOVE events of second finger should be processed.
9778 processSlot(mapper, SECOND_SLOT);
9779 processId(mapper, SECOND_TRACKING_ID);
9780 processPosition(mapper, x2 + 1, y2 + 1);
9781 processSync(mapper);
9782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9784 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9785
9786 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9787 // it. Second finger receive move.
9788 processSlot(mapper, FIRST_SLOT);
9789 processId(mapper, INVALID_TRACKING_ID);
9790 processSync(mapper);
9791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9793 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9794
9795 // Second finger keeps moving.
9796 processSlot(mapper, SECOND_SLOT);
9797 processId(mapper, SECOND_TRACKING_ID);
9798 processPosition(mapper, x2 + 2, y2 + 2);
9799 processSync(mapper);
9800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9801 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9802 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9803
9804 // Second finger up.
9805 processId(mapper, INVALID_TRACKING_ID);
9806 processSync(mapper);
9807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9808 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9809 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9810}
9811
9812/**
9813 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9814 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9815 */
9816TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9817 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009818 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009819 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009820 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009821
9822 NotifyMotionArgs motionArgs;
9823
9824 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9825 // First finger down.
9826 processId(mapper, FIRST_TRACKING_ID);
9827 processPosition(mapper, x1, y1);
9828 processSync(mapper);
9829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9830 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009831 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009832
9833 // Second finger down.
9834 processSlot(mapper, SECOND_SLOT);
9835 processId(mapper, SECOND_TRACKING_ID);
9836 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009837 processSync(mapper);
9838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009839 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009840 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009841
arthurhungcc7f9802020-04-30 17:55:40 +08009842 // If the tool type of the first finger changes to MT_TOOL_PALM,
9843 // we expect to receive ACTION_POINTER_UP with cancel flag.
9844 processSlot(mapper, FIRST_SLOT);
9845 processId(mapper, FIRST_TRACKING_ID);
9846 processToolType(mapper, MT_TOOL_PALM);
9847 processSync(mapper);
9848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009849 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009850 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9851
9852 // Second finger keeps moving.
9853 processSlot(mapper, SECOND_SLOT);
9854 processId(mapper, SECOND_TRACKING_ID);
9855 processPosition(mapper, x2 + 1, y2 + 1);
9856 processSync(mapper);
9857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9859
9860 // second finger becomes palm, receive cancel due to only 1 finger is active.
9861 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009862 processToolType(mapper, MT_TOOL_PALM);
9863 processSync(mapper);
9864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9865 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9866
arthurhungcc7f9802020-04-30 17:55:40 +08009867 // third finger down.
9868 processSlot(mapper, THIRD_SLOT);
9869 processId(mapper, THIRD_TRACKING_ID);
9870 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009871 processPosition(mapper, x3, y3);
9872 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9874 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009875 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009876 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9877
9878 // third finger move
9879 processId(mapper, THIRD_TRACKING_ID);
9880 processPosition(mapper, x3 + 1, y3 + 1);
9881 processSync(mapper);
9882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9884
9885 // first finger up, third finger receive move.
9886 processSlot(mapper, FIRST_SLOT);
9887 processId(mapper, INVALID_TRACKING_ID);
9888 processSync(mapper);
9889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9891 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9892
9893 // second finger up, third finger receive move.
9894 processSlot(mapper, SECOND_SLOT);
9895 processId(mapper, INVALID_TRACKING_ID);
9896 processSync(mapper);
9897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9899 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9900
9901 // third finger up.
9902 processSlot(mapper, THIRD_SLOT);
9903 processId(mapper, INVALID_TRACKING_ID);
9904 processSync(mapper);
9905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9906 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9907 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9908}
9909
9910/**
9911 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9912 * and the active finger could still be allowed to receive the events
9913 */
9914TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9915 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009916 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009917 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009918 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009919
9920 NotifyMotionArgs motionArgs;
9921
9922 // default tool type is finger
9923 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9924 processId(mapper, FIRST_TRACKING_ID);
9925 processPosition(mapper, x1, y1);
9926 processSync(mapper);
9927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9928 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009929 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009930
9931 // Second finger down.
9932 processSlot(mapper, SECOND_SLOT);
9933 processId(mapper, SECOND_TRACKING_ID);
9934 processPosition(mapper, x2, y2);
9935 processSync(mapper);
9936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009937 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009938 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009939
9940 // If the tool type of the second finger changes to MT_TOOL_PALM,
9941 // we expect to receive ACTION_POINTER_UP with cancel flag.
9942 processId(mapper, SECOND_TRACKING_ID);
9943 processToolType(mapper, MT_TOOL_PALM);
9944 processSync(mapper);
9945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009946 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009947 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9948
9949 // The following MOVE event should be processed.
9950 processSlot(mapper, FIRST_SLOT);
9951 processId(mapper, FIRST_TRACKING_ID);
9952 processPosition(mapper, x1 + 1, y1 + 1);
9953 processSync(mapper);
9954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9956 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9957
9958 // second finger up.
9959 processSlot(mapper, SECOND_SLOT);
9960 processId(mapper, INVALID_TRACKING_ID);
9961 processSync(mapper);
9962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9964
9965 // first finger keep moving
9966 processSlot(mapper, FIRST_SLOT);
9967 processId(mapper, FIRST_TRACKING_ID);
9968 processPosition(mapper, x1 + 2, y1 + 2);
9969 processSync(mapper);
9970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9971 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9972
9973 // first finger up.
9974 processId(mapper, INVALID_TRACKING_ID);
9975 processSync(mapper);
9976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9977 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9978 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009979}
9980
Arthur Hung9ad18942021-06-19 02:04:46 +00009981/**
9982 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9983 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9984 * cause slot be valid again.
9985 */
9986TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9987 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009988 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009989 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009990 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +00009991
9992 NotifyMotionArgs motionArgs;
9993
9994 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9995 // First finger down.
9996 processId(mapper, FIRST_TRACKING_ID);
9997 processPosition(mapper, x1, y1);
9998 processPressure(mapper, RAW_PRESSURE_MAX);
9999 processSync(mapper);
10000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10001 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10002 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10003
10004 // First finger move.
10005 processId(mapper, FIRST_TRACKING_ID);
10006 processPosition(mapper, x1 + 1, y1 + 1);
10007 processPressure(mapper, RAW_PRESSURE_MAX);
10008 processSync(mapper);
10009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10011 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10012
10013 // Second finger down.
10014 processSlot(mapper, SECOND_SLOT);
10015 processId(mapper, SECOND_TRACKING_ID);
10016 processPosition(mapper, x2, y2);
10017 processPressure(mapper, RAW_PRESSURE_MAX);
10018 processSync(mapper);
10019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010020 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010021 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10022
10023 // second finger up with some unexpected data.
10024 processSlot(mapper, SECOND_SLOT);
10025 processId(mapper, INVALID_TRACKING_ID);
10026 processPosition(mapper, x2, y2);
10027 processSync(mapper);
10028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010029 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010030 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10031
10032 // first finger up with some unexpected data.
10033 processSlot(mapper, FIRST_SLOT);
10034 processId(mapper, INVALID_TRACKING_ID);
10035 processPosition(mapper, x2, y2);
10036 processPressure(mapper, RAW_PRESSURE_MAX);
10037 processSync(mapper);
10038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10039 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10040 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10041}
10042
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010043TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10044 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010045 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010046 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010047 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010048
10049 // First finger down.
10050 processId(mapper, FIRST_TRACKING_ID);
10051 processPosition(mapper, 100, 200);
10052 processPressure(mapper, RAW_PRESSURE_MAX);
10053 processSync(mapper);
10054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10055 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10056
10057 // Second finger down.
10058 processSlot(mapper, SECOND_SLOT);
10059 processId(mapper, SECOND_TRACKING_ID);
10060 processPosition(mapper, 300, 400);
10061 processPressure(mapper, RAW_PRESSURE_MAX);
10062 processSync(mapper);
10063 ASSERT_NO_FATAL_FAILURE(
10064 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10065
10066 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010067 // preserved. Resetting should cancel the ongoing gesture.
10068 resetMapper(mapper, ARBITRARY_TIME);
10069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10070 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010071
10072 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10073 // the existing touch state to generate a down event.
10074 processPosition(mapper, 301, 302);
10075 processSync(mapper);
10076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10077 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10079 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10080
10081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10082}
10083
10084TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10085 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010086 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010087 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010088 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010089
10090 // First finger touches down and releases.
10091 processId(mapper, FIRST_TRACKING_ID);
10092 processPosition(mapper, 100, 200);
10093 processPressure(mapper, RAW_PRESSURE_MAX);
10094 processSync(mapper);
10095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10096 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10097 processId(mapper, INVALID_TRACKING_ID);
10098 processSync(mapper);
10099 ASSERT_NO_FATAL_FAILURE(
10100 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10101
10102 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10103 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010104 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10106
10107 // Send an empty sync frame. Since there are no pointers, no events are generated.
10108 processSync(mapper);
10109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10110}
10111
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010112TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010113 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010114 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010115 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010116 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010118
10119 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10120 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10121 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10122 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10123 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10124
10125 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010126 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010127 processId(mapper, FIRST_TRACKING_ID);
10128 processToolType(mapper, MT_TOOL_PEN);
10129 processPosition(mapper, 100, 200);
10130 processPressure(mapper, RAW_PRESSURE_MAX);
10131 processSync(mapper);
10132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10133 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10134 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010135 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010136
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010137 // Now that we know the device supports styluses, ensure that the device is re-configured with
10138 // the stylus source.
10139 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10140 {
10141 const auto& devices = mReader->getInputDevices();
10142 auto deviceInfo =
10143 std::find_if(devices.begin(), devices.end(),
10144 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10145 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10146 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10147 }
10148
10149 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10151
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010152 processId(mapper, INVALID_TRACKING_ID);
10153 processSync(mapper);
10154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10155 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10156 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010157 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010158}
10159
Seunghwan Choi356026c2023-02-01 14:37:25 +090010160TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10161 addConfigurationProperty("touch.deviceType", "touchScreen");
10162 prepareDisplay(ui::ROTATION_0);
10163 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10164 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10165 // indicate stylus presence dynamically.
10166 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10167 std::shared_ptr<FakePointerController> fakePointerController =
10168 std::make_shared<FakePointerController>();
10169 mFakePolicy->setPointerController(fakePointerController);
10170 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singh56adebc2023-04-25 13:56:05 +000010171 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010172
10173 processId(mapper, FIRST_TRACKING_ID);
10174 processPressure(mapper, RAW_PRESSURE_MIN);
10175 processPosition(mapper, 100, 200);
10176 processToolType(mapper, MT_TOOL_PEN);
10177 processSync(mapper);
10178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10179 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010180 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010181 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10182 ASSERT_TRUE(fakePointerController->isPointerShown());
10183 ASSERT_NO_FATAL_FAILURE(
10184 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10185}
10186
10187TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10188 addConfigurationProperty("touch.deviceType", "touchScreen");
10189 prepareDisplay(ui::ROTATION_0);
10190 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10191 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10192 // indicate stylus presence dynamically.
10193 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10194 std::shared_ptr<FakePointerController> fakePointerController =
10195 std::make_shared<FakePointerController>();
10196 mFakePolicy->setPointerController(fakePointerController);
10197 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singh56adebc2023-04-25 13:56:05 +000010198 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010199
10200 processId(mapper, FIRST_TRACKING_ID);
10201 processPressure(mapper, RAW_PRESSURE_MIN);
10202 processPosition(mapper, 100, 200);
10203 processToolType(mapper, MT_TOOL_PEN);
10204 processSync(mapper);
10205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10206 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010207 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010208 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10209 ASSERT_FALSE(fakePointerController->isPointerShown());
10210}
10211
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010212// --- MultiTouchInputMapperTest_ExternalDevice ---
10213
10214class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10215protected:
Chris Yea52ade12020-08-27 16:49:20 -070010216 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010217};
10218
10219/**
10220 * Expect fallback to internal viewport if device is external and external viewport is not present.
10221 */
10222TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10223 prepareAxes(POSITION);
10224 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010225 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +000010226 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010227
10228 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10229
10230 NotifyMotionArgs motionArgs;
10231
10232 // Expect the event to be sent to the internal viewport,
10233 // because an external viewport is not present.
10234 processPosition(mapper, 100, 100);
10235 processSync(mapper);
10236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10237 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10238
10239 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010240 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010241 processPosition(mapper, 100, 100);
10242 processSync(mapper);
10243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10244 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10245}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010246
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010247TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10248 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10249 std::shared_ptr<FakePointerController> fakePointerController =
10250 std::make_shared<FakePointerController>();
10251 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10252 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010253
10254 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010255 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010256 prepareAxes(POSITION | ID | SLOT);
10257 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10258 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10259 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010260 mFakePolicy->setPointerController(fakePointerController);
Arpit Singh56adebc2023-04-25 13:56:05 +000010261 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010262
10263 // captured touchpad should be a touchpad source
10264 NotifyDeviceResetArgs resetArgs;
10265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10266 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10267
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010268 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010269
10270 const InputDeviceInfo::MotionRange* relRangeX =
10271 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10272 ASSERT_NE(relRangeX, nullptr);
10273 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10274 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10275 const InputDeviceInfo::MotionRange* relRangeY =
10276 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10277 ASSERT_NE(relRangeY, nullptr);
10278 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10279 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10280
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010281 // run captured pointer tests - note that this is unscaled, so input listener events should be
10282 // identical to what the hardware sends (accounting for any
10283 // calibration).
10284 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010285 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010286 processId(mapper, 1);
10287 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10288 processKey(mapper, BTN_TOUCH, 1);
10289 processSync(mapper);
10290
10291 // expect coord[0] to contain initial location of touch 0
10292 NotifyMotionArgs args;
10293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10294 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10295 ASSERT_EQ(1U, args.pointerCount);
10296 ASSERT_EQ(0, args.pointerProperties[0].id);
10297 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10298 ASSERT_NO_FATAL_FAILURE(
10299 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10300
10301 // FINGER 1 DOWN
10302 processSlot(mapper, 1);
10303 processId(mapper, 2);
10304 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10305 processSync(mapper);
10306
10307 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010309 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010310 ASSERT_EQ(2U, args.pointerCount);
10311 ASSERT_EQ(0, args.pointerProperties[0].id);
10312 ASSERT_EQ(1, args.pointerProperties[1].id);
10313 ASSERT_NO_FATAL_FAILURE(
10314 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10315 ASSERT_NO_FATAL_FAILURE(
10316 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10317
10318 // FINGER 1 MOVE
10319 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10320 processSync(mapper);
10321
10322 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10323 // from move
10324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10326 ASSERT_NO_FATAL_FAILURE(
10327 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10328 ASSERT_NO_FATAL_FAILURE(
10329 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10330
10331 // FINGER 0 MOVE
10332 processSlot(mapper, 0);
10333 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10334 processSync(mapper);
10335
10336 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10339 ASSERT_NO_FATAL_FAILURE(
10340 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10341 ASSERT_NO_FATAL_FAILURE(
10342 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10343
10344 // BUTTON DOWN
10345 processKey(mapper, BTN_LEFT, 1);
10346 processSync(mapper);
10347
10348 // touchinputmapper design sends a move before button press
10349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10352 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10353
10354 // BUTTON UP
10355 processKey(mapper, BTN_LEFT, 0);
10356 processSync(mapper);
10357
10358 // touchinputmapper design sends a move after button release
10359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10360 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10363
10364 // FINGER 0 UP
10365 processId(mapper, -1);
10366 processSync(mapper);
10367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10368 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10369
10370 // FINGER 1 MOVE
10371 processSlot(mapper, 1);
10372 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10373 processSync(mapper);
10374
10375 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10378 ASSERT_EQ(1U, args.pointerCount);
10379 ASSERT_EQ(1, args.pointerProperties[0].id);
10380 ASSERT_NO_FATAL_FAILURE(
10381 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10382
10383 // FINGER 1 UP
10384 processId(mapper, -1);
10385 processKey(mapper, BTN_TOUCH, 0);
10386 processSync(mapper);
10387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10388 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10389
Josep del Río2d8c79a2023-01-23 19:33:50 +000010390 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010391 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010392 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010394 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010395}
10396
10397TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10398 std::shared_ptr<FakePointerController> fakePointerController =
10399 std::make_shared<FakePointerController>();
10400 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10401 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010402
10403 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010404 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010405 prepareAxes(POSITION | ID | SLOT);
10406 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10407 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010408 mFakePolicy->setPointerController(fakePointerController);
Arpit Singh56adebc2023-04-25 13:56:05 +000010409 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010410 // run uncaptured pointer tests - pushes out generic events
10411 // FINGER 0 DOWN
10412 processId(mapper, 3);
10413 processPosition(mapper, 100, 100);
10414 processKey(mapper, BTN_TOUCH, 1);
10415 processSync(mapper);
10416
10417 // start at (100,100), cursor should be at (0,0) * scale
10418 NotifyMotionArgs args;
10419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10420 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10421 ASSERT_NO_FATAL_FAILURE(
10422 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10423
10424 // FINGER 0 MOVE
10425 processPosition(mapper, 200, 200);
10426 processSync(mapper);
10427
10428 // compute scaling to help with touch position checking
10429 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10430 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10431 float scale =
10432 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10433
10434 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10436 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10438 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010439
10440 // BUTTON DOWN
10441 processKey(mapper, BTN_LEFT, 1);
10442 processSync(mapper);
10443
10444 // touchinputmapper design sends a move before button press
10445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10446 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10449
10450 // BUTTON UP
10451 processKey(mapper, BTN_LEFT, 0);
10452 processSync(mapper);
10453
10454 // touchinputmapper design sends a move after button release
10455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10456 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10458 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010459}
10460
10461TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10462 std::shared_ptr<FakePointerController> fakePointerController =
10463 std::make_shared<FakePointerController>();
10464
Michael Wrighta9cf4192022-12-01 23:46:39 +000010465 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010466 prepareAxes(POSITION | ID | SLOT);
10467 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010468 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010469 mFakePolicy->setPointerCapture(false);
Arpit Singh56adebc2023-04-25 13:56:05 +000010470 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010471
Josep del Río2d8c79a2023-01-23 19:33:50 +000010472 // uncaptured touchpad should be a pointer device
10473 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010474
Josep del Río2d8c79a2023-01-23 19:33:50 +000010475 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010476 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010477 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010478 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10479}
10480
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010481// --- BluetoothMultiTouchInputMapperTest ---
10482
10483class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10484protected:
10485 void SetUp() override {
10486 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10487 }
10488};
10489
10490TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10491 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010492 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010493 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010494 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010495
10496 nsecs_t kernelEventTime = ARBITRARY_TIME;
10497 nsecs_t expectedEventTime = ARBITRARY_TIME;
10498 // Touch down.
10499 processId(mapper, FIRST_TRACKING_ID);
10500 processPosition(mapper, 100, 200);
10501 processPressure(mapper, RAW_PRESSURE_MAX);
10502 processSync(mapper, ARBITRARY_TIME);
10503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10504 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10505
10506 // Process several events that come in quick succession, according to their timestamps.
10507 for (int i = 0; i < 3; i++) {
10508 constexpr static nsecs_t delta = ms2ns(1);
10509 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10510 kernelEventTime += delta;
10511 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10512
10513 processPosition(mapper, 101 + i, 201 + i);
10514 processSync(mapper, kernelEventTime);
10515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10516 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10517 WithEventTime(expectedEventTime))));
10518 }
10519
10520 // Release the touch.
10521 processId(mapper, INVALID_TRACKING_ID);
10522 processPressure(mapper, RAW_PRESSURE_MIN);
10523 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10525 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10526 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10527}
10528
10529// --- MultiTouchPointerModeTest ---
10530
HQ Liue6983c72022-04-19 22:14:56 +000010531class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10532protected:
10533 float mPointerMovementScale;
10534 float mPointerXZoomScale;
10535 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10536 addConfigurationProperty("touch.deviceType", "pointer");
10537 std::shared_ptr<FakePointerController> fakePointerController =
10538 std::make_shared<FakePointerController>();
10539 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10540 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010541 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010542
10543 prepareAxes(POSITION);
10544 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10545 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10546 // needs to be disabled, and the pointer gesture needs to be enabled.
10547 mFakePolicy->setPointerCapture(false);
10548 mFakePolicy->setPointerGestureEnabled(true);
10549 mFakePolicy->setPointerController(fakePointerController);
10550
10551 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10552 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10553 mPointerMovementScale =
10554 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10555 mPointerXZoomScale =
10556 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10557 }
10558
10559 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10560 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10561 /*flat*/ 0,
10562 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10563 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10564 /*flat*/ 0,
10565 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10566 }
10567};
10568
10569/**
10570 * Two fingers down on a pointer mode touch pad. The width
10571 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10572 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10573 * be greater than the both value to be freeform gesture, so that after two
10574 * fingers start to move downwards, the gesture should be swipe.
10575 */
10576TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10577 // The min freeform gesture width is 25units/mm x 30mm = 750
10578 // which is greater than fraction of the diagnal length of the touchpad (349).
10579 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010580 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010581 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010582 NotifyMotionArgs motionArgs;
10583
10584 // Two fingers down at once.
10585 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10586 // Pointer's initial position is used the [0,0] coordinate.
10587 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10588
10589 processId(mapper, FIRST_TRACKING_ID);
10590 processPosition(mapper, x1, y1);
10591 processMTSync(mapper);
10592 processId(mapper, SECOND_TRACKING_ID);
10593 processPosition(mapper, x2, y2);
10594 processMTSync(mapper);
10595 processSync(mapper);
10596
10597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10598 ASSERT_EQ(1U, motionArgs.pointerCount);
10599 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010600 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010601 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010602 ASSERT_NO_FATAL_FAILURE(
10603 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10604
10605 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10606 // that there should be 1 pointer.
10607 int32_t movingDistance = 200;
10608 y1 += movingDistance;
10609 y2 += movingDistance;
10610
10611 processId(mapper, FIRST_TRACKING_ID);
10612 processPosition(mapper, x1, y1);
10613 processMTSync(mapper);
10614 processId(mapper, SECOND_TRACKING_ID);
10615 processPosition(mapper, x2, y2);
10616 processMTSync(mapper);
10617 processSync(mapper);
10618
10619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10620 ASSERT_EQ(1U, motionArgs.pointerCount);
10621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010622 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010623 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10625 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10626 0, 0, 0, 0));
10627}
10628
10629/**
10630 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10631 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10632 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10633 * value to be freeform gesture, so that after two fingers start to move downwards,
10634 * the gesture should be swipe.
10635 */
10636TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10637 // The min freeform gesture width is 5units/mm x 30mm = 150
10638 // which is greater than fraction of the diagnal length of the touchpad (349).
10639 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010640 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singh56adebc2023-04-25 13:56:05 +000010641 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010642 NotifyMotionArgs motionArgs;
10643
10644 // Two fingers down at once.
10645 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10646 // Pointer's initial position is used the [0,0] coordinate.
10647 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10648
10649 processId(mapper, FIRST_TRACKING_ID);
10650 processPosition(mapper, x1, y1);
10651 processMTSync(mapper);
10652 processId(mapper, SECOND_TRACKING_ID);
10653 processPosition(mapper, x2, y2);
10654 processMTSync(mapper);
10655 processSync(mapper);
10656
10657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10658 ASSERT_EQ(1U, motionArgs.pointerCount);
10659 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010660 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010661 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010662 ASSERT_NO_FATAL_FAILURE(
10663 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10664
10665 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10666 // and there should be 1 pointer.
10667 int32_t movingDistance = 200;
10668 y1 += movingDistance;
10669 y2 += movingDistance;
10670
10671 processId(mapper, FIRST_TRACKING_ID);
10672 processPosition(mapper, x1, y1);
10673 processMTSync(mapper);
10674 processId(mapper, SECOND_TRACKING_ID);
10675 processPosition(mapper, x2, y2);
10676 processMTSync(mapper);
10677 processSync(mapper);
10678
10679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10680 ASSERT_EQ(1U, motionArgs.pointerCount);
10681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010682 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010683 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010684 // New coordinate is the scaled relative coordinate from the initial coordinate.
10685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10686 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10687 0, 0, 0, 0));
10688}
10689
10690/**
10691 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10692 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10693 * freeform gestures after two fingers start to move downwards.
10694 */
10695TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010696 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010697 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010698
10699 NotifyMotionArgs motionArgs;
10700
10701 // Two fingers down at once. Wider than the max swipe width.
10702 // The gesture is expected to be PRESS, then transformed to FREEFORM
10703 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10704
10705 processId(mapper, FIRST_TRACKING_ID);
10706 processPosition(mapper, x1, y1);
10707 processMTSync(mapper);
10708 processId(mapper, SECOND_TRACKING_ID);
10709 processPosition(mapper, x2, y2);
10710 processMTSync(mapper);
10711 processSync(mapper);
10712
10713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10714 ASSERT_EQ(1U, motionArgs.pointerCount);
10715 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010716 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010717 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010718 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10719 ASSERT_NO_FATAL_FAILURE(
10720 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10721
10722 int32_t movingDistance = 200;
10723
10724 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10725 // then two down events for two pointers.
10726 y1 += movingDistance;
10727 y2 += movingDistance;
10728
10729 processId(mapper, FIRST_TRACKING_ID);
10730 processPosition(mapper, x1, y1);
10731 processMTSync(mapper);
10732 processId(mapper, SECOND_TRACKING_ID);
10733 processPosition(mapper, x2, y2);
10734 processMTSync(mapper);
10735 processSync(mapper);
10736
10737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10738 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10739 ASSERT_EQ(1U, motionArgs.pointerCount);
10740 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010742 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
HQ Liue6983c72022-04-19 22:14:56 +000010743 ASSERT_EQ(1U, motionArgs.pointerCount);
10744 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010746 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010747 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010748 ASSERT_EQ(2U, motionArgs.pointerCount);
10749 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010750 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010751 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010752 // Two pointers' scaled relative coordinates from their initial centroid.
10753 // Initial y coordinates are 0 as y1 and y2 have the same value.
10754 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10755 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10756 // When pointers move, the new coordinates equal to the initial coordinates plus
10757 // scaled moving distance.
10758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10759 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10760 0, 0, 0, 0));
10761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10762 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10763 0, 0, 0, 0));
10764
10765 // Move two fingers down again, expect one MOVE motion event.
10766 y1 += movingDistance;
10767 y2 += movingDistance;
10768
10769 processId(mapper, FIRST_TRACKING_ID);
10770 processPosition(mapper, x1, y1);
10771 processMTSync(mapper);
10772 processId(mapper, SECOND_TRACKING_ID);
10773 processPosition(mapper, x2, y2);
10774 processMTSync(mapper);
10775 processSync(mapper);
10776
10777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10778 ASSERT_EQ(2U, motionArgs.pointerCount);
10779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010780 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010781 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10783 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10784 0, 0, 0, 0, 0));
10785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10786 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10787 0, 0, 0, 0, 0));
10788}
10789
Harry Cutts39b7ca22022-10-05 15:55:48 +000010790TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010791 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010792 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010793 NotifyMotionArgs motionArgs;
10794
10795 // Place two fingers down.
10796 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10797
10798 processId(mapper, FIRST_TRACKING_ID);
10799 processPosition(mapper, x1, y1);
10800 processMTSync(mapper);
10801 processId(mapper, SECOND_TRACKING_ID);
10802 processPosition(mapper, x2, y2);
10803 processMTSync(mapper);
10804 processSync(mapper);
10805
10806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10807 ASSERT_EQ(1U, motionArgs.pointerCount);
10808 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10809 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10810 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10811 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10812
10813 // Move the two fingers down and to the left.
10814 int32_t movingDistance = 200;
10815 x1 -= movingDistance;
10816 y1 += movingDistance;
10817 x2 -= movingDistance;
10818 y2 += movingDistance;
10819
10820 processId(mapper, FIRST_TRACKING_ID);
10821 processPosition(mapper, x1, y1);
10822 processMTSync(mapper);
10823 processId(mapper, SECOND_TRACKING_ID);
10824 processPosition(mapper, x2, y2);
10825 processMTSync(mapper);
10826 processSync(mapper);
10827
10828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10829 ASSERT_EQ(1U, motionArgs.pointerCount);
10830 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10831 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10832 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10833 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10834}
10835
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010836TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010837 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010838 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +000010839 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10841
10842 // Start a stylus gesture.
10843 processKey(mapper, BTN_TOOL_PEN, 1);
10844 processId(mapper, FIRST_TRACKING_ID);
10845 processPosition(mapper, 100, 200);
10846 processSync(mapper);
10847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10848 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10849 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010850 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010851 // TODO(b/257078296): Pointer mode generates extra event.
10852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10853 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10854 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010855 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10857
10858 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10859 // gesture should be disabled.
10860 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10861 viewport->isActive = false;
10862 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010863 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10865 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10866 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010867 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010868 // TODO(b/257078296): Pointer mode generates extra event.
10869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10870 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10871 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010872 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10874}
10875
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010876// --- JoystickInputMapperTest ---
10877
10878class JoystickInputMapperTest : public InputMapperTest {
10879protected:
10880 static const int32_t RAW_X_MIN;
10881 static const int32_t RAW_X_MAX;
10882 static const int32_t RAW_Y_MIN;
10883 static const int32_t RAW_Y_MAX;
10884
10885 void SetUp() override {
10886 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10887 }
10888 void prepareAxes() {
10889 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10890 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10891 }
10892
10893 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10895 }
10896
10897 void processSync(JoystickInputMapper& mapper) {
10898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10899 }
10900
Michael Wrighta9cf4192022-12-01 23:46:39 +000010901 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010902 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10903 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10904 NO_PORT, ViewportType::VIRTUAL);
10905 }
10906};
10907
10908const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10909const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10910const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10911const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10912
10913TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10914 prepareAxes();
Arpit Singh2be4a362023-04-26 14:16:50 +000010915 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010916
10917 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10918
Michael Wrighta9cf4192022-12-01 23:46:39 +000010919 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010920
10921 // Send an axis event
10922 processAxis(mapper, ABS_X, 100);
10923 processSync(mapper);
10924
10925 NotifyMotionArgs args;
10926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10927 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10928
10929 // Send another axis event
10930 processAxis(mapper, ABS_Y, 100);
10931 processSync(mapper);
10932
10933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10934 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10935}
10936
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010937// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010938
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010939class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010940protected:
10941 static const char* DEVICE_NAME;
10942 static const char* DEVICE_LOCATION;
10943 static const int32_t DEVICE_ID;
10944 static const int32_t DEVICE_GENERATION;
10945 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010946 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010947 static const int32_t EVENTHUB_ID;
10948
10949 std::shared_ptr<FakeEventHub> mFakeEventHub;
10950 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010951 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010952 std::unique_ptr<InstrumentedInputReader> mReader;
10953 std::shared_ptr<InputDevice> mDevice;
10954
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010955 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010956 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010957 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010958 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010959 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010960 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010961 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10962 }
10963
10964 void SetUp() override { SetUp(DEVICE_CLASSES); }
10965
10966 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010967 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010968 mFakePolicy.clear();
10969 }
10970
Chris Yee2b1e5c2021-03-10 22:45:12 -080010971 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10972 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010973 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010974 InputDeviceIdentifier identifier;
10975 identifier.name = name;
10976 identifier.location = location;
10977 std::shared_ptr<InputDevice> device =
10978 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10979 identifier);
10980 mReader->pushNextDevice(device);
10981 mFakeEventHub->addDevice(eventHubId, name, classes);
10982 mReader->loopOnce();
10983 return device;
10984 }
10985
10986 template <class T, typename... Args>
10987 T& addControllerAndConfigure(Args... args) {
10988 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10989
10990 return controller;
10991 }
10992};
10993
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010994const char* PeripheralControllerTest::DEVICE_NAME = "device";
10995const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10996const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10997const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10998const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010999const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11000 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011001const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011002
11003// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011004class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011005protected:
11006 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011007 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011008 }
11009};
11010
11011TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011012 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011013
Harry Cuttsa5b71292022-11-28 12:56:17 +000011014 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11015 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11016 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011017}
11018
11019TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011020 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011021
Harry Cuttsa5b71292022-11-28 12:56:17 +000011022 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11023 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11024 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011025}
11026
11027// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011028class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011029protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011030 void SetUp() override {
11031 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11032 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011033};
11034
Chris Ye85758332021-05-16 23:05:17 -070011035TEST_F(LightControllerTest, MonoLight) {
11036 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011037 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011038 .maxBrightness = 255,
11039 .flags = InputLightClass::BRIGHTNESS,
11040 .path = ""};
11041 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011042
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011043 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011044 InputDeviceInfo info;
11045 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011046 std::vector<InputDeviceLightInfo> lights = info.getLights();
11047 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011048 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11049 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11050
11051 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11052 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11053}
11054
11055TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11056 RawLightInfo infoMono = {.id = 1,
11057 .name = "mono_keyboard_backlight",
11058 .maxBrightness = 255,
11059 .flags = InputLightClass::BRIGHTNESS |
11060 InputLightClass::KEYBOARD_BACKLIGHT,
11061 .path = ""};
11062 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11063
11064 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11065 InputDeviceInfo info;
11066 controller.populateDeviceInfo(&info);
11067 std::vector<InputDeviceLightInfo> lights = info.getLights();
11068 ASSERT_EQ(1U, lights.size());
11069 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11070 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011071
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011072 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11073 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011074}
11075
11076TEST_F(LightControllerTest, RGBLight) {
11077 RawLightInfo infoRed = {.id = 1,
11078 .name = "red",
11079 .maxBrightness = 255,
11080 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11081 .path = ""};
11082 RawLightInfo infoGreen = {.id = 2,
11083 .name = "green",
11084 .maxBrightness = 255,
11085 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11086 .path = ""};
11087 RawLightInfo infoBlue = {.id = 3,
11088 .name = "blue",
11089 .maxBrightness = 255,
11090 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11091 .path = ""};
11092 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11093 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11094 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11095
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011096 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011097 InputDeviceInfo info;
11098 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011099 std::vector<InputDeviceLightInfo> lights = info.getLights();
11100 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011101 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11102 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11103 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11104
11105 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11106 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11107}
11108
11109TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11110 RawLightInfo infoRed = {.id = 1,
11111 .name = "red_keyboard_backlight",
11112 .maxBrightness = 255,
11113 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11114 InputLightClass::KEYBOARD_BACKLIGHT,
11115 .path = ""};
11116 RawLightInfo infoGreen = {.id = 2,
11117 .name = "green_keyboard_backlight",
11118 .maxBrightness = 255,
11119 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11120 InputLightClass::KEYBOARD_BACKLIGHT,
11121 .path = ""};
11122 RawLightInfo infoBlue = {.id = 3,
11123 .name = "blue_keyboard_backlight",
11124 .maxBrightness = 255,
11125 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11126 InputLightClass::KEYBOARD_BACKLIGHT,
11127 .path = ""};
11128 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11129 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11130 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11131
11132 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11133 InputDeviceInfo info;
11134 controller.populateDeviceInfo(&info);
11135 std::vector<InputDeviceLightInfo> lights = info.getLights();
11136 ASSERT_EQ(1U, lights.size());
11137 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11138 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11139 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11140
11141 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11142 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11143}
11144
11145TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11146 RawLightInfo infoRed = {.id = 1,
11147 .name = "red",
11148 .maxBrightness = 255,
11149 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11150 .path = ""};
11151 RawLightInfo infoGreen = {.id = 2,
11152 .name = "green",
11153 .maxBrightness = 255,
11154 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11155 .path = ""};
11156 RawLightInfo infoBlue = {.id = 3,
11157 .name = "blue",
11158 .maxBrightness = 255,
11159 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11160 .path = ""};
11161 RawLightInfo infoGlobal = {.id = 3,
11162 .name = "global_keyboard_backlight",
11163 .maxBrightness = 255,
11164 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11165 InputLightClass::KEYBOARD_BACKLIGHT,
11166 .path = ""};
11167 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11168 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11169 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11170 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11171
11172 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11173 InputDeviceInfo info;
11174 controller.populateDeviceInfo(&info);
11175 std::vector<InputDeviceLightInfo> lights = info.getLights();
11176 ASSERT_EQ(1U, lights.size());
11177 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11178 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11179 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011180
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011181 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11182 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011183}
11184
11185TEST_F(LightControllerTest, MultiColorRGBLight) {
11186 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011187 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011188 .maxBrightness = 255,
11189 .flags = InputLightClass::BRIGHTNESS |
11190 InputLightClass::MULTI_INTENSITY |
11191 InputLightClass::MULTI_INDEX,
11192 .path = ""};
11193
11194 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11195
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011196 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011197 InputDeviceInfo info;
11198 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011199 std::vector<InputDeviceLightInfo> lights = info.getLights();
11200 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011201 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11202 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11203 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11204
11205 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11206 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11207}
11208
11209TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11210 RawLightInfo infoColor = {.id = 1,
11211 .name = "multi_color_keyboard_backlight",
11212 .maxBrightness = 255,
11213 .flags = InputLightClass::BRIGHTNESS |
11214 InputLightClass::MULTI_INTENSITY |
11215 InputLightClass::MULTI_INDEX |
11216 InputLightClass::KEYBOARD_BACKLIGHT,
11217 .path = ""};
11218
11219 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11220
11221 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11222 InputDeviceInfo info;
11223 controller.populateDeviceInfo(&info);
11224 std::vector<InputDeviceLightInfo> lights = info.getLights();
11225 ASSERT_EQ(1U, lights.size());
11226 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11227 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11228 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011229
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011230 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11231 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011232}
11233
11234TEST_F(LightControllerTest, PlayerIdLight) {
11235 RawLightInfo info1 = {.id = 1,
11236 .name = "player1",
11237 .maxBrightness = 255,
11238 .flags = InputLightClass::BRIGHTNESS,
11239 .path = ""};
11240 RawLightInfo info2 = {.id = 2,
11241 .name = "player2",
11242 .maxBrightness = 255,
11243 .flags = InputLightClass::BRIGHTNESS,
11244 .path = ""};
11245 RawLightInfo info3 = {.id = 3,
11246 .name = "player3",
11247 .maxBrightness = 255,
11248 .flags = InputLightClass::BRIGHTNESS,
11249 .path = ""};
11250 RawLightInfo info4 = {.id = 4,
11251 .name = "player4",
11252 .maxBrightness = 255,
11253 .flags = InputLightClass::BRIGHTNESS,
11254 .path = ""};
11255 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11256 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11257 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11258 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11259
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011260 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011261 InputDeviceInfo info;
11262 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011263 std::vector<InputDeviceLightInfo> lights = info.getLights();
11264 ASSERT_EQ(1U, lights.size());
11265 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011266 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11267 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011268
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011269 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11270 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11271 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011272}
11273
Michael Wrightd02c5b62014-02-10 15:10:22 -080011274} // namespace android