blob: 2df44ff2a4a9ec3168fb340f3e0b0651f1c89f73 [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 Singh8e6fb252023-04-06 11:49:17 +00002587 device.addEventHubDevice(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002588 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2589 std::string dumpStr, eventHubDevStr;
2590 device.dump(dumpStr, eventHubDevStr);
2591}
2592
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002593TEST_F(InputDeviceTest, GetBluetoothAddress) {
2594 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2595 ASSERT_TRUE(address);
2596 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2597}
2598
Michael Wrightd02c5b62014-02-10 15:10:22 -08002599// --- SwitchInputMapperTest ---
2600
2601class SwitchInputMapperTest : public InputMapperTest {
2602protected:
2603};
2604
2605TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002606 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002608 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002609}
2610
2611TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002612 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002613
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002614 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002615 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002617 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002618 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002619}
2620
2621TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002622 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002623 std::list<NotifyArgs> out;
2624 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2625 ASSERT_TRUE(out.empty());
2626 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2627 ASSERT_TRUE(out.empty());
2628 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2629 ASSERT_TRUE(out.empty());
2630 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002631
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002632 ASSERT_EQ(1u, out.size());
2633 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002634 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002635 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2636 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637 args.switchMask);
2638 ASSERT_EQ(uint32_t(0), args.policyFlags);
2639}
2640
Chris Ye87143712020-11-10 05:05:58 +00002641// --- VibratorInputMapperTest ---
2642class VibratorInputMapperTest : public InputMapperTest {
2643protected:
2644 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2645};
2646
2647TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002648 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002649
2650 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2651}
2652
2653TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002654 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002655
2656 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2657}
2658
2659TEST_F(VibratorInputMapperTest, Vibrate) {
2660 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002661 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00002662 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002663
2664 VibrationElement pattern(2);
2665 VibrationSequence sequence(2);
2666 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002667 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2668 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002669 sequence.addElement(pattern);
2670 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002671 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2672 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002673 sequence.addElement(pattern);
2674
2675 std::vector<int64_t> timings = {0, 1};
2676 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2677
2678 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002679 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002680 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002681 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002682 // Verify vibrator state listener was notified.
2683 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002684 ASSERT_EQ(1u, out.size());
2685 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2686 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2687 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002688 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002689 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002690 ASSERT_FALSE(mapper.isVibrating());
2691 // Verify vibrator state listener was notified.
2692 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002693 ASSERT_EQ(1u, out.size());
2694 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2695 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2696 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002697}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002698
Chris Yef59a2f42020-10-16 12:55:26 -07002699// --- SensorInputMapperTest ---
2700
2701class SensorInputMapperTest : public InputMapperTest {
2702protected:
2703 static const int32_t ACCEL_RAW_MIN;
2704 static const int32_t ACCEL_RAW_MAX;
2705 static const int32_t ACCEL_RAW_FUZZ;
2706 static const int32_t ACCEL_RAW_FLAT;
2707 static const int32_t ACCEL_RAW_RESOLUTION;
2708
2709 static const int32_t GYRO_RAW_MIN;
2710 static const int32_t GYRO_RAW_MAX;
2711 static const int32_t GYRO_RAW_FUZZ;
2712 static const int32_t GYRO_RAW_FLAT;
2713 static const int32_t GYRO_RAW_RESOLUTION;
2714
2715 static const float GRAVITY_MS2_UNIT;
2716 static const float DEGREE_RADIAN_UNIT;
2717
2718 void prepareAccelAxes();
2719 void prepareGyroAxes();
2720 void setAccelProperties();
2721 void setGyroProperties();
2722 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2723};
2724
2725const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2726const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2727const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2728const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2729const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2730
2731const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2732const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2733const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2734const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2735const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2736
2737const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2738const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2739
2740void SensorInputMapperTest::prepareAccelAxes() {
2741 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2742 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2743 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2744 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2745 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2746 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2747}
2748
2749void SensorInputMapperTest::prepareGyroAxes() {
2750 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2751 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2752 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2753 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2754 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2755 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2756}
2757
2758void SensorInputMapperTest::setAccelProperties() {
2759 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2760 /* sensorDataIndex */ 0);
2761 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2762 /* sensorDataIndex */ 1);
2763 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2764 /* sensorDataIndex */ 2);
2765 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2766 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2767 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2768 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2769 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2770}
2771
2772void SensorInputMapperTest::setGyroProperties() {
2773 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2774 /* sensorDataIndex */ 0);
2775 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2776 /* sensorDataIndex */ 1);
2777 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2778 /* sensorDataIndex */ 2);
2779 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2780 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2781 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2782 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2783 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2784}
2785
2786TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00002787 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002788
2789 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2790}
2791
2792TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2793 setAccelProperties();
2794 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00002795 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002796
2797 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2798 std::chrono::microseconds(10000),
2799 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002800 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2805 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002806
2807 NotifySensorArgs args;
2808 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2809 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2810 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2811
2812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2813 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2814 ASSERT_EQ(args.deviceId, DEVICE_ID);
2815 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2816 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2817 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2818 ASSERT_EQ(args.values, values);
2819 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2820}
2821
2822TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2823 setGyroProperties();
2824 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00002825 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002826
2827 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2828 std::chrono::microseconds(10000),
2829 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002830 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002836
2837 NotifySensorArgs args;
2838 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2839 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2840 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2841
2842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2843 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2844 ASSERT_EQ(args.deviceId, DEVICE_ID);
2845 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2846 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2847 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2848 ASSERT_EQ(args.values, values);
2849 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2850}
2851
Michael Wrightd02c5b62014-02-10 15:10:22 -08002852// --- KeyboardInputMapperTest ---
2853
2854class KeyboardInputMapperTest : public InputMapperTest {
2855protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002856 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002857 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002858 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002859
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002860 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002861 int32_t originalKeyCode, int32_t rotatedKeyCode,
2862 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002863};
2864
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002865/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2866 * orientation.
2867 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002868void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002869 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2870 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002871}
2872
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002873void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002874 int32_t originalScanCode, int32_t originalKeyCode,
2875 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002876 NotifyKeyArgs args;
2877
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2880 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2881 ASSERT_EQ(originalScanCode, args.scanCode);
2882 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002883 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002884
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2887 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2888 ASSERT_EQ(originalScanCode, args.scanCode);
2889 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002890 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002891}
2892
Michael Wrightd02c5b62014-02-10 15:10:22 -08002893TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002894 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00002895 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002896 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002897
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002898 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002899}
2900
2901TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2902 const int32_t USAGE_A = 0x070004;
2903 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002904 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2905 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002906 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2907 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2908 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002909
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002910 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00002911 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002912 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002913 // Initial metastate is AMETA_NONE.
2914 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915
2916 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002918 NotifyKeyArgs args;
2919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2920 ASSERT_EQ(DEVICE_ID, args.deviceId);
2921 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2922 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2923 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2924 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2925 ASSERT_EQ(KEY_HOME, args.scanCode);
2926 ASSERT_EQ(AMETA_NONE, args.metaState);
2927 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2928 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2929 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2930
2931 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002932 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2934 ASSERT_EQ(DEVICE_ID, args.deviceId);
2935 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2936 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2937 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2938 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2939 ASSERT_EQ(KEY_HOME, args.scanCode);
2940 ASSERT_EQ(AMETA_NONE, args.metaState);
2941 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2942 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2943 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2944
2945 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2949 ASSERT_EQ(DEVICE_ID, args.deviceId);
2950 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2951 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2952 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2953 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2954 ASSERT_EQ(0, args.scanCode);
2955 ASSERT_EQ(AMETA_NONE, args.metaState);
2956 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2957 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2958 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2959
2960 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2962 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2964 ASSERT_EQ(DEVICE_ID, args.deviceId);
2965 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2966 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2967 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2968 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2969 ASSERT_EQ(0, args.scanCode);
2970 ASSERT_EQ(AMETA_NONE, args.metaState);
2971 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2972 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2973 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2974
2975 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2979 ASSERT_EQ(DEVICE_ID, args.deviceId);
2980 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2981 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2982 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2983 ASSERT_EQ(0, args.keyCode);
2984 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2985 ASSERT_EQ(AMETA_NONE, args.metaState);
2986 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2987 ASSERT_EQ(0U, args.policyFlags);
2988 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2989
2990 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2992 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2994 ASSERT_EQ(DEVICE_ID, args.deviceId);
2995 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2996 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2997 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2998 ASSERT_EQ(0, args.keyCode);
2999 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3000 ASSERT_EQ(AMETA_NONE, args.metaState);
3001 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3002 ASSERT_EQ(0U, args.policyFlags);
3003 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3004}
3005
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003006TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3007 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3008 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3009 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3010
3011 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003012 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003013 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3014
3015 // Key down by scan code.
3016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3017 NotifyKeyArgs args;
3018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3019 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3020
3021 // Key up by scan code.
3022 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3024 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3025}
3026
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003027/**
3028 * Ensure that the readTime is set to the time when the EV_KEY is received.
3029 */
3030TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3031 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3032
3033 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003034 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003035 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3036 NotifyKeyArgs args;
3037
3038 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003039 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3041 ASSERT_EQ(12, args.readTime);
3042
3043 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003044 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3046 ASSERT_EQ(15, args.readTime);
3047}
3048
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003050 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3051 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003052 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3053 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3054 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003056 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003057 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003058 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059
Arthur Hung95f68612022-04-07 14:08:22 +08003060 // Initial metastate is AMETA_NONE.
3061 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062
3063 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 NotifyKeyArgs args;
3066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3067 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003068 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003069 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070
3071 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003072 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3074 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003075 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076
3077 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003078 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3080 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003081 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082
3083 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003084 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3086 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003087 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003088 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089}
3090
3091TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003092 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3093 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3094 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3095 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003097 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003098 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003099 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100
Michael Wrighta9cf4192022-12-01 23:46:39 +00003101 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3103 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3104 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3105 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3106 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3107 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3108 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3109 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3110}
3111
3112TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003113 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3114 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3115 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3116 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003117
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003119 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003120 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003121 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003122
Michael Wrighta9cf4192022-12-01 23:46:39 +00003123 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003124 ASSERT_NO_FATAL_FAILURE(
3125 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3126 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3127 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3128 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3129 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3130 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3131 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003133 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003134 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003135 ASSERT_NO_FATAL_FAILURE(
3136 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3137 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3138 AKEYCODE_DPAD_UP, DISPLAY_ID));
3139 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3140 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3141 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3142 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003144 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003145 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003146 ASSERT_NO_FATAL_FAILURE(
3147 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3148 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3149 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3150 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3151 AKEYCODE_DPAD_UP, DISPLAY_ID));
3152 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3153 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003154
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003155 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003156 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003157 ASSERT_NO_FATAL_FAILURE(
3158 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3159 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3160 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3161 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3162 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3163 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3164 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003165
3166 // Special case: if orientation changes while key is down, we still emit the same keycode
3167 // in the key up as we did in the key down.
3168 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003169 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003170 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3173 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3174 ASSERT_EQ(KEY_UP, args.scanCode);
3175 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3176
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003177 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003178 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3181 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3182 ASSERT_EQ(KEY_UP, args.scanCode);
3183 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3184}
3185
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003186TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3187 // If the keyboard is not orientation aware,
3188 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003189 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003190
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003191 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003192 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003193 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003194 NotifyKeyArgs args;
3195
3196 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3201 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3202
Michael Wrighta9cf4192022-12-01 23:46:39 +00003203 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3208 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3209}
3210
3211TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3212 // If the keyboard is orientation aware,
3213 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003214 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003215
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003216 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003217 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003218 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003219 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003220 NotifyKeyArgs args;
3221
3222 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3223 // ^--- already checked by the previous test
3224
Michael Wrighta9cf4192022-12-01 23:46:39 +00003225 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003226 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3231 ASSERT_EQ(DISPLAY_ID, args.displayId);
3232
3233 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003234 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003235 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003236 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3241 ASSERT_EQ(newDisplayId, args.displayId);
3242}
3243
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003245 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003246 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003247 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003249 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003250 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003252 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003253 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254}
3255
Philip Junker4af3b3d2021-12-14 10:36:55 +01003256TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3257 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003258 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003259 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3260
3261 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3262 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3263 << "If a mapping is available, the result is equal to the mapping";
3264
3265 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3266 << "If no mapping is available, the result is the key location";
3267}
3268
Michael Wrightd02c5b62014-02-10 15:10:22 -08003269TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003270 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003271 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003272 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003274 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003275 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003277 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003278 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279}
3280
3281TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003282 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003283 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003284 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003286 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003289 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290 ASSERT_TRUE(flags[0]);
3291 ASSERT_FALSE(flags[1]);
3292}
3293
3294TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003295 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3296 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3297 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3298 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3299 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3300 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003302 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003303 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003304 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003305 // Initial metastate is AMETA_NONE.
3306 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307
3308 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003309 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3310 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3311 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312
3313 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003316 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3317 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3318 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003319 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320
3321 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003324 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3325 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3326 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003327 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328
3329 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003332 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3333 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3334 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003335 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336
3337 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003340 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3341 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3342 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003343 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344
3345 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003348 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3349 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3350 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003351 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352
3353 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003356 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3357 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3358 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003359 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360}
3361
Chris Yea52ade12020-08-27 16:49:20 -07003362TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3363 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3364 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3365 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3366 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3367
3368 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003369 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003370 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3371
Chris Yea52ade12020-08-27 16:49:20 -07003372 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003373 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003374 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3375 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3376 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3377 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3378
3379 NotifyKeyArgs args;
3380 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3383 ASSERT_EQ(AMETA_NONE, args.metaState);
3384 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3385 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3386 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3387
3388 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003389 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3391 ASSERT_EQ(AMETA_NONE, args.metaState);
3392 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3393 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3394 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3395}
3396
Arthur Hung2c9a3342019-07-23 14:18:59 +08003397TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3398 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003399 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3400 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3401 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3402 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003403
3404 // keyboard 2.
3405 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003406 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003407 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003408 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003409 std::shared_ptr<InputDevice> device2 =
3410 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003411 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003412
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003413 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3414 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3415 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3416 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003417
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003418 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003419 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003420 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003421
Arpit Singh67ca6842023-04-26 14:43:16 +00003422 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003423 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003424 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3425 mFakePolicy
3426 ->getReaderConfiguration(),
3427 AINPUT_SOURCE_KEYBOARD,
3428 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003429 std::list<NotifyArgs> unused =
3430 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003431 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003432 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003433
3434 // Prepared displays and associated info.
3435 constexpr uint8_t hdmi1 = 0;
3436 constexpr uint8_t hdmi2 = 1;
3437 const std::string SECONDARY_UNIQUE_ID = "local:1";
3438
3439 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3440 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3441
3442 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003443 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003444 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003445 ASSERT_FALSE(device2->isEnabled());
3446
3447 // Prepare second display.
3448 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003449 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003450 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003451 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003452 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003453 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003454 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003455 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003456
3457 // Device should be enabled after the associated display is found.
3458 ASSERT_TRUE(mDevice->isEnabled());
3459 ASSERT_TRUE(device2->isEnabled());
3460
3461 // Test pad key events
3462 ASSERT_NO_FATAL_FAILURE(
3463 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3464 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3465 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3466 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3467 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3468 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3469 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3470
3471 ASSERT_NO_FATAL_FAILURE(
3472 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3473 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3474 AKEYCODE_DPAD_RIGHT, newDisplayId));
3475 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3476 AKEYCODE_DPAD_DOWN, newDisplayId));
3477 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3478 AKEYCODE_DPAD_LEFT, newDisplayId));
3479}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480
arthurhungc903df12020-08-11 15:08:42 +08003481TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3482 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3483 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3484 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3485 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3486 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3487 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3488
3489 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003490 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003491 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003492 // Initial metastate is AMETA_NONE.
3493 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003494
3495 // Initialization should have turned all of the lights off.
3496 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3497 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3498 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3499
3500 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003503 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3504 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3505
3506 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003509 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3510 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3511
3512 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003513 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3514 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003515 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3516 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3517
3518 mFakeEventHub->removeDevice(EVENTHUB_ID);
3519 mReader->loopOnce();
3520
3521 // keyboard 2 should default toggle keys.
3522 const std::string USB2 = "USB2";
3523 const std::string DEVICE_NAME2 = "KEYBOARD2";
3524 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3525 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3526 std::shared_ptr<InputDevice> device2 =
3527 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003528 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003529 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3530 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3531 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3532 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3533 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3534 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3535
Arpit Singh67ca6842023-04-26 14:43:16 +00003536 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003537 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003538 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3539 mFakePolicy
3540 ->getReaderConfiguration(),
3541 AINPUT_SOURCE_KEYBOARD,
3542 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003543 std::list<NotifyArgs> unused =
3544 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003545 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003546 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003547
3548 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3549 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3550 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003551 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3552 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003553}
3554
Arthur Hungcb40a002021-08-03 14:31:01 +00003555TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3556 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3557 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3558 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3559
3560 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003561 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003562 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3563 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003564 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003565 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003566 // Initial metastate is AMETA_NONE.
3567 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003568
3569 mReader->toggleCapsLockState(DEVICE_ID);
3570 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3571}
3572
Arthur Hungfb3cc112022-04-13 07:39:50 +00003573TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3574 // keyboard 1.
3575 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3576 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3577 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3578 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3579 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3580 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3581
3582 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003583 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003584 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3585
3586 // keyboard 2.
3587 const std::string USB2 = "USB2";
3588 const std::string DEVICE_NAME2 = "KEYBOARD2";
3589 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3590 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3591 std::shared_ptr<InputDevice> device2 =
3592 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3593 ftl::Flags<InputDeviceClass>(0));
3594 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3595 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3596 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3597 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3598 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3599 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3600
Arpit Singh67ca6842023-04-26 14:43:16 +00003601 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003602 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003603 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3604 mFakePolicy
3605 ->getReaderConfiguration(),
3606 AINPUT_SOURCE_KEYBOARD,
3607 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003608 std::list<NotifyArgs> unused =
3609 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003610 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003611 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003612
Arthur Hung95f68612022-04-07 14:08:22 +08003613 // Initial metastate is AMETA_NONE.
3614 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3615 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3616
3617 // Toggle num lock on and off.
3618 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3619 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003620 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3621 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3622 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3623
3624 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3625 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3626 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3627 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3628 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3629
3630 // Toggle caps lock on and off.
3631 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3632 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3633 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3634 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3635 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3636
3637 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3638 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3639 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3640 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3641 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3642
3643 // Toggle scroll lock on and off.
3644 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3645 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3646 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3647 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3648 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3649
3650 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3651 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3652 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3653 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3654 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3655}
3656
Arthur Hung2141d542022-08-23 07:45:21 +00003657TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3658 const int32_t USAGE_A = 0x070004;
3659 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3660 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3661
3662 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003663 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003664 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3665 // Key down by scan code.
3666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3667 NotifyKeyArgs args;
3668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3669 ASSERT_EQ(DEVICE_ID, args.deviceId);
3670 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3671 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3672 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3673 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3674 ASSERT_EQ(KEY_HOME, args.scanCode);
3675 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3676
3677 // Disable device, it should synthesize cancellation events for down events.
3678 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003679 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003680
3681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3682 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3683 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3684 ASSERT_EQ(KEY_HOME, args.scanCode);
3685 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3686}
3687
Zixuan Qufecb6062022-11-12 04:44:31 +00003688TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00003689 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3690 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003691 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003692 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3693 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003694
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003695 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003696 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3697
3698 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003699 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003700
3701 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3702 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3703 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3704 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3705 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003706 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3707
3708 // Call change layout association with the same values: Generation shouldn't change
3709 generation = mReader->getContext()->getGeneration();
3710 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3711 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3712 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3713 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003714}
3715
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003716TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3717 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3718 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3719
3720 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00003721 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003722 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3723 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003724 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003725
3726 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3727 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3728}
3729
Justin Chung71ddb432023-03-27 04:29:07 +00003730TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
3731 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
3732 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003733 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00003734 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3735 NotifyKeyArgs args;
3736
3737 // Key down
3738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
3739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3740 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
3741}
3742
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003743// --- KeyboardInputMapperTest_ExternalDevice ---
3744
3745class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3746protected:
Chris Yea52ade12020-08-27 16:49:20 -07003747 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003748};
3749
3750TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003751 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3752 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003753
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003754 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3755 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3756 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3757 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003758
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003759 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003760 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003761 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003762
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003764 NotifyKeyArgs args;
3765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3766 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3767
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003768 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3770 ASSERT_EQ(uint32_t(0), args.policyFlags);
3771
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003774 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003775
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003776 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3778 ASSERT_EQ(uint32_t(0), args.policyFlags);
3779
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3782 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3783
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003784 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3786 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3787}
3788
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003789TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003790 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003791
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003792 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3793 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3794 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003795
Powei Fengd041c5d2019-05-03 17:11:33 -07003796 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003797 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003798 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003799 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003800
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003802 NotifyKeyArgs args;
3803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3804 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3805
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003806 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3808 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3809
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3812 ASSERT_EQ(uint32_t(0), args.policyFlags);
3813
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003814 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3816 ASSERT_EQ(uint32_t(0), args.policyFlags);
3817
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003818 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3820 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3821
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003822 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3824 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3825}
3826
Michael Wrightd02c5b62014-02-10 15:10:22 -08003827// --- CursorInputMapperTest ---
3828
3829class CursorInputMapperTest : public InputMapperTest {
3830protected:
3831 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3832
Michael Wright17db18e2020-06-26 20:51:44 +01003833 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003834
Chris Yea52ade12020-08-27 16:49:20 -07003835 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836 InputMapperTest::SetUp();
3837
Michael Wright17db18e2020-06-26 20:51:44 +01003838 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003839 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003840 }
3841
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003842 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3843 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003844
Michael Wrighta9cf4192022-12-01 23:46:39 +00003845 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003846 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3847 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3848 }
3849
3850 void prepareSecondaryDisplay() {
3851 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003852 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003853 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003854 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003855
3856 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3857 float pressure) {
3858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3859 0.0f, 0.0f, 0.0f, EPSILON));
3860 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003861};
3862
3863const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3864
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003865void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3866 int32_t originalY, int32_t rotatedX,
3867 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003868 NotifyMotionArgs args;
3869
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3872 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003875 ASSERT_NO_FATAL_FAILURE(
3876 assertCursorPointerCoords(args.pointerCoords[0],
3877 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3878 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879}
3880
3881TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003882 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00003883 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003884
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003885 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886}
3887
3888TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003889 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00003890 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003891
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003892 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893}
3894
3895TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003896 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00003897 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003898
3899 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003900 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901
3902 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003903 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3904 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3906 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3907
3908 // When the bounds are set, then there should be a valid motion range.
3909 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3910
3911 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003912 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913
3914 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3915 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3916 1, 800 - 1, 0.0f, 0.0f));
3917 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3918 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3919 2, 480 - 1, 0.0f, 0.0f));
3920 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3921 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3922 0.0f, 1.0f, 0.0f, 0.0f));
3923}
3924
3925TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003926 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00003927 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003928
3929 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003930 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931
3932 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3933 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3934 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3935 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3936 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3937 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3938 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3939 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3940 0.0f, 1.0f, 0.0f, 0.0f));
3941}
3942
3943TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003944 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00003945 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003946
arthurhungdcef2dc2020-08-11 14:47:50 +08003947 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003948
3949 NotifyMotionArgs args;
3950
3951 // Button press.
3952 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3956 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3957 ASSERT_EQ(DEVICE_ID, args.deviceId);
3958 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3959 ASSERT_EQ(uint32_t(0), args.policyFlags);
3960 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3961 ASSERT_EQ(0, args.flags);
3962 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3963 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3964 ASSERT_EQ(0, args.edgeFlags);
3965 ASSERT_EQ(uint32_t(1), args.pointerCount);
3966 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003967 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003968 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3970 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3971 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3972
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3974 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3975 ASSERT_EQ(DEVICE_ID, args.deviceId);
3976 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3977 ASSERT_EQ(uint32_t(0), args.policyFlags);
3978 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3979 ASSERT_EQ(0, args.flags);
3980 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3981 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3982 ASSERT_EQ(0, args.edgeFlags);
3983 ASSERT_EQ(uint32_t(1), args.pointerCount);
3984 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003985 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003986 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003987 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3988 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3989 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3990
Michael Wrightd02c5b62014-02-10 15:10:22 -08003991 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003992 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3993 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3995 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3996 ASSERT_EQ(DEVICE_ID, args.deviceId);
3997 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3998 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003999 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4000 ASSERT_EQ(0, args.flags);
4001 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4002 ASSERT_EQ(0, args.buttonState);
4003 ASSERT_EQ(0, args.edgeFlags);
4004 ASSERT_EQ(uint32_t(1), args.pointerCount);
4005 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004006 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004007 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004008 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4009 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4010 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4011
4012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4013 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4014 ASSERT_EQ(DEVICE_ID, args.deviceId);
4015 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4016 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4018 ASSERT_EQ(0, args.flags);
4019 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4020 ASSERT_EQ(0, args.buttonState);
4021 ASSERT_EQ(0, args.edgeFlags);
4022 ASSERT_EQ(uint32_t(1), args.pointerCount);
4023 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004024 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004025 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4027 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4028 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4029}
4030
4031TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004032 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004033 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034
4035 NotifyMotionArgs args;
4036
4037 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4041 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004042 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4043 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4044 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045
4046 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004051 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4052 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004053}
4054
4055TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004057 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004058
4059 NotifyMotionArgs args;
4060
4061 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4065 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004066 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004070 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004071
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004077 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004078
4079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004081 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004082}
4083
4084TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004086 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087
4088 NotifyMotionArgs args;
4089
4090 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4096 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004097 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4098 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4099 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4102 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004103 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4104 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4105 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004106
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004113 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4114 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4115 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004116
4117 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004122 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004123
4124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004126 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127}
4128
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004129TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004130 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004131 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004132 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4133 // need to be rotated.
4134 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhe036ad72023-04-27 12:48:15 +00004135 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004136
Michael Wrighta9cf4192022-12-01 23:46:39 +00004137 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4139 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4140 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4141 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4142 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4143 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4146}
4147
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004148TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004149 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004151 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4152 // orientation-aware are affected by display rotation.
Arpit Singhe036ad72023-04-27 12:48:15 +00004153 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004155 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004156 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4158 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4160 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4161 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4162 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4163 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4164 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4165
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004166 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004167 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004168 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4169 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4170 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4171 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4172 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4173 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4174 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4175 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004177 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004178 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4180 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4181 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4182 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4183 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4187
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004188 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004189 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004190 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4191 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4192 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4193 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4194 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4195 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4196 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4197 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004198}
4199
4200TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004202 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203
4204 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4205 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206
4207 NotifyMotionArgs motionArgs;
4208 NotifyKeyArgs keyArgs;
4209
4210 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4214 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4215 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004216 ASSERT_NO_FATAL_FAILURE(
4217 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4220 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4221 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004222 ASSERT_NO_FATAL_FAILURE(
4223 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004224
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004228 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004230 ASSERT_NO_FATAL_FAILURE(
4231 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232
4233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004234 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004236 ASSERT_NO_FATAL_FAILURE(
4237 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004238
4239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004241 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004242 ASSERT_NO_FATAL_FAILURE(
4243 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244
4245 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4250 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4251 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004252 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004253 ASSERT_NO_FATAL_FAILURE(
4254 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4257 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4258 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004259 ASSERT_NO_FATAL_FAILURE(
4260 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004261
4262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4263 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4264 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004265 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004266 ASSERT_NO_FATAL_FAILURE(
4267 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004268
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004272 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004274 ASSERT_NO_FATAL_FAILURE(
4275 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004276
4277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004279 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004280 ASSERT_NO_FATAL_FAILURE(
4281 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004283 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4287 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004288 ASSERT_NO_FATAL_FAILURE(
4289 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004292
4293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004296 ASSERT_NO_FATAL_FAILURE(
4297 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004298
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4300 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004302 ASSERT_NO_FATAL_FAILURE(
4303 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304
4305 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4309 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4310 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004311
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004313 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004315 ASSERT_NO_FATAL_FAILURE(
4316 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004317
4318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4319 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4320 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004321 ASSERT_NO_FATAL_FAILURE(
4322 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004327 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004329 ASSERT_NO_FATAL_FAILURE(
4330 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004331
4332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004334 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004335
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004336 ASSERT_NO_FATAL_FAILURE(
4337 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4339 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4340 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4341
4342 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4346 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4347 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004348
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004350 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004352 ASSERT_NO_FATAL_FAILURE(
4353 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004354
4355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4356 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4357 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004358 ASSERT_NO_FATAL_FAILURE(
4359 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004366 ASSERT_NO_FATAL_FAILURE(
4367 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004368
4369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4370 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4371 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004372 ASSERT_NO_FATAL_FAILURE(
4373 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004374
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4376 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4377 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4378
4379 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004380 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4383 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4384 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004385
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004387 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004389 ASSERT_NO_FATAL_FAILURE(
4390 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004391
4392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4393 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4394 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004395 ASSERT_NO_FATAL_FAILURE(
4396 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004401 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004403 ASSERT_NO_FATAL_FAILURE(
4404 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004405
4406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4407 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4408 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004409 ASSERT_NO_FATAL_FAILURE(
4410 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004411
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4413 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4414 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4415
4416 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4420 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4421 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004422
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004424 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004426 ASSERT_NO_FATAL_FAILURE(
4427 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004428
4429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4430 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4431 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004432 ASSERT_NO_FATAL_FAILURE(
4433 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004440 ASSERT_NO_FATAL_FAILURE(
4441 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004442
4443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4444 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4445 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004446 ASSERT_NO_FATAL_FAILURE(
4447 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004448
Michael Wrightd02c5b62014-02-10 15:10:22 -08004449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4450 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4451 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4452}
4453
4454TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004456 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457
4458 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4459 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460
4461 NotifyMotionArgs args;
4462
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4464 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4465 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004467 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4468 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4470 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 +00004471 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004472}
4473
4474TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004475 addConfigurationProperty("cursor.mode", "pointer");
4476 mFakePolicy->setPointerCapture(true);
Arpit Singhe036ad72023-04-27 12:48:15 +00004477 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004478
4479 NotifyDeviceResetArgs resetArgs;
4480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4481 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4482 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4483
4484 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4485 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004486
4487 NotifyMotionArgs args;
4488
4489 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4492 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
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_MOVE, args.action);
4496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4497 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 +00004498 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004499
4500 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4504 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4505 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4507 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4509 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4510 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4512 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4513
4514 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004515 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4516 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4518 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4519 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4521 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4523 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4524 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4526 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4527
4528 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004529 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4530 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4533 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4536 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 +00004537 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004538
4539 // Disable pointer capture and check that the device generation got bumped
4540 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004541 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004542 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004543 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004544 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004545
4546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004547 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4548
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4553 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004554 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4556 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 +00004557 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558}
4559
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004560/**
4561 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4562 * pointer acceleration or speed processing should not be applied.
4563 */
4564TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4565 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004566 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4567 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004568 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhe036ad72023-04-27 12:48:15 +00004569 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004570
4571 NotifyDeviceResetArgs resetArgs;
4572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4573 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4574 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4575
4576 NotifyMotionArgs args;
4577
4578 // Move and verify scale is applied.
4579 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4581 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4583 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4584 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4585 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4586 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4587 ASSERT_GT(relX, 10);
4588 ASSERT_GT(relY, 20);
4589
4590 // Enable Pointer Capture
4591 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004592 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004593 NotifyPointerCaptureChangedArgs captureArgs;
4594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4595 ASSERT_TRUE(captureArgs.request.enable);
4596
4597 // Move and verify scale is not applied.
4598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4599 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4600 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4602 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4604 ASSERT_EQ(10, args.pointerCoords[0].getX());
4605 ASSERT_EQ(20, args.pointerCoords[0].getY());
4606}
4607
Prabir Pradhan208360b2022-06-24 18:37:04 +00004608TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4609 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004610 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004611
4612 NotifyDeviceResetArgs resetArgs;
4613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4614 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4615 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4616
4617 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004618 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004619
4620 NotifyMotionArgs args;
4621
4622 // Verify that the coordinates are rotated.
4623 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4624 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4627 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4628 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4629 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4630 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4631
4632 // Enable Pointer Capture.
4633 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004634 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004635 NotifyPointerCaptureChangedArgs captureArgs;
4636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4637 ASSERT_TRUE(captureArgs.request.enable);
4638
4639 // Move and verify rotation is not applied.
4640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4644 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4645 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4646 ASSERT_EQ(10, args.pointerCoords[0].getX());
4647 ASSERT_EQ(20, args.pointerCoords[0].getY());
4648}
4649
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004650TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004651 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004652
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004653 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004654 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004655
4656 // Set up the secondary display as the display on which the pointer should be shown.
4657 // The InputDevice is not associated with any display.
4658 prepareSecondaryDisplay();
4659 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004660 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004661
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004662 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004663 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004664
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004665 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4667 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004670 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4671 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4672 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004673 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004674}
4675
4676TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004677 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004678
4679 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004680 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004681
4682 // Set up the secondary display as the display on which the pointer should be shown,
4683 // and associate the InputDevice with the secondary display.
4684 prepareSecondaryDisplay();
4685 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4686 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004687 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004688
4689 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4690 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004691
4692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004696 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4697 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4698 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004699 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004700}
4701
4702TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004703 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004704
4705 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004706 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004707 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4708
4709 // Associate the InputDevice with the secondary display.
4710 prepareSecondaryDisplay();
4711 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004712 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004713
4714 // The mapper should not generate any events because it is associated with a display that is
4715 // different from the pointer display.
4716 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4717 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004720}
4721
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004722// --- BluetoothCursorInputMapperTest ---
4723
4724class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4725protected:
4726 void SetUp() override {
4727 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4728
4729 mFakePointerController = std::make_shared<FakePointerController>();
4730 mFakePolicy->setPointerController(mFakePointerController);
4731 }
4732};
4733
4734TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4735 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004736 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004737
4738 nsecs_t kernelEventTime = ARBITRARY_TIME;
4739 nsecs_t expectedEventTime = ARBITRARY_TIME;
4740 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4741 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4743 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4744 WithEventTime(expectedEventTime))));
4745
4746 // Process several events that come in quick succession, according to their timestamps.
4747 for (int i = 0; i < 3; i++) {
4748 constexpr static nsecs_t delta = ms2ns(1);
4749 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4750 kernelEventTime += delta;
4751 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4752
4753 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4754 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4756 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4757 WithEventTime(expectedEventTime))));
4758 }
4759}
4760
4761TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4762 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004763 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004764
4765 nsecs_t expectedEventTime = ARBITRARY_TIME;
4766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4769 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4770 WithEventTime(expectedEventTime))));
4771
4772 // Process several events with the same timestamp from the kernel.
4773 // Ensure that we do not generate events too far into the future.
4774 constexpr static int32_t numEvents =
4775 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4776 for (int i = 0; i < numEvents; i++) {
4777 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4778
4779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4782 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4783 WithEventTime(expectedEventTime))));
4784 }
4785
4786 // By processing more events with the same timestamp, we should not generate events with a
4787 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4788 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4789 for (int i = 0; i < 3; i++) {
4790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4793 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4794 WithEventTime(cappedEventTime))));
4795 }
4796}
4797
4798TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4799 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004800 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004801
4802 nsecs_t kernelEventTime = ARBITRARY_TIME;
4803 nsecs_t expectedEventTime = ARBITRARY_TIME;
4804 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4805 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4807 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4808 WithEventTime(expectedEventTime))));
4809
4810 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4811 // smoothening is not needed, its timestamp is not affected.
4812 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4813 expectedEventTime = kernelEventTime;
4814
4815 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4816 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4818 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4819 WithEventTime(expectedEventTime))));
4820}
4821
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822// --- TouchInputMapperTest ---
4823
4824class TouchInputMapperTest : public InputMapperTest {
4825protected:
4826 static const int32_t RAW_X_MIN;
4827 static const int32_t RAW_X_MAX;
4828 static const int32_t RAW_Y_MIN;
4829 static const int32_t RAW_Y_MAX;
4830 static const int32_t RAW_TOUCH_MIN;
4831 static const int32_t RAW_TOUCH_MAX;
4832 static const int32_t RAW_TOOL_MIN;
4833 static const int32_t RAW_TOOL_MAX;
4834 static const int32_t RAW_PRESSURE_MIN;
4835 static const int32_t RAW_PRESSURE_MAX;
4836 static const int32_t RAW_ORIENTATION_MIN;
4837 static const int32_t RAW_ORIENTATION_MAX;
4838 static const int32_t RAW_DISTANCE_MIN;
4839 static const int32_t RAW_DISTANCE_MAX;
4840 static const int32_t RAW_TILT_MIN;
4841 static const int32_t RAW_TILT_MAX;
4842 static const int32_t RAW_ID_MIN;
4843 static const int32_t RAW_ID_MAX;
4844 static const int32_t RAW_SLOT_MIN;
4845 static const int32_t RAW_SLOT_MAX;
4846 static const float X_PRECISION;
4847 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004848 static const float X_PRECISION_VIRTUAL;
4849 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850
4851 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004852 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853
4854 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4855
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004856 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004857 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004858
Michael Wrightd02c5b62014-02-10 15:10:22 -08004859 enum Axes {
4860 POSITION = 1 << 0,
4861 TOUCH = 1 << 1,
4862 TOOL = 1 << 2,
4863 PRESSURE = 1 << 3,
4864 ORIENTATION = 1 << 4,
4865 MINOR = 1 << 5,
4866 ID = 1 << 6,
4867 DISTANCE = 1 << 7,
4868 TILT = 1 << 8,
4869 SLOT = 1 << 9,
4870 TOOL_TYPE = 1 << 10,
4871 };
4872
Michael Wrighta9cf4192022-12-01 23:46:39 +00004873 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004874 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004875 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004876 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004877 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878 int32_t toRawX(float displayX);
4879 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004880 int32_t toRotatedRawX(float displayX);
4881 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004882 float toCookedX(float rawX, float rawY);
4883 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004885 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004886 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004887 float toDisplayY(int32_t rawY, int32_t displayHeight);
4888
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889};
4890
4891const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4892const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4893const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4894const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4895const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4896const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4897const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4898const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004899const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4900const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004901const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4902const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4903const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4904const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4905const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4906const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4907const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4908const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4909const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4910const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4911const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4912const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004913const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4914 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4915const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4916 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004917const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4918 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919
4920const float TouchInputMapperTest::GEOMETRIC_SCALE =
4921 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4922 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4923
4924const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4925 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4926 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4927};
4928
Michael Wrighta9cf4192022-12-01 23:46:39 +00004929void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004930 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4931 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004932}
4933
4934void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4935 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004936 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004937}
4938
Michael Wrighta9cf4192022-12-01 23:46:39 +00004939void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004940 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4941 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4942 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004943}
4944
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004946 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4947 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4948 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4949 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950}
4951
Jason Gerecke489fda82012-09-07 17:19:40 -07004952void TouchInputMapperTest::prepareLocationCalibration() {
4953 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4954}
4955
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956int32_t TouchInputMapperTest::toRawX(float displayX) {
4957 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4958}
4959
4960int32_t TouchInputMapperTest::toRawY(float displayY) {
4961 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4962}
4963
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004964int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4965 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4966}
4967
4968int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4969 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4970}
4971
Jason Gerecke489fda82012-09-07 17:19:40 -07004972float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4973 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4974 return rawX;
4975}
4976
4977float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4978 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4979 return rawY;
4980}
4981
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004983 return toDisplayX(rawX, DISPLAY_WIDTH);
4984}
4985
4986float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4987 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988}
4989
4990float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004991 return toDisplayY(rawY, DISPLAY_HEIGHT);
4992}
4993
4994float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4995 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996}
4997
4998
4999// --- SingleTouchInputMapperTest ---
5000
5001class SingleTouchInputMapperTest : public TouchInputMapperTest {
5002protected:
5003 void prepareButtons();
5004 void prepareAxes(int axes);
5005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005006 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5007 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5008 void processUp(SingleTouchInputMapper& mappery);
5009 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5010 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5011 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5012 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5013 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5014 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015};
5016
5017void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005018 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005019}
5020
5021void SingleTouchInputMapperTest::prepareAxes(int axes) {
5022 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005023 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5024 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005025 }
5026 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005027 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5028 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005029 }
5030 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005031 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5032 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033 }
5034 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005035 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5036 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005037 }
5038 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005039 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5040 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041 }
5042}
5043
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005044void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5046 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005048}
5049
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005050void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005053}
5054
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005055void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057}
5058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005059void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061}
5062
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005063void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5064 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066}
5067
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005068void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070}
5071
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005072void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5073 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005076}
5077
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005078void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5079 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005081}
5082
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005083void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005084 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005085}
5086
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088 prepareButtons();
5089 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005090 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005091
Josep del Río2d8c79a2023-01-23 19:33:50 +00005092 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005093}
5094
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005096 prepareButtons();
5097 prepareAxes(POSITION);
5098 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00005099 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005101 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102}
5103
5104TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005106 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107 prepareButtons();
5108 prepareAxes(POSITION);
5109 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005110 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111
5112 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005113 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005114
5115 // Virtual key is down.
5116 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5117 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5118 processDown(mapper, x, y);
5119 processSync(mapper);
5120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5121
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005122 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123
5124 // Virtual key is up.
5125 processUp(mapper);
5126 processSync(mapper);
5127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005129 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130}
5131
5132TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005133 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005134 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005135 prepareButtons();
5136 prepareAxes(POSITION);
5137 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005138 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005139
5140 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005141 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005142
5143 // Virtual key is down.
5144 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5145 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5146 processDown(mapper, x, y);
5147 processSync(mapper);
5148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5149
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005150 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151
5152 // Virtual key is up.
5153 processUp(mapper);
5154 processSync(mapper);
5155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5156
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005157 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005158}
5159
5160TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005162 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005163 prepareButtons();
5164 prepareAxes(POSITION);
5165 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005166 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005167
Michael Wrightd02c5b62014-02-10 15:10:22 -08005168 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005169 ASSERT_TRUE(
5170 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005171 ASSERT_TRUE(flags[0]);
5172 ASSERT_FALSE(flags[1]);
5173}
5174
5175TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005177 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178 prepareButtons();
5179 prepareAxes(POSITION);
5180 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005181 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182
arthurhungdcef2dc2020-08-11 14:47:50 +08005183 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005184
5185 NotifyKeyArgs args;
5186
5187 // Press virtual key.
5188 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5189 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5190 processDown(mapper, x, y);
5191 processSync(mapper);
5192
5193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5194 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5195 ASSERT_EQ(DEVICE_ID, args.deviceId);
5196 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5197 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5198 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5199 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5200 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5201 ASSERT_EQ(KEY_HOME, args.scanCode);
5202 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5203 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5204
5205 // Release virtual key.
5206 processUp(mapper);
5207 processSync(mapper);
5208
5209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5210 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5211 ASSERT_EQ(DEVICE_ID, args.deviceId);
5212 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5213 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5214 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5215 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5216 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5217 ASSERT_EQ(KEY_HOME, args.scanCode);
5218 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5219 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5220
5221 // Should not have sent any motions.
5222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5223}
5224
5225TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005226 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005227 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005228 prepareButtons();
5229 prepareAxes(POSITION);
5230 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005231 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005232
arthurhungdcef2dc2020-08-11 14:47:50 +08005233 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005234
5235 NotifyKeyArgs keyArgs;
5236
5237 // Press virtual key.
5238 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5239 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5240 processDown(mapper, x, y);
5241 processSync(mapper);
5242
5243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5244 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5245 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5246 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5247 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5248 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5249 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5250 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5251 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5252 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5253 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5254
5255 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5256 // into the display area.
5257 y -= 100;
5258 processMove(mapper, x, y);
5259 processSync(mapper);
5260
5261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5262 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5263 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5264 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5265 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5266 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5267 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5268 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5269 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5270 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5271 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5272 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5273
5274 NotifyMotionArgs motionArgs;
5275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5276 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5277 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5278 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5279 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5280 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5281 ASSERT_EQ(0, motionArgs.flags);
5282 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5283 ASSERT_EQ(0, motionArgs.buttonState);
5284 ASSERT_EQ(0, motionArgs.edgeFlags);
5285 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5286 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005287 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5289 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5290 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5291 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5292 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5293
5294 // Keep moving out of bounds. Should generate a pointer move.
5295 y -= 50;
5296 processMove(mapper, x, y);
5297 processSync(mapper);
5298
5299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5300 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5301 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5302 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5303 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5305 ASSERT_EQ(0, motionArgs.flags);
5306 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5307 ASSERT_EQ(0, motionArgs.buttonState);
5308 ASSERT_EQ(0, motionArgs.edgeFlags);
5309 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5310 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005311 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5313 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5314 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5315 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5316 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5317
5318 // Release out of bounds. Should generate a pointer up.
5319 processUp(mapper);
5320 processSync(mapper);
5321
5322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5323 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5324 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5325 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5326 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5327 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5328 ASSERT_EQ(0, motionArgs.flags);
5329 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5330 ASSERT_EQ(0, motionArgs.buttonState);
5331 ASSERT_EQ(0, motionArgs.edgeFlags);
5332 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5333 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005334 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5336 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5337 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5338 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5339 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5340
5341 // Should not have sent any more keys or motions.
5342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5344}
5345
5346TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005347 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005348 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005349 prepareButtons();
5350 prepareAxes(POSITION);
5351 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005352 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353
arthurhungdcef2dc2020-08-11 14:47:50 +08005354 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005355
5356 NotifyMotionArgs motionArgs;
5357
5358 // Initially go down out of bounds.
5359 int32_t x = -10;
5360 int32_t y = -10;
5361 processDown(mapper, x, y);
5362 processSync(mapper);
5363
5364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5365
5366 // Move into the display area. Should generate a pointer down.
5367 x = 50;
5368 y = 75;
5369 processMove(mapper, x, y);
5370 processSync(mapper);
5371
5372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5373 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5374 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5375 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5376 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5377 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5378 ASSERT_EQ(0, motionArgs.flags);
5379 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5380 ASSERT_EQ(0, motionArgs.buttonState);
5381 ASSERT_EQ(0, motionArgs.edgeFlags);
5382 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5383 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005384 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5386 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5387 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5388 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5390
5391 // Release. Should generate a pointer up.
5392 processUp(mapper);
5393 processSync(mapper);
5394
5395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5396 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5397 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5398 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5399 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5400 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5401 ASSERT_EQ(0, motionArgs.flags);
5402 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5403 ASSERT_EQ(0, motionArgs.buttonState);
5404 ASSERT_EQ(0, motionArgs.edgeFlags);
5405 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5406 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005407 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5409 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5410 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5411 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5412 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5413
5414 // Should not have sent any more keys or motions.
5415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5417}
5418
Santos Cordonfa5cf462017-04-05 10:37:00 -07005419TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005420 addConfigurationProperty("touch.deviceType", "touchScreen");
5421 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5422
Michael Wrighta9cf4192022-12-01 23:46:39 +00005423 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005424 prepareButtons();
5425 prepareAxes(POSITION);
5426 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005427 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005428
arthurhungdcef2dc2020-08-11 14:47:50 +08005429 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005430
5431 NotifyMotionArgs motionArgs;
5432
5433 // Down.
5434 int32_t x = 100;
5435 int32_t y = 125;
5436 processDown(mapper, x, y);
5437 processSync(mapper);
5438
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5440 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5441 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5442 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5443 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5444 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5445 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5446 ASSERT_EQ(0, motionArgs.flags);
5447 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5448 ASSERT_EQ(0, motionArgs.buttonState);
5449 ASSERT_EQ(0, motionArgs.edgeFlags);
5450 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5451 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005452 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5454 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5455 1, 0, 0, 0, 0, 0, 0, 0));
5456 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5457 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5458 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5459
5460 // Move.
5461 x += 50;
5462 y += 75;
5463 processMove(mapper, x, y);
5464 processSync(mapper);
5465
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5467 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5468 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5469 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5470 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5471 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5473 ASSERT_EQ(0, motionArgs.flags);
5474 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5475 ASSERT_EQ(0, motionArgs.buttonState);
5476 ASSERT_EQ(0, motionArgs.edgeFlags);
5477 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5478 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005479 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5481 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5482 1, 0, 0, 0, 0, 0, 0, 0));
5483 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5484 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5485 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5486
5487 // Up.
5488 processUp(mapper);
5489 processSync(mapper);
5490
5491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5492 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5493 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5494 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5495 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5496 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5497 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5498 ASSERT_EQ(0, motionArgs.flags);
5499 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5500 ASSERT_EQ(0, motionArgs.buttonState);
5501 ASSERT_EQ(0, motionArgs.edgeFlags);
5502 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5503 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005504 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5506 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5507 1, 0, 0, 0, 0, 0, 0, 0));
5508 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5509 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5510 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5511
5512 // Should not have sent any more keys or motions.
5513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5515}
5516
Michael Wrightd02c5b62014-02-10 15:10:22 -08005517TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005518 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005519 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005520 prepareButtons();
5521 prepareAxes(POSITION);
5522 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005523 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005524
arthurhungdcef2dc2020-08-11 14:47:50 +08005525 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005526
5527 NotifyMotionArgs motionArgs;
5528
5529 // Down.
5530 int32_t x = 100;
5531 int32_t y = 125;
5532 processDown(mapper, x, y);
5533 processSync(mapper);
5534
5535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5536 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5537 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5538 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5539 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5540 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5541 ASSERT_EQ(0, motionArgs.flags);
5542 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5543 ASSERT_EQ(0, motionArgs.buttonState);
5544 ASSERT_EQ(0, motionArgs.edgeFlags);
5545 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5546 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005547 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5549 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5550 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5551 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5552 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5553
5554 // Move.
5555 x += 50;
5556 y += 75;
5557 processMove(mapper, x, y);
5558 processSync(mapper);
5559
5560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5561 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5562 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5563 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5564 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5566 ASSERT_EQ(0, motionArgs.flags);
5567 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5568 ASSERT_EQ(0, motionArgs.buttonState);
5569 ASSERT_EQ(0, motionArgs.edgeFlags);
5570 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5571 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005572 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5574 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5575 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5576 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5577 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5578
5579 // Up.
5580 processUp(mapper);
5581 processSync(mapper);
5582
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5584 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5585 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5586 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5587 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5588 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5589 ASSERT_EQ(0, motionArgs.flags);
5590 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5591 ASSERT_EQ(0, motionArgs.buttonState);
5592 ASSERT_EQ(0, motionArgs.edgeFlags);
5593 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5594 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005595 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5597 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5598 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5599 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5600 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5601
5602 // Should not have sent any more keys or motions.
5603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5605}
5606
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005607TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005608 addConfigurationProperty("touch.deviceType", "touchScreen");
5609 prepareButtons();
5610 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005611 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5612 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005613 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005614
5615 NotifyMotionArgs args;
5616
5617 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005618 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005619 processDown(mapper, toRawX(50), toRawY(75));
5620 processSync(mapper);
5621
5622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5623 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5624 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5625
5626 processUp(mapper);
5627 processSync(mapper);
5628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5629}
5630
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005631TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005632 addConfigurationProperty("touch.deviceType", "touchScreen");
5633 prepareButtons();
5634 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005635 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5636 // orientation-aware are affected by display rotation.
5637 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005638 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005639
5640 NotifyMotionArgs args;
5641
5642 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005643 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005644 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005645 processDown(mapper, toRawX(50), toRawY(75));
5646 processSync(mapper);
5647
5648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5649 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5650 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5651
5652 processUp(mapper);
5653 processSync(mapper);
5654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5655
5656 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005657 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005658 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005659 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005660 processSync(mapper);
5661
5662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5663 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5664 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5665
5666 processUp(mapper);
5667 processSync(mapper);
5668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5669
5670 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005671 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005672 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005673 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5674 processSync(mapper);
5675
5676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5677 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5678 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5679
5680 processUp(mapper);
5681 processSync(mapper);
5682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5683
5684 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005685 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005686 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005687 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005688 processSync(mapper);
5689
5690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5691 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5692 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5693
5694 processUp(mapper);
5695 processSync(mapper);
5696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5697}
5698
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005699TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5700 addConfigurationProperty("touch.deviceType", "touchScreen");
5701 prepareButtons();
5702 prepareAxes(POSITION);
5703 addConfigurationProperty("touch.orientationAware", "1");
5704 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5705 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005706 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005707 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005708 NotifyMotionArgs args;
5709
5710 // Orientation 0.
5711 processDown(mapper, toRawX(50), toRawY(75));
5712 processSync(mapper);
5713
5714 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5715 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5716 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5717
5718 processUp(mapper);
5719 processSync(mapper);
5720 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5721}
5722
5723TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5724 addConfigurationProperty("touch.deviceType", "touchScreen");
5725 prepareButtons();
5726 prepareAxes(POSITION);
5727 addConfigurationProperty("touch.orientationAware", "1");
5728 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5729 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005730 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005731 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005732 NotifyMotionArgs args;
5733
5734 // Orientation 90.
5735 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5736 processSync(mapper);
5737
5738 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5739 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5740 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5741
5742 processUp(mapper);
5743 processSync(mapper);
5744 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5745}
5746
5747TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5748 addConfigurationProperty("touch.deviceType", "touchScreen");
5749 prepareButtons();
5750 prepareAxes(POSITION);
5751 addConfigurationProperty("touch.orientationAware", "1");
5752 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5753 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005754 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005755 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005756 NotifyMotionArgs args;
5757
5758 // Orientation 180.
5759 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5760 processSync(mapper);
5761
5762 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5763 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5764 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5765
5766 processUp(mapper);
5767 processSync(mapper);
5768 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5769}
5770
5771TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5772 addConfigurationProperty("touch.deviceType", "touchScreen");
5773 prepareButtons();
5774 prepareAxes(POSITION);
5775 addConfigurationProperty("touch.orientationAware", "1");
5776 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5777 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005778 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005779 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005780 NotifyMotionArgs args;
5781
5782 // Orientation 270.
5783 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5784 processSync(mapper);
5785
5786 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5787 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5788 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5789
5790 processUp(mapper);
5791 processSync(mapper);
5792 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5793}
5794
5795TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5796 addConfigurationProperty("touch.deviceType", "touchScreen");
5797 prepareButtons();
5798 prepareAxes(POSITION);
5799 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5800 // orientation-aware are affected by display rotation.
5801 addConfigurationProperty("touch.orientationAware", "0");
5802 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005803 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005804
5805 NotifyMotionArgs args;
5806
5807 // Orientation 90, Rotation 0.
5808 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005809 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005810 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5811 processSync(mapper);
5812
5813 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5814 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5815 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5816
5817 processUp(mapper);
5818 processSync(mapper);
5819 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5820
5821 // Orientation 90, Rotation 90.
5822 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005823 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005824 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005825 processSync(mapper);
5826
5827 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5828 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5829 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5830
5831 processUp(mapper);
5832 processSync(mapper);
5833 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5834
5835 // Orientation 90, Rotation 180.
5836 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005837 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005838 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5839 processSync(mapper);
5840
5841 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5842 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5843 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5844
5845 processUp(mapper);
5846 processSync(mapper);
5847 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5848
5849 // Orientation 90, Rotation 270.
5850 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005851 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005852 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 -07005853 processSync(mapper);
5854
5855 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5856 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5857 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5858
5859 processUp(mapper);
5860 processSync(mapper);
5861 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5862}
5863
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005864TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5865 addConfigurationProperty("touch.deviceType", "touchScreen");
5866 prepareButtons();
5867 prepareAxes(POSITION);
5868 addConfigurationProperty("touch.orientationAware", "1");
5869 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005870 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005871
5872 // Set a physical frame in the display viewport.
5873 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5874 viewport->physicalLeft = 20;
5875 viewport->physicalTop = 600;
5876 viewport->physicalRight = 30;
5877 viewport->physicalBottom = 610;
5878 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005879 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005880
5881 // Start the touch.
5882 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5883 processSync(mapper);
5884
5885 // Expect all input starting outside the physical frame to be ignored.
5886 const std::array<Point, 6> outsidePoints = {
5887 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5888 for (const auto& p : outsidePoints) {
5889 processMove(mapper, toRawX(p.x), toRawY(p.y));
5890 processSync(mapper);
5891 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5892 }
5893
5894 // Move the touch into the physical frame.
5895 processMove(mapper, toRawX(25), toRawY(605));
5896 processSync(mapper);
5897 NotifyMotionArgs args;
5898 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5899 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5900 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5901 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5902
5903 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5904 for (const auto& p : outsidePoints) {
5905 processMove(mapper, toRawX(p.x), toRawY(p.y));
5906 processSync(mapper);
5907 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5908 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5909 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5910 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5911 }
5912
5913 processUp(mapper);
5914 processSync(mapper);
5915 EXPECT_NO_FATAL_FAILURE(
5916 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5917}
5918
Michael Wrightd02c5b62014-02-10 15:10:22 -08005919TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005920 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005921 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005922 prepareButtons();
5923 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00005924 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005925
5926 // These calculations are based on the input device calibration documentation.
5927 int32_t rawX = 100;
5928 int32_t rawY = 200;
5929 int32_t rawPressure = 10;
5930 int32_t rawToolMajor = 12;
5931 int32_t rawDistance = 2;
5932 int32_t rawTiltX = 30;
5933 int32_t rawTiltY = 110;
5934
5935 float x = toDisplayX(rawX);
5936 float y = toDisplayY(rawY);
5937 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5938 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5939 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5940 float distance = float(rawDistance);
5941
5942 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5943 float tiltScale = M_PI / 180;
5944 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5945 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5946 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5947 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5948
5949 processDown(mapper, rawX, rawY);
5950 processPressure(mapper, rawPressure);
5951 processToolMajor(mapper, rawToolMajor);
5952 processDistance(mapper, rawDistance);
5953 processTilt(mapper, rawTiltX, rawTiltY);
5954 processSync(mapper);
5955
5956 NotifyMotionArgs args;
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5959 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5960 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5961}
5962
Jason Gerecke489fda82012-09-07 17:19:40 -07005963TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005964 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005965 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005966 prepareLocationCalibration();
5967 prepareButtons();
5968 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005969 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005970
5971 int32_t rawX = 100;
5972 int32_t rawY = 200;
5973
5974 float x = toDisplayX(toCookedX(rawX, rawY));
5975 float y = toDisplayY(toCookedY(rawX, rawY));
5976
5977 processDown(mapper, rawX, rawY);
5978 processSync(mapper);
5979
5980 NotifyMotionArgs args;
5981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5983 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5984}
5985
Michael Wrightd02c5b62014-02-10 15:10:22 -08005986TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005987 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005988 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005989 prepareButtons();
5990 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005991 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005992
5993 NotifyMotionArgs motionArgs;
5994 NotifyKeyArgs keyArgs;
5995
5996 processDown(mapper, 100, 200);
5997 processSync(mapper);
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5999 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6000 ASSERT_EQ(0, motionArgs.buttonState);
6001
6002 // press BTN_LEFT, release BTN_LEFT
6003 processKey(mapper, BTN_LEFT, 1);
6004 processSync(mapper);
6005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6007 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6008
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6010 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6011 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6012
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013 processKey(mapper, BTN_LEFT, 0);
6014 processSync(mapper);
6015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006016 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006017 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006018
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006021 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022
6023 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6024 processKey(mapper, BTN_RIGHT, 1);
6025 processKey(mapper, BTN_MIDDLE, 1);
6026 processSync(mapper);
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6029 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6030 motionArgs.buttonState);
6031
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6033 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6034 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6035
6036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6037 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6038 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6039 motionArgs.buttonState);
6040
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041 processKey(mapper, BTN_RIGHT, 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(AMOTION_EVENT_BUTTON_TERTIARY, 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(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006050
6051 processKey(mapper, BTN_MIDDLE, 0);
6052 processSync(mapper);
6053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006054 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006055 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006056
6057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006059 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006060
6061 // press BTN_BACK, release BTN_BACK
6062 processKey(mapper, BTN_BACK, 1);
6063 processSync(mapper);
6064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6065 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6066 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006067
Michael Wrightd02c5b62014-02-10 15:10:22 -08006068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006070 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6071
6072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6073 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6074 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006075
6076 processKey(mapper, BTN_BACK, 0);
6077 processSync(mapper);
6078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006079 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006081
6082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006084 ASSERT_EQ(0, motionArgs.buttonState);
6085
Michael Wrightd02c5b62014-02-10 15:10:22 -08006086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6087 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6088 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6089
6090 // press BTN_SIDE, release BTN_SIDE
6091 processKey(mapper, BTN_SIDE, 1);
6092 processSync(mapper);
6093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6094 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6095 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006096
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006099 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6100
6101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6102 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6103 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006104
6105 processKey(mapper, BTN_SIDE, 0);
6106 processSync(mapper);
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006108 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006110
6111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006113 ASSERT_EQ(0, motionArgs.buttonState);
6114
Michael Wrightd02c5b62014-02-10 15:10:22 -08006115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6116 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6117 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6118
6119 // press BTN_FORWARD, release BTN_FORWARD
6120 processKey(mapper, BTN_FORWARD, 1);
6121 processSync(mapper);
6122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6123 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6124 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006125
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006127 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006128 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6129
6130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6131 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6132 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006133
6134 processKey(mapper, BTN_FORWARD, 0);
6135 processSync(mapper);
6136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006137 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006139
6140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006142 ASSERT_EQ(0, motionArgs.buttonState);
6143
Michael Wrightd02c5b62014-02-10 15:10:22 -08006144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6145 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6146 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6147
6148 // press BTN_EXTRA, release BTN_EXTRA
6149 processKey(mapper, BTN_EXTRA, 1);
6150 processSync(mapper);
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6152 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6153 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006154
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006157 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6158
6159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6161 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006162
6163 processKey(mapper, BTN_EXTRA, 0);
6164 processSync(mapper);
6165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006166 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006168
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006171 ASSERT_EQ(0, motionArgs.buttonState);
6172
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6174 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6175 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6176
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6178
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179 // press BTN_STYLUS, release BTN_STYLUS
6180 processKey(mapper, BTN_STYLUS, 1);
6181 processSync(mapper);
6182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006184 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6185
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6187 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6188 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189
6190 processKey(mapper, BTN_STYLUS, 0);
6191 processSync(mapper);
6192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006193 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006195
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006198 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006199
6200 // press BTN_STYLUS2, release BTN_STYLUS2
6201 processKey(mapper, BTN_STYLUS2, 1);
6202 processSync(mapper);
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006205 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6206
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6208 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6209 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210
6211 processKey(mapper, BTN_STYLUS2, 0);
6212 processSync(mapper);
6213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006214 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006215 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006216
6217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006219 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220
6221 // release touch
6222 processUp(mapper);
6223 processSync(mapper);
6224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6225 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6226 ASSERT_EQ(0, motionArgs.buttonState);
6227}
6228
6229TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006230 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006231 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006232 prepareButtons();
6233 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006234 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006235
6236 NotifyMotionArgs motionArgs;
6237
6238 // default tool type is finger
6239 processDown(mapper, 100, 200);
6240 processSync(mapper);
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6242 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006243 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244
6245 // eraser
6246 processKey(mapper, BTN_TOOL_RUBBER, 1);
6247 processSync(mapper);
6248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006250 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006251
6252 // stylus
6253 processKey(mapper, BTN_TOOL_RUBBER, 0);
6254 processKey(mapper, BTN_TOOL_PEN, 1);
6255 processSync(mapper);
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006258 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006259
6260 // brush
6261 processKey(mapper, BTN_TOOL_PEN, 0);
6262 processKey(mapper, BTN_TOOL_BRUSH, 1);
6263 processSync(mapper);
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006266 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267
6268 // pencil
6269 processKey(mapper, BTN_TOOL_BRUSH, 0);
6270 processKey(mapper, BTN_TOOL_PENCIL, 1);
6271 processSync(mapper);
6272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006274 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006276 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006277 processKey(mapper, BTN_TOOL_PENCIL, 0);
6278 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6279 processSync(mapper);
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006282 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283
6284 // mouse
6285 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6286 processKey(mapper, BTN_TOOL_MOUSE, 1);
6287 processSync(mapper);
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006290 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006291
6292 // lens
6293 processKey(mapper, BTN_TOOL_MOUSE, 0);
6294 processKey(mapper, BTN_TOOL_LENS, 1);
6295 processSync(mapper);
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006298 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006299
6300 // double-tap
6301 processKey(mapper, BTN_TOOL_LENS, 0);
6302 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6303 processSync(mapper);
6304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006306 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307
6308 // triple-tap
6309 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6310 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6311 processSync(mapper);
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006314 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006315
6316 // quad-tap
6317 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6318 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6319 processSync(mapper);
6320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006322 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006323
6324 // finger
6325 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6326 processKey(mapper, BTN_TOOL_FINGER, 1);
6327 processSync(mapper);
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006330 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331
6332 // stylus trumps finger
6333 processKey(mapper, BTN_TOOL_PEN, 1);
6334 processSync(mapper);
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006337 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338
6339 // eraser trumps stylus
6340 processKey(mapper, BTN_TOOL_RUBBER, 1);
6341 processSync(mapper);
6342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006344 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006345
6346 // mouse trumps eraser
6347 processKey(mapper, BTN_TOOL_MOUSE, 1);
6348 processSync(mapper);
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006351 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006352
6353 // back to default tool type
6354 processKey(mapper, BTN_TOOL_MOUSE, 0);
6355 processKey(mapper, BTN_TOOL_RUBBER, 0);
6356 processKey(mapper, BTN_TOOL_PEN, 0);
6357 processKey(mapper, BTN_TOOL_FINGER, 0);
6358 processSync(mapper);
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006361 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362}
6363
6364TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006366 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367 prepareButtons();
6368 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006369 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006370 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371
6372 NotifyMotionArgs motionArgs;
6373
6374 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6375 processKey(mapper, BTN_TOOL_FINGER, 1);
6376 processMove(mapper, 100, 200);
6377 processSync(mapper);
6378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6379 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6381 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6382
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6384 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6386 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6387
6388 // move a little
6389 processMove(mapper, 150, 250);
6390 processSync(mapper);
6391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6392 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6394 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6395
6396 // down when BTN_TOUCH is pressed, pressure defaults to 1
6397 processKey(mapper, BTN_TOUCH, 1);
6398 processSync(mapper);
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6400 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6402 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6403
6404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6405 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6407 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6408
6409 // up when BTN_TOUCH is released, hover restored
6410 processKey(mapper, BTN_TOUCH, 0);
6411 processSync(mapper);
6412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6413 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6415 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6416
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6418 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6420 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6421
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6425 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6426
6427 // exit hover when pointer goes away
6428 processKey(mapper, BTN_TOOL_FINGER, 0);
6429 processSync(mapper);
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6431 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6433 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6434}
6435
6436TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006437 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006438 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006439 prepareButtons();
6440 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006441 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006442
6443 NotifyMotionArgs motionArgs;
6444
6445 // initially hovering because pressure is 0
6446 processDown(mapper, 100, 200);
6447 processPressure(mapper, 0);
6448 processSync(mapper);
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6450 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6452 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6453
6454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6455 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6457 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6458
6459 // move a little
6460 processMove(mapper, 150, 250);
6461 processSync(mapper);
6462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6463 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6465 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6466
6467 // down when pressure is non-zero
6468 processPressure(mapper, RAW_PRESSURE_MAX);
6469 processSync(mapper);
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6473 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6474
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6476 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6478 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6479
6480 // up when pressure becomes 0, hover restored
6481 processPressure(mapper, 0);
6482 processSync(mapper);
6483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6484 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6486 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6487
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6491 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6492
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6496 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6497
6498 // exit hover when pointer goes away
6499 processUp(mapper);
6500 processSync(mapper);
6501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6502 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6504 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6505}
6506
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006507TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6508 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006509 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006510 prepareButtons();
6511 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006512 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006513
6514 // Touch down.
6515 processDown(mapper, 100, 200);
6516 processPressure(mapper, 1);
6517 processSync(mapper);
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6519 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6520
6521 // Reset the mapper. This should cancel the ongoing gesture.
6522 resetMapper(mapper, ARBITRARY_TIME);
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6524 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6525
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6527}
6528
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006529TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6530 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006531 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006532 prepareButtons();
6533 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006534 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006535
6536 // Set the initial state for the touch pointer.
6537 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6538 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6539 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6540 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6541
6542 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006543 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6544 // does not generate any events.
6545 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006546
6547 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6548 // the recreated touch state to generate a down event.
6549 processSync(mapper);
6550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6551 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6552
6553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6554}
6555
lilinnan687e58f2022-07-19 16:00:50 +08006556TEST_F(SingleTouchInputMapperTest,
6557 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6558 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006559 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006560 prepareButtons();
6561 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006562 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006563 NotifyMotionArgs motionArgs;
6564
6565 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006566 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006567 processSync(mapper);
6568
6569 // We should receive a down event
6570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6571 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6572
6573 // Change display id
6574 clearViewports();
6575 prepareSecondaryDisplay(ViewportType::INTERNAL);
6576
6577 // We should receive a cancel event
6578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6579 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6580 // Then receive reset called
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6582}
6583
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006584TEST_F(SingleTouchInputMapperTest,
6585 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6586 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006587 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006588 prepareButtons();
6589 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006590 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6592 NotifyMotionArgs motionArgs;
6593
6594 // Start a new gesture.
6595 processDown(mapper, 100, 200);
6596 processSync(mapper);
6597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6598 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6599
6600 // Make the viewport inactive. This will put the device in disabled mode.
6601 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6602 viewport->isActive = false;
6603 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006604 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006605
6606 // We should receive a cancel event for the ongoing gesture.
6607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6608 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6609 // Then we should be notified that the device was reset.
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6611
6612 // No events are generated while the viewport is inactive.
6613 processMove(mapper, 101, 201);
6614 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006615 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006616 processSync(mapper);
6617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6618
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006619 // Start a new gesture while the viewport is still inactive.
6620 processDown(mapper, 300, 400);
6621 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6622 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6623 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6624 processSync(mapper);
6625
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006626 // Make the viewport active again. The device should resume processing events.
6627 viewport->isActive = true;
6628 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006629 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006630
6631 // The device is reset because it changes back to direct mode, without generating any events.
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6634
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006635 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006636 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6638 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006639
6640 // No more events.
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6643}
6644
Prabir Pradhan211ba622022-10-31 21:09:21 +00006645TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6646 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006647 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006648 prepareButtons();
6649 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006650 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6652
6653 // Press a stylus button.
6654 processKey(mapper, BTN_STYLUS, 1);
6655 processSync(mapper);
6656
6657 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6658 processDown(mapper, 100, 200);
6659 processSync(mapper);
6660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6661 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6662 WithCoords(toDisplayX(100), toDisplayY(200)),
6663 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6665 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6666 WithCoords(toDisplayX(100), toDisplayY(200)),
6667 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6668
6669 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6670 // the button has not actually been released, since there will be no pointers through which the
6671 // button state can be reported. The event is generated at the location of the pointer before
6672 // it went up.
6673 processUp(mapper);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6676 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6677 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6679 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6680 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6681}
6682
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006683TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6684 addConfigurationProperty("touch.deviceType", "touchScreen");
6685 prepareDisplay(ui::ROTATION_0);
6686 prepareButtons();
6687 prepareAxes(POSITION);
6688
6689 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6690
Arpit Singha8c236b2023-04-25 13:56:05 +00006691 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6693
6694 // Press a stylus button.
6695 processKey(mapper, BTN_STYLUS, 1);
6696 processSync(mapper);
6697
6698 // Start a touch gesture and ensure that the stylus button is not reported.
6699 processDown(mapper, 100, 200);
6700 processSync(mapper);
6701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6702 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6703
6704 // Release and press the stylus button again.
6705 processKey(mapper, BTN_STYLUS, 0);
6706 processSync(mapper);
6707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6708 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6709 processKey(mapper, BTN_STYLUS, 1);
6710 processSync(mapper);
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6712 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6713
6714 // Release the touch gesture.
6715 processUp(mapper);
6716 processSync(mapper);
6717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6718 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6719
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6721}
6722
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006723TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6724 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6725 prepareDisplay(ui::ROTATION_0);
6726 prepareButtons();
6727 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006728 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6730
6731 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6732}
6733
Seunghwan Choi356026c2023-02-01 14:37:25 +09006734TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6735 std::shared_ptr<FakePointerController> fakePointerController =
6736 std::make_shared<FakePointerController>();
6737 addConfigurationProperty("touch.deviceType", "touchScreen");
6738 prepareDisplay(ui::ROTATION_0);
6739 prepareButtons();
6740 prepareAxes(POSITION);
6741 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6742 mFakePolicy->setPointerController(fakePointerController);
6743 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00006744 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006745
6746 processKey(mapper, BTN_TOOL_PEN, 1);
6747 processMove(mapper, 100, 200);
6748 processSync(mapper);
6749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6750 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006751 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006752 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6753 ASSERT_TRUE(fakePointerController->isPointerShown());
6754 ASSERT_NO_FATAL_FAILURE(
6755 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6756}
6757
6758TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6759 std::shared_ptr<FakePointerController> fakePointerController =
6760 std::make_shared<FakePointerController>();
6761 addConfigurationProperty("touch.deviceType", "touchScreen");
6762 prepareDisplay(ui::ROTATION_0);
6763 prepareButtons();
6764 prepareAxes(POSITION);
6765 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6766 mFakePolicy->setPointerController(fakePointerController);
6767 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00006768 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006769
6770 processKey(mapper, BTN_TOOL_PEN, 1);
6771 processMove(mapper, 100, 200);
6772 processSync(mapper);
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6774 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006775 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006776 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6777 ASSERT_FALSE(fakePointerController->isPointerShown());
6778}
6779
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006780TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6781 // Initialize the device without setting device source to touch navigation.
6782 addConfigurationProperty("touch.deviceType", "touchScreen");
6783 prepareDisplay(ui::ROTATION_0);
6784 prepareButtons();
6785 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006786 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006787
6788 // Ensure that the device is created as a touchscreen, not touch navigation.
6789 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6790
6791 // Add device type association after the device was created.
6792 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6793
6794 // Send update to the mapper.
6795 std::list<NotifyArgs> unused2 =
6796 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006797 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006798
6799 // Check whether device type update was successful.
6800 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6801}
6802
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006803TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6804 // Initialize the device without setting device source to touch navigation.
6805 addConfigurationProperty("touch.deviceType", "touchScreen");
6806 prepareDisplay(ui::ROTATION_0);
6807 prepareButtons();
6808 prepareAxes(POSITION);
6809 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6810
6811 // Set a physical frame in the display viewport.
6812 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6813 viewport->physicalLeft = 0;
6814 viewport->physicalTop = 0;
6815 viewport->physicalRight = DISPLAY_WIDTH / 2;
6816 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6817 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006818 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006819
Arpit Singha8c236b2023-04-25 13:56:05 +00006820 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006821
6822 // Hovering inside the physical frame produces events.
6823 processKey(mapper, BTN_TOOL_PEN, 1);
6824 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6825 processSync(mapper);
6826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6827 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6829 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6830
6831 // Leaving the physical frame ends the hovering gesture.
6832 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6833 processSync(mapper);
6834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6835 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6836
6837 // Moving outside the physical frame does not produce events.
6838 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6839 processSync(mapper);
6840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6841
6842 // Re-entering the physical frame produces events.
6843 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6844 processSync(mapper);
6845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6846 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6848 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6849}
6850
Prabir Pradhan5632d622021-09-06 07:57:20 -07006851// --- TouchDisplayProjectionTest ---
6852
6853class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6854public:
6855 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6856 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6857 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006858 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6859 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6860 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006861 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006862 auto rotatedWidth = naturalDisplayWidth;
6863 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006864 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006865 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006866 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006867 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006868 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006869 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006870 inverseRotationFlags = ui::Transform::ROT_180;
6871 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006872 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006873 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006874 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006875 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006876 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006877 inverseRotationFlags = ui::Transform::ROT_0;
6878 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006879 }
6880
Prabir Pradhana9df3162022-12-05 23:57:27 +00006881 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006882 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6883
6884 std::optional<DisplayViewport> internalViewport =
6885 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6886 DisplayViewport& v = *internalViewport;
6887 v.displayId = DISPLAY_ID;
6888 v.orientation = orientation;
6889
6890 v.logicalLeft = 0;
6891 v.logicalTop = 0;
6892 v.logicalRight = 100;
6893 v.logicalBottom = 100;
6894
6895 v.physicalLeft = rotatedPhysicalDisplay.left;
6896 v.physicalTop = rotatedPhysicalDisplay.top;
6897 v.physicalRight = rotatedPhysicalDisplay.right;
6898 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6899
Prabir Pradhana9df3162022-12-05 23:57:27 +00006900 v.deviceWidth = rotatedWidth;
6901 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006902
6903 v.isActive = true;
6904 v.uniqueId = UNIQUE_ID;
6905 v.type = ViewportType::INTERNAL;
6906 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006907 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006908 }
6909
6910 void assertReceivedMove(const Point& point) {
6911 NotifyMotionArgs motionArgs;
6912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6914 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6916 1, 0, 0, 0, 0, 0, 0, 0));
6917 }
6918};
6919
6920TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6921 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006922 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006923
6924 prepareButtons();
6925 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006926 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006927
6928 NotifyMotionArgs motionArgs;
6929
6930 // Configure the DisplayViewport such that the logical display maps to a subsection of
6931 // the display panel called the physical display. Here, the physical display is bounded by the
6932 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6933 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6934 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6935 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6936
Michael Wrighta9cf4192022-12-01 23:46:39 +00006937 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006938 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6939
6940 // Touches outside the physical display should be ignored, and should not generate any
6941 // events. Ensure touches at the following points that lie outside of the physical display
6942 // area do not generate any events.
6943 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6944 processDown(mapper, toRawX(point.x), toRawY(point.y));
6945 processSync(mapper);
6946 processUp(mapper);
6947 processSync(mapper);
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6949 << "Unexpected event generated for touch outside physical display at point: "
6950 << point.x << ", " << point.y;
6951 }
6952 }
6953}
6954
6955TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6956 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006957 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006958
6959 prepareButtons();
6960 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006961 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006962
6963 NotifyMotionArgs motionArgs;
6964
6965 // Configure the DisplayViewport such that the logical display maps to a subsection of
6966 // the display panel called the physical display. Here, the physical display is bounded by the
6967 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6968 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6969
Michael Wrighta9cf4192022-12-01 23:46:39 +00006970 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006971 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6972
6973 // Touches that start outside the physical display should be ignored until it enters the
6974 // physical display bounds, at which point it should generate a down event. Start a touch at
6975 // the point (5, 100), which is outside the physical display bounds.
6976 static const Point kOutsidePoint{5, 100};
6977 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6978 processSync(mapper);
6979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6980
6981 // Move the touch into the physical display area. This should generate a pointer down.
6982 processMove(mapper, toRawX(11), toRawY(21));
6983 processSync(mapper);
6984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6985 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6986 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6987 ASSERT_NO_FATAL_FAILURE(
6988 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6989
6990 // Move the touch inside the physical display area. This should generate a pointer move.
6991 processMove(mapper, toRawX(69), toRawY(159));
6992 processSync(mapper);
6993 assertReceivedMove({69, 159});
6994
6995 // Move outside the physical display area. Since the pointer is already down, this should
6996 // now continue generating events.
6997 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6998 processSync(mapper);
6999 assertReceivedMove(kOutsidePoint);
7000
7001 // Release. This should generate a pointer up.
7002 processUp(mapper);
7003 processSync(mapper);
7004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7005 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7007 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7008
7009 // Ensure no more events were generated.
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7012 }
7013}
7014
Prabir Pradhana9df3162022-12-05 23:57:27 +00007015// --- TouchscreenPrecisionTests ---
7016
7017// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7018// in various orientations and with different display rotations. We configure the touchscreen to
7019// have a higher resolution than that of the display by an integer scale factor in each axis so that
7020// we can enforce that coordinates match precisely as expected.
7021class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7022 public ::testing::WithParamInterface<ui::Rotation> {
7023public:
7024 void SetUp() override {
7025 SingleTouchInputMapperTest::SetUp();
7026
7027 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7028 // four times the resolution of the display in the Y axis.
7029 prepareButtons();
7030 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007031 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7032 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007033 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007034 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7035 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007036 }
7037
7038 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7039 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7040 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7041 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7042
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007043 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7044 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7045
7046 static const int32_t PRECISION_RAW_X_FLAT = 16;
7047 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7048
7049 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7050 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7051
Prabir Pradhana9df3162022-12-05 23:57:27 +00007052 static const std::array<Point, 4> kRawCorners;
7053};
7054
7055const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7056 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7057 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7058 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7059 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7060}};
7061
7062// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7063// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7064// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7065TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7066 enum class Orientation {
7067 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7068 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7069 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7070 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7071 ftl_last = ORIENTATION_270,
7072 };
7073 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7074 Orientation::ORIENTATION_270;
7075 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7076 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7077 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7078 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7079 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7080 };
7081
7082 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7083
7084 // Configure the touchscreen as being installed in the one of the four different orientations
7085 // relative to the display.
7086 addConfigurationProperty("touch.deviceType", "touchScreen");
7087 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7088 prepareDisplay(ui::ROTATION_0);
7089
Arpit Singha8c236b2023-04-25 13:56:05 +00007090 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007091
7092 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7093 // orientations of either 90 or 270) this means the display's natural resolution will be
7094 // flipped.
7095 const bool displayRotated =
7096 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7097 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7098 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7099 const Rect physicalFrame{0, 0, width, height};
7100 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7101
7102 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7103 const float expectedPrecisionX = displayRotated ? 4 : 2;
7104 const float expectedPrecisionY = displayRotated ? 2 : 4;
7105
7106 // Test all four corners.
7107 for (int i = 0; i < 4; i++) {
7108 const auto& raw = kRawCorners[i];
7109 processDown(mapper, raw.x, raw.y);
7110 processSync(mapper);
7111 const auto& expected = expectedPoints[i];
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7113 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7114 WithCoords(expected.x, expected.y),
7115 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7116 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7117 << "with touchscreen orientation "
7118 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7119 << expected.x << ", " << expected.y << ").";
7120 processUp(mapper);
7121 processSync(mapper);
7122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7123 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7124 WithCoords(expected.x, expected.y))));
7125 }
7126}
7127
Prabir Pradhan82687402022-12-06 01:32:53 +00007128TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7129 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7130 kMappedCorners = {
7131 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7132 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7133 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7134 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7135 };
7136
7137 const ui::Rotation displayRotation = GetParam();
7138
7139 addConfigurationProperty("touch.deviceType", "touchScreen");
7140 prepareDisplay(displayRotation);
7141
Arpit Singha8c236b2023-04-25 13:56:05 +00007142 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007143
7144 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7145
7146 // Test all four corners.
7147 for (int i = 0; i < 4; i++) {
7148 const auto& expected = expectedPoints[i];
7149 const auto& raw = kRawCorners[i];
7150 processDown(mapper, raw.x, raw.y);
7151 processSync(mapper);
7152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7153 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7154 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7155 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7156 << "with display rotation " << ui::toCString(displayRotation)
7157 << ", expected point (" << expected.x << ", " << expected.y << ").";
7158 processUp(mapper);
7159 processSync(mapper);
7160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7161 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7162 WithCoords(expected.x, expected.y))));
7163 }
7164}
7165
Prabir Pradhan3e798762022-12-02 21:02:11 +00007166TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7167 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7168 kMappedCorners = {
7169 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7170 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7171 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7172 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7173 };
7174
7175 const ui::Rotation displayRotation = GetParam();
7176
7177 addConfigurationProperty("touch.deviceType", "touchScreen");
7178 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7179
Arpit Singha8c236b2023-04-25 13:56:05 +00007180 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007181
7182 // Ori 270, so width and height swapped
7183 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7184 prepareDisplay(displayRotation);
7185 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7186
7187 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7188
7189 // Test all four corners.
7190 for (int i = 0; i < 4; i++) {
7191 const auto& expected = expectedPoints[i];
7192 const auto& raw = kRawCorners[i];
7193 processDown(mapper, raw.x, raw.y);
7194 processSync(mapper);
7195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7196 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7197 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7198 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7199 << "with display rotation " << ui::toCString(displayRotation)
7200 << ", expected point (" << expected.x << ", " << expected.y << ").";
7201 processUp(mapper);
7202 processSync(mapper);
7203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7204 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7205 WithCoords(expected.x, expected.y))));
7206 }
7207}
7208
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007209TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7210 const ui::Rotation displayRotation = GetParam();
7211
7212 addConfigurationProperty("touch.deviceType", "touchScreen");
7213 prepareDisplay(displayRotation);
7214
7215 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00007216 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007217
7218 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7219 // MotionRanges use display pixels as their units
7220 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7221 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7222
7223 // The MotionRanges should be oriented in the rotated display's coordinate space
7224 const bool displayRotated =
7225 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7226
7227 constexpr float MAX_X = 479.5;
7228 constexpr float MAX_Y = 799.75;
7229 EXPECT_EQ(xRange->min, 0.f);
7230 EXPECT_EQ(yRange->min, 0.f);
7231 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7232 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7233
7234 EXPECT_EQ(xRange->flat, 8.f);
7235 EXPECT_EQ(yRange->flat, 8.f);
7236
7237 EXPECT_EQ(xRange->fuzz, 2.f);
7238 EXPECT_EQ(yRange->fuzz, 2.f);
7239
7240 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7241 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7242}
7243
Prabir Pradhana9df3162022-12-05 23:57:27 +00007244// Run the precision tests for all rotations.
7245INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7246 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7247 ui::ROTATION_270),
7248 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7249 return ftl::enum_string(testParamInfo.param);
7250 });
7251
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007252// --- ExternalStylusFusionTest ---
7253
7254class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7255public:
7256 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7257 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007258 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007259 prepareButtons();
7260 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007261 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007262
7263 mStylusState.when = ARBITRARY_TIME;
7264 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007265 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007266 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007267 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007268 processExternalStylusState(mapper);
7269 return mapper;
7270 }
7271
7272 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7273 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7274 for (const NotifyArgs& args : generatedArgs) {
7275 mFakeListener->notify(args);
7276 }
7277 // Loop the reader to flush the input listener queue.
7278 mReader->loopOnce();
7279 return generatedArgs;
7280 }
7281
7282protected:
7283 StylusState mStylusState{};
7284 static constexpr uint32_t EXPECTED_SOURCE =
7285 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7286
7287 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7288 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007289 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007290
7291 // The first pointer is withheld.
7292 processDown(mapper, 100, 200);
7293 processSync(mapper);
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7295 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7296 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7297
7298 // The external stylus reports pressure. The withheld finger pointer is released as a
7299 // stylus.
7300 mStylusState.pressure = 1.f;
7301 processExternalStylusState(mapper);
7302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7303 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7304 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7305
7306 // Subsequent pointer events are not withheld.
7307 processMove(mapper, 101, 201);
7308 processSync(mapper);
7309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7310 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7311
7312 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7314 }
7315
7316 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7317 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7318
7319 // Releasing the touch pointer ends the gesture.
7320 processUp(mapper);
7321 processSync(mapper);
7322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7323 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007324 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007325
7326 mStylusState.pressure = 0.f;
7327 processExternalStylusState(mapper);
7328 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7330 }
7331
7332 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7333 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007334 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007335
7336 // The first pointer is withheld when an external stylus is connected,
7337 // and a timeout is requested.
7338 processDown(mapper, 100, 200);
7339 processSync(mapper);
7340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7341 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7342 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7343
7344 // If the timeout expires early, it is requested again.
7345 handleTimeout(mapper, ARBITRARY_TIME + 1);
7346 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7347 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7348
7349 // When the timeout expires, the withheld touch is released as a finger pointer.
7350 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7352 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7353
7354 // Subsequent pointer events are not withheld.
7355 processMove(mapper, 101, 201);
7356 processSync(mapper);
7357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7358 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7359 processUp(mapper);
7360 processSync(mapper);
7361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7362 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7363
7364 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7366 }
7367
7368private:
7369 InputDeviceInfo mExternalStylusDeviceInfo{};
7370};
7371
7372TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7373 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7374 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7375}
7376
7377TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7378 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7379 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7380}
7381
7382TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7383 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7384 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7385}
7386
7387// Test a successful stylus fusion gesture where the pressure is reported by the external
7388// before the touch is reported by the touchscreen.
7389TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7390 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7391 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007392 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007393
7394 // The external stylus reports pressure first. It is ignored for now.
7395 mStylusState.pressure = 1.f;
7396 processExternalStylusState(mapper);
7397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7398 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7399
7400 // When the touch goes down afterwards, it is reported as a stylus pointer.
7401 processDown(mapper, 100, 200);
7402 processSync(mapper);
7403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7404 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7405 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7406
7407 processMove(mapper, 101, 201);
7408 processSync(mapper);
7409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7410 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7411 processUp(mapper);
7412 processSync(mapper);
7413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7414 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7415
7416 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7418}
7419
7420TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7421 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7422
7423 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7424 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7425
7426 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7427 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7428 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7429 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7430}
7431
7432TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7433 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7434 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007435 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007436
7437 mStylusState.pressure = 0.8f;
7438 processExternalStylusState(mapper);
7439 processDown(mapper, 100, 200);
7440 processSync(mapper);
7441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7442 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7443 WithPressure(0.8f))));
7444 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7445
7446 // The external stylus reports a pressure change. We wait for some time for a touch event.
7447 mStylusState.pressure = 0.6f;
7448 processExternalStylusState(mapper);
7449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7450 ASSERT_NO_FATAL_FAILURE(
7451 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7452
7453 // If a touch is reported within the timeout, it reports the updated pressure.
7454 processMove(mapper, 101, 201);
7455 processSync(mapper);
7456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7457 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7458 WithPressure(0.6f))));
7459 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7460
7461 // There is another pressure change.
7462 mStylusState.pressure = 0.5f;
7463 processExternalStylusState(mapper);
7464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7465 ASSERT_NO_FATAL_FAILURE(
7466 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7467
7468 // If a touch is not reported within the timeout, a move event is generated to report
7469 // the new pressure.
7470 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7472 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7473 WithPressure(0.5f))));
7474
7475 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7476 // repeated indefinitely.
7477 mStylusState.pressure = 0.0f;
7478 processExternalStylusState(mapper);
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7480 ASSERT_NO_FATAL_FAILURE(
7481 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7482 processMove(mapper, 102, 202);
7483 processSync(mapper);
7484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7485 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7486 WithPressure(0.5f))));
7487 processMove(mapper, 103, 203);
7488 processSync(mapper);
7489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7490 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7491 WithPressure(0.5f))));
7492
7493 processUp(mapper);
7494 processSync(mapper);
7495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7496 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007497 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007498
7499 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7501}
7502
7503TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7504 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7505 auto source = WithSource(EXPECTED_SOURCE);
7506
7507 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007508 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007509 processExternalStylusState(mapper);
7510 processDown(mapper, 100, 200);
7511 processSync(mapper);
7512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7513 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007514 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007515 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7516
7517 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007518 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007519 processExternalStylusState(mapper);
7520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7521 ASSERT_NO_FATAL_FAILURE(
7522 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7523
7524 // If a touch is reported within the timeout, it reports the updated pressure.
7525 processMove(mapper, 101, 201);
7526 processSync(mapper);
7527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7528 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007529 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007530 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7531
7532 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007533 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007534 processExternalStylusState(mapper);
7535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7536 ASSERT_NO_FATAL_FAILURE(
7537 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7538
7539 // If a touch is not reported within the timeout, a move event is generated to report
7540 // the new tool type.
7541 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7543 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007544 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007545
7546 processUp(mapper);
7547 processSync(mapper);
7548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7549 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007550 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007551
7552 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7554}
7555
7556TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7557 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7558 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007559 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007560
7561 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7562
7563 // The external stylus reports a button change. We wait for some time for a touch event.
7564 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7565 processExternalStylusState(mapper);
7566 ASSERT_NO_FATAL_FAILURE(
7567 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7568
7569 // If a touch is reported within the timeout, it reports the updated button state.
7570 processMove(mapper, 101, 201);
7571 processSync(mapper);
7572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7573 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7574 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7576 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7577 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7578 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7579
7580 // The button is now released.
7581 mStylusState.buttons = 0;
7582 processExternalStylusState(mapper);
7583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7584 ASSERT_NO_FATAL_FAILURE(
7585 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7586
7587 // If a touch is not reported within the timeout, a move event is generated to report
7588 // the new button state.
7589 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7591 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7592 WithButtonState(0))));
7593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007594 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7595 WithButtonState(0))));
7596
7597 processUp(mapper);
7598 processSync(mapper);
7599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007600 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7601
7602 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7604}
7605
Michael Wrightd02c5b62014-02-10 15:10:22 -08007606// --- MultiTouchInputMapperTest ---
7607
7608class MultiTouchInputMapperTest : public TouchInputMapperTest {
7609protected:
7610 void prepareAxes(int axes);
7611
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007612 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7613 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7614 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7615 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7616 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7617 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7618 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7619 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7620 void processId(MultiTouchInputMapper& mapper, int32_t id);
7621 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7622 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7623 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007624 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007625 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007626 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7627 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007628};
7629
7630void MultiTouchInputMapperTest::prepareAxes(int axes) {
7631 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007632 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7633 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007634 }
7635 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007636 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7637 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007638 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007639 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7640 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007641 }
7642 }
7643 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007644 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7645 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007646 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007647 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007648 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007649 }
7650 }
7651 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007652 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7653 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007654 }
7655 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007656 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7657 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007658 }
7659 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007660 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7661 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007662 }
7663 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007664 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7665 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007666 }
7667 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007668 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7669 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007670 }
7671 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007672 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007673 }
7674}
7675
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007676void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7677 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7679 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007680}
7681
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007682void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7683 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007685}
7686
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007687void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7688 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007689 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007690}
7691
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007692void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007694}
7695
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007696void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007697 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007698}
7699
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007700void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7701 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007702 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007703}
7704
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007705void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007707}
7708
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007709void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007711}
7712
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007713void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007715}
7716
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007717void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007719}
7720
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007721void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007722 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007723}
7724
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007725void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7726 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007728}
7729
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007730void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7731 int32_t value) {
7732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7733 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7734}
7735
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007736void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007737 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007738}
7739
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007740void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7741 nsecs_t readTime) {
7742 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007743}
7744
Michael Wrightd02c5b62014-02-10 15:10:22 -08007745TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007746 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007747 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007748 prepareAxes(POSITION);
7749 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007750 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007751
arthurhungdcef2dc2020-08-11 14:47:50 +08007752 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007753
7754 NotifyMotionArgs motionArgs;
7755
7756 // Two fingers down at once.
7757 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7758 processPosition(mapper, x1, y1);
7759 processMTSync(mapper);
7760 processPosition(mapper, x2, y2);
7761 processMTSync(mapper);
7762 processSync(mapper);
7763
7764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7765 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7766 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7767 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7768 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7769 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7770 ASSERT_EQ(0, motionArgs.flags);
7771 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7772 ASSERT_EQ(0, motionArgs.buttonState);
7773 ASSERT_EQ(0, motionArgs.edgeFlags);
7774 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7775 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007776 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7778 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7779 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7780 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7781 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7782
7783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7784 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7785 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7786 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7787 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007788 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007789 ASSERT_EQ(0, motionArgs.flags);
7790 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7791 ASSERT_EQ(0, motionArgs.buttonState);
7792 ASSERT_EQ(0, motionArgs.edgeFlags);
7793 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7794 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007795 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007796 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007797 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7799 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7801 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7802 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7803 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7804 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7805
7806 // Move.
7807 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7808 processPosition(mapper, x1, y1);
7809 processMTSync(mapper);
7810 processPosition(mapper, x2, y2);
7811 processMTSync(mapper);
7812 processSync(mapper);
7813
7814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7815 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7816 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7817 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7818 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7819 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7820 ASSERT_EQ(0, motionArgs.flags);
7821 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7822 ASSERT_EQ(0, motionArgs.buttonState);
7823 ASSERT_EQ(0, motionArgs.edgeFlags);
7824 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7825 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007826 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007827 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007828 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7830 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7832 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7833 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7834 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7835 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7836
7837 // First finger up.
7838 x2 += 15; y2 -= 20;
7839 processPosition(mapper, x2, y2);
7840 processMTSync(mapper);
7841 processSync(mapper);
7842
7843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7844 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7845 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7846 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7847 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007848 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007849 ASSERT_EQ(0, motionArgs.flags);
7850 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7851 ASSERT_EQ(0, motionArgs.buttonState);
7852 ASSERT_EQ(0, motionArgs.edgeFlags);
7853 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7854 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007855 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007856 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007857 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7859 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7861 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7862 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7863 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7864 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7865
7866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7867 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7868 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7869 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7870 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7871 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7872 ASSERT_EQ(0, motionArgs.flags);
7873 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7874 ASSERT_EQ(0, motionArgs.buttonState);
7875 ASSERT_EQ(0, motionArgs.edgeFlags);
7876 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7877 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007878 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7880 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7881 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7882 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7883 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7884
7885 // Move.
7886 x2 += 20; y2 -= 25;
7887 processPosition(mapper, x2, y2);
7888 processMTSync(mapper);
7889 processSync(mapper);
7890
7891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7892 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7893 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7894 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7895 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7897 ASSERT_EQ(0, motionArgs.flags);
7898 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7899 ASSERT_EQ(0, motionArgs.buttonState);
7900 ASSERT_EQ(0, motionArgs.edgeFlags);
7901 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7902 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007903 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007904 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7905 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7906 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7907 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7908 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7909
7910 // New finger down.
7911 int32_t x3 = 700, y3 = 300;
7912 processPosition(mapper, x2, y2);
7913 processMTSync(mapper);
7914 processPosition(mapper, x3, y3);
7915 processMTSync(mapper);
7916 processSync(mapper);
7917
7918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7919 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7920 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7921 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7922 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007923 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007924 ASSERT_EQ(0, motionArgs.flags);
7925 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7926 ASSERT_EQ(0, motionArgs.buttonState);
7927 ASSERT_EQ(0, motionArgs.edgeFlags);
7928 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7929 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007930 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007931 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007932 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7934 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7936 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7937 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7938 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7939 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7940
7941 // Second finger up.
7942 x3 += 30; y3 -= 20;
7943 processPosition(mapper, x3, y3);
7944 processMTSync(mapper);
7945 processSync(mapper);
7946
7947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7948 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7949 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7950 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7951 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007952 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953 ASSERT_EQ(0, motionArgs.flags);
7954 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7955 ASSERT_EQ(0, motionArgs.buttonState);
7956 ASSERT_EQ(0, motionArgs.edgeFlags);
7957 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7958 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007959 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007960 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007961 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7963 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7965 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7966 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7967 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7968 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7969
7970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7971 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7972 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7973 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7974 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7976 ASSERT_EQ(0, motionArgs.flags);
7977 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7978 ASSERT_EQ(0, motionArgs.buttonState);
7979 ASSERT_EQ(0, motionArgs.edgeFlags);
7980 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7981 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007982 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7984 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7985 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7986 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7987 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7988
7989 // Last finger up.
7990 processMTSync(mapper);
7991 processSync(mapper);
7992
7993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7994 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7995 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7996 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7997 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7998 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7999 ASSERT_EQ(0, motionArgs.flags);
8000 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8001 ASSERT_EQ(0, motionArgs.buttonState);
8002 ASSERT_EQ(0, motionArgs.edgeFlags);
8003 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8004 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008005 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8007 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8008 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8009 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8010 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8011
8012 // Should not have sent any more keys or motions.
8013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8015}
8016
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008017TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8018 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008019 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008020
8021 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8022 /*fuzz*/ 0, /*resolution*/ 10);
8023 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8024 /*fuzz*/ 0, /*resolution*/ 11);
8025 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8026 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8027 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8028 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8029 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8030 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8031 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8032 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8033
Arpit Singha8c236b2023-04-25 13:56:05 +00008034 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008035
8036 // X and Y axes
8037 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8038 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8039 // Touch major and minor
8040 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8041 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8042 // Tool major and minor
8043 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8044 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8045}
8046
8047TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8048 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008049 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008050
8051 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8052 /*fuzz*/ 0, /*resolution*/ 10);
8053 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8054 /*fuzz*/ 0, /*resolution*/ 11);
8055
8056 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8057
Arpit Singha8c236b2023-04-25 13:56:05 +00008058 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008059
8060 // Touch major and minor
8061 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8062 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8063 // Tool major and minor
8064 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8065 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8066}
8067
Michael Wrightd02c5b62014-02-10 15:10:22 -08008068TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008069 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008070 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008071 prepareAxes(POSITION | ID);
8072 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008073 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008074
arthurhungdcef2dc2020-08-11 14:47:50 +08008075 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008076
8077 NotifyMotionArgs motionArgs;
8078
8079 // Two fingers down at once.
8080 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8081 processPosition(mapper, x1, y1);
8082 processId(mapper, 1);
8083 processMTSync(mapper);
8084 processPosition(mapper, x2, y2);
8085 processId(mapper, 2);
8086 processMTSync(mapper);
8087 processSync(mapper);
8088
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8090 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8091 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8092 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008093 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].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
8097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008098 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008099 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8100 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008101 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008102 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008103 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8105 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8107 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8108
8109 // Move.
8110 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8111 processPosition(mapper, x1, y1);
8112 processId(mapper, 1);
8113 processMTSync(mapper);
8114 processPosition(mapper, x2, y2);
8115 processId(mapper, 2);
8116 processMTSync(mapper);
8117 processSync(mapper);
8118
8119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8121 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8122 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008123 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008124 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008125 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8127 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8128 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8129 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8130
8131 // First finger up.
8132 x2 += 15; y2 -= 20;
8133 processPosition(mapper, x2, y2);
8134 processId(mapper, 2);
8135 processMTSync(mapper);
8136 processSync(mapper);
8137
8138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008139 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008140 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8141 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008142 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008143 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008144 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8146 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8148 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8149
8150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8152 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8153 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008154 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8156 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8157
8158 // Move.
8159 x2 += 20; y2 -= 25;
8160 processPosition(mapper, x2, y2);
8161 processId(mapper, 2);
8162 processMTSync(mapper);
8163 processSync(mapper);
8164
8165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8167 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8168 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008169 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8171 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8172
8173 // New finger down.
8174 int32_t x3 = 700, y3 = 300;
8175 processPosition(mapper, x2, y2);
8176 processId(mapper, 2);
8177 processMTSync(mapper);
8178 processPosition(mapper, x3, y3);
8179 processId(mapper, 3);
8180 processMTSync(mapper);
8181 processSync(mapper);
8182
8183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008184 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008185 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8186 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008187 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008188 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008189 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008190 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8191 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8193 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8194
8195 // Second finger up.
8196 x3 += 30; y3 -= 20;
8197 processPosition(mapper, x3, y3);
8198 processId(mapper, 3);
8199 processMTSync(mapper);
8200 processSync(mapper);
8201
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008203 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008204 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8205 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008206 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008208 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].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 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8212 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8213
8214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8216 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8217 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008218 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8220 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8221
8222 // Last finger up.
8223 processMTSync(mapper);
8224 processSync(mapper);
8225
8226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8227 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8228 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8229 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008230 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8232 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8233
8234 // Should not have sent any more keys or motions.
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8237}
8238
8239TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008240 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008241 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242 prepareAxes(POSITION | ID | SLOT);
8243 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008244 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008245
arthurhungdcef2dc2020-08-11 14:47:50 +08008246 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008247
8248 NotifyMotionArgs motionArgs;
8249
8250 // Two fingers down at once.
8251 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8252 processPosition(mapper, x1, y1);
8253 processId(mapper, 1);
8254 processSlot(mapper, 1);
8255 processPosition(mapper, x2, y2);
8256 processId(mapper, 2);
8257 processSync(mapper);
8258
8259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8260 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8261 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8262 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008263 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].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
8267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008268 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008269 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8270 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008271 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008272 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008273 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8275 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8277 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8278
8279 // Move.
8280 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8281 processSlot(mapper, 0);
8282 processPosition(mapper, x1, y1);
8283 processSlot(mapper, 1);
8284 processPosition(mapper, x2, y2);
8285 processSync(mapper);
8286
8287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8289 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8290 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008291 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008292 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008293 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8295 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8297 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8298
8299 // First finger up.
8300 x2 += 15; y2 -= 20;
8301 processSlot(mapper, 0);
8302 processId(mapper, -1);
8303 processSlot(mapper, 1);
8304 processPosition(mapper, x2, y2);
8305 processSync(mapper);
8306
8307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008308 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008309 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8310 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008311 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008312 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008313 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8315 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8317 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8318
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8321 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8322 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008323 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8325 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8326
8327 // Move.
8328 x2 += 20; y2 -= 25;
8329 processPosition(mapper, x2, y2);
8330 processSync(mapper);
8331
8332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8334 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8335 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008336 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8338 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8339
8340 // New finger down.
8341 int32_t x3 = 700, y3 = 300;
8342 processPosition(mapper, x2, y2);
8343 processSlot(mapper, 0);
8344 processId(mapper, 3);
8345 processPosition(mapper, x3, y3);
8346 processSync(mapper);
8347
8348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008349 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008350 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8351 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008352 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008353 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008354 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8356 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8358 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8359
8360 // Second finger up.
8361 x3 += 30; y3 -= 20;
8362 processSlot(mapper, 1);
8363 processId(mapper, -1);
8364 processSlot(mapper, 0);
8365 processPosition(mapper, x3, y3);
8366 processSync(mapper);
8367
8368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008369 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008370 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8371 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008372 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008373 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008374 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8376 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8378 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8379
8380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8382 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8383 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008384 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8386 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8387
8388 // Last finger up.
8389 processId(mapper, -1);
8390 processSync(mapper);
8391
8392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8393 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8394 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8395 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008396 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8398 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8399
8400 // Should not have sent any more keys or motions.
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8403}
8404
8405TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008406 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008407 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008408 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008409 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008410
8411 // These calculations are based on the input device calibration documentation.
8412 int32_t rawX = 100;
8413 int32_t rawY = 200;
8414 int32_t rawTouchMajor = 7;
8415 int32_t rawTouchMinor = 6;
8416 int32_t rawToolMajor = 9;
8417 int32_t rawToolMinor = 8;
8418 int32_t rawPressure = 11;
8419 int32_t rawDistance = 0;
8420 int32_t rawOrientation = 3;
8421 int32_t id = 5;
8422
8423 float x = toDisplayX(rawX);
8424 float y = toDisplayY(rawY);
8425 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8426 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8427 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8428 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8429 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8430 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8431 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8432 float distance = float(rawDistance);
8433
8434 processPosition(mapper, rawX, rawY);
8435 processTouchMajor(mapper, rawTouchMajor);
8436 processTouchMinor(mapper, rawTouchMinor);
8437 processToolMajor(mapper, rawToolMajor);
8438 processToolMinor(mapper, rawToolMinor);
8439 processPressure(mapper, rawPressure);
8440 processOrientation(mapper, rawOrientation);
8441 processDistance(mapper, rawDistance);
8442 processId(mapper, id);
8443 processMTSync(mapper);
8444 processSync(mapper);
8445
8446 NotifyMotionArgs args;
8447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8448 ASSERT_EQ(0, args.pointerProperties[0].id);
8449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8450 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8451 orientation, distance));
8452}
8453
8454TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008455 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008456 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008457 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8458 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00008459 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008460
8461 // These calculations are based on the input device calibration documentation.
8462 int32_t rawX = 100;
8463 int32_t rawY = 200;
8464 int32_t rawTouchMajor = 140;
8465 int32_t rawTouchMinor = 120;
8466 int32_t rawToolMajor = 180;
8467 int32_t rawToolMinor = 160;
8468
8469 float x = toDisplayX(rawX);
8470 float y = toDisplayY(rawY);
8471 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8472 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8473 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8474 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8475 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8476
8477 processPosition(mapper, rawX, rawY);
8478 processTouchMajor(mapper, rawTouchMajor);
8479 processTouchMinor(mapper, rawTouchMinor);
8480 processToolMajor(mapper, rawToolMajor);
8481 processToolMinor(mapper, rawToolMinor);
8482 processMTSync(mapper);
8483 processSync(mapper);
8484
8485 NotifyMotionArgs args;
8486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8488 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8489}
8490
8491TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008492 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008493 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008494 prepareAxes(POSITION | TOUCH | TOOL);
8495 addConfigurationProperty("touch.size.calibration", "diameter");
8496 addConfigurationProperty("touch.size.scale", "10");
8497 addConfigurationProperty("touch.size.bias", "160");
8498 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00008499 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008500
8501 // These calculations are based on the input device calibration documentation.
8502 // Note: We only provide a single common touch/tool value because the device is assumed
8503 // not to emit separate values for each pointer (isSummed = 1).
8504 int32_t rawX = 100;
8505 int32_t rawY = 200;
8506 int32_t rawX2 = 150;
8507 int32_t rawY2 = 250;
8508 int32_t rawTouchMajor = 5;
8509 int32_t rawToolMajor = 8;
8510
8511 float x = toDisplayX(rawX);
8512 float y = toDisplayY(rawY);
8513 float x2 = toDisplayX(rawX2);
8514 float y2 = toDisplayY(rawY2);
8515 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8516 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8517 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8518
8519 processPosition(mapper, rawX, rawY);
8520 processTouchMajor(mapper, rawTouchMajor);
8521 processToolMajor(mapper, rawToolMajor);
8522 processMTSync(mapper);
8523 processPosition(mapper, rawX2, rawY2);
8524 processTouchMajor(mapper, rawTouchMajor);
8525 processToolMajor(mapper, rawToolMajor);
8526 processMTSync(mapper);
8527 processSync(mapper);
8528
8529 NotifyMotionArgs args;
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8531 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8532
8533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008534 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008535 ASSERT_EQ(size_t(2), args.pointerCount);
8536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8537 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8539 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8540}
8541
8542TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008543 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008544 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008545 prepareAxes(POSITION | TOUCH | TOOL);
8546 addConfigurationProperty("touch.size.calibration", "area");
8547 addConfigurationProperty("touch.size.scale", "43");
8548 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008549 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008550
8551 // These calculations are based on the input device calibration documentation.
8552 int32_t rawX = 100;
8553 int32_t rawY = 200;
8554 int32_t rawTouchMajor = 5;
8555 int32_t rawToolMajor = 8;
8556
8557 float x = toDisplayX(rawX);
8558 float y = toDisplayY(rawY);
8559 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8560 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8561 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8562
8563 processPosition(mapper, rawX, rawY);
8564 processTouchMajor(mapper, rawTouchMajor);
8565 processToolMajor(mapper, rawToolMajor);
8566 processMTSync(mapper);
8567 processSync(mapper);
8568
8569 NotifyMotionArgs args;
8570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8572 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8573}
8574
8575TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008576 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008577 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008578 prepareAxes(POSITION | PRESSURE);
8579 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8580 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008581 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008582
Michael Wrightaa449c92017-12-13 21:21:43 +00008583 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008584 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008585 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8586 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8587 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8588
Michael Wrightd02c5b62014-02-10 15:10:22 -08008589 // These calculations are based on the input device calibration documentation.
8590 int32_t rawX = 100;
8591 int32_t rawY = 200;
8592 int32_t rawPressure = 60;
8593
8594 float x = toDisplayX(rawX);
8595 float y = toDisplayY(rawY);
8596 float pressure = float(rawPressure) * 0.01f;
8597
8598 processPosition(mapper, rawX, rawY);
8599 processPressure(mapper, rawPressure);
8600 processMTSync(mapper);
8601 processSync(mapper);
8602
8603 NotifyMotionArgs args;
8604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8606 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8607}
8608
8609TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008610 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008611 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008612 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008613 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008614
8615 NotifyMotionArgs motionArgs;
8616 NotifyKeyArgs keyArgs;
8617
8618 processId(mapper, 1);
8619 processPosition(mapper, 100, 200);
8620 processSync(mapper);
8621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8622 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8623 ASSERT_EQ(0, motionArgs.buttonState);
8624
8625 // press BTN_LEFT, release BTN_LEFT
8626 processKey(mapper, BTN_LEFT, 1);
8627 processSync(mapper);
8628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8630 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8631
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8633 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8634 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8635
Michael Wrightd02c5b62014-02-10 15:10:22 -08008636 processKey(mapper, BTN_LEFT, 0);
8637 processSync(mapper);
8638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008639 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008640 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008641
8642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008643 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008644 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008645
8646 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8647 processKey(mapper, BTN_RIGHT, 1);
8648 processKey(mapper, BTN_MIDDLE, 1);
8649 processSync(mapper);
8650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8652 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8653 motionArgs.buttonState);
8654
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8656 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8657 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8658
8659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8660 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8661 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8662 motionArgs.buttonState);
8663
Michael Wrightd02c5b62014-02-10 15:10:22 -08008664 processKey(mapper, BTN_RIGHT, 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(AMOTION_EVENT_BUTTON_TERTIARY, 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(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008673
8674 processKey(mapper, BTN_MIDDLE, 0);
8675 processSync(mapper);
8676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008677 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008678 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008679
8680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008682 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008683
8684 // press BTN_BACK, release BTN_BACK
8685 processKey(mapper, BTN_BACK, 1);
8686 processSync(mapper);
8687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8688 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8689 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008690
Michael Wrightd02c5b62014-02-10 15:10:22 -08008691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008693 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8694
8695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8696 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8697 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008698
8699 processKey(mapper, BTN_BACK, 0);
8700 processSync(mapper);
8701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008702 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008703 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008704
8705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008706 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008707 ASSERT_EQ(0, motionArgs.buttonState);
8708
Michael Wrightd02c5b62014-02-10 15:10:22 -08008709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8710 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8711 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8712
8713 // press BTN_SIDE, release BTN_SIDE
8714 processKey(mapper, BTN_SIDE, 1);
8715 processSync(mapper);
8716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8717 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8718 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008719
Michael Wrightd02c5b62014-02-10 15:10:22 -08008720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008722 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8723
8724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8725 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8726 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008727
8728 processKey(mapper, BTN_SIDE, 0);
8729 processSync(mapper);
8730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008731 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008732 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008733
8734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008736 ASSERT_EQ(0, motionArgs.buttonState);
8737
Michael Wrightd02c5b62014-02-10 15:10:22 -08008738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8739 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8740 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8741
8742 // press BTN_FORWARD, release BTN_FORWARD
8743 processKey(mapper, BTN_FORWARD, 1);
8744 processSync(mapper);
8745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8746 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8747 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008748
Michael Wrightd02c5b62014-02-10 15:10:22 -08008749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008751 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8752
8753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8754 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8755 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008756
8757 processKey(mapper, BTN_FORWARD, 0);
8758 processSync(mapper);
8759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008760 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008761 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008762
8763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008764 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008765 ASSERT_EQ(0, motionArgs.buttonState);
8766
Michael Wrightd02c5b62014-02-10 15:10:22 -08008767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8768 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8769 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8770
8771 // press BTN_EXTRA, release BTN_EXTRA
8772 processKey(mapper, BTN_EXTRA, 1);
8773 processSync(mapper);
8774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8775 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8776 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008777
Michael Wrightd02c5b62014-02-10 15:10:22 -08008778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008780 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8781
8782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8783 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8784 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008785
8786 processKey(mapper, BTN_EXTRA, 0);
8787 processSync(mapper);
8788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008789 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008790 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008791
8792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008793 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008794 ASSERT_EQ(0, motionArgs.buttonState);
8795
Michael Wrightd02c5b62014-02-10 15:10:22 -08008796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8797 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8798 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8799
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8801
Michael Wrightd02c5b62014-02-10 15:10:22 -08008802 // press BTN_STYLUS, release BTN_STYLUS
8803 processKey(mapper, BTN_STYLUS, 1);
8804 processSync(mapper);
8805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008807 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8808
8809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8810 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8811 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008812
8813 processKey(mapper, BTN_STYLUS, 0);
8814 processSync(mapper);
8815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008816 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008817 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008818
8819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008820 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008821 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008822
8823 // press BTN_STYLUS2, release BTN_STYLUS2
8824 processKey(mapper, BTN_STYLUS2, 1);
8825 processSync(mapper);
8826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8827 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008828 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8829
8830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8831 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8832 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008833
8834 processKey(mapper, BTN_STYLUS2, 0);
8835 processSync(mapper);
8836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008837 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008838 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008839
8840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008842 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008843
8844 // release touch
8845 processId(mapper, -1);
8846 processSync(mapper);
8847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8848 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8849 ASSERT_EQ(0, motionArgs.buttonState);
8850}
8851
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008852TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8853 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008854 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008855 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008856 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008857
8858 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8859 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8860
8861 // Touch down.
8862 processId(mapper, 1);
8863 processPosition(mapper, 100, 200);
8864 processSync(mapper);
8865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8866 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8867
8868 // Press and release button mapped to the primary stylus button.
8869 processKey(mapper, BTN_A, 1);
8870 processSync(mapper);
8871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8872 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8873 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8875 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8876 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8877
8878 processKey(mapper, BTN_A, 0);
8879 processSync(mapper);
8880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8881 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8883 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8884
8885 // Press and release the HID usage mapped to the secondary stylus button.
8886 processHidUsage(mapper, 0xabcd, 1);
8887 processSync(mapper);
8888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8889 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8890 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8892 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8893 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8894
8895 processHidUsage(mapper, 0xabcd, 0);
8896 processSync(mapper);
8897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8898 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8900 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8901
8902 // Release touch.
8903 processId(mapper, -1);
8904 processSync(mapper);
8905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8906 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8907}
8908
Michael Wrightd02c5b62014-02-10 15:10:22 -08008909TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008910 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008911 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008912 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008913 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008914
8915 NotifyMotionArgs motionArgs;
8916
8917 // default tool type is finger
8918 processId(mapper, 1);
8919 processPosition(mapper, 100, 200);
8920 processSync(mapper);
8921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8922 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008923 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008924
8925 // eraser
8926 processKey(mapper, BTN_TOOL_RUBBER, 1);
8927 processSync(mapper);
8928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008930 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008931
8932 // stylus
8933 processKey(mapper, BTN_TOOL_RUBBER, 0);
8934 processKey(mapper, BTN_TOOL_PEN, 1);
8935 processSync(mapper);
8936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008938 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008939
8940 // brush
8941 processKey(mapper, BTN_TOOL_PEN, 0);
8942 processKey(mapper, BTN_TOOL_BRUSH, 1);
8943 processSync(mapper);
8944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8945 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008946 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008947
8948 // pencil
8949 processKey(mapper, BTN_TOOL_BRUSH, 0);
8950 processKey(mapper, BTN_TOOL_PENCIL, 1);
8951 processSync(mapper);
8952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008954 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008955
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008956 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008957 processKey(mapper, BTN_TOOL_PENCIL, 0);
8958 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8959 processSync(mapper);
8960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8961 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008962 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008963
8964 // mouse
8965 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8966 processKey(mapper, BTN_TOOL_MOUSE, 1);
8967 processSync(mapper);
8968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008970 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008971
8972 // lens
8973 processKey(mapper, BTN_TOOL_MOUSE, 0);
8974 processKey(mapper, BTN_TOOL_LENS, 1);
8975 processSync(mapper);
8976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8977 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008978 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008979
8980 // double-tap
8981 processKey(mapper, BTN_TOOL_LENS, 0);
8982 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8983 processSync(mapper);
8984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8985 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008986 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008987
8988 // triple-tap
8989 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8990 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8991 processSync(mapper);
8992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8993 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008994 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008995
8996 // quad-tap
8997 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8998 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8999 processSync(mapper);
9000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9001 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009002 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009003
9004 // finger
9005 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9006 processKey(mapper, BTN_TOOL_FINGER, 1);
9007 processSync(mapper);
9008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009010 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009011
9012 // stylus trumps finger
9013 processKey(mapper, BTN_TOOL_PEN, 1);
9014 processSync(mapper);
9015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9016 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009017 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009018
9019 // eraser trumps stylus
9020 processKey(mapper, BTN_TOOL_RUBBER, 1);
9021 processSync(mapper);
9022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009024 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009025
9026 // mouse trumps eraser
9027 processKey(mapper, BTN_TOOL_MOUSE, 1);
9028 processSync(mapper);
9029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9030 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009031 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009032
9033 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9034 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9035 processSync(mapper);
9036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009038 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009039
9040 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9041 processToolType(mapper, MT_TOOL_PEN);
9042 processSync(mapper);
9043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009045 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009046
9047 // back to default tool type
9048 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9049 processKey(mapper, BTN_TOOL_MOUSE, 0);
9050 processKey(mapper, BTN_TOOL_RUBBER, 0);
9051 processKey(mapper, BTN_TOOL_PEN, 0);
9052 processKey(mapper, BTN_TOOL_FINGER, 0);
9053 processSync(mapper);
9054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9055 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009056 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009057}
9058
9059TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009060 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009061 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009062 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009063 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009064 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009065
9066 NotifyMotionArgs motionArgs;
9067
9068 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9069 processId(mapper, 1);
9070 processPosition(mapper, 100, 200);
9071 processSync(mapper);
9072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9073 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9075 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9076
9077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9078 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9080 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9081
9082 // move a little
9083 processPosition(mapper, 150, 250);
9084 processSync(mapper);
9085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9086 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9088 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9089
9090 // down when BTN_TOUCH is pressed, pressure defaults to 1
9091 processKey(mapper, BTN_TOUCH, 1);
9092 processSync(mapper);
9093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9094 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9096 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9097
9098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9099 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9100 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9101 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9102
9103 // up when BTN_TOUCH is released, hover restored
9104 processKey(mapper, BTN_TOUCH, 0);
9105 processSync(mapper);
9106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9107 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9109 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9110
9111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9112 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9114 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9115
9116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9117 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9119 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9120
9121 // exit hover when pointer goes away
9122 processId(mapper, -1);
9123 processSync(mapper);
9124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9125 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9127 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9128}
9129
9130TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009131 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009132 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009133 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009134 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009135
9136 NotifyMotionArgs motionArgs;
9137
9138 // initially hovering because pressure is 0
9139 processId(mapper, 1);
9140 processPosition(mapper, 100, 200);
9141 processPressure(mapper, 0);
9142 processSync(mapper);
9143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9144 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9146 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9147
9148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9149 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9151 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9152
9153 // move a little
9154 processPosition(mapper, 150, 250);
9155 processSync(mapper);
9156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9157 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9159 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9160
9161 // down when pressure becomes non-zero
9162 processPressure(mapper, RAW_PRESSURE_MAX);
9163 processSync(mapper);
9164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9165 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9167 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9168
9169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9170 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9172 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9173
9174 // up when pressure becomes 0, hover restored
9175 processPressure(mapper, 0);
9176 processSync(mapper);
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9178 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9180 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9181
9182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9183 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9185 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9186
9187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9188 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9190 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9191
9192 // exit hover when pointer goes away
9193 processId(mapper, -1);
9194 processSync(mapper);
9195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9196 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9198 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9199}
9200
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009201/**
9202 * Set the input device port <--> display port associations, and check that the
9203 * events are routed to the display that matches the display port.
9204 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9205 */
9206TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009207 const std::string usb2 = "USB2";
9208 const uint8_t hdmi1 = 0;
9209 const uint8_t hdmi2 = 1;
9210 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009211 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009212
9213 addConfigurationProperty("touch.deviceType", "touchScreen");
9214 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009215 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009216
9217 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9218 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9219
9220 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9221 // for this input device is specified, and the matching viewport is not present,
9222 // the input device should be disabled (at the mapper level).
9223
9224 // Add viewport for display 2 on hdmi2
9225 prepareSecondaryDisplay(type, hdmi2);
9226 // Send a touch event
9227 processPosition(mapper, 100, 100);
9228 processSync(mapper);
9229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9230
9231 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009232 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009233 // Send a touch event again
9234 processPosition(mapper, 100, 100);
9235 processSync(mapper);
9236
9237 NotifyMotionArgs args;
9238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9239 ASSERT_EQ(DISPLAY_ID, args.displayId);
9240}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009241
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009242TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9243 addConfigurationProperty("touch.deviceType", "touchScreen");
9244 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009245 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009246
9247 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9248
Michael Wrighta9cf4192022-12-01 23:46:39 +00009249 prepareDisplay(ui::ROTATION_0);
9250 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009251
9252 // Send a touch event
9253 processPosition(mapper, 100, 100);
9254 processSync(mapper);
9255
9256 NotifyMotionArgs args;
9257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9258 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9259}
9260
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009261TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009262 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009263 std::shared_ptr<FakePointerController> fakePointerController =
9264 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009265 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009266 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009267 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009268
Garfield Tan888a6a42020-01-09 11:39:16 -08009269 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009270 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009271
Michael Wrighta9cf4192022-12-01 23:46:39 +00009272 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009273 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009274 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009275
Josep del Río2d8c79a2023-01-23 19:33:50 +00009276 // Check source is mouse that would obtain the PointerController.
9277 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009278
9279 NotifyMotionArgs motionArgs;
9280 processPosition(mapper, 100, 100);
9281 processSync(mapper);
9282
9283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9284 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9285 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9286}
9287
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009288/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009289 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9290 */
9291TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9292 addConfigurationProperty("touch.deviceType", "touchScreen");
9293 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009294 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009295
Michael Wrighta9cf4192022-12-01 23:46:39 +00009296 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009297 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9298 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9299 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9300 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009301
9302 NotifyMotionArgs args;
9303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9304 ASSERT_EQ(26, args.readTime);
9305
Harry Cutts33476232023-01-30 19:57:29 +00009306 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9307 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9308 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009309
9310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9311 ASSERT_EQ(33, args.readTime);
9312}
9313
9314/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009315 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9316 * events should not be delivered to the listener.
9317 */
9318TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9319 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009320 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009321 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009322 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009323 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009324 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009325 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009326
9327 NotifyMotionArgs motionArgs;
9328 processPosition(mapper, 100, 100);
9329 processSync(mapper);
9330
9331 mFakeListener->assertNotifyMotionWasNotCalled();
9332}
9333
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009334/**
9335 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9336 * the touch mapper can process the events and the events can be delivered to the listener.
9337 */
9338TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9339 addConfigurationProperty("touch.deviceType", "touchScreen");
9340 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009341 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009342 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009343 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009344 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009345 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009346
9347 NotifyMotionArgs motionArgs;
9348 processPosition(mapper, 100, 100);
9349 processSync(mapper);
9350
9351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9352 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9353}
9354
Garfield Tanc734e4f2021-01-15 20:01:39 -08009355TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9356 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009357 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009358 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009359 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009360 std::optional<DisplayViewport> optionalDisplayViewport =
9361 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9362 ASSERT_TRUE(optionalDisplayViewport.has_value());
9363 DisplayViewport displayViewport = *optionalDisplayViewport;
9364
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009365 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009366 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009367 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009368
9369 // Finger down
9370 int32_t x = 100, y = 100;
9371 processPosition(mapper, x, y);
9372 processSync(mapper);
9373
9374 NotifyMotionArgs motionArgs;
9375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9376 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9377
9378 // Deactivate display viewport
9379 displayViewport.isActive = false;
9380 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009381 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009382
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009383 // The ongoing touch should be canceled immediately
9384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9385 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9386
9387 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009388 x += 10, y += 10;
9389 processPosition(mapper, x, y);
9390 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009392
9393 // Reactivate display viewport
9394 displayViewport.isActive = true;
9395 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009396 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009397
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009398 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009399 x += 10, y += 10;
9400 processPosition(mapper, x, y);
9401 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9403 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009404}
9405
Arthur Hung7c645402019-01-25 17:45:42 +08009406TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9407 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009408 prepareAxes(POSITION | ID | SLOT);
9409 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009410 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009411
9412 // Create the second touch screen device, and enable multi fingers.
9413 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009414 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009415 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009416 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009417 std::shared_ptr<InputDevice> device2 =
9418 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009419 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009420
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009421 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009422 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009423 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009424 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009425 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009426 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009427 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009428 /*flat=*/0, /*fuzz=*/0);
9429 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009430 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9431 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009432
9433 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +00009434 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9435 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9436 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009437 std::list<NotifyArgs> unused =
9438 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009439 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009440 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009441
9442 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009443 std::shared_ptr<FakePointerController> fakePointerController =
9444 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009445 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009446
9447 // Setup policy for associated displays and show touches.
9448 const uint8_t hdmi1 = 0;
9449 const uint8_t hdmi2 = 1;
9450 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9451 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9452 mFakePolicy->setShowTouches(true);
9453
9454 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009455 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009456 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009457
9458 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009459 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009460 InputReaderConfiguration::Change::DISPLAY_INFO |
9461 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009462
9463 // Two fingers down at default display.
9464 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9465 processPosition(mapper, x1, y1);
9466 processId(mapper, 1);
9467 processSlot(mapper, 1);
9468 processPosition(mapper, x2, y2);
9469 processId(mapper, 2);
9470 processSync(mapper);
9471
9472 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9473 fakePointerController->getSpots().find(DISPLAY_ID);
9474 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9475 ASSERT_EQ(size_t(2), iter->second.size());
9476
9477 // Two fingers down at second display.
9478 processPosition(mapper2, x1, y1);
9479 processId(mapper2, 1);
9480 processSlot(mapper2, 1);
9481 processPosition(mapper2, x2, y2);
9482 processId(mapper2, 2);
9483 processSync(mapper2);
9484
9485 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9486 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9487 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009488
9489 // Disable the show touches configuration and ensure the spots are cleared.
9490 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009491 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009492 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009493
9494 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009495}
9496
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009497TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009498 prepareAxes(POSITION);
9499 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009500 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009501 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009502
9503 NotifyMotionArgs motionArgs;
9504 // Unrotated video frame
9505 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9506 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009507 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009508 processPosition(mapper, 100, 200);
9509 processSync(mapper);
9510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9511 ASSERT_EQ(frames, motionArgs.videoFrames);
9512
9513 // Subsequent touch events should not have any videoframes
9514 // This is implemented separately in FakeEventHub,
9515 // but that should match the behaviour of TouchVideoDevice.
9516 processPosition(mapper, 200, 200);
9517 processSync(mapper);
9518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9519 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9520}
9521
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009522TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009523 prepareAxes(POSITION);
9524 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009525 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009526 // Unrotated video frame
9527 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9528 NotifyMotionArgs motionArgs;
9529
9530 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009531 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009532 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9533 clearViewports();
9534 prepareDisplay(orientation);
9535 std::vector<TouchVideoFrame> frames{frame};
9536 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9537 processPosition(mapper, 100, 200);
9538 processSync(mapper);
9539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9540 ASSERT_EQ(frames, motionArgs.videoFrames);
9541 }
9542}
9543
9544TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9545 prepareAxes(POSITION);
9546 addConfigurationProperty("touch.deviceType", "touchScreen");
9547 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9548 // orientation-aware are affected by display rotation.
9549 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009550 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009551 // Unrotated video frame
9552 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9553 NotifyMotionArgs motionArgs;
9554
9555 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009556 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009557 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9558 clearViewports();
9559 prepareDisplay(orientation);
9560 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009561 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009562 processPosition(mapper, 100, 200);
9563 processSync(mapper);
9564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009565 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9566 // compared to the display. This is so that when the window transform (which contains the
9567 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9568 // window's coordinate space.
9569 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009570 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009571
9572 // Release finger.
9573 processSync(mapper);
9574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009575 }
9576}
9577
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009578TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009579 prepareAxes(POSITION);
9580 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009581 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009582 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9583 // so mix these.
9584 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9585 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9586 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9587 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9588 NotifyMotionArgs motionArgs;
9589
Michael Wrighta9cf4192022-12-01 23:46:39 +00009590 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009591 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009592 processPosition(mapper, 100, 200);
9593 processSync(mapper);
9594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009595 ASSERT_EQ(frames, motionArgs.videoFrames);
9596}
9597
9598TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9599 prepareAxes(POSITION);
9600 addConfigurationProperty("touch.deviceType", "touchScreen");
9601 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9602 // orientation-aware are affected by display rotation.
9603 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009604 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009605 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9606 // so mix these.
9607 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9608 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9609 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9610 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9611 NotifyMotionArgs motionArgs;
9612
Michael Wrighta9cf4192022-12-01 23:46:39 +00009613 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009614 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9615 processPosition(mapper, 100, 200);
9616 processSync(mapper);
9617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9618 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9619 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9620 // compared to the display. This is so that when the window transform (which contains the
9621 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9622 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009623 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009624 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009625 ASSERT_EQ(frames, motionArgs.videoFrames);
9626}
9627
Arthur Hung9da14732019-09-02 16:16:58 +08009628/**
9629 * If we had defined port associations, but the viewport is not ready, the touch device would be
9630 * expected to be disabled, and it should be enabled after the viewport has found.
9631 */
9632TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009633 constexpr uint8_t hdmi2 = 1;
9634 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009635 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009636
9637 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9638
9639 addConfigurationProperty("touch.deviceType", "touchScreen");
9640 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009641 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009642
9643 ASSERT_EQ(mDevice->isEnabled(), false);
9644
9645 // Add display on hdmi2, the device should be enabled and can receive touch event.
9646 prepareSecondaryDisplay(type, hdmi2);
9647 ASSERT_EQ(mDevice->isEnabled(), true);
9648
9649 // Send a touch event.
9650 processPosition(mapper, 100, 100);
9651 processSync(mapper);
9652
9653 NotifyMotionArgs args;
9654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9655 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9656}
9657
Arthur Hung421eb1c2020-01-16 00:09:42 +08009658TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009659 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009660 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009661 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009662 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009663
9664 NotifyMotionArgs motionArgs;
9665
9666 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9667 // finger down
9668 processId(mapper, 1);
9669 processPosition(mapper, x1, y1);
9670 processSync(mapper);
9671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9672 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009673 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009674
9675 // finger move
9676 processId(mapper, 1);
9677 processPosition(mapper, x2, y2);
9678 processSync(mapper);
9679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9680 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009681 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009682
9683 // finger up.
9684 processId(mapper, -1);
9685 processSync(mapper);
9686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9687 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009688 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009689
9690 // new finger down
9691 processId(mapper, 1);
9692 processPosition(mapper, x3, y3);
9693 processSync(mapper);
9694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9695 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009696 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009697}
9698
9699/**
arthurhungcc7f9802020-04-30 17:55:40 +08009700 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9701 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009702 */
arthurhungcc7f9802020-04-30 17:55:40 +08009703TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009704 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009705 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009706 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009707 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009708
9709 NotifyMotionArgs motionArgs;
9710
9711 // default tool type is finger
9712 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009713 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009714 processPosition(mapper, x1, y1);
9715 processSync(mapper);
9716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9717 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009718 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009719
9720 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9721 processToolType(mapper, MT_TOOL_PALM);
9722 processSync(mapper);
9723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9724 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9725
9726 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009727 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009728 processPosition(mapper, x2, y2);
9729 processSync(mapper);
9730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9731
9732 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009733 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009734 processSync(mapper);
9735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9736
9737 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009738 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009739 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009740 processPosition(mapper, x3, y3);
9741 processSync(mapper);
9742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9743 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009744 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009745}
9746
arthurhungbf89a482020-04-17 17:37:55 +08009747/**
arthurhungcc7f9802020-04-30 17:55:40 +08009748 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9749 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009750 */
arthurhungcc7f9802020-04-30 17:55:40 +08009751TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009752 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009753 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009754 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009755 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009756
9757 NotifyMotionArgs motionArgs;
9758
9759 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009760 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9761 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009762 processPosition(mapper, x1, y1);
9763 processSync(mapper);
9764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9765 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009766 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009767
9768 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009769 processSlot(mapper, SECOND_SLOT);
9770 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009771 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009772 processSync(mapper);
9773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009774 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009775 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009776
9777 // If the tool type of the first finger changes to MT_TOOL_PALM,
9778 // we expect to receive ACTION_POINTER_UP with cancel flag.
9779 processSlot(mapper, FIRST_SLOT);
9780 processId(mapper, FIRST_TRACKING_ID);
9781 processToolType(mapper, MT_TOOL_PALM);
9782 processSync(mapper);
9783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009784 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009785 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9786
9787 // The following MOVE events of second finger should be processed.
9788 processSlot(mapper, SECOND_SLOT);
9789 processId(mapper, SECOND_TRACKING_ID);
9790 processPosition(mapper, x2 + 1, y2 + 1);
9791 processSync(mapper);
9792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9793 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9794 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9795
9796 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9797 // it. Second finger receive move.
9798 processSlot(mapper, FIRST_SLOT);
9799 processId(mapper, INVALID_TRACKING_ID);
9800 processSync(mapper);
9801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9802 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9803 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9804
9805 // Second finger keeps moving.
9806 processSlot(mapper, SECOND_SLOT);
9807 processId(mapper, SECOND_TRACKING_ID);
9808 processPosition(mapper, x2 + 2, y2 + 2);
9809 processSync(mapper);
9810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9812 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9813
9814 // Second finger up.
9815 processId(mapper, INVALID_TRACKING_ID);
9816 processSync(mapper);
9817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9818 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9819 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9820}
9821
9822/**
9823 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9824 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9825 */
9826TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9827 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009828 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009829 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009830 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009831
9832 NotifyMotionArgs motionArgs;
9833
9834 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9835 // First finger down.
9836 processId(mapper, FIRST_TRACKING_ID);
9837 processPosition(mapper, x1, y1);
9838 processSync(mapper);
9839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9840 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009841 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009842
9843 // Second finger down.
9844 processSlot(mapper, SECOND_SLOT);
9845 processId(mapper, SECOND_TRACKING_ID);
9846 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009847 processSync(mapper);
9848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009849 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009850 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009851
arthurhungcc7f9802020-04-30 17:55:40 +08009852 // If the tool type of the first finger changes to MT_TOOL_PALM,
9853 // we expect to receive ACTION_POINTER_UP with cancel flag.
9854 processSlot(mapper, FIRST_SLOT);
9855 processId(mapper, FIRST_TRACKING_ID);
9856 processToolType(mapper, MT_TOOL_PALM);
9857 processSync(mapper);
9858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009859 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009860 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9861
9862 // Second finger keeps moving.
9863 processSlot(mapper, SECOND_SLOT);
9864 processId(mapper, SECOND_TRACKING_ID);
9865 processPosition(mapper, x2 + 1, y2 + 1);
9866 processSync(mapper);
9867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9869
9870 // second finger becomes palm, receive cancel due to only 1 finger is active.
9871 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009872 processToolType(mapper, MT_TOOL_PALM);
9873 processSync(mapper);
9874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9875 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9876
arthurhungcc7f9802020-04-30 17:55:40 +08009877 // third finger down.
9878 processSlot(mapper, THIRD_SLOT);
9879 processId(mapper, THIRD_TRACKING_ID);
9880 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009881 processPosition(mapper, x3, y3);
9882 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9884 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009885 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009886 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9887
9888 // third finger move
9889 processId(mapper, THIRD_TRACKING_ID);
9890 processPosition(mapper, x3 + 1, y3 + 1);
9891 processSync(mapper);
9892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9893 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9894
9895 // first finger up, third finger receive move.
9896 processSlot(mapper, FIRST_SLOT);
9897 processId(mapper, INVALID_TRACKING_ID);
9898 processSync(mapper);
9899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9901 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9902
9903 // second finger up, third finger receive move.
9904 processSlot(mapper, SECOND_SLOT);
9905 processId(mapper, INVALID_TRACKING_ID);
9906 processSync(mapper);
9907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9909 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9910
9911 // third finger up.
9912 processSlot(mapper, THIRD_SLOT);
9913 processId(mapper, INVALID_TRACKING_ID);
9914 processSync(mapper);
9915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9916 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9917 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9918}
9919
9920/**
9921 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9922 * and the active finger could still be allowed to receive the events
9923 */
9924TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9925 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009926 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009927 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009928 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009929
9930 NotifyMotionArgs motionArgs;
9931
9932 // default tool type is finger
9933 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9934 processId(mapper, FIRST_TRACKING_ID);
9935 processPosition(mapper, x1, y1);
9936 processSync(mapper);
9937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9938 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009939 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009940
9941 // Second finger down.
9942 processSlot(mapper, SECOND_SLOT);
9943 processId(mapper, SECOND_TRACKING_ID);
9944 processPosition(mapper, x2, y2);
9945 processSync(mapper);
9946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009947 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009948 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009949
9950 // If the tool type of the second finger changes to MT_TOOL_PALM,
9951 // we expect to receive ACTION_POINTER_UP with cancel flag.
9952 processId(mapper, SECOND_TRACKING_ID);
9953 processToolType(mapper, MT_TOOL_PALM);
9954 processSync(mapper);
9955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009956 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009957 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9958
9959 // The following MOVE event should be processed.
9960 processSlot(mapper, FIRST_SLOT);
9961 processId(mapper, FIRST_TRACKING_ID);
9962 processPosition(mapper, x1 + 1, y1 + 1);
9963 processSync(mapper);
9964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9966 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9967
9968 // second finger up.
9969 processSlot(mapper, SECOND_SLOT);
9970 processId(mapper, INVALID_TRACKING_ID);
9971 processSync(mapper);
9972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9974
9975 // first finger keep moving
9976 processSlot(mapper, FIRST_SLOT);
9977 processId(mapper, FIRST_TRACKING_ID);
9978 processPosition(mapper, x1 + 2, y1 + 2);
9979 processSync(mapper);
9980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9982
9983 // first finger up.
9984 processId(mapper, INVALID_TRACKING_ID);
9985 processSync(mapper);
9986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9987 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9988 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009989}
9990
Arthur Hung9ad18942021-06-19 02:04:46 +00009991/**
9992 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9993 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9994 * cause slot be valid again.
9995 */
9996TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9997 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009998 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009999 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010000 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010001
10002 NotifyMotionArgs motionArgs;
10003
10004 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10005 // First finger down.
10006 processId(mapper, FIRST_TRACKING_ID);
10007 processPosition(mapper, x1, y1);
10008 processPressure(mapper, RAW_PRESSURE_MAX);
10009 processSync(mapper);
10010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10011 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10012 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10013
10014 // First finger move.
10015 processId(mapper, FIRST_TRACKING_ID);
10016 processPosition(mapper, x1 + 1, y1 + 1);
10017 processPressure(mapper, RAW_PRESSURE_MAX);
10018 processSync(mapper);
10019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10021 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10022
10023 // Second finger down.
10024 processSlot(mapper, SECOND_SLOT);
10025 processId(mapper, SECOND_TRACKING_ID);
10026 processPosition(mapper, x2, y2);
10027 processPressure(mapper, RAW_PRESSURE_MAX);
10028 processSync(mapper);
10029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010030 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010031 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10032
10033 // second finger up with some unexpected data.
10034 processSlot(mapper, SECOND_SLOT);
10035 processId(mapper, INVALID_TRACKING_ID);
10036 processPosition(mapper, x2, y2);
10037 processSync(mapper);
10038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010039 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010040 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10041
10042 // first finger up with some unexpected data.
10043 processSlot(mapper, FIRST_SLOT);
10044 processId(mapper, INVALID_TRACKING_ID);
10045 processPosition(mapper, x2, y2);
10046 processPressure(mapper, RAW_PRESSURE_MAX);
10047 processSync(mapper);
10048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10049 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10050 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10051}
10052
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010053TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10054 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010055 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010056 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010057 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010058
10059 // First finger down.
10060 processId(mapper, FIRST_TRACKING_ID);
10061 processPosition(mapper, 100, 200);
10062 processPressure(mapper, RAW_PRESSURE_MAX);
10063 processSync(mapper);
10064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10065 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10066
10067 // Second finger down.
10068 processSlot(mapper, SECOND_SLOT);
10069 processId(mapper, SECOND_TRACKING_ID);
10070 processPosition(mapper, 300, 400);
10071 processPressure(mapper, RAW_PRESSURE_MAX);
10072 processSync(mapper);
10073 ASSERT_NO_FATAL_FAILURE(
10074 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10075
10076 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010077 // preserved. Resetting should cancel the ongoing gesture.
10078 resetMapper(mapper, ARBITRARY_TIME);
10079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10080 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010081
10082 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10083 // the existing touch state to generate a down event.
10084 processPosition(mapper, 301, 302);
10085 processSync(mapper);
10086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10087 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10089 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10090
10091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10092}
10093
10094TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10095 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010096 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010097 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010098 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010099
10100 // First finger touches down and releases.
10101 processId(mapper, FIRST_TRACKING_ID);
10102 processPosition(mapper, 100, 200);
10103 processPressure(mapper, RAW_PRESSURE_MAX);
10104 processSync(mapper);
10105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10106 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10107 processId(mapper, INVALID_TRACKING_ID);
10108 processSync(mapper);
10109 ASSERT_NO_FATAL_FAILURE(
10110 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10111
10112 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10113 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010114 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10116
10117 // Send an empty sync frame. Since there are no pointers, no events are generated.
10118 processSync(mapper);
10119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10120}
10121
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010122TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010123 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010124 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010125 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010126 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010128
10129 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10130 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10131 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10132 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10133 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10134
10135 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010136 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010137 processId(mapper, FIRST_TRACKING_ID);
10138 processToolType(mapper, MT_TOOL_PEN);
10139 processPosition(mapper, 100, 200);
10140 processPressure(mapper, RAW_PRESSURE_MAX);
10141 processSync(mapper);
10142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10143 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10144 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010145 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010146
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010147 // Now that we know the device supports styluses, ensure that the device is re-configured with
10148 // the stylus source.
10149 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10150 {
10151 const auto& devices = mReader->getInputDevices();
10152 auto deviceInfo =
10153 std::find_if(devices.begin(), devices.end(),
10154 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10155 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10156 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10157 }
10158
10159 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10161
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010162 processId(mapper, INVALID_TRACKING_ID);
10163 processSync(mapper);
10164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10165 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10166 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010167 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010168}
10169
Seunghwan Choi356026c2023-02-01 14:37:25 +090010170TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10171 addConfigurationProperty("touch.deviceType", "touchScreen");
10172 prepareDisplay(ui::ROTATION_0);
10173 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10174 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10175 // indicate stylus presence dynamically.
10176 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10177 std::shared_ptr<FakePointerController> fakePointerController =
10178 std::make_shared<FakePointerController>();
10179 mFakePolicy->setPointerController(fakePointerController);
10180 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +000010181 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010182
10183 processId(mapper, FIRST_TRACKING_ID);
10184 processPressure(mapper, RAW_PRESSURE_MIN);
10185 processPosition(mapper, 100, 200);
10186 processToolType(mapper, MT_TOOL_PEN);
10187 processSync(mapper);
10188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10189 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010190 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010191 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10192 ASSERT_TRUE(fakePointerController->isPointerShown());
10193 ASSERT_NO_FATAL_FAILURE(
10194 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10195}
10196
10197TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10198 addConfigurationProperty("touch.deviceType", "touchScreen");
10199 prepareDisplay(ui::ROTATION_0);
10200 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10201 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10202 // indicate stylus presence dynamically.
10203 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10204 std::shared_ptr<FakePointerController> fakePointerController =
10205 std::make_shared<FakePointerController>();
10206 mFakePolicy->setPointerController(fakePointerController);
10207 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010208 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010209
10210 processId(mapper, FIRST_TRACKING_ID);
10211 processPressure(mapper, RAW_PRESSURE_MIN);
10212 processPosition(mapper, 100, 200);
10213 processToolType(mapper, MT_TOOL_PEN);
10214 processSync(mapper);
10215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10216 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010217 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010218 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10219 ASSERT_FALSE(fakePointerController->isPointerShown());
10220}
10221
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010222// --- MultiTouchInputMapperTest_ExternalDevice ---
10223
10224class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10225protected:
Chris Yea52ade12020-08-27 16:49:20 -070010226 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010227};
10228
10229/**
10230 * Expect fallback to internal viewport if device is external and external viewport is not present.
10231 */
10232TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10233 prepareAxes(POSITION);
10234 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010235 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010236 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010237
10238 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10239
10240 NotifyMotionArgs motionArgs;
10241
10242 // Expect the event to be sent to the internal viewport,
10243 // because an external viewport is not present.
10244 processPosition(mapper, 100, 100);
10245 processSync(mapper);
10246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10247 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10248
10249 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010250 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010251 processPosition(mapper, 100, 100);
10252 processSync(mapper);
10253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10254 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10255}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010256
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010257TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10258 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10259 std::shared_ptr<FakePointerController> fakePointerController =
10260 std::make_shared<FakePointerController>();
10261 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10262 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010263
10264 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010265 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010266 prepareAxes(POSITION | ID | SLOT);
10267 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10268 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10269 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010270 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010271 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010272
10273 // captured touchpad should be a touchpad source
10274 NotifyDeviceResetArgs resetArgs;
10275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10276 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10277
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010278 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010279
10280 const InputDeviceInfo::MotionRange* relRangeX =
10281 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10282 ASSERT_NE(relRangeX, nullptr);
10283 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10284 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10285 const InputDeviceInfo::MotionRange* relRangeY =
10286 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10287 ASSERT_NE(relRangeY, nullptr);
10288 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10289 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10290
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010291 // run captured pointer tests - note that this is unscaled, so input listener events should be
10292 // identical to what the hardware sends (accounting for any
10293 // calibration).
10294 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010295 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010296 processId(mapper, 1);
10297 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10298 processKey(mapper, BTN_TOUCH, 1);
10299 processSync(mapper);
10300
10301 // expect coord[0] to contain initial location of touch 0
10302 NotifyMotionArgs args;
10303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10304 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10305 ASSERT_EQ(1U, args.pointerCount);
10306 ASSERT_EQ(0, args.pointerProperties[0].id);
10307 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10308 ASSERT_NO_FATAL_FAILURE(
10309 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10310
10311 // FINGER 1 DOWN
10312 processSlot(mapper, 1);
10313 processId(mapper, 2);
10314 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10315 processSync(mapper);
10316
10317 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010319 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010320 ASSERT_EQ(2U, args.pointerCount);
10321 ASSERT_EQ(0, args.pointerProperties[0].id);
10322 ASSERT_EQ(1, args.pointerProperties[1].id);
10323 ASSERT_NO_FATAL_FAILURE(
10324 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10325 ASSERT_NO_FATAL_FAILURE(
10326 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10327
10328 // FINGER 1 MOVE
10329 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10330 processSync(mapper);
10331
10332 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10333 // from move
10334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10336 ASSERT_NO_FATAL_FAILURE(
10337 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10338 ASSERT_NO_FATAL_FAILURE(
10339 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10340
10341 // FINGER 0 MOVE
10342 processSlot(mapper, 0);
10343 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10344 processSync(mapper);
10345
10346 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10349 ASSERT_NO_FATAL_FAILURE(
10350 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10351 ASSERT_NO_FATAL_FAILURE(
10352 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10353
10354 // BUTTON DOWN
10355 processKey(mapper, BTN_LEFT, 1);
10356 processSync(mapper);
10357
10358 // touchinputmapper design sends a move before button press
10359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10362 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10363
10364 // BUTTON UP
10365 processKey(mapper, BTN_LEFT, 0);
10366 processSync(mapper);
10367
10368 // touchinputmapper design sends a move after button release
10369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10373
10374 // FINGER 0 UP
10375 processId(mapper, -1);
10376 processSync(mapper);
10377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10378 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10379
10380 // FINGER 1 MOVE
10381 processSlot(mapper, 1);
10382 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10383 processSync(mapper);
10384
10385 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10388 ASSERT_EQ(1U, args.pointerCount);
10389 ASSERT_EQ(1, args.pointerProperties[0].id);
10390 ASSERT_NO_FATAL_FAILURE(
10391 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10392
10393 // FINGER 1 UP
10394 processId(mapper, -1);
10395 processKey(mapper, BTN_TOUCH, 0);
10396 processSync(mapper);
10397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10398 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10399
Josep del Río2d8c79a2023-01-23 19:33:50 +000010400 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010401 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010402 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010404 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010405}
10406
10407TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10408 std::shared_ptr<FakePointerController> fakePointerController =
10409 std::make_shared<FakePointerController>();
10410 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10411 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010412
10413 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010414 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010415 prepareAxes(POSITION | ID | SLOT);
10416 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10417 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010418 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000010419 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010420 // run uncaptured pointer tests - pushes out generic events
10421 // FINGER 0 DOWN
10422 processId(mapper, 3);
10423 processPosition(mapper, 100, 100);
10424 processKey(mapper, BTN_TOUCH, 1);
10425 processSync(mapper);
10426
10427 // start at (100,100), cursor should be at (0,0) * scale
10428 NotifyMotionArgs args;
10429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10430 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10431 ASSERT_NO_FATAL_FAILURE(
10432 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10433
10434 // FINGER 0 MOVE
10435 processPosition(mapper, 200, 200);
10436 processSync(mapper);
10437
10438 // compute scaling to help with touch position checking
10439 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10440 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10441 float scale =
10442 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10443
10444 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10446 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10448 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010449
10450 // BUTTON DOWN
10451 processKey(mapper, BTN_LEFT, 1);
10452 processSync(mapper);
10453
10454 // touchinputmapper design sends a move before button press
10455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10456 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10458 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10459
10460 // BUTTON UP
10461 processKey(mapper, BTN_LEFT, 0);
10462 processSync(mapper);
10463
10464 // touchinputmapper design sends a move after button release
10465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10466 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10468 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010469}
10470
10471TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10472 std::shared_ptr<FakePointerController> fakePointerController =
10473 std::make_shared<FakePointerController>();
10474
Michael Wrighta9cf4192022-12-01 23:46:39 +000010475 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010476 prepareAxes(POSITION | ID | SLOT);
10477 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010478 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010479 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010480 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010481
Josep del Río2d8c79a2023-01-23 19:33:50 +000010482 // uncaptured touchpad should be a pointer device
10483 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010484
Josep del Río2d8c79a2023-01-23 19:33:50 +000010485 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010486 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010487 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010488 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10489}
10490
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010491// --- BluetoothMultiTouchInputMapperTest ---
10492
10493class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10494protected:
10495 void SetUp() override {
10496 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10497 }
10498};
10499
10500TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10501 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010502 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010503 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010504 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010505
10506 nsecs_t kernelEventTime = ARBITRARY_TIME;
10507 nsecs_t expectedEventTime = ARBITRARY_TIME;
10508 // Touch down.
10509 processId(mapper, FIRST_TRACKING_ID);
10510 processPosition(mapper, 100, 200);
10511 processPressure(mapper, RAW_PRESSURE_MAX);
10512 processSync(mapper, ARBITRARY_TIME);
10513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10514 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10515
10516 // Process several events that come in quick succession, according to their timestamps.
10517 for (int i = 0; i < 3; i++) {
10518 constexpr static nsecs_t delta = ms2ns(1);
10519 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10520 kernelEventTime += delta;
10521 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10522
10523 processPosition(mapper, 101 + i, 201 + i);
10524 processSync(mapper, kernelEventTime);
10525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10526 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10527 WithEventTime(expectedEventTime))));
10528 }
10529
10530 // Release the touch.
10531 processId(mapper, INVALID_TRACKING_ID);
10532 processPressure(mapper, RAW_PRESSURE_MIN);
10533 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10535 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10536 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10537}
10538
10539// --- MultiTouchPointerModeTest ---
10540
HQ Liue6983c72022-04-19 22:14:56 +000010541class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10542protected:
10543 float mPointerMovementScale;
10544 float mPointerXZoomScale;
10545 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10546 addConfigurationProperty("touch.deviceType", "pointer");
10547 std::shared_ptr<FakePointerController> fakePointerController =
10548 std::make_shared<FakePointerController>();
10549 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10550 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010551 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010552
10553 prepareAxes(POSITION);
10554 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10555 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10556 // needs to be disabled, and the pointer gesture needs to be enabled.
10557 mFakePolicy->setPointerCapture(false);
10558 mFakePolicy->setPointerGestureEnabled(true);
10559 mFakePolicy->setPointerController(fakePointerController);
10560
10561 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10562 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10563 mPointerMovementScale =
10564 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10565 mPointerXZoomScale =
10566 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10567 }
10568
10569 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10570 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10571 /*flat*/ 0,
10572 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10573 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10574 /*flat*/ 0,
10575 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10576 }
10577};
10578
10579/**
10580 * Two fingers down on a pointer mode touch pad. The width
10581 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10582 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10583 * be greater than the both value to be freeform gesture, so that after two
10584 * fingers start to move downwards, the gesture should be swipe.
10585 */
10586TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10587 // The min freeform gesture width is 25units/mm x 30mm = 750
10588 // which is greater than fraction of the diagnal length of the touchpad (349).
10589 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010590 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010591 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010592 NotifyMotionArgs motionArgs;
10593
10594 // Two fingers down at once.
10595 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10596 // Pointer's initial position is used the [0,0] coordinate.
10597 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10598
10599 processId(mapper, FIRST_TRACKING_ID);
10600 processPosition(mapper, x1, y1);
10601 processMTSync(mapper);
10602 processId(mapper, SECOND_TRACKING_ID);
10603 processPosition(mapper, x2, y2);
10604 processMTSync(mapper);
10605 processSync(mapper);
10606
10607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10608 ASSERT_EQ(1U, motionArgs.pointerCount);
10609 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010610 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010611 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010612 ASSERT_NO_FATAL_FAILURE(
10613 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10614
10615 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10616 // that there should be 1 pointer.
10617 int32_t movingDistance = 200;
10618 y1 += movingDistance;
10619 y2 += movingDistance;
10620
10621 processId(mapper, FIRST_TRACKING_ID);
10622 processPosition(mapper, x1, y1);
10623 processMTSync(mapper);
10624 processId(mapper, SECOND_TRACKING_ID);
10625 processPosition(mapper, x2, y2);
10626 processMTSync(mapper);
10627 processSync(mapper);
10628
10629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10630 ASSERT_EQ(1U, motionArgs.pointerCount);
10631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010632 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010633 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10635 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10636 0, 0, 0, 0));
10637}
10638
10639/**
10640 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10641 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10642 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10643 * value to be freeform gesture, so that after two fingers start to move downwards,
10644 * the gesture should be swipe.
10645 */
10646TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10647 // The min freeform gesture width is 5units/mm x 30mm = 150
10648 // which is greater than fraction of the diagnal length of the touchpad (349).
10649 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010650 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000010651 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010652 NotifyMotionArgs motionArgs;
10653
10654 // Two fingers down at once.
10655 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10656 // Pointer's initial position is used the [0,0] coordinate.
10657 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10658
10659 processId(mapper, FIRST_TRACKING_ID);
10660 processPosition(mapper, x1, y1);
10661 processMTSync(mapper);
10662 processId(mapper, SECOND_TRACKING_ID);
10663 processPosition(mapper, x2, y2);
10664 processMTSync(mapper);
10665 processSync(mapper);
10666
10667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10668 ASSERT_EQ(1U, motionArgs.pointerCount);
10669 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010670 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010671 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010672 ASSERT_NO_FATAL_FAILURE(
10673 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10674
10675 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10676 // and there should be 1 pointer.
10677 int32_t movingDistance = 200;
10678 y1 += movingDistance;
10679 y2 += movingDistance;
10680
10681 processId(mapper, FIRST_TRACKING_ID);
10682 processPosition(mapper, x1, y1);
10683 processMTSync(mapper);
10684 processId(mapper, SECOND_TRACKING_ID);
10685 processPosition(mapper, x2, y2);
10686 processMTSync(mapper);
10687 processSync(mapper);
10688
10689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10690 ASSERT_EQ(1U, motionArgs.pointerCount);
10691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010692 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010693 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010694 // New coordinate is the scaled relative coordinate from the initial coordinate.
10695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10696 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10697 0, 0, 0, 0));
10698}
10699
10700/**
10701 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10702 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10703 * freeform gestures after two fingers start to move downwards.
10704 */
10705TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010706 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010707 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010708
10709 NotifyMotionArgs motionArgs;
10710
10711 // Two fingers down at once. Wider than the max swipe width.
10712 // The gesture is expected to be PRESS, then transformed to FREEFORM
10713 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10714
10715 processId(mapper, FIRST_TRACKING_ID);
10716 processPosition(mapper, x1, y1);
10717 processMTSync(mapper);
10718 processId(mapper, SECOND_TRACKING_ID);
10719 processPosition(mapper, x2, y2);
10720 processMTSync(mapper);
10721 processSync(mapper);
10722
10723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10724 ASSERT_EQ(1U, motionArgs.pointerCount);
10725 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010726 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010727 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010728 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10729 ASSERT_NO_FATAL_FAILURE(
10730 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10731
10732 int32_t movingDistance = 200;
10733
10734 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10735 // then two down events for two pointers.
10736 y1 += movingDistance;
10737 y2 += movingDistance;
10738
10739 processId(mapper, FIRST_TRACKING_ID);
10740 processPosition(mapper, x1, y1);
10741 processMTSync(mapper);
10742 processId(mapper, SECOND_TRACKING_ID);
10743 processPosition(mapper, x2, y2);
10744 processMTSync(mapper);
10745 processSync(mapper);
10746
10747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10748 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10749 ASSERT_EQ(1U, motionArgs.pointerCount);
10750 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010752 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
HQ Liue6983c72022-04-19 22:14:56 +000010753 ASSERT_EQ(1U, motionArgs.pointerCount);
10754 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010756 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010757 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010758 ASSERT_EQ(2U, motionArgs.pointerCount);
10759 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010760 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010761 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010762 // Two pointers' scaled relative coordinates from their initial centroid.
10763 // Initial y coordinates are 0 as y1 and y2 have the same value.
10764 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10765 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10766 // When pointers move, the new coordinates equal to the initial coordinates plus
10767 // scaled moving distance.
10768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10769 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10770 0, 0, 0, 0));
10771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10772 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10773 0, 0, 0, 0));
10774
10775 // Move two fingers down again, expect one MOVE motion event.
10776 y1 += movingDistance;
10777 y2 += movingDistance;
10778
10779 processId(mapper, FIRST_TRACKING_ID);
10780 processPosition(mapper, x1, y1);
10781 processMTSync(mapper);
10782 processId(mapper, SECOND_TRACKING_ID);
10783 processPosition(mapper, x2, y2);
10784 processMTSync(mapper);
10785 processSync(mapper);
10786
10787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10788 ASSERT_EQ(2U, motionArgs.pointerCount);
10789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010790 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010791 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10793 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10794 0, 0, 0, 0, 0));
10795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10796 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10797 0, 0, 0, 0, 0));
10798}
10799
Harry Cutts39b7ca22022-10-05 15:55:48 +000010800TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010801 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010802 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010803 NotifyMotionArgs motionArgs;
10804
10805 // Place two fingers down.
10806 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10807
10808 processId(mapper, FIRST_TRACKING_ID);
10809 processPosition(mapper, x1, y1);
10810 processMTSync(mapper);
10811 processId(mapper, SECOND_TRACKING_ID);
10812 processPosition(mapper, x2, y2);
10813 processMTSync(mapper);
10814 processSync(mapper);
10815
10816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10817 ASSERT_EQ(1U, motionArgs.pointerCount);
10818 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10819 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10820 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10821 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10822
10823 // Move the two fingers down and to the left.
10824 int32_t movingDistance = 200;
10825 x1 -= movingDistance;
10826 y1 += movingDistance;
10827 x2 -= movingDistance;
10828 y2 += movingDistance;
10829
10830 processId(mapper, FIRST_TRACKING_ID);
10831 processPosition(mapper, x1, y1);
10832 processMTSync(mapper);
10833 processId(mapper, SECOND_TRACKING_ID);
10834 processPosition(mapper, x2, y2);
10835 processMTSync(mapper);
10836 processSync(mapper);
10837
10838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10839 ASSERT_EQ(1U, motionArgs.pointerCount);
10840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10841 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10842 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10843 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10844}
10845
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010846TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010847 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010848 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010849 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10851
10852 // Start a stylus gesture.
10853 processKey(mapper, BTN_TOOL_PEN, 1);
10854 processId(mapper, FIRST_TRACKING_ID);
10855 processPosition(mapper, 100, 200);
10856 processSync(mapper);
10857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10858 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10859 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010860 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010861 // TODO(b/257078296): Pointer mode generates extra event.
10862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10863 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10864 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010865 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10867
10868 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10869 // gesture should be disabled.
10870 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10871 viewport->isActive = false;
10872 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010873 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10875 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10876 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010877 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010878 // TODO(b/257078296): Pointer mode generates extra event.
10879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10880 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10881 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010882 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10884}
10885
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010886// --- JoystickInputMapperTest ---
10887
10888class JoystickInputMapperTest : public InputMapperTest {
10889protected:
10890 static const int32_t RAW_X_MIN;
10891 static const int32_t RAW_X_MAX;
10892 static const int32_t RAW_Y_MIN;
10893 static const int32_t RAW_Y_MAX;
10894
10895 void SetUp() override {
10896 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10897 }
10898 void prepareAxes() {
10899 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10900 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10901 }
10902
10903 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10904 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10905 }
10906
10907 void processSync(JoystickInputMapper& mapper) {
10908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10909 }
10910
Michael Wrighta9cf4192022-12-01 23:46:39 +000010911 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010912 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10913 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10914 NO_PORT, ViewportType::VIRTUAL);
10915 }
10916};
10917
10918const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10919const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10920const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10921const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10922
10923TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10924 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000010925 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010926
10927 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10928
Michael Wrighta9cf4192022-12-01 23:46:39 +000010929 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010930
10931 // Send an axis event
10932 processAxis(mapper, ABS_X, 100);
10933 processSync(mapper);
10934
10935 NotifyMotionArgs args;
10936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10937 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10938
10939 // Send another axis event
10940 processAxis(mapper, ABS_Y, 100);
10941 processSync(mapper);
10942
10943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10944 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10945}
10946
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010947// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010948
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010949class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010950protected:
10951 static const char* DEVICE_NAME;
10952 static const char* DEVICE_LOCATION;
10953 static const int32_t DEVICE_ID;
10954 static const int32_t DEVICE_GENERATION;
10955 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010956 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010957 static const int32_t EVENTHUB_ID;
10958
10959 std::shared_ptr<FakeEventHub> mFakeEventHub;
10960 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010961 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010962 std::unique_ptr<InstrumentedInputReader> mReader;
10963 std::shared_ptr<InputDevice> mDevice;
10964
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010965 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010966 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010967 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010968 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010969 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010970 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010971 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10972 }
10973
10974 void SetUp() override { SetUp(DEVICE_CLASSES); }
10975
10976 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010977 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010978 mFakePolicy.clear();
10979 }
10980
Chris Yee2b1e5c2021-03-10 22:45:12 -080010981 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10982 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010983 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010984 InputDeviceIdentifier identifier;
10985 identifier.name = name;
10986 identifier.location = location;
10987 std::shared_ptr<InputDevice> device =
10988 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10989 identifier);
10990 mReader->pushNextDevice(device);
10991 mFakeEventHub->addDevice(eventHubId, name, classes);
10992 mReader->loopOnce();
10993 return device;
10994 }
10995
10996 template <class T, typename... Args>
10997 T& addControllerAndConfigure(Args... args) {
10998 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10999
11000 return controller;
11001 }
11002};
11003
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011004const char* PeripheralControllerTest::DEVICE_NAME = "device";
11005const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11006const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11007const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11008const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011009const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11010 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011011const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011012
11013// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011014class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011015protected:
11016 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011017 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011018 }
11019};
11020
11021TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011022 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011023
Harry Cuttsa5b71292022-11-28 12:56:17 +000011024 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11025 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11026 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011027}
11028
11029TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011030 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011031
Harry Cuttsa5b71292022-11-28 12:56:17 +000011032 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11033 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11034 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011035}
11036
11037// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011038class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011039protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011040 void SetUp() override {
11041 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11042 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011043};
11044
Chris Ye85758332021-05-16 23:05:17 -070011045TEST_F(LightControllerTest, MonoLight) {
11046 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011047 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011048 .maxBrightness = 255,
11049 .flags = InputLightClass::BRIGHTNESS,
11050 .path = ""};
11051 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011052
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011053 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011054 InputDeviceInfo info;
11055 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011056 std::vector<InputDeviceLightInfo> lights = info.getLights();
11057 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011058 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11059 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11060
11061 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11062 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11063}
11064
11065TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11066 RawLightInfo infoMono = {.id = 1,
11067 .name = "mono_keyboard_backlight",
11068 .maxBrightness = 255,
11069 .flags = InputLightClass::BRIGHTNESS |
11070 InputLightClass::KEYBOARD_BACKLIGHT,
11071 .path = ""};
11072 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11073
11074 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11075 InputDeviceInfo info;
11076 controller.populateDeviceInfo(&info);
11077 std::vector<InputDeviceLightInfo> lights = info.getLights();
11078 ASSERT_EQ(1U, lights.size());
11079 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11080 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011081
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011082 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11083 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011084}
11085
11086TEST_F(LightControllerTest, RGBLight) {
11087 RawLightInfo infoRed = {.id = 1,
11088 .name = "red",
11089 .maxBrightness = 255,
11090 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11091 .path = ""};
11092 RawLightInfo infoGreen = {.id = 2,
11093 .name = "green",
11094 .maxBrightness = 255,
11095 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11096 .path = ""};
11097 RawLightInfo infoBlue = {.id = 3,
11098 .name = "blue",
11099 .maxBrightness = 255,
11100 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11101 .path = ""};
11102 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11103 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11104 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11105
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011106 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011107 InputDeviceInfo info;
11108 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011109 std::vector<InputDeviceLightInfo> lights = info.getLights();
11110 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011111 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11112 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11113 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11114
11115 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11116 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11117}
11118
11119TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11120 RawLightInfo infoRed = {.id = 1,
11121 .name = "red_keyboard_backlight",
11122 .maxBrightness = 255,
11123 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11124 InputLightClass::KEYBOARD_BACKLIGHT,
11125 .path = ""};
11126 RawLightInfo infoGreen = {.id = 2,
11127 .name = "green_keyboard_backlight",
11128 .maxBrightness = 255,
11129 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11130 InputLightClass::KEYBOARD_BACKLIGHT,
11131 .path = ""};
11132 RawLightInfo infoBlue = {.id = 3,
11133 .name = "blue_keyboard_backlight",
11134 .maxBrightness = 255,
11135 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11136 InputLightClass::KEYBOARD_BACKLIGHT,
11137 .path = ""};
11138 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11139 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11140 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11141
11142 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11143 InputDeviceInfo info;
11144 controller.populateDeviceInfo(&info);
11145 std::vector<InputDeviceLightInfo> lights = info.getLights();
11146 ASSERT_EQ(1U, lights.size());
11147 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11148 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11149 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11150
11151 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11152 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11153}
11154
11155TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11156 RawLightInfo infoRed = {.id = 1,
11157 .name = "red",
11158 .maxBrightness = 255,
11159 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11160 .path = ""};
11161 RawLightInfo infoGreen = {.id = 2,
11162 .name = "green",
11163 .maxBrightness = 255,
11164 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11165 .path = ""};
11166 RawLightInfo infoBlue = {.id = 3,
11167 .name = "blue",
11168 .maxBrightness = 255,
11169 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11170 .path = ""};
11171 RawLightInfo infoGlobal = {.id = 3,
11172 .name = "global_keyboard_backlight",
11173 .maxBrightness = 255,
11174 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11175 InputLightClass::KEYBOARD_BACKLIGHT,
11176 .path = ""};
11177 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11178 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11179 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11180 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11181
11182 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11183 InputDeviceInfo info;
11184 controller.populateDeviceInfo(&info);
11185 std::vector<InputDeviceLightInfo> lights = info.getLights();
11186 ASSERT_EQ(1U, lights.size());
11187 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11188 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11189 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011190
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011191 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11192 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011193}
11194
11195TEST_F(LightControllerTest, MultiColorRGBLight) {
11196 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011197 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011198 .maxBrightness = 255,
11199 .flags = InputLightClass::BRIGHTNESS |
11200 InputLightClass::MULTI_INTENSITY |
11201 InputLightClass::MULTI_INDEX,
11202 .path = ""};
11203
11204 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11205
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011206 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011207 InputDeviceInfo info;
11208 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011209 std::vector<InputDeviceLightInfo> lights = info.getLights();
11210 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011211 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11212 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11213 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11214
11215 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11216 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11217}
11218
11219TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11220 RawLightInfo infoColor = {.id = 1,
11221 .name = "multi_color_keyboard_backlight",
11222 .maxBrightness = 255,
11223 .flags = InputLightClass::BRIGHTNESS |
11224 InputLightClass::MULTI_INTENSITY |
11225 InputLightClass::MULTI_INDEX |
11226 InputLightClass::KEYBOARD_BACKLIGHT,
11227 .path = ""};
11228
11229 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11230
11231 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11232 InputDeviceInfo info;
11233 controller.populateDeviceInfo(&info);
11234 std::vector<InputDeviceLightInfo> lights = info.getLights();
11235 ASSERT_EQ(1U, lights.size());
11236 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11237 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11238 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011239
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011240 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11241 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011242}
11243
11244TEST_F(LightControllerTest, PlayerIdLight) {
11245 RawLightInfo info1 = {.id = 1,
11246 .name = "player1",
11247 .maxBrightness = 255,
11248 .flags = InputLightClass::BRIGHTNESS,
11249 .path = ""};
11250 RawLightInfo info2 = {.id = 2,
11251 .name = "player2",
11252 .maxBrightness = 255,
11253 .flags = InputLightClass::BRIGHTNESS,
11254 .path = ""};
11255 RawLightInfo info3 = {.id = 3,
11256 .name = "player3",
11257 .maxBrightness = 255,
11258 .flags = InputLightClass::BRIGHTNESS,
11259 .path = ""};
11260 RawLightInfo info4 = {.id = 4,
11261 .name = "player4",
11262 .maxBrightness = 255,
11263 .flags = InputLightClass::BRIGHTNESS,
11264 .path = ""};
11265 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11266 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11267 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11268 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11269
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011270 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011271 InputDeviceInfo info;
11272 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011273 std::vector<InputDeviceLightInfo> lights = info.getLights();
11274 ASSERT_EQ(1U, lights.size());
11275 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011276 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11277 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011278
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011279 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11280 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11281 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011282}
11283
Michael Wrightd02c5b62014-02-10 15:10:22 -080011284} // namespace android