blob: 9ccd965dce95bc1ec639bf1bd9e33ac93d2e2b40 [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);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001337
Arpit Singha11fc942023-08-09 09:23:43 +00001338 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001339 }
1340
Chris Yea52ade12020-08-27 16:49:20 -07001341 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001342#if !defined(__ANDROID__)
1343 return;
1344#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001345 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001346 mReader.reset();
1347 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001348 mFakePolicy.clear();
1349 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001350
1351 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1352 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1353 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1354 [&name](const InputDeviceInfo& info) {
1355 return info.getIdentifier().name == name;
1356 });
1357 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1358 }
Arpit Singha11fc942023-08-09 09:23:43 +00001359
1360 void setupInputReader() {
1361 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1362 /*eventDidNotHappenTimeout=*/30ms);
1363
1364 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1365 *mTestListener);
1366 ASSERT_EQ(mReader->start(), OK);
1367
1368 // Since this test is run on a real device, all the input devices connected
1369 // to the test device will show up in mReader. We wait for those input devices to
1370 // show up before beginning the tests.
1371 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1372 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1373 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1374 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001375};
1376
1377TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1378 // An invalid input device that is only used for this test.
1379 class InvalidUinputDevice : public UinputDevice {
1380 public:
Harry Cutts33476232023-01-30 19:57:29 +00001381 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001382
1383 private:
1384 void configureDevice(int fd, uinput_user_dev* device) override {}
1385 };
1386
1387 const size_t numDevices = mFakePolicy->getInputDevices().size();
1388
1389 // UinputDevice does not set any event or key bits, so InputReader should not
1390 // consider it as a valid device.
1391 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1392 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1394 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1395
1396 invalidDevice.reset();
1397 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1398 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1399 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1400}
1401
1402TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1403 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1404
1405 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1406 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1407 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1408 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1409
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001410 const auto device = findDeviceByName(keyboard->getName());
1411 ASSERT_TRUE(device.has_value());
1412 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1413 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1414 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001415
1416 keyboard.reset();
1417 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1418 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1419 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1420}
1421
1422TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1423 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1424 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1425
1426 NotifyConfigurationChangedArgs configChangedArgs;
1427 ASSERT_NO_FATAL_FAILURE(
1428 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001429 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001430 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1431
1432 NotifyKeyArgs keyArgs;
1433 keyboard->pressAndReleaseHomeKey();
1434 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1435 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001436 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001437 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001438 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001439 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001440 prevTimestamp = keyArgs.eventTime;
1441
1442 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1443 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001444 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001445 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001446 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001447}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001448
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001449TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1450 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1451 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1452
1453 const auto device = findDeviceByName(stylus->getName());
1454 ASSERT_TRUE(device.has_value());
1455
Prabir Pradhana3621852022-10-14 18:57:23 +00001456 // An external stylus with buttons should also be recognized as a keyboard.
1457 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001458 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1459 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1460
1461 const auto DOWN =
1462 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1463 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1464
1465 stylus->pressAndReleaseKey(BTN_STYLUS);
1466 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1467 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1468 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1469 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1470
1471 stylus->pressAndReleaseKey(BTN_STYLUS2);
1472 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1473 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1474 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1475 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1476
1477 stylus->pressAndReleaseKey(BTN_STYLUS3);
1478 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1479 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1480 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1481 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1482}
1483
Prabir Pradhan05f6c602023-08-18 20:02:01 +00001484TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1485 std::unique_ptr<UinputKeyboard> keyboard =
1486 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1487 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1488 KEY_R, KEY_T, KEY_Y,
1489 BTN_STYLUS, BTN_STYLUS2,
1490 BTN_STYLUS3});
1491 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1492
1493 const auto device = findDeviceByName(keyboard->getName());
1494 ASSERT_TRUE(device.has_value());
1495
1496 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1497 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1498 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1499 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1500}
1501
Prabir Pradhanada1a2a2023-08-22 23:20:16 +00001502TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1503 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1504 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1505 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1506 createUinputDevice<UinputKeyboardWithHidUsage>(
1507 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1508 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1509
1510 const auto device = findDeviceByName(keyboard->getName());
1511 ASSERT_TRUE(device.has_value());
1512
1513 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1514 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1515
1516 // If a device supports reporting HID usage codes, it shouldn't automatically support
1517 // stylus keys.
1518 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1519 uint8_t outFlags[] = {0};
1520 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1521 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1522}
1523
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001524/**
1525 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1526 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1527 * are passed to the listener.
1528 */
1529static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1530TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1531 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1532 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1533 NotifyKeyArgs keyArgs;
1534
1535 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1536 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1537 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1538 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1539
1540 controller->pressAndReleaseKey(BTN_GEAR_UP);
1541 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1542 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1543 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1544}
1545
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001546// --- TouchIntegrationTest ---
1547
Arpit Singha11fc942023-08-09 09:23:43 +00001548class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001549protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001550 const std::string UNIQUE_ID = "local:0";
1551
Chris Yea52ade12020-08-27 16:49:20 -07001552 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001553#if !defined(__ANDROID__)
1554 GTEST_SKIP();
1555#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001556 InputReaderIntegrationTest::SetUp();
1557 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001558 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1559 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001560
1561 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1562 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1563 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001564 const auto info = findDeviceByName(mDevice->getName());
1565 ASSERT_TRUE(info);
1566 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001567 }
1568
1569 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001570 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001571 std::optional<uint8_t> physicalPort,
1572 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001573 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001574 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001575 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001576 }
1577
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001578 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1579 NotifyMotionArgs args;
1580 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1581 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001582 ASSERT_EQ(points.size(), args.getPointerCount());
1583 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001584 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1585 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1586 }
1587 }
1588
Arthur Hungaab25622020-01-16 11:22:11 +08001589 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001590 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001591};
1592
Arpit Singha11fc942023-08-09 09:23:43 +00001593enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1594
1595class TouchIntegrationTest : public BaseTouchIntegrationTest,
1596 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1597protected:
1598 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1599 const std::string INPUT_PORT = "uinput_touch/input0";
1600
1601 void SetUp() override {
1602#if !defined(__ANDROID__)
1603 GTEST_SKIP();
1604#endif
1605 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1606 BaseTouchIntegrationTest::SetUp();
1607 return;
1608 }
1609
1610 // setup policy with a input-port or UniqueId association to the display
1611 bool isInputPortAssociation =
1612 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1613
1614 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1615 if (isInputPortAssociation) {
1616 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1617 } else {
1618 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1619 }
1620 mFakePointerController = std::make_shared<FakePointerController>();
1621 mFakePolicy->setPointerController(mFakePointerController);
1622
1623 InputReaderIntegrationTest::setupInputReader();
1624
1625 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1626 INPUT_PORT);
1627 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1628
1629 // Add a display linked to a physical port or UniqueId.
1630 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1631 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1632 ViewportType::INTERNAL);
1633 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1634 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1635 const auto info = findDeviceByName(mDevice->getName());
1636 ASSERT_TRUE(info);
1637 mDeviceInfo = *info;
1638 }
1639};
1640
1641TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001642 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1643 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1644 // presses).
1645 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1646 mDeviceInfo.getSources());
1647}
1648
Arpit Singha11fc942023-08-09 09:23:43 +00001649TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001650 NotifyMotionArgs args;
1651 const Point centerPoint = mDevice->getCenterPoint();
1652
1653 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001654 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001655 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001656 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001657 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1658 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1659
1660 // ACTION_MOVE
1661 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001662 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001663 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1665
1666 // ACTION_UP
1667 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001668 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001669 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1670 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1671}
1672
Arpit Singha11fc942023-08-09 09:23:43 +00001673TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001674 NotifyMotionArgs args;
1675 const Point centerPoint = mDevice->getCenterPoint();
1676
1677 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001678 mDevice->sendSlot(FIRST_SLOT);
1679 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001680 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001681 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001682 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1683 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1684
1685 // ACTION_POINTER_DOWN (Second slot)
1686 const Point secondPoint = centerPoint + Point(100, 100);
1687 mDevice->sendSlot(SECOND_SLOT);
1688 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001689 mDevice->sendDown(secondPoint);
1690 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001691 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001692 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001693
1694 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001695 mDevice->sendMove(secondPoint + Point(1, 1));
1696 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001697 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1699
1700 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001701 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001702 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001703 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001704 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001705
1706 // ACTION_UP
1707 mDevice->sendSlot(FIRST_SLOT);
1708 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001709 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001710 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1711 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1712}
1713
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001714/**
1715 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1716 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1717 * data?
1718 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1719 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1720 * for Pointer 0 only is generated after.
1721 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1722 * events, we will not miss any information.
1723 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1724 * event generated afterwards that contains the newest movement of pointer 0.
1725 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1726 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1727 * losing information about non-palm pointers.
1728 */
Arpit Singha11fc942023-08-09 09:23:43 +00001729TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001730 NotifyMotionArgs args;
1731 const Point centerPoint = mDevice->getCenterPoint();
1732
1733 // ACTION_DOWN
1734 mDevice->sendSlot(FIRST_SLOT);
1735 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1736 mDevice->sendDown(centerPoint);
1737 mDevice->sendSync();
1738 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1739
1740 // ACTION_POINTER_DOWN (Second slot)
1741 const Point secondPoint = centerPoint + Point(100, 100);
1742 mDevice->sendSlot(SECOND_SLOT);
1743 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1744 mDevice->sendDown(secondPoint);
1745 mDevice->sendSync();
1746 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1747
1748 // ACTION_MOVE (First slot)
1749 mDevice->sendSlot(FIRST_SLOT);
1750 mDevice->sendMove(centerPoint + Point(5, 5));
1751 // ACTION_POINTER_UP (Second slot)
1752 mDevice->sendSlot(SECOND_SLOT);
1753 mDevice->sendPointerUp();
1754 // Send a single sync for the above 2 pointer updates
1755 mDevice->sendSync();
1756
1757 // First, we should get POINTER_UP for the second pointer
1758 assertReceivedMotion(ACTION_POINTER_1_UP,
1759 {/*first pointer */ centerPoint + Point(5, 5),
1760 /*second pointer*/ secondPoint});
1761
1762 // Next, the MOVE event for the first pointer
1763 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1764}
1765
1766/**
1767 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1768 * move, and then it will go up, all in the same frame.
1769 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1770 * gets sent to the listener.
1771 */
Arpit Singha11fc942023-08-09 09:23:43 +00001772TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001773 NotifyMotionArgs args;
1774 const Point centerPoint = mDevice->getCenterPoint();
1775
1776 // ACTION_DOWN
1777 mDevice->sendSlot(FIRST_SLOT);
1778 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1779 mDevice->sendDown(centerPoint);
1780 mDevice->sendSync();
1781 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1782
1783 // ACTION_POINTER_DOWN (Second slot)
1784 const Point secondPoint = centerPoint + Point(100, 100);
1785 mDevice->sendSlot(SECOND_SLOT);
1786 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1787 mDevice->sendDown(secondPoint);
1788 mDevice->sendSync();
1789 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1790
1791 // ACTION_MOVE (First slot)
1792 mDevice->sendSlot(FIRST_SLOT);
1793 mDevice->sendMove(centerPoint + Point(5, 5));
1794 // ACTION_POINTER_UP (Second slot)
1795 mDevice->sendSlot(SECOND_SLOT);
1796 mDevice->sendMove(secondPoint + Point(6, 6));
1797 mDevice->sendPointerUp();
1798 // Send a single sync for the above 2 pointer updates
1799 mDevice->sendSync();
1800
1801 // First, we should get POINTER_UP for the second pointer
1802 // The movement of the second pointer during the liftoff frame is ignored.
1803 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1804 assertReceivedMotion(ACTION_POINTER_1_UP,
1805 {/*first pointer */ centerPoint + Point(5, 5),
1806 /*second pointer*/ secondPoint});
1807
1808 // Next, the MOVE event for the first pointer
1809 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1810}
1811
Arpit Singha11fc942023-08-09 09:23:43 +00001812TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001813 NotifyMotionArgs args;
1814 const Point centerPoint = mDevice->getCenterPoint();
1815
1816 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001817 mDevice->sendSlot(FIRST_SLOT);
1818 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001819 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001820 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001821 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1822 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1823
arthurhungcc7f9802020-04-30 17:55:40 +08001824 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001825 const Point secondPoint = centerPoint + Point(100, 100);
1826 mDevice->sendSlot(SECOND_SLOT);
1827 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1828 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001829 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001830 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001831 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001832
arthurhungcc7f9802020-04-30 17:55:40 +08001833 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001834 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001835 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001836 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1838
arthurhungcc7f9802020-04-30 17:55:40 +08001839 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1840 // a palm event.
1841 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001842 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001843 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001844 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001845 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001846 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001847
arthurhungcc7f9802020-04-30 17:55:40 +08001848 // Send up to second slot, expect first slot send moving.
1849 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001850 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001851 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001853
arthurhungcc7f9802020-04-30 17:55:40 +08001854 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001855 mDevice->sendSlot(FIRST_SLOT);
1856 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001857 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001858
arthurhungcc7f9802020-04-30 17:55:40 +08001859 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1860 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001861}
1862
Arpit Singha11fc942023-08-09 09:23:43 +00001863TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001864 const Point centerPoint = mDevice->getCenterPoint();
1865
1866 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1867 mDevice->sendSlot(FIRST_SLOT);
1868 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1869 mDevice->sendToolType(MT_TOOL_PEN);
1870 mDevice->sendDown(centerPoint);
1871 mDevice->sendSync();
1872 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1873 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001874 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001875
1876 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1877
1878 // Release the stylus touch.
1879 mDevice->sendUp();
1880 mDevice->sendSync();
1881 ASSERT_NO_FATAL_FAILURE(
1882 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1883
1884 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1885
1886 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1887 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1888 mDevice->sendToolType(MT_TOOL_FINGER);
1889 mDevice->sendDown(centerPoint);
1890 mDevice->sendSync();
1891 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1892 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001893 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001894
1895 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1896
1897 mDevice->sendUp();
1898 mDevice->sendSync();
1899 ASSERT_NO_FATAL_FAILURE(
1900 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1901
1902 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1903 // The policy should be notified of the stylus presence.
1904 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1905 mDevice->sendToolType(MT_TOOL_PEN);
1906 mDevice->sendMove(centerPoint);
1907 mDevice->sendSync();
1908 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1909 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001910 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001911
1912 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1913}
1914
Arpit Singha11fc942023-08-09 09:23:43 +00001915INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
1916 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
1917 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
1918 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
1919
Prabir Pradhan124ea442022-10-28 20:27:44 +00001920// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001921
Prabir Pradhan124ea442022-10-28 20:27:44 +00001922// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1923// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1924// stylus.
1925template <typename UinputStylusDevice>
Arpit Singha11fc942023-08-09 09:23:43 +00001926class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001927protected:
1928 void SetUp() override {
1929#if !defined(__ANDROID__)
1930 GTEST_SKIP();
1931#endif
Arpit Singha11fc942023-08-09 09:23:43 +00001932 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00001933 mTouchscreen = mDevice.get();
1934 mTouchscreenInfo = mDeviceInfo;
1935
1936 setUpStylusDevice();
1937 }
1938
1939 UinputStylusDevice* mStylus{nullptr};
1940 InputDeviceInfo mStylusInfo{};
1941
1942 UinputTouchScreen* mTouchscreen{nullptr};
1943 InputDeviceInfo mTouchscreenInfo{};
1944
1945private:
1946 // When we are attempting to test stylus button events that are sent from the touchscreen,
1947 // use the same Uinput device for the touchscreen and the stylus.
1948 template <typename T = UinputStylusDevice>
1949 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1950 mStylus = mDevice.get();
1951 mStylusInfo = mDeviceInfo;
1952 }
1953
1954 // When we are attempting to stylus buttons from an external stylus being merged with touches
1955 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1956 template <typename T = UinputStylusDevice>
1957 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1958 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1959 mStylus = mStylusDeviceLifecycleTracker.get();
1960 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1961 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1962 const auto info = findDeviceByName(mStylus->getName());
1963 ASSERT_TRUE(info);
1964 mStylusInfo = *info;
1965 }
1966
1967 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1968
1969 // Hide the base class's device to expose it with a different name for readability.
Arpit Singha11fc942023-08-09 09:23:43 +00001970 using BaseTouchIntegrationTest::mDevice;
1971 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00001972};
1973
1974using StylusButtonIntegrationTestTypes =
1975 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1976TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1977
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001978TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001979 const auto stylusId = TestFixture::mStylusInfo.getId();
1980
1981 TestFixture::mStylus->pressKey(BTN_STYLUS);
1982 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1983 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1984 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1985
1986 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1987 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001988 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001989 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001990}
1991
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001992TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001993 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1994 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1995 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001996
1997 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001998 TestFixture::mStylus->pressKey(BTN_STYLUS);
1999 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002000 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002001 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002002
2003 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002004 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2005 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2006 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2007 TestFixture::mTouchscreen->sendDown(centerPoint);
2008 TestFixture::mTouchscreen->sendSync();
2009 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002010 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002011 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002012 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2013 WithDeviceId(touchscreenId))));
2014 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002015 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002016 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002017 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2018 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002019
Prabir Pradhan124ea442022-10-28 20:27:44 +00002020 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2021 TestFixture::mTouchscreen->sendSync();
2022 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002023 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002024 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002025 WithDeviceId(touchscreenId))));
2026 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002027 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002028 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002029 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002030
2031 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002032 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2033 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002034 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002035 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002036}
2037
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002038TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002039 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2040 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2041 const auto stylusId = TestFixture::mStylusInfo.getId();
2042 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002043 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002044
2045 // Press the stylus button.
2046 TestFixture::mStylus->pressKey(BTN_STYLUS);
2047 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2048 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2049 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2050
2051 // Start hovering with the stylus.
2052 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2053 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2054 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2055 TestFixture::mTouchscreen->sendMove(centerPoint);
2056 TestFixture::mTouchscreen->sendSync();
2057 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2058 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2059 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2060 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2061 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2062 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2063 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2064 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2065 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2066
2067 // Touch down with the stylus.
2068 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2069 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2070 TestFixture::mTouchscreen->sendDown(centerPoint);
2071 TestFixture::mTouchscreen->sendSync();
2072 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2073 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2074 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2075
2076 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2077 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2078 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2079
2080 // Stop touching with the stylus, and start hovering.
2081 TestFixture::mTouchscreen->sendUp();
2082 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2083 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2084 TestFixture::mTouchscreen->sendMove(centerPoint);
2085 TestFixture::mTouchscreen->sendSync();
2086 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2087 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2088 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2089 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2090 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2091 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2092 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2093 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2094 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2095
2096 // Stop hovering.
2097 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2098 TestFixture::mTouchscreen->sendSync();
2099 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2100 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2101 WithButtonState(0))));
2102 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2103 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2104 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2105 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2106
2107 // Release the stylus button.
2108 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2109 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2110 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2111 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2112}
2113
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002114TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002115 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2116 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2117 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002118
2119 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002120 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2121 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2122 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2123 TestFixture::mTouchscreen->sendDown(centerPoint);
2124 TestFixture::mTouchscreen->sendSync();
2125 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002126 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002127 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002128 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002129
2130 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002131 TestFixture::mStylus->pressKey(BTN_STYLUS);
2132 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002133 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002134 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2135 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002136 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002137 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002138 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2139 WithDeviceId(touchscreenId))));
2140 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002141 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002142 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002143 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2144 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002145
Prabir Pradhan124ea442022-10-28 20:27:44 +00002146 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2147 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002148 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002149 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2150 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002151 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002152 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002153 WithDeviceId(touchscreenId))));
2154 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002155 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002156 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002157 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002158
2159 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002160 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2161 TestFixture::mTouchscreen->sendSync();
2162 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002163 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002164 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002165 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002166}
2167
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002168TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002169 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2170 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002171 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002172
2173 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2174 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2175 const auto stylusId = TestFixture::mStylusInfo.getId();
2176
2177 // Start a stylus gesture. By the time this event is processed, the configuration change that
2178 // was requested is guaranteed to be completed.
2179 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2180 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2181 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2182 TestFixture::mTouchscreen->sendDown(centerPoint);
2183 TestFixture::mTouchscreen->sendSync();
2184 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2185 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002186 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002187 WithDeviceId(touchscreenId))));
2188
2189 // Press and release a stylus button. Each change only generates a MOVE motion event.
2190 // Key events are unaffected.
2191 TestFixture::mStylus->pressKey(BTN_STYLUS);
2192 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2193 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2194 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2195 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2196 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002197 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002198 WithDeviceId(touchscreenId))));
2199
2200 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2201 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2202 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2203 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2204 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2205 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002206 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002207 WithDeviceId(touchscreenId))));
2208
2209 // Finish the stylus gesture.
2210 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2211 TestFixture::mTouchscreen->sendSync();
2212 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2213 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002214 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002215 WithDeviceId(touchscreenId))));
2216}
2217
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002218// --- ExternalStylusIntegrationTest ---
2219
2220// Verify the behavior of an external stylus. An external stylus can report pressure or button
2221// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2222// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singha11fc942023-08-09 09:23:43 +00002223using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002224
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002225TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002226 const Point centerPoint = mDevice->getCenterPoint();
2227
2228 // Create an external stylus capable of reporting pressure data that
2229 // should be fused with a touch pointer.
2230 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2231 createUinputDevice<UinputExternalStylusWithPressure>();
2232 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2233 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2234 const auto stylusInfo = findDeviceByName(stylus->getName());
2235 ASSERT_TRUE(stylusInfo);
2236
2237 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2238
2239 const auto touchscreenId = mDeviceInfo.getId();
2240
2241 // Set a pressure value on the stylus. It doesn't generate any events.
2242 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2243 stylus->setPressure(100);
2244 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2245
2246 // Start a finger gesture, and ensure it shows up as stylus gesture
2247 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002248 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002249 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002250 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002251 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002252 mDevice->sendSync();
2253 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2254 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002255 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002256 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002257
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002258 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2259 // event with the updated pressure.
2260 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002261 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2262 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002263 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002264 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002265
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002266 // The external stylus did not generate any events.
2267 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2268 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2269}
2270
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002271TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002272 const Point centerPoint = mDevice->getCenterPoint();
2273
2274 // Create an external stylus capable of reporting pressure data that
2275 // should be fused with a touch pointer.
2276 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2277 createUinputDevice<UinputExternalStylusWithPressure>();
2278 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2279 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2280 const auto stylusInfo = findDeviceByName(stylus->getName());
2281 ASSERT_TRUE(stylusInfo);
2282
2283 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2284
2285 const auto touchscreenId = mDeviceInfo.getId();
2286
2287 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2288 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002289 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2290 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002291 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002292 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002293
2294 // Start a finger gesture. The touch device will withhold generating any touches for
2295 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2296 mDevice->sendSlot(FIRST_SLOT);
2297 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2298 mDevice->sendToolType(MT_TOOL_FINGER);
2299 mDevice->sendDown(centerPoint);
2300 auto waitUntil = std::chrono::system_clock::now() +
2301 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002302 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002303 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002304
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002305 // Since the external stylus did not report a pressure value within the timeout,
2306 // it shows up as a finger pointer.
2307 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2308 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002309 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002310 WithPressure(1.f))));
2311
2312 // Change the pressure on the external stylus. Since the pressure was not present at the start
2313 // of the gesture, it is ignored for now.
2314 stylus->setPressure(200);
2315 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2316
2317 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002318 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2319 mDevice->sendSync();
2320 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2321 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002322 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002323
2324 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2325 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2326 mDevice->sendToolType(MT_TOOL_FINGER);
2327 mDevice->sendDown(centerPoint);
2328 mDevice->sendSync();
2329 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2330 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002331 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002332 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2333
2334 // The external stylus did not generate any events.
2335 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2336 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002337}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002338
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002339TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002340 const Point centerPoint = mDevice->getCenterPoint();
2341
2342 // Create an external stylus device that does not support pressure. It should not affect any
2343 // touch pointers.
2344 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2345 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2346 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2347 const auto stylusInfo = findDeviceByName(stylus->getName());
2348 ASSERT_TRUE(stylusInfo);
2349
2350 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2351
2352 const auto touchscreenId = mDeviceInfo.getId();
2353
2354 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2355 // pressure data from the external stylus.
2356 mDevice->sendSlot(FIRST_SLOT);
2357 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2358 mDevice->sendToolType(MT_TOOL_FINGER);
2359 mDevice->sendDown(centerPoint);
2360 auto waitUntil = std::chrono::system_clock::now() +
2361 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2362 mDevice->sendSync();
2363 ASSERT_NO_FATAL_FAILURE(
2364 mTestListener
2365 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2366 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002367 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002368 WithButtonState(0),
2369 WithDeviceId(touchscreenId),
2370 WithPressure(1.f)),
2371 waitUntil));
2372
2373 // The external stylus did not generate any events.
2374 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2375 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2376}
2377
Michael Wrightd02c5b62014-02-10 15:10:22 -08002378// --- InputDeviceTest ---
2379class InputDeviceTest : public testing::Test {
2380protected:
2381 static const char* DEVICE_NAME;
2382 static const char* DEVICE_LOCATION;
2383 static const int32_t DEVICE_ID;
2384 static const int32_t DEVICE_GENERATION;
2385 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002386 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002387 static const int32_t EVENTHUB_ID;
2388 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2389
2390 std::shared_ptr<FakeEventHub> mFakeEventHub;
2391 sp<FakeInputReaderPolicy> mFakePolicy;
2392 std::unique_ptr<TestInputListener> mFakeListener;
2393 std::unique_ptr<InstrumentedInputReader> mReader;
2394 std::shared_ptr<InputDevice> mDevice;
2395
2396 void SetUp() override {
2397 mFakeEventHub = std::make_unique<FakeEventHub>();
2398 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2399 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002400 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002401 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002402 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002403 identifier.name = DEVICE_NAME;
2404 identifier.location = DEVICE_LOCATION;
2405 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2406 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2407 identifier);
2408 mReader->pushNextDevice(mDevice);
2409 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002410 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002411 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002412
2413 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002414 mFakeListener.reset();
2415 mFakePolicy.clear();
2416 }
2417};
2418
2419const char* InputDeviceTest::DEVICE_NAME = "device";
2420const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2421const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2422const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002423const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002424const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2425 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002426const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002427const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2428
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002429TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002431 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2432 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002433}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002434
Michael Wrightd02c5b62014-02-10 15:10:22 -08002435TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2436 ASSERT_EQ(mDevice->isEnabled(), false);
2437}
2438
2439TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2440 // Configuration.
2441 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002442 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002443
2444 // Reset.
2445 unused += mDevice->reset(ARBITRARY_TIME);
2446
2447 NotifyDeviceResetArgs resetArgs;
2448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2449 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2450 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2451
2452 // Metadata.
2453 ASSERT_TRUE(mDevice->isIgnored());
2454 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2455
2456 InputDeviceInfo info = mDevice->getDeviceInfo();
2457 ASSERT_EQ(DEVICE_ID, info.getId());
2458 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2459 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2460 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2461
2462 // State queries.
2463 ASSERT_EQ(0, mDevice->getMetaState());
2464
2465 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2466 << "Ignored device should return unknown key code state.";
2467 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2468 << "Ignored device should return unknown scan code state.";
2469 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2470 << "Ignored device should return unknown switch state.";
2471
2472 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2473 uint8_t flags[2] = { 0, 1 };
2474 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2475 << "Ignored device should never mark any key codes.";
2476 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2477 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2478}
2479
2480TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2481 // Configuration.
2482 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2483
2484 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002485 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2486 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002487 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2488 mapper1.setMetaState(AMETA_ALT_ON);
2489 mapper1.addSupportedKeyCode(AKEYCODE_A);
2490 mapper1.addSupportedKeyCode(AKEYCODE_B);
2491 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2492 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2493 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2494 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2495 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2496
2497 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002498 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2499 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002500 mapper2.setMetaState(AMETA_SHIFT_ON);
2501
2502 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002503 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002504
Harry Cuttsf13161a2023-03-08 14:15:49 +00002505 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2506 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002507 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002508 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002509
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002510 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2511 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002512
2513 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002514 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002515 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2516 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002517
2518 NotifyDeviceResetArgs resetArgs;
2519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2520 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2521 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2522
2523 // Metadata.
2524 ASSERT_FALSE(mDevice->isIgnored());
2525 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2526
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002527 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002528 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002529 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002530 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2531 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2532
2533 // State queries.
2534 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2535 << "Should query mappers and combine meta states.";
2536
2537 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2538 << "Should return unknown key code state when source not supported.";
2539 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2540 << "Should return unknown scan code state when source not supported.";
2541 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2542 << "Should return unknown switch state when source not supported.";
2543
2544 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2545 << "Should query mapper when source is supported.";
2546 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2547 << "Should query mapper when source is supported.";
2548 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2549 << "Should query mapper when source is supported.";
2550
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002551 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002552 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002553 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002554 << "Should do nothing when source is unsupported.";
2555 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2556 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2557 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2558 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2559
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002560 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002561 << "Should query mapper when source is supported.";
2562 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2563 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2564 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2565 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2566
2567 // Event handling.
2568 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002569 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002570 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002571
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002572 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2573 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002574}
2575
Arthur Hung2c9a3342019-07-23 14:18:59 +08002576// A single input device is associated with a specific display. Check that:
2577// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh3e56f7e2023-07-07 13:12:37 +00002578// 2. Device is disabled when setEnabled API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002579TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002580 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2581 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002582
2583 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002584 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002585 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2586 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002587
2588 // Device should be enabled by default.
2589 ASSERT_TRUE(mDevice->isEnabled());
2590
2591 // Prepare associated info.
2592 constexpr uint8_t hdmi = 1;
2593 const std::string UNIQUE_ID = "local:1";
2594
2595 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002596 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002597 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002598 // Device should be disabled because it is associated with a specific display via
2599 // input port <-> display port association, but the corresponding display is not found
2600 ASSERT_FALSE(mDevice->isEnabled());
2601
2602 // Prepare displays.
2603 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002604 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002605 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002606 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002607 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002608 ASSERT_TRUE(mDevice->isEnabled());
2609
2610 // Device should be disabled after set disable.
2611 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002612 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002613 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002614 ASSERT_FALSE(mDevice->isEnabled());
2615
2616 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002617 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002618 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002619 ASSERT_FALSE(mDevice->isEnabled());
2620}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002621
Christine Franks1ba71cc2021-04-07 14:37:42 -07002622TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2623 // Device should be enabled by default.
2624 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002625 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2626 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002627 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002628 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2629 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002630 ASSERT_TRUE(mDevice->isEnabled());
2631
2632 // Device should be disabled because it is associated with a specific display, but the
2633 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002634 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002635 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002636 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002637 ASSERT_FALSE(mDevice->isEnabled());
2638
2639 // Device should be enabled when a display is found.
2640 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002641 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002642 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002643 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002644 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002645 ASSERT_TRUE(mDevice->isEnabled());
2646
2647 // Device should be disabled after set disable.
2648 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002649 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002650 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002651 ASSERT_FALSE(mDevice->isEnabled());
2652
2653 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002654 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002655 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002656 ASSERT_FALSE(mDevice->isEnabled());
2657}
2658
Christine Franks2a2293c2022-01-18 11:51:16 -08002659TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2660 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002661 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2662 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002663 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002664 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2665 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002666
Christine Franks2a2293c2022-01-18 11:51:16 -08002667 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2668 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002669 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002670 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002671 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002672 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002673 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2674}
2675
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002676/**
2677 * This test reproduces a crash caused by a dangling reference that remains after device is added
2678 * and removed. The reference is accessed in InputDevice::dump(..);
2679 */
2680TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2681 constexpr int32_t TEST_EVENTHUB_ID = 10;
2682 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2683
Harry Cutts33476232023-01-30 19:57:29 +00002684 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh7f1765e2023-07-07 13:12:37 +00002685 device.addEventHubDevice(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002686 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2687 std::string dumpStr, eventHubDevStr;
2688 device.dump(dumpStr, eventHubDevStr);
2689}
2690
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002691TEST_F(InputDeviceTest, GetBluetoothAddress) {
2692 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2693 ASSERT_TRUE(address);
2694 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2695}
2696
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697// --- SwitchInputMapperTest ---
2698
2699class SwitchInputMapperTest : public InputMapperTest {
2700protected:
2701};
2702
2703TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002704 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002706 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002707}
2708
2709TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002710 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002712 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002713 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002714
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002715 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002716 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002717}
2718
2719TEST_F(SwitchInputMapperTest, Process) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002720 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002721 std::list<NotifyArgs> out;
2722 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2723 ASSERT_TRUE(out.empty());
2724 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2725 ASSERT_TRUE(out.empty());
2726 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2727 ASSERT_TRUE(out.empty());
2728 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002730 ASSERT_EQ(1u, out.size());
2731 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002732 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002733 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2734 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002735 args.switchMask);
2736 ASSERT_EQ(uint32_t(0), args.policyFlags);
2737}
2738
Chris Ye87143712020-11-10 05:05:58 +00002739// --- VibratorInputMapperTest ---
2740class VibratorInputMapperTest : public InputMapperTest {
2741protected:
2742 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2743};
2744
2745TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singhc68f85b2023-04-26 16:23:13 +00002746 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002747
2748 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2749}
2750
2751TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singhc68f85b2023-04-26 16:23:13 +00002752 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002753
2754 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2755}
2756
2757TEST_F(VibratorInputMapperTest, Vibrate) {
2758 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002759 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singhc68f85b2023-04-26 16:23:13 +00002760 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002761
2762 VibrationElement pattern(2);
2763 VibrationSequence sequence(2);
2764 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002765 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2766 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002767 sequence.addElement(pattern);
2768 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002769 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2770 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002771 sequence.addElement(pattern);
2772
2773 std::vector<int64_t> timings = {0, 1};
2774 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2775
2776 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002777 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002778 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002779 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002780 // Verify vibrator state listener was notified.
2781 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002782 ASSERT_EQ(1u, out.size());
2783 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2784 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2785 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002786 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002787 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002788 ASSERT_FALSE(mapper.isVibrating());
2789 // Verify vibrator state listener was notified.
2790 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002791 ASSERT_EQ(1u, out.size());
2792 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2793 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2794 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002795}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002796
Chris Yef59a2f42020-10-16 12:55:26 -07002797// --- SensorInputMapperTest ---
2798
2799class SensorInputMapperTest : public InputMapperTest {
2800protected:
2801 static const int32_t ACCEL_RAW_MIN;
2802 static const int32_t ACCEL_RAW_MAX;
2803 static const int32_t ACCEL_RAW_FUZZ;
2804 static const int32_t ACCEL_RAW_FLAT;
2805 static const int32_t ACCEL_RAW_RESOLUTION;
2806
2807 static const int32_t GYRO_RAW_MIN;
2808 static const int32_t GYRO_RAW_MAX;
2809 static const int32_t GYRO_RAW_FUZZ;
2810 static const int32_t GYRO_RAW_FLAT;
2811 static const int32_t GYRO_RAW_RESOLUTION;
2812
2813 static const float GRAVITY_MS2_UNIT;
2814 static const float DEGREE_RADIAN_UNIT;
2815
2816 void prepareAccelAxes();
2817 void prepareGyroAxes();
2818 void setAccelProperties();
2819 void setGyroProperties();
2820 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2821};
2822
2823const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2824const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2825const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2826const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2827const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2828
2829const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2830const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2831const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2832const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2833const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2834
2835const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2836const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2837
2838void SensorInputMapperTest::prepareAccelAxes() {
2839 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2840 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2841 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2842 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2843 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2844 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2845}
2846
2847void SensorInputMapperTest::prepareGyroAxes() {
2848 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2849 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2850 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2851 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2852 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2853 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2854}
2855
2856void SensorInputMapperTest::setAccelProperties() {
2857 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2858 /* sensorDataIndex */ 0);
2859 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2860 /* sensorDataIndex */ 1);
2861 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2862 /* sensorDataIndex */ 2);
2863 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2864 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2865 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2866 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2867 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2868}
2869
2870void SensorInputMapperTest::setGyroProperties() {
2871 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2872 /* sensorDataIndex */ 0);
2873 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2874 /* sensorDataIndex */ 1);
2875 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2876 /* sensorDataIndex */ 2);
2877 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2878 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2879 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2880 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2881 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2882}
2883
2884TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singh61dd33e2023-04-26 15:07:55 +00002885 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002886
2887 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2888}
2889
2890TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2891 setAccelProperties();
2892 prepareAccelAxes();
Arpit Singh61dd33e2023-04-26 15:07:55 +00002893 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002894
2895 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2896 std::chrono::microseconds(10000),
2897 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002898 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2901 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2902 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2903 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002904
2905 NotifySensorArgs args;
2906 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2907 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2908 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2909
2910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2911 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2912 ASSERT_EQ(args.deviceId, DEVICE_ID);
2913 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2914 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2915 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2916 ASSERT_EQ(args.values, values);
2917 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2918}
2919
2920TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2921 setGyroProperties();
2922 prepareGyroAxes();
Arpit Singh61dd33e2023-04-26 15:07:55 +00002923 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002924
2925 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2926 std::chrono::microseconds(10000),
2927 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002928 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002929 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2932 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002934
2935 NotifySensorArgs args;
2936 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2937 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2938 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2939
2940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2941 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2942 ASSERT_EQ(args.deviceId, DEVICE_ID);
2943 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2944 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2945 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2946 ASSERT_EQ(args.values, values);
2947 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2948}
2949
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950// --- KeyboardInputMapperTest ---
2951
2952class KeyboardInputMapperTest : public InputMapperTest {
2953protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002954 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002955 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002956 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002957
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002958 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002959 int32_t originalKeyCode, int32_t rotatedKeyCode,
2960 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002961};
2962
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002963/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2964 * orientation.
2965 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002966void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002967 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2968 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002969}
2970
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002971void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002972 int32_t originalScanCode, int32_t originalKeyCode,
2973 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974 NotifyKeyArgs args;
2975
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2978 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2979 ASSERT_EQ(originalScanCode, args.scanCode);
2980 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002981 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002982
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2985 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2986 ASSERT_EQ(originalScanCode, args.scanCode);
2987 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002988 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002989}
2990
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002992 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00002993 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002994 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002995
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002996 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002997}
2998
2999TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3000 const int32_t USAGE_A = 0x070004;
3001 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003002 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3003 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003004 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3005 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3006 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003007
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003008 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003009 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003010 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003011 // Initial metastate is AMETA_NONE.
3012 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003013
3014 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003016 NotifyKeyArgs args;
3017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3018 ASSERT_EQ(DEVICE_ID, args.deviceId);
3019 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3020 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3021 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3022 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3023 ASSERT_EQ(KEY_HOME, args.scanCode);
3024 ASSERT_EQ(AMETA_NONE, args.metaState);
3025 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3026 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3027 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3028
3029 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003030 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3032 ASSERT_EQ(DEVICE_ID, args.deviceId);
3033 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3034 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3035 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3036 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3037 ASSERT_EQ(KEY_HOME, args.scanCode);
3038 ASSERT_EQ(AMETA_NONE, args.metaState);
3039 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3040 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3041 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3042
3043 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3047 ASSERT_EQ(DEVICE_ID, args.deviceId);
3048 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3049 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3050 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3051 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3052 ASSERT_EQ(0, args.scanCode);
3053 ASSERT_EQ(AMETA_NONE, args.metaState);
3054 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3055 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3056 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3057
3058 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003059 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3060 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3062 ASSERT_EQ(DEVICE_ID, args.deviceId);
3063 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3064 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3065 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3066 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3067 ASSERT_EQ(0, args.scanCode);
3068 ASSERT_EQ(AMETA_NONE, args.metaState);
3069 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3070 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3071 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3072
3073 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3077 ASSERT_EQ(DEVICE_ID, args.deviceId);
3078 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3079 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3080 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3081 ASSERT_EQ(0, args.keyCode);
3082 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3083 ASSERT_EQ(AMETA_NONE, args.metaState);
3084 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3085 ASSERT_EQ(0U, args.policyFlags);
3086 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3087
3088 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003089 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3090 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3092 ASSERT_EQ(DEVICE_ID, args.deviceId);
3093 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3094 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3095 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3096 ASSERT_EQ(0, args.keyCode);
3097 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3098 ASSERT_EQ(AMETA_NONE, args.metaState);
3099 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3100 ASSERT_EQ(0U, args.policyFlags);
3101 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3102}
3103
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003104TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3105 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3106 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3107 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3108
3109 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003110 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003111 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3112
3113 // Key down by scan code.
3114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3115 NotifyKeyArgs args;
3116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3117 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3118
3119 // Key up by scan code.
3120 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3122 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3123}
3124
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003125/**
3126 * Ensure that the readTime is set to the time when the EV_KEY is received.
3127 */
3128TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3129 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3130
3131 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003132 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003133 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3134 NotifyKeyArgs args;
3135
3136 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003137 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3139 ASSERT_EQ(12, args.readTime);
3140
3141 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003142 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3144 ASSERT_EQ(15, args.readTime);
3145}
3146
Michael Wrightd02c5b62014-02-10 15:10:22 -08003147TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003148 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3149 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003150 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3151 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3152 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003154 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003155 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003156 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157
Arthur Hung95f68612022-04-07 14:08:22 +08003158 // Initial metastate is AMETA_NONE.
3159 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160
3161 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003163 NotifyKeyArgs args;
3164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3165 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003166 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003167 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168
3169 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003170 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3172 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003173 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003174
3175 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003176 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3178 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003179 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003180
3181 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003182 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3184 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003185 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003186 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003187}
3188
3189TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003190 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3191 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3192 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3193 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003195 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003196 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003197 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198
Michael Wrighta9cf4192022-12-01 23:46:39 +00003199 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003200 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3201 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3202 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3203 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3204 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3205 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3206 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3207 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3208}
3209
3210TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003211 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3212 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3213 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3214 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003215
Michael Wrightd02c5b62014-02-10 15:10:22 -08003216 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003217 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003218 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003219 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003220
Michael Wrighta9cf4192022-12-01 23:46:39 +00003221 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003222 ASSERT_NO_FATAL_FAILURE(
3223 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3224 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3225 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3226 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3227 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3228 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3229 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003230
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003231 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003232 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003233 ASSERT_NO_FATAL_FAILURE(
3234 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3235 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3236 AKEYCODE_DPAD_UP, DISPLAY_ID));
3237 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3238 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3239 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3240 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003242 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003243 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003244 ASSERT_NO_FATAL_FAILURE(
3245 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3246 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3247 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3248 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3249 AKEYCODE_DPAD_UP, DISPLAY_ID));
3250 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3251 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003253 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003254 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003255 ASSERT_NO_FATAL_FAILURE(
3256 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3257 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3258 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3259 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3260 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3261 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3262 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003263
3264 // Special case: if orientation changes while key is down, we still emit the same keycode
3265 // in the key up as we did in the key down.
3266 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003267 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003268 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3271 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3272 ASSERT_EQ(KEY_UP, args.scanCode);
3273 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3274
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003275 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003276 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3279 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3280 ASSERT_EQ(KEY_UP, args.scanCode);
3281 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3282}
3283
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003284TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3285 // If the keyboard is not orientation aware,
3286 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003287 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003288
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003289 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003290 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003291 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003292 NotifyKeyArgs args;
3293
3294 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3299 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3300
Michael Wrighta9cf4192022-12-01 23:46:39 +00003301 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3306 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3307}
3308
3309TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3310 // If the keyboard is orientation aware,
3311 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003312 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003313
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003314 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003315 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003316 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003317 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003318 NotifyKeyArgs args;
3319
3320 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3321 // ^--- already checked by the previous test
3322
Michael Wrighta9cf4192022-12-01 23:46:39 +00003323 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003324 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3329 ASSERT_EQ(DISPLAY_ID, args.displayId);
3330
3331 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003332 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003333 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003334 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003337 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3339 ASSERT_EQ(newDisplayId, args.displayId);
3340}
3341
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003343 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003344 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003345 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003347 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003348 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003350 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003351 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352}
3353
Philip Junker4af3b3d2021-12-14 10:36:55 +01003354TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3355 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003356 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003357 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3358
3359 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3360 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3361 << "If a mapping is available, the result is equal to the mapping";
3362
3363 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3364 << "If no mapping is available, the result is the key location";
3365}
3366
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003368 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003369 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003370 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003372 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003373 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003374
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003375 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003376 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377}
3378
3379TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003380 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003381 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003382 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003384 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385
Michael Wrightd02c5b62014-02-10 15:10:22 -08003386 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003387 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388 ASSERT_TRUE(flags[0]);
3389 ASSERT_FALSE(flags[1]);
3390}
3391
3392TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003393 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3394 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3395 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3396 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3397 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3398 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003399
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003400 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003401 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003402 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003403 // Initial metastate is AMETA_NONE.
3404 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405
3406 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003407 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3408 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3409 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410
3411 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003414 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3415 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3416 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003417 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418
3419 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003422 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3423 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3424 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003425 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003426
3427 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003430 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3431 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3432 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003433 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003434
3435 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003438 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3439 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3440 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003441 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003442
3443 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003446 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3447 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3448 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003449 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003450
3451 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003454 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3455 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3456 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003457 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003458}
3459
Chris Yea52ade12020-08-27 16:49:20 -07003460TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3461 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3462 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3463 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3464 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3465
3466 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003467 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003468 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3469
Chris Yea52ade12020-08-27 16:49:20 -07003470 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003471 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003472 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3473 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3474 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3475 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3476
3477 NotifyKeyArgs args;
3478 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3481 ASSERT_EQ(AMETA_NONE, args.metaState);
3482 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3483 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3484 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3485
3486 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003487 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3489 ASSERT_EQ(AMETA_NONE, args.metaState);
3490 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3491 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3492 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3493}
3494
Arthur Hung2c9a3342019-07-23 14:18:59 +08003495TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3496 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003497 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3498 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3499 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3500 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003501
3502 // keyboard 2.
3503 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003504 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003505 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003506 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003507 std::shared_ptr<InputDevice> device2 =
3508 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003509 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003510
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003511 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3512 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3513 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3514 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003515
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003516 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003517 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003518 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003519
Arpit Singh033e3ec2023-04-26 14:43:16 +00003520 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003521 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003522 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3523 mFakePolicy
3524 ->getReaderConfiguration(),
3525 AINPUT_SOURCE_KEYBOARD,
3526 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003527 std::list<NotifyArgs> unused =
3528 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003529 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003530 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003531
3532 // Prepared displays and associated info.
3533 constexpr uint8_t hdmi1 = 0;
3534 constexpr uint8_t hdmi2 = 1;
3535 const std::string SECONDARY_UNIQUE_ID = "local:1";
3536
3537 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3538 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3539
3540 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003541 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003542 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003543 ASSERT_FALSE(device2->isEnabled());
3544
3545 // Prepare second display.
3546 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003547 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003548 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003549 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003550 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003551 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003552 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003553 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003554
3555 // Device should be enabled after the associated display is found.
3556 ASSERT_TRUE(mDevice->isEnabled());
3557 ASSERT_TRUE(device2->isEnabled());
3558
3559 // Test pad key events
3560 ASSERT_NO_FATAL_FAILURE(
3561 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3562 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3563 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3564 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3565 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3566 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3567 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3568
3569 ASSERT_NO_FATAL_FAILURE(
3570 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3571 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3572 AKEYCODE_DPAD_RIGHT, newDisplayId));
3573 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3574 AKEYCODE_DPAD_DOWN, newDisplayId));
3575 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3576 AKEYCODE_DPAD_LEFT, newDisplayId));
3577}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003578
arthurhungc903df12020-08-11 15:08:42 +08003579TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3580 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3581 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3582 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3583 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3584 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3585 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3586
3587 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003588 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003589 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003590 // Initial metastate is AMETA_NONE.
3591 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003592
3593 // Initialization should have turned all of the lights off.
3594 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3595 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3596 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3597
3598 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003599 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3600 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003601 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3602 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3603
3604 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3606 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003607 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3608 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3609
3610 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003611 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003613 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3614 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3615
3616 mFakeEventHub->removeDevice(EVENTHUB_ID);
3617 mReader->loopOnce();
3618
3619 // keyboard 2 should default toggle keys.
3620 const std::string USB2 = "USB2";
3621 const std::string DEVICE_NAME2 = "KEYBOARD2";
3622 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3623 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3624 std::shared_ptr<InputDevice> device2 =
3625 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003626 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003627 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3628 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3629 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3630 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3631 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3632 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3633
Arpit Singh033e3ec2023-04-26 14:43:16 +00003634 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003635 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003636 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3637 mFakePolicy
3638 ->getReaderConfiguration(),
3639 AINPUT_SOURCE_KEYBOARD,
3640 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003641 std::list<NotifyArgs> unused =
3642 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003643 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003644 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003645
3646 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3647 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3648 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003649 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3650 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003651}
3652
Arthur Hungcb40a002021-08-03 14:31:01 +00003653TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3654 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3655 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3656 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3657
3658 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh033e3ec2023-04-26 14:43:16 +00003659 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003660 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3661 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003662 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003663 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003664 // Initial metastate is AMETA_NONE.
3665 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003666
3667 mReader->toggleCapsLockState(DEVICE_ID);
3668 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3669}
3670
Arthur Hungfb3cc112022-04-13 07:39:50 +00003671TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3672 // keyboard 1.
3673 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3674 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3675 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3676 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3677 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3678 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3679
3680 KeyboardInputMapper& mapper1 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003681 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003682 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3683
3684 // keyboard 2.
3685 const std::string USB2 = "USB2";
3686 const std::string DEVICE_NAME2 = "KEYBOARD2";
3687 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3688 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3689 std::shared_ptr<InputDevice> device2 =
3690 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3691 ftl::Flags<InputDeviceClass>(0));
3692 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3693 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3694 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3695 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3696 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3697 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3698
Arpit Singh033e3ec2023-04-26 14:43:16 +00003699 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003700 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003701 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3702 mFakePolicy
3703 ->getReaderConfiguration(),
3704 AINPUT_SOURCE_KEYBOARD,
3705 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003706 std::list<NotifyArgs> unused =
3707 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003708 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003709 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003710
Arthur Hung95f68612022-04-07 14:08:22 +08003711 // Initial metastate is AMETA_NONE.
3712 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3713 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3714
3715 // Toggle num lock on and off.
3716 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3717 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003718 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3719 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3720 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3721
3722 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3723 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3724 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3725 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3726 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3727
3728 // Toggle caps lock on and off.
3729 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3730 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3731 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3732 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3733 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3734
3735 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3736 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3737 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3738 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3739 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3740
3741 // Toggle scroll lock on and off.
3742 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3743 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3744 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3745 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3746 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3747
3748 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3749 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3750 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3751 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3752 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3753}
3754
Arthur Hung2141d542022-08-23 07:45:21 +00003755TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3756 const int32_t USAGE_A = 0x070004;
3757 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3758 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3759
3760 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003761 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003762 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3763 // Key down by scan code.
3764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3765 NotifyKeyArgs args;
3766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3767 ASSERT_EQ(DEVICE_ID, args.deviceId);
3768 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3769 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3770 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3771 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3772 ASSERT_EQ(KEY_HOME, args.scanCode);
3773 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3774
3775 // Disable device, it should synthesize cancellation events for down events.
3776 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003777 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003778
3779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3780 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3781 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3782 ASSERT_EQ(KEY_HOME, args.scanCode);
3783 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3784}
3785
Zixuan Qufecb6062022-11-12 04:44:31 +00003786TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh033e3ec2023-04-26 14:43:16 +00003787 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3788 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003789 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003790 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3791 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003792
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003793 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003794 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3795
3796 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003797 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003798
3799 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3800 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3801 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3802 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3803 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003804 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3805
3806 // Call change layout association with the same values: Generation shouldn't change
3807 generation = mReader->getContext()->getGeneration();
3808 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3809 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3810 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3811 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003812}
3813
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003814TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3815 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3816 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3817
3818 // Configuration
Arpit Singh033e3ec2023-04-26 14:43:16 +00003819 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003820 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3821 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003822 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003823
3824 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3825 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3826}
3827
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003828// --- KeyboardInputMapperTest_ExternalDevice ---
3829
3830class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3831protected:
Chris Yea52ade12020-08-27 16:49:20 -07003832 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003833};
3834
3835TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003836 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3837 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003838
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003839 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3840 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3841 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3842 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003843
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003844 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003845 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003846 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003847
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003848 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003849 NotifyKeyArgs args;
3850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3851 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3852
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003853 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3855 ASSERT_EQ(uint32_t(0), args.policyFlags);
3856
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003859 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003860
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003861 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3863 ASSERT_EQ(uint32_t(0), args.policyFlags);
3864
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3867 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3868
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003869 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3871 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3872}
3873
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003874TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003875 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003876
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003877 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3878 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3879 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003880
Powei Fengd041c5d2019-05-03 17:11:33 -07003881 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003882 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003883 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003884 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003885
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003887 NotifyKeyArgs args;
3888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3889 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3890
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003891 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3893 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3894
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3897 ASSERT_EQ(uint32_t(0), args.policyFlags);
3898
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003899 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3901 ASSERT_EQ(uint32_t(0), args.policyFlags);
3902
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003903 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3905 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3906
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003907 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3909 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3910}
3911
Michael Wrightd02c5b62014-02-10 15:10:22 -08003912// --- CursorInputMapperTest ---
3913
3914class CursorInputMapperTest : public InputMapperTest {
3915protected:
3916 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3917
Michael Wright17db18e2020-06-26 20:51:44 +01003918 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003919
Chris Yea52ade12020-08-27 16:49:20 -07003920 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921 InputMapperTest::SetUp();
3922
Michael Wright17db18e2020-06-26 20:51:44 +01003923 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003924 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003925 }
3926
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003927 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3928 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003929
Michael Wrighta9cf4192022-12-01 23:46:39 +00003930 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003931 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3932 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3933 }
3934
3935 void prepareSecondaryDisplay() {
3936 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003937 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003938 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003939 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003940
3941 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3942 float pressure) {
3943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3944 0.0f, 0.0f, 0.0f, EPSILON));
3945 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003946};
3947
3948const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3949
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003950void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3951 int32_t originalY, int32_t rotatedX,
3952 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953 NotifyMotionArgs args;
3954
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003960 ASSERT_NO_FATAL_FAILURE(
3961 assertCursorPointerCoords(args.pointerCoords[0],
3962 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3963 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003964}
3965
3966TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003967 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003968 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003970 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003971}
3972
3973TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003974 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003975 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003976
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003977 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003978}
3979
3980TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003981 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003982 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003983
3984 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003985 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003986
3987 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003988 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3989 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003990 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3991 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3992
3993 // When the bounds are set, then there should be a valid motion range.
3994 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3995
3996 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003997 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003998
3999 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4000 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4001 1, 800 - 1, 0.0f, 0.0f));
4002 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4003 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4004 2, 480 - 1, 0.0f, 0.0f));
4005 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4006 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4007 0.0f, 1.0f, 0.0f, 0.0f));
4008}
4009
4010TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004012 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004013
4014 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004015 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016
4017 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4018 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4019 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4020 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4021 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4022 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4023 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4024 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4025 0.0f, 1.0f, 0.0f, 0.0f));
4026}
4027
4028TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004029 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004030 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031
arthurhungdcef2dc2020-08-11 14:47:50 +08004032 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004033
4034 NotifyMotionArgs args;
4035
4036 // Button press.
4037 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 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(ARBITRARY_TIME, args.eventTime);
4042 ASSERT_EQ(DEVICE_ID, args.deviceId);
4043 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4044 ASSERT_EQ(uint32_t(0), args.policyFlags);
4045 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4046 ASSERT_EQ(0, args.flags);
4047 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4048 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4049 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004050 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004052 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004053 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4055 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4056 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4057
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4059 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4060 ASSERT_EQ(DEVICE_ID, args.deviceId);
4061 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4062 ASSERT_EQ(uint32_t(0), args.policyFlags);
4063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4064 ASSERT_EQ(0, args.flags);
4065 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4066 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4067 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004068 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004069 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004070 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004071 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004072 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4073 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4074 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4075
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004077 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4078 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4080 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4081 ASSERT_EQ(DEVICE_ID, args.deviceId);
4082 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4083 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004084 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4085 ASSERT_EQ(0, args.flags);
4086 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4087 ASSERT_EQ(0, args.buttonState);
4088 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004089 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004090 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004091 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004092 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004093 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4094 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4095 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4096
4097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4098 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4099 ASSERT_EQ(DEVICE_ID, args.deviceId);
4100 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4101 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4103 ASSERT_EQ(0, args.flags);
4104 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4105 ASSERT_EQ(0, args.buttonState);
4106 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004107 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004109 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004110 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4112 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4113 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4114}
4115
4116TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004118 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004119
4120 NotifyMotionArgs args;
4121
4122 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004127 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4128 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4129 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130
4131 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004136 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4137 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138}
4139
4140TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004142 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143
4144 NotifyMotionArgs args;
4145
4146 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4150 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004151 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004155 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004156
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004162 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004163
4164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004166 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167}
4168
4169TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004171 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172
4173 NotifyMotionArgs args;
4174
4175 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4181 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004182 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4183 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4184 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4187 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004188 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4189 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4190 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004191
Michael Wrightd02c5b62014-02-10 15:10:22 -08004192 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004198 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4199 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4200 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201
4202 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004206 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004207 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004208
4209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004211 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212}
4213
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004214TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004215 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004217 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4218 // need to be rotated.
4219 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004220 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221
Michael Wrighta9cf4192022-12-01 23:46:39 +00004222 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4224 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4225 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4226 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4227 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4228 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4229 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4230 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4231}
4232
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004233TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004234 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004236 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4237 // orientation-aware are affected by display rotation.
Arpit Singhd8510bd2023-04-27 12:48:15 +00004238 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004240 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004241 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4243 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4244 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4245 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4246 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4247 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4248 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4249 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4250
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004251 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004252 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004253 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4254 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4255 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4256 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4257 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4258 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4259 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4260 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004262 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004263 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4265 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4266 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4267 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4268 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4269 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4270 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4271 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4272
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004273 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004274 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004275 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4276 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4277 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4278 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4279 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4280 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4281 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4282 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283}
4284
4285TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004287 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288
4289 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4290 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291
4292 NotifyMotionArgs motionArgs;
4293 NotifyKeyArgs keyArgs;
4294
4295 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4299 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4300 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004301 ASSERT_NO_FATAL_FAILURE(
4302 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4305 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4306 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004307 ASSERT_NO_FATAL_FAILURE(
4308 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004309
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004313 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004315 ASSERT_NO_FATAL_FAILURE(
4316 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317
4318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004319 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320 ASSERT_EQ(0, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004323
4324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004326 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004327 ASSERT_NO_FATAL_FAILURE(
4328 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329
4330 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4335 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4336 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004337 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004338 ASSERT_NO_FATAL_FAILURE(
4339 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4342 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4343 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004344 ASSERT_NO_FATAL_FAILURE(
4345 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004346
4347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4348 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4349 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004350 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004351 ASSERT_NO_FATAL_FAILURE(
4352 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004353
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004357 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004359 ASSERT_NO_FATAL_FAILURE(
4360 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004361
4362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004364 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004365 ASSERT_NO_FATAL_FAILURE(
4366 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004371 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4372 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004373 ASSERT_NO_FATAL_FAILURE(
4374 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004377
4378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004381 ASSERT_NO_FATAL_FAILURE(
4382 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004383
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4385 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004387 ASSERT_NO_FATAL_FAILURE(
4388 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389
4390 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4392 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4394 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4395 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004396
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004398 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004400 ASSERT_NO_FATAL_FAILURE(
4401 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004402
4403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4404 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4405 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004406 ASSERT_NO_FATAL_FAILURE(
4407 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004414 ASSERT_NO_FATAL_FAILURE(
4415 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004416
4417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004418 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004419 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004420
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004421 ASSERT_NO_FATAL_FAILURE(
4422 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4424 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4425 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4426
4427 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4431 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4432 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004435 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004437 ASSERT_NO_FATAL_FAILURE(
4438 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004439
4440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4441 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4442 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004443 ASSERT_NO_FATAL_FAILURE(
4444 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004449 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004451 ASSERT_NO_FATAL_FAILURE(
4452 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004453
4454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4455 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4456 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004457 ASSERT_NO_FATAL_FAILURE(
4458 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004459
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4461 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4462 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4463
4464 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004465 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4468 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4469 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004470
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004472 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004474 ASSERT_NO_FATAL_FAILURE(
4475 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004476
4477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4478 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4479 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004480 ASSERT_NO_FATAL_FAILURE(
4481 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004486 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004488 ASSERT_NO_FATAL_FAILURE(
4489 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004490
4491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4492 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4493 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004494 ASSERT_NO_FATAL_FAILURE(
4495 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004496
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4498 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4499 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4500
4501 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4505 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4506 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004507
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004509 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004510 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004511 ASSERT_NO_FATAL_FAILURE(
4512 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004513
4514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4515 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4516 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004517 ASSERT_NO_FATAL_FAILURE(
4518 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004523 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004525 ASSERT_NO_FATAL_FAILURE(
4526 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004527
4528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4529 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4530 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004531 ASSERT_NO_FATAL_FAILURE(
4532 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004533
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4535 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4536 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4537}
4538
4539TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004541 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542
4543 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4544 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545
4546 NotifyMotionArgs args;
4547
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004552 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4553 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4555 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 +00004556 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004557}
4558
4559TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004560 addConfigurationProperty("cursor.mode", "pointer");
4561 mFakePolicy->setPointerCapture(true);
Arpit Singhd8510bd2023-04-27 12:48:15 +00004562 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004563
4564 NotifyDeviceResetArgs resetArgs;
4565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4566 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4567 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4568
4569 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4570 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004571
4572 NotifyMotionArgs args;
4573
4574 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4579 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4582 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 +00004583 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004584
4585 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4589 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4590 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4592 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4594 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4595 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4597 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4598
4599 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004600 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4601 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4603 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4604 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4606 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4608 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4609 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4611 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4612
4613 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4616 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4618 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4621 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 +00004622 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004623
4624 // Disable pointer capture and check that the device generation got bumped
4625 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004626 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004627 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004628 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004629 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004630
4631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004632 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4633
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4638 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4641 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 +00004642 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643}
4644
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004645/**
4646 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4647 * pointer acceleration or speed processing should not be applied.
4648 */
4649TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4650 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004651 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4652 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004653 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhd8510bd2023-04-27 12:48:15 +00004654 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004655
4656 NotifyDeviceResetArgs resetArgs;
4657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4658 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4659 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4660
4661 NotifyMotionArgs args;
4662
4663 // Move and verify scale is applied.
4664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4668 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4669 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4670 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4671 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4672 ASSERT_GT(relX, 10);
4673 ASSERT_GT(relY, 20);
4674
4675 // Enable Pointer Capture
4676 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004677 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004678 NotifyPointerCaptureChangedArgs captureArgs;
4679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4680 ASSERT_TRUE(captureArgs.request.enable);
4681
4682 // Move and verify scale is not applied.
4683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4687 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4689 ASSERT_EQ(10, args.pointerCoords[0].getX());
4690 ASSERT_EQ(20, args.pointerCoords[0].getY());
4691}
4692
Prabir Pradhan208360b2022-06-24 18:37:04 +00004693TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4694 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004695 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004696
4697 NotifyDeviceResetArgs resetArgs;
4698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4699 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4700 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4701
4702 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004703 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004704
4705 NotifyMotionArgs args;
4706
4707 // Verify that the coordinates are rotated.
4708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4709 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4712 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4713 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4714 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4715 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4716
4717 // Enable Pointer Capture.
4718 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004719 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004720 NotifyPointerCaptureChangedArgs captureArgs;
4721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4722 ASSERT_TRUE(captureArgs.request.enable);
4723
4724 // Move and verify rotation is not applied.
4725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4726 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4729 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4730 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4731 ASSERT_EQ(10, args.pointerCoords[0].getX());
4732 ASSERT_EQ(20, args.pointerCoords[0].getY());
4733}
4734
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004735TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004736 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004737
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004738 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004739 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004740
4741 // Set up the secondary display as the display on which the pointer should be shown.
4742 // The InputDevice is not associated with any display.
4743 prepareSecondaryDisplay();
4744 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004745 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004746
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004747 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004748 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004749
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004750 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004751 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4752 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004755 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4756 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4757 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004758 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004759}
4760
4761TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004762 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004763
4764 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004765 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004766
4767 // Set up the secondary display as the display on which the pointer should be shown,
4768 // and associate the InputDevice with the secondary display.
4769 prepareSecondaryDisplay();
4770 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4771 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004772 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004773
4774 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4775 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004776
4777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004781 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4782 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4783 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004784 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004785}
4786
4787TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004788 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004789
4790 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004791 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004792 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4793
4794 // Associate the InputDevice with the secondary display.
4795 prepareSecondaryDisplay();
4796 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004797 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004798
4799 // The mapper should not generate any events because it is associated with a display that is
4800 // different from the pointer display.
4801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004805}
4806
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004807// --- BluetoothCursorInputMapperTest ---
4808
4809class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4810protected:
4811 void SetUp() override {
4812 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4813
4814 mFakePointerController = std::make_shared<FakePointerController>();
4815 mFakePolicy->setPointerController(mFakePointerController);
4816 }
4817};
4818
4819TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4820 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004821 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004822
4823 nsecs_t kernelEventTime = ARBITRARY_TIME;
4824 nsecs_t expectedEventTime = ARBITRARY_TIME;
4825 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4826 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4828 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4829 WithEventTime(expectedEventTime))));
4830
4831 // Process several events that come in quick succession, according to their timestamps.
4832 for (int i = 0; i < 3; i++) {
4833 constexpr static nsecs_t delta = ms2ns(1);
4834 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4835 kernelEventTime += delta;
4836 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4837
4838 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4839 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4841 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4842 WithEventTime(expectedEventTime))));
4843 }
4844}
4845
4846TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4847 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004848 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004849
4850 nsecs_t expectedEventTime = ARBITRARY_TIME;
4851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4852 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4854 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4855 WithEventTime(expectedEventTime))));
4856
4857 // Process several events with the same timestamp from the kernel.
4858 // Ensure that we do not generate events too far into the future.
4859 constexpr static int32_t numEvents =
4860 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4861 for (int i = 0; i < numEvents; i++) {
4862 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4863
4864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4867 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4868 WithEventTime(expectedEventTime))));
4869 }
4870
4871 // By processing more events with the same timestamp, we should not generate events with a
4872 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4873 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4874 for (int i = 0; i < 3; i++) {
4875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4876 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4878 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4879 WithEventTime(cappedEventTime))));
4880 }
4881}
4882
4883TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4884 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004885 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004886
4887 nsecs_t kernelEventTime = ARBITRARY_TIME;
4888 nsecs_t expectedEventTime = ARBITRARY_TIME;
4889 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4890 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4892 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4893 WithEventTime(expectedEventTime))));
4894
4895 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4896 // smoothening is not needed, its timestamp is not affected.
4897 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4898 expectedEventTime = kernelEventTime;
4899
4900 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4901 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4903 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4904 WithEventTime(expectedEventTime))));
4905}
4906
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907// --- TouchInputMapperTest ---
4908
4909class TouchInputMapperTest : public InputMapperTest {
4910protected:
4911 static const int32_t RAW_X_MIN;
4912 static const int32_t RAW_X_MAX;
4913 static const int32_t RAW_Y_MIN;
4914 static const int32_t RAW_Y_MAX;
4915 static const int32_t RAW_TOUCH_MIN;
4916 static const int32_t RAW_TOUCH_MAX;
4917 static const int32_t RAW_TOOL_MIN;
4918 static const int32_t RAW_TOOL_MAX;
4919 static const int32_t RAW_PRESSURE_MIN;
4920 static const int32_t RAW_PRESSURE_MAX;
4921 static const int32_t RAW_ORIENTATION_MIN;
4922 static const int32_t RAW_ORIENTATION_MAX;
4923 static const int32_t RAW_DISTANCE_MIN;
4924 static const int32_t RAW_DISTANCE_MAX;
4925 static const int32_t RAW_TILT_MIN;
4926 static const int32_t RAW_TILT_MAX;
4927 static const int32_t RAW_ID_MIN;
4928 static const int32_t RAW_ID_MAX;
4929 static const int32_t RAW_SLOT_MIN;
4930 static const int32_t RAW_SLOT_MAX;
4931 static const float X_PRECISION;
4932 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004933 static const float X_PRECISION_VIRTUAL;
4934 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935
4936 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004937 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938
4939 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4940
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004941 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004942 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004943
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944 enum Axes {
4945 POSITION = 1 << 0,
4946 TOUCH = 1 << 1,
4947 TOOL = 1 << 2,
4948 PRESSURE = 1 << 3,
4949 ORIENTATION = 1 << 4,
4950 MINOR = 1 << 5,
4951 ID = 1 << 6,
4952 DISTANCE = 1 << 7,
4953 TILT = 1 << 8,
4954 SLOT = 1 << 9,
4955 TOOL_TYPE = 1 << 10,
4956 };
4957
Michael Wrighta9cf4192022-12-01 23:46:39 +00004958 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004959 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004960 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004962 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963 int32_t toRawX(float displayX);
4964 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004965 int32_t toRotatedRawX(float displayX);
4966 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004967 float toCookedX(float rawX, float rawY);
4968 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004969 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004970 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004971 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004972 float toDisplayY(int32_t rawY, int32_t displayHeight);
4973
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974};
4975
4976const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4977const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4978const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4979const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4980const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4981const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4982const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4983const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004984const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4985const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4987const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4988const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4989const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4990const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4991const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4992const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4993const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4994const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4995const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4996const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4997const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004998const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4999 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5000const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5001 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005002const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5003 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004
5005const float TouchInputMapperTest::GEOMETRIC_SCALE =
5006 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5007 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5008
5009const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5010 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5011 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5012};
5013
Michael Wrighta9cf4192022-12-01 23:46:39 +00005014void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005015 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5016 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005017}
5018
5019void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5020 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00005021 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022}
5023
Michael Wrighta9cf4192022-12-01 23:46:39 +00005024void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005025 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5026 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5027 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005028}
5029
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005031 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5032 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5033 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5034 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035}
5036
Jason Gerecke489fda82012-09-07 17:19:40 -07005037void TouchInputMapperTest::prepareLocationCalibration() {
5038 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5039}
5040
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041int32_t TouchInputMapperTest::toRawX(float displayX) {
5042 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5043}
5044
5045int32_t TouchInputMapperTest::toRawY(float displayY) {
5046 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5047}
5048
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005049int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5050 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5051}
5052
5053int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5054 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5055}
5056
Jason Gerecke489fda82012-09-07 17:19:40 -07005057float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5058 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5059 return rawX;
5060}
5061
5062float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5063 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5064 return rawY;
5065}
5066
Michael Wrightd02c5b62014-02-10 15:10:22 -08005067float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005068 return toDisplayX(rawX, DISPLAY_WIDTH);
5069}
5070
5071float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5072 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073}
5074
5075float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005076 return toDisplayY(rawY, DISPLAY_HEIGHT);
5077}
5078
5079float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5080 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005081}
5082
5083
5084// --- SingleTouchInputMapperTest ---
5085
5086class SingleTouchInputMapperTest : public TouchInputMapperTest {
5087protected:
5088 void prepareButtons();
5089 void prepareAxes(int axes);
5090
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005091 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5092 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5093 void processUp(SingleTouchInputMapper& mappery);
5094 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5095 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5096 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5097 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5098 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5099 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100};
5101
5102void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005103 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005104}
5105
5106void SingleTouchInputMapperTest::prepareAxes(int axes) {
5107 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005108 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5109 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110 }
5111 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005112 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5113 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005114 }
5115 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005116 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5117 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118 }
5119 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005120 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5121 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005122 }
5123 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005124 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5125 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005126 }
5127}
5128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005129void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005133}
5134
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005135void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138}
5139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005140void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005142}
5143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005144void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005146}
5147
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005148void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5149 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151}
5152
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005153void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005155}
5156
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005157void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5158 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161}
5162
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005163void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5164 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166}
5167
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005168void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170}
5171
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173 prepareButtons();
5174 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00005175 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176
Josep del Río2d8c79a2023-01-23 19:33:50 +00005177 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178}
5179
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005181 prepareButtons();
5182 prepareAxes(POSITION);
5183 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00005184 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005186 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005187}
5188
5189TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005190 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005191 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005192 prepareButtons();
5193 prepareAxes(POSITION);
5194 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005195 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005196
5197 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005198 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005199
5200 // Virtual key is down.
5201 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5202 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5203 processDown(mapper, x, y);
5204 processSync(mapper);
5205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5206
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005207 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005208
5209 // Virtual key is up.
5210 processUp(mapper);
5211 processSync(mapper);
5212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5213
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005214 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215}
5216
5217TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005218 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005219 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220 prepareButtons();
5221 prepareAxes(POSITION);
5222 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005223 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005224
5225 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005226 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005227
5228 // Virtual key is down.
5229 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5230 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5231 processDown(mapper, x, y);
5232 processSync(mapper);
5233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5234
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005235 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005236
5237 // Virtual key is up.
5238 processUp(mapper);
5239 processSync(mapper);
5240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5241
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005242 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005243}
5244
5245TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005246 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005247 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248 prepareButtons();
5249 prepareAxes(POSITION);
5250 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005251 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252
Michael Wrightd02c5b62014-02-10 15:10:22 -08005253 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005254 ASSERT_TRUE(
5255 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005256 ASSERT_TRUE(flags[0]);
5257 ASSERT_FALSE(flags[1]);
5258}
5259
5260TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005261 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005262 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005263 prepareButtons();
5264 prepareAxes(POSITION);
5265 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005266 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005267
arthurhungdcef2dc2020-08-11 14:47:50 +08005268 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005269
5270 NotifyKeyArgs args;
5271
5272 // Press virtual key.
5273 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5274 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5275 processDown(mapper, x, y);
5276 processSync(mapper);
5277
5278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5279 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5280 ASSERT_EQ(DEVICE_ID, args.deviceId);
5281 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5282 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5283 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5284 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5285 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5286 ASSERT_EQ(KEY_HOME, args.scanCode);
5287 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5288 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5289
5290 // Release virtual key.
5291 processUp(mapper);
5292 processSync(mapper);
5293
5294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5295 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5296 ASSERT_EQ(DEVICE_ID, args.deviceId);
5297 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5298 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5299 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5300 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5301 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5302 ASSERT_EQ(KEY_HOME, args.scanCode);
5303 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5304 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5305
5306 // Should not have sent any motions.
5307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5308}
5309
5310TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005312 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005313 prepareButtons();
5314 prepareAxes(POSITION);
5315 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005316 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005317
arthurhungdcef2dc2020-08-11 14:47:50 +08005318 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005319
5320 NotifyKeyArgs keyArgs;
5321
5322 // Press virtual key.
5323 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5324 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5325 processDown(mapper, x, y);
5326 processSync(mapper);
5327
5328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5329 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5330 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5331 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5332 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5333 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5334 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5335 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5336 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5337 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5338 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5339
5340 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5341 // into the display area.
5342 y -= 100;
5343 processMove(mapper, x, y);
5344 processSync(mapper);
5345
5346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5347 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5348 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5349 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5350 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5351 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5352 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5353 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5354 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5355 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5356 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5357 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5358
5359 NotifyMotionArgs motionArgs;
5360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5361 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5362 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5363 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5364 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5365 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5366 ASSERT_EQ(0, motionArgs.flags);
5367 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5368 ASSERT_EQ(0, motionArgs.buttonState);
5369 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005370 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005371 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005372 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5374 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5375 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5376 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5377 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5378
5379 // Keep moving out of bounds. Should generate a pointer move.
5380 y -= 50;
5381 processMove(mapper, x, y);
5382 processSync(mapper);
5383
5384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5385 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5386 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5387 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5388 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5390 ASSERT_EQ(0, motionArgs.flags);
5391 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5392 ASSERT_EQ(0, motionArgs.buttonState);
5393 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005394 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005396 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5398 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5399 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5400 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5401 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5402
5403 // Release out of bounds. Should generate a pointer up.
5404 processUp(mapper);
5405 processSync(mapper);
5406
5407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5408 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5409 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5410 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5411 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5412 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5413 ASSERT_EQ(0, motionArgs.flags);
5414 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5415 ASSERT_EQ(0, motionArgs.buttonState);
5416 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005417 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005419 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5421 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5422 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5423 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5424 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5425
5426 // Should not have sent any more keys or motions.
5427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5429}
5430
5431TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005432 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005433 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005434 prepareButtons();
5435 prepareAxes(POSITION);
5436 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005437 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005438
arthurhungdcef2dc2020-08-11 14:47:50 +08005439 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440
5441 NotifyMotionArgs motionArgs;
5442
5443 // Initially go down out of bounds.
5444 int32_t x = -10;
5445 int32_t y = -10;
5446 processDown(mapper, x, y);
5447 processSync(mapper);
5448
5449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5450
5451 // Move into the display area. Should generate a pointer down.
5452 x = 50;
5453 y = 75;
5454 processMove(mapper, x, y);
5455 processSync(mapper);
5456
5457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5458 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5459 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5460 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5461 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5462 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5463 ASSERT_EQ(0, motionArgs.flags);
5464 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5465 ASSERT_EQ(0, motionArgs.buttonState);
5466 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005467 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005468 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005469 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5471 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5472 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5473 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5474 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5475
5476 // Release. Should generate a pointer up.
5477 processUp(mapper);
5478 processSync(mapper);
5479
5480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5481 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5482 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5483 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5484 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5485 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5486 ASSERT_EQ(0, motionArgs.flags);
5487 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5488 ASSERT_EQ(0, motionArgs.buttonState);
5489 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005490 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005492 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5494 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5495 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5496 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5497 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5498
5499 // Should not have sent any more keys or motions.
5500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5502}
5503
Santos Cordonfa5cf462017-04-05 10:37:00 -07005504TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005505 addConfigurationProperty("touch.deviceType", "touchScreen");
5506 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5507
Michael Wrighta9cf4192022-12-01 23:46:39 +00005508 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005509 prepareButtons();
5510 prepareAxes(POSITION);
5511 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005512 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005513
arthurhungdcef2dc2020-08-11 14:47:50 +08005514 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005515
5516 NotifyMotionArgs motionArgs;
5517
5518 // Down.
5519 int32_t x = 100;
5520 int32_t y = 125;
5521 processDown(mapper, x, y);
5522 processSync(mapper);
5523
5524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5525 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5526 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5527 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5528 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5529 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5530 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5531 ASSERT_EQ(0, motionArgs.flags);
5532 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5533 ASSERT_EQ(0, motionArgs.buttonState);
5534 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005535 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005536 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005537 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5539 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5540 1, 0, 0, 0, 0, 0, 0, 0));
5541 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5542 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5543 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5544
5545 // Move.
5546 x += 50;
5547 y += 75;
5548 processMove(mapper, x, y);
5549 processSync(mapper);
5550
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5552 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5553 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5554 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5555 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5556 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5558 ASSERT_EQ(0, motionArgs.flags);
5559 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5560 ASSERT_EQ(0, motionArgs.buttonState);
5561 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005562 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005563 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005564 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5566 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5567 1, 0, 0, 0, 0, 0, 0, 0));
5568 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5569 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5570 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5571
5572 // Up.
5573 processUp(mapper);
5574 processSync(mapper);
5575
5576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5577 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5578 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5579 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5580 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5581 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5582 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5583 ASSERT_EQ(0, motionArgs.flags);
5584 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5585 ASSERT_EQ(0, motionArgs.buttonState);
5586 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005587 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005588 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005589 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5591 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5592 1, 0, 0, 0, 0, 0, 0, 0));
5593 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5594 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5595 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5596
5597 // Should not have sent any more keys or motions.
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5600}
5601
Michael Wrightd02c5b62014-02-10 15:10:22 -08005602TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005603 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005604 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605 prepareButtons();
5606 prepareAxes(POSITION);
5607 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005608 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005609
arthurhungdcef2dc2020-08-11 14:47:50 +08005610 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005611
5612 NotifyMotionArgs motionArgs;
5613
5614 // Down.
5615 int32_t x = 100;
5616 int32_t y = 125;
5617 processDown(mapper, x, y);
5618 processSync(mapper);
5619
5620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5621 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5622 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5623 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5624 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5625 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5626 ASSERT_EQ(0, motionArgs.flags);
5627 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5628 ASSERT_EQ(0, motionArgs.buttonState);
5629 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005630 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005631 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005632 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5634 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5635 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5636 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5637 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5638
5639 // Move.
5640 x += 50;
5641 y += 75;
5642 processMove(mapper, x, y);
5643 processSync(mapper);
5644
5645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5646 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5647 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5648 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5649 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5651 ASSERT_EQ(0, motionArgs.flags);
5652 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5653 ASSERT_EQ(0, motionArgs.buttonState);
5654 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005655 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005656 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005657 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5659 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5660 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5661 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5662 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5663
5664 // Up.
5665 processUp(mapper);
5666 processSync(mapper);
5667
5668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5669 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5670 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5671 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5672 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5673 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5674 ASSERT_EQ(0, motionArgs.flags);
5675 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5676 ASSERT_EQ(0, motionArgs.buttonState);
5677 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005678 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005679 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005680 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5682 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5683 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5684 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5685 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5686
5687 // Should not have sent any more keys or motions.
5688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5690}
5691
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005692TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005693 addConfigurationProperty("touch.deviceType", "touchScreen");
5694 prepareButtons();
5695 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005696 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5697 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singh56adebc2023-04-25 13:56:05 +00005698 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699
5700 NotifyMotionArgs args;
5701
5702 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005703 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005704 processDown(mapper, toRawX(50), toRawY(75));
5705 processSync(mapper);
5706
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5708 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5709 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5710
5711 processUp(mapper);
5712 processSync(mapper);
5713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5714}
5715
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005716TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005717 addConfigurationProperty("touch.deviceType", "touchScreen");
5718 prepareButtons();
5719 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005720 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5721 // orientation-aware are affected by display rotation.
5722 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00005723 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724
5725 NotifyMotionArgs args;
5726
5727 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005728 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005729 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005730 processDown(mapper, toRawX(50), toRawY(75));
5731 processSync(mapper);
5732
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5734 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5735 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5736
5737 processUp(mapper);
5738 processSync(mapper);
5739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5740
5741 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005742 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005743 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005744 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005745 processSync(mapper);
5746
5747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5748 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5749 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5750
5751 processUp(mapper);
5752 processSync(mapper);
5753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5754
5755 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005756 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005757 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005758 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5759 processSync(mapper);
5760
5761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5762 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5763 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5764
5765 processUp(mapper);
5766 processSync(mapper);
5767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5768
5769 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005770 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005771 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005772 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005773 processSync(mapper);
5774
5775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5776 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5777 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5778
5779 processUp(mapper);
5780 processSync(mapper);
5781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5782}
5783
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005784TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5785 addConfigurationProperty("touch.deviceType", "touchScreen");
5786 prepareButtons();
5787 prepareAxes(POSITION);
5788 addConfigurationProperty("touch.orientationAware", "1");
5789 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5790 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005791 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005792 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005793 NotifyMotionArgs args;
5794
5795 // Orientation 0.
5796 processDown(mapper, toRawX(50), toRawY(75));
5797 processSync(mapper);
5798
5799 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5800 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5801 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5802
5803 processUp(mapper);
5804 processSync(mapper);
5805 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5806}
5807
5808TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5809 addConfigurationProperty("touch.deviceType", "touchScreen");
5810 prepareButtons();
5811 prepareAxes(POSITION);
5812 addConfigurationProperty("touch.orientationAware", "1");
5813 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5814 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005815 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005816 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005817 NotifyMotionArgs args;
5818
5819 // Orientation 90.
5820 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5821 processSync(mapper);
5822
5823 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5824 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5825 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5826
5827 processUp(mapper);
5828 processSync(mapper);
5829 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5830}
5831
5832TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5833 addConfigurationProperty("touch.deviceType", "touchScreen");
5834 prepareButtons();
5835 prepareAxes(POSITION);
5836 addConfigurationProperty("touch.orientationAware", "1");
5837 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5838 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005839 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005840 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005841 NotifyMotionArgs args;
5842
5843 // Orientation 180.
5844 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5845 processSync(mapper);
5846
5847 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5848 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5849 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5850
5851 processUp(mapper);
5852 processSync(mapper);
5853 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5854}
5855
5856TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5857 addConfigurationProperty("touch.deviceType", "touchScreen");
5858 prepareButtons();
5859 prepareAxes(POSITION);
5860 addConfigurationProperty("touch.orientationAware", "1");
5861 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5862 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005863 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005864 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005865 NotifyMotionArgs args;
5866
5867 // Orientation 270.
5868 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5869 processSync(mapper);
5870
5871 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5872 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5873 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5874
5875 processUp(mapper);
5876 processSync(mapper);
5877 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5878}
5879
5880TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5881 addConfigurationProperty("touch.deviceType", "touchScreen");
5882 prepareButtons();
5883 prepareAxes(POSITION);
5884 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5885 // orientation-aware are affected by display rotation.
5886 addConfigurationProperty("touch.orientationAware", "0");
5887 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singh56adebc2023-04-25 13:56:05 +00005888 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005889
5890 NotifyMotionArgs args;
5891
5892 // Orientation 90, Rotation 0.
5893 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005894 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005895 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5896 processSync(mapper);
5897
5898 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5899 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5900 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5901
5902 processUp(mapper);
5903 processSync(mapper);
5904 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5905
5906 // Orientation 90, Rotation 90.
5907 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005908 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005909 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005910 processSync(mapper);
5911
5912 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5913 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5914 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5915
5916 processUp(mapper);
5917 processSync(mapper);
5918 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5919
5920 // Orientation 90, Rotation 180.
5921 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005922 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005923 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5924 processSync(mapper);
5925
5926 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5927 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5928 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5929
5930 processUp(mapper);
5931 processSync(mapper);
5932 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5933
5934 // Orientation 90, Rotation 270.
5935 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005936 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005937 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 -07005938 processSync(mapper);
5939
5940 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5941 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5942 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5943
5944 processUp(mapper);
5945 processSync(mapper);
5946 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5947}
5948
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005949TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5950 addConfigurationProperty("touch.deviceType", "touchScreen");
5951 prepareButtons();
5952 prepareAxes(POSITION);
5953 addConfigurationProperty("touch.orientationAware", "1");
5954 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005955 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005956
5957 // Set a physical frame in the display viewport.
5958 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5959 viewport->physicalLeft = 20;
5960 viewport->physicalTop = 600;
5961 viewport->physicalRight = 30;
5962 viewport->physicalBottom = 610;
5963 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005964 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005965
5966 // Start the touch.
5967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5968 processSync(mapper);
5969
5970 // Expect all input starting outside the physical frame to be ignored.
5971 const std::array<Point, 6> outsidePoints = {
5972 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5973 for (const auto& p : outsidePoints) {
5974 processMove(mapper, toRawX(p.x), toRawY(p.y));
5975 processSync(mapper);
5976 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5977 }
5978
5979 // Move the touch into the physical frame.
5980 processMove(mapper, toRawX(25), toRawY(605));
5981 processSync(mapper);
5982 NotifyMotionArgs args;
5983 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5984 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5985 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5986 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5987
5988 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5989 for (const auto& p : outsidePoints) {
5990 processMove(mapper, toRawX(p.x), toRawY(p.y));
5991 processSync(mapper);
5992 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5993 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5994 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5995 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5996 }
5997
5998 processUp(mapper);
5999 processSync(mapper);
6000 EXPECT_NO_FATAL_FAILURE(
6001 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
6002}
6003
Harry Cutts1db43992023-06-19 17:05:07 +00006004TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
6005 std::shared_ptr<FakePointerController> fakePointerController =
6006 std::make_shared<FakePointerController>();
6007 mFakePolicy->setPointerController(fakePointerController);
6008
6009 addConfigurationProperty("touch.deviceType", "pointer");
6010 prepareAxes(POSITION);
6011 prepareDisplay(ui::ROTATION_0);
6012 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
6013
6014 // Set a physical frame in the display viewport.
6015 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6016 viewport->physicalLeft = 20;
6017 viewport->physicalTop = 600;
6018 viewport->physicalRight = 30;
6019 viewport->physicalBottom = 610;
6020 mFakePolicy->updateViewport(*viewport);
6021 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
6022
6023 // Start the touch.
6024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6025 processSync(mapper);
6026
6027 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
6028 // produced.
6029 const std::array<Point, 6> outsidePoints = {
6030 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6031 for (const auto& p : outsidePoints) {
6032 processMove(mapper, toRawX(p.x), toRawY(p.y));
6033 processSync(mapper);
6034 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6035 }
6036}
6037
Michael Wrightd02c5b62014-02-10 15:10:22 -08006038TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006039 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006040 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041 prepareButtons();
6042 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singh56adebc2023-04-25 13:56:05 +00006043 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044
6045 // These calculations are based on the input device calibration documentation.
6046 int32_t rawX = 100;
6047 int32_t rawY = 200;
6048 int32_t rawPressure = 10;
6049 int32_t rawToolMajor = 12;
6050 int32_t rawDistance = 2;
6051 int32_t rawTiltX = 30;
6052 int32_t rawTiltY = 110;
6053
6054 float x = toDisplayX(rawX);
6055 float y = toDisplayY(rawY);
6056 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6057 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6058 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6059 float distance = float(rawDistance);
6060
6061 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6062 float tiltScale = M_PI / 180;
6063 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6064 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6065 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6066 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6067
6068 processDown(mapper, rawX, rawY);
6069 processPressure(mapper, rawPressure);
6070 processToolMajor(mapper, rawToolMajor);
6071 processDistance(mapper, rawDistance);
6072 processTilt(mapper, rawTiltX, rawTiltY);
6073 processSync(mapper);
6074
6075 NotifyMotionArgs args;
6076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6078 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6079 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6080}
6081
Jason Gerecke489fda82012-09-07 17:19:40 -07006082TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006083 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006084 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006085 prepareLocationCalibration();
6086 prepareButtons();
6087 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006088 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006089
6090 int32_t rawX = 100;
6091 int32_t rawY = 200;
6092
6093 float x = toDisplayX(toCookedX(rawX, rawY));
6094 float y = toDisplayY(toCookedY(rawX, rawY));
6095
6096 processDown(mapper, rawX, rawY);
6097 processSync(mapper);
6098
6099 NotifyMotionArgs args;
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6102 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6103}
6104
Michael Wrightd02c5b62014-02-10 15:10:22 -08006105TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006106 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006107 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108 prepareButtons();
6109 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006110 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111
6112 NotifyMotionArgs motionArgs;
6113 NotifyKeyArgs keyArgs;
6114
6115 processDown(mapper, 100, 200);
6116 processSync(mapper);
6117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6118 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6119 ASSERT_EQ(0, motionArgs.buttonState);
6120
6121 // press BTN_LEFT, release BTN_LEFT
6122 processKey(mapper, BTN_LEFT, 1);
6123 processSync(mapper);
6124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6126 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6127
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6129 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6130 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6131
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132 processKey(mapper, BTN_LEFT, 0);
6133 processSync(mapper);
6134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006135 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006137
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006140 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141
6142 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6143 processKey(mapper, BTN_RIGHT, 1);
6144 processKey(mapper, BTN_MIDDLE, 1);
6145 processSync(mapper);
6146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6148 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6149 motionArgs.buttonState);
6150
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6152 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6153 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6154
6155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6156 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6157 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6158 motionArgs.buttonState);
6159
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160 processKey(mapper, BTN_RIGHT, 0);
6161 processSync(mapper);
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006163 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006164 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006165
6166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006168 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169
6170 processKey(mapper, BTN_MIDDLE, 0);
6171 processSync(mapper);
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006173 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006174 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006175
6176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006178 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179
6180 // press BTN_BACK, release BTN_BACK
6181 processKey(mapper, BTN_BACK, 1);
6182 processSync(mapper);
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6184 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6185 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006186
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006189 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6190
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6192 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6193 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194
6195 processKey(mapper, BTN_BACK, 0);
6196 processSync(mapper);
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006198 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006199 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006200
6201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006203 ASSERT_EQ(0, motionArgs.buttonState);
6204
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6206 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6207 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6208
6209 // press BTN_SIDE, release BTN_SIDE
6210 processKey(mapper, BTN_SIDE, 1);
6211 processSync(mapper);
6212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6213 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6214 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006215
Michael Wrightd02c5b62014-02-10 15:10:22 -08006216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006218 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6219
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6221 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6222 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006223
6224 processKey(mapper, BTN_SIDE, 0);
6225 processSync(mapper);
6226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006227 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006228 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006229
6230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006232 ASSERT_EQ(0, motionArgs.buttonState);
6233
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6235 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6236 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6237
6238 // press BTN_FORWARD, release BTN_FORWARD
6239 processKey(mapper, BTN_FORWARD, 1);
6240 processSync(mapper);
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6242 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6243 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006244
Michael Wrightd02c5b62014-02-10 15:10:22 -08006245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006247 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6248
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6250 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6251 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252
6253 processKey(mapper, BTN_FORWARD, 0);
6254 processSync(mapper);
6255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006256 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006257 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006258
6259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006261 ASSERT_EQ(0, motionArgs.buttonState);
6262
Michael Wrightd02c5b62014-02-10 15:10:22 -08006263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6264 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6265 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6266
6267 // press BTN_EXTRA, release BTN_EXTRA
6268 processKey(mapper, BTN_EXTRA, 1);
6269 processSync(mapper);
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6271 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6272 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006273
Michael Wrightd02c5b62014-02-10 15:10:22 -08006274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006276 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6277
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6279 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6280 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281
6282 processKey(mapper, BTN_EXTRA, 0);
6283 processSync(mapper);
6284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006285 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006286 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006287
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006290 ASSERT_EQ(0, motionArgs.buttonState);
6291
Michael Wrightd02c5b62014-02-10 15:10:22 -08006292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6293 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6294 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6295
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6297
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298 // press BTN_STYLUS, release BTN_STYLUS
6299 processKey(mapper, BTN_STYLUS, 1);
6300 processSync(mapper);
6301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006303 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6304
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6306 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6307 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308
6309 processKey(mapper, BTN_STYLUS, 0);
6310 processSync(mapper);
6311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006312 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006314
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318
6319 // press BTN_STYLUS2, release BTN_STYLUS2
6320 processKey(mapper, BTN_STYLUS2, 1);
6321 processSync(mapper);
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006324 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6325
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6327 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6328 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329
6330 processKey(mapper, BTN_STYLUS2, 0);
6331 processSync(mapper);
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006333 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006334 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006335
6336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006338 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339
6340 // release touch
6341 processUp(mapper);
6342 processSync(mapper);
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6344 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6345 ASSERT_EQ(0, motionArgs.buttonState);
6346}
6347
6348TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006349 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006350 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006351 prepareButtons();
6352 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006353 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354
6355 NotifyMotionArgs motionArgs;
6356
6357 // default tool type is finger
6358 processDown(mapper, 100, 200);
6359 processSync(mapper);
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6361 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006362 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006363
6364 // eraser
6365 processKey(mapper, BTN_TOOL_RUBBER, 1);
6366 processSync(mapper);
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006369 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370
6371 // stylus
6372 processKey(mapper, BTN_TOOL_RUBBER, 0);
6373 processKey(mapper, BTN_TOOL_PEN, 1);
6374 processSync(mapper);
6375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006377 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378
6379 // brush
6380 processKey(mapper, BTN_TOOL_PEN, 0);
6381 processKey(mapper, BTN_TOOL_BRUSH, 1);
6382 processSync(mapper);
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006385 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386
6387 // pencil
6388 processKey(mapper, BTN_TOOL_BRUSH, 0);
6389 processKey(mapper, BTN_TOOL_PENCIL, 1);
6390 processSync(mapper);
6391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006393 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006394
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006395 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006396 processKey(mapper, BTN_TOOL_PENCIL, 0);
6397 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6398 processSync(mapper);
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006401 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402
6403 // mouse
6404 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6405 processKey(mapper, BTN_TOOL_MOUSE, 1);
6406 processSync(mapper);
6407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006409 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410
6411 // lens
6412 processKey(mapper, BTN_TOOL_MOUSE, 0);
6413 processKey(mapper, BTN_TOOL_LENS, 1);
6414 processSync(mapper);
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006417 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418
6419 // double-tap
6420 processKey(mapper, BTN_TOOL_LENS, 0);
6421 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6422 processSync(mapper);
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006425 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006426
6427 // triple-tap
6428 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6429 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6430 processSync(mapper);
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6432 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006433 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006434
6435 // quad-tap
6436 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6437 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6438 processSync(mapper);
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006441 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006442
6443 // finger
6444 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6445 processKey(mapper, BTN_TOOL_FINGER, 1);
6446 processSync(mapper);
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006449 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006450
6451 // stylus trumps finger
6452 processKey(mapper, BTN_TOOL_PEN, 1);
6453 processSync(mapper);
6454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006456 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457
6458 // eraser trumps stylus
6459 processKey(mapper, BTN_TOOL_RUBBER, 1);
6460 processSync(mapper);
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006463 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006464
6465 // mouse trumps eraser
6466 processKey(mapper, BTN_TOOL_MOUSE, 1);
6467 processSync(mapper);
6468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006470 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006471
6472 // back to default tool type
6473 processKey(mapper, BTN_TOOL_MOUSE, 0);
6474 processKey(mapper, BTN_TOOL_RUBBER, 0);
6475 processKey(mapper, BTN_TOOL_PEN, 0);
6476 processKey(mapper, BTN_TOOL_FINGER, 0);
6477 processSync(mapper);
6478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006480 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006481}
6482
6483TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006484 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006485 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006486 prepareButtons();
6487 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006488 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +00006489 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006490
6491 NotifyMotionArgs motionArgs;
6492
6493 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6494 processKey(mapper, BTN_TOOL_FINGER, 1);
6495 processMove(mapper, 100, 200);
6496 processSync(mapper);
6497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6498 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6500 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6501
6502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6503 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6505 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6506
6507 // move a little
6508 processMove(mapper, 150, 250);
6509 processSync(mapper);
6510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6511 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6513 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6514
6515 // down when BTN_TOUCH is pressed, pressure defaults to 1
6516 processKey(mapper, BTN_TOUCH, 1);
6517 processSync(mapper);
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6519 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6521 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6522
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6524 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6526 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6527
6528 // up when BTN_TOUCH is released, hover restored
6529 processKey(mapper, BTN_TOUCH, 0);
6530 processSync(mapper);
6531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6532 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6534 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6535
6536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6537 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6539 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6540
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6544 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6545
6546 // exit hover when pointer goes away
6547 processKey(mapper, BTN_TOOL_FINGER, 0);
6548 processSync(mapper);
6549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6550 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6552 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6553}
6554
6555TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006556 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006557 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006558 prepareButtons();
6559 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006560 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006561
6562 NotifyMotionArgs motionArgs;
6563
6564 // initially hovering because pressure is 0
6565 processDown(mapper, 100, 200);
6566 processPressure(mapper, 0);
6567 processSync(mapper);
6568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6569 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6571 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6572
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6574 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6576 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6577
6578 // move a little
6579 processMove(mapper, 150, 250);
6580 processSync(mapper);
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6582 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6584 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6585
6586 // down when pressure is non-zero
6587 processPressure(mapper, RAW_PRESSURE_MAX);
6588 processSync(mapper);
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6590 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6592 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6593
6594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6595 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6597 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6598
6599 // up when pressure becomes 0, hover restored
6600 processPressure(mapper, 0);
6601 processSync(mapper);
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6603 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6605 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6606
6607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6608 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6610 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6611
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6613 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6615 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6616
6617 // exit hover when pointer goes away
6618 processUp(mapper);
6619 processSync(mapper);
6620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6621 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6623 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6624}
6625
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006626TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6627 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006628 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006629 prepareButtons();
6630 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006631 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006632
6633 // Touch down.
6634 processDown(mapper, 100, 200);
6635 processPressure(mapper, 1);
6636 processSync(mapper);
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6638 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6639
6640 // Reset the mapper. This should cancel the ongoing gesture.
6641 resetMapper(mapper, ARBITRARY_TIME);
6642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6643 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6644
6645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6646}
6647
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006648TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6649 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006650 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006651 prepareButtons();
6652 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006653 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006654
6655 // Set the initial state for the touch pointer.
6656 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6657 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6658 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6659 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6660
6661 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006662 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6663 // does not generate any events.
6664 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006665
6666 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6667 // the recreated touch state to generate a down event.
6668 processSync(mapper);
6669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6670 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6671
6672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6673}
6674
lilinnan687e58f2022-07-19 16:00:50 +08006675TEST_F(SingleTouchInputMapperTest,
6676 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6677 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006678 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006679 prepareButtons();
6680 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006681 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006682 NotifyMotionArgs motionArgs;
6683
6684 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006685 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006686 processSync(mapper);
6687
6688 // We should receive a down event
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6690 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6691
6692 // Change display id
6693 clearViewports();
6694 prepareSecondaryDisplay(ViewportType::INTERNAL);
6695
6696 // We should receive a cancel event
6697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6698 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6699 // Then receive reset called
6700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6701}
6702
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006703TEST_F(SingleTouchInputMapperTest,
6704 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6705 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006706 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006707 prepareButtons();
6708 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006709 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6711 NotifyMotionArgs motionArgs;
6712
6713 // Start a new gesture.
6714 processDown(mapper, 100, 200);
6715 processSync(mapper);
6716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6717 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6718
6719 // Make the viewport inactive. This will put the device in disabled mode.
6720 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6721 viewport->isActive = false;
6722 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006723 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006724
6725 // We should receive a cancel event for the ongoing gesture.
6726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6727 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6728 // Then we should be notified that the device was reset.
6729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6730
6731 // No events are generated while the viewport is inactive.
6732 processMove(mapper, 101, 201);
6733 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006734 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006735 processSync(mapper);
6736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6737
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006738 // Start a new gesture while the viewport is still inactive.
6739 processDown(mapper, 300, 400);
6740 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6741 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6742 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6743 processSync(mapper);
6744
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006745 // Make the viewport active again. The device should resume processing events.
6746 viewport->isActive = true;
6747 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006748 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006749
6750 // The device is reset because it changes back to direct mode, without generating any events.
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6753
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006754 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006755 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6757 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006758
6759 // No more events.
6760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6762}
6763
Prabir Pradhan211ba622022-10-31 21:09:21 +00006764TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6765 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006766 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006767 prepareButtons();
6768 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006769 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6771
6772 // Press a stylus button.
6773 processKey(mapper, BTN_STYLUS, 1);
6774 processSync(mapper);
6775
6776 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6777 processDown(mapper, 100, 200);
6778 processSync(mapper);
6779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6780 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6781 WithCoords(toDisplayX(100), toDisplayY(200)),
6782 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6784 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6785 WithCoords(toDisplayX(100), toDisplayY(200)),
6786 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6787
6788 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6789 // the button has not actually been released, since there will be no pointers through which the
6790 // button state can be reported. The event is generated at the location of the pointer before
6791 // it went up.
6792 processUp(mapper);
6793 processSync(mapper);
6794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6795 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6796 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6798 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6799 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6800}
6801
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006802TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6803 addConfigurationProperty("touch.deviceType", "touchScreen");
6804 prepareDisplay(ui::ROTATION_0);
6805 prepareButtons();
6806 prepareAxes(POSITION);
6807
6808 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6809
Arpit Singh56adebc2023-04-25 13:56:05 +00006810 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6812
6813 // Press a stylus button.
6814 processKey(mapper, BTN_STYLUS, 1);
6815 processSync(mapper);
6816
6817 // Start a touch gesture and ensure that the stylus button is not reported.
6818 processDown(mapper, 100, 200);
6819 processSync(mapper);
6820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6821 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6822
6823 // Release and press the stylus button again.
6824 processKey(mapper, BTN_STYLUS, 0);
6825 processSync(mapper);
6826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6827 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6828 processKey(mapper, BTN_STYLUS, 1);
6829 processSync(mapper);
6830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6831 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6832
6833 // Release the touch gesture.
6834 processUp(mapper);
6835 processSync(mapper);
6836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6837 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6838
6839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6840}
6841
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006842TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6843 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6844 prepareDisplay(ui::ROTATION_0);
6845 prepareButtons();
6846 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006847 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6849
6850 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6851}
6852
Seunghwan Choi356026c2023-02-01 14:37:25 +09006853TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6854 std::shared_ptr<FakePointerController> fakePointerController =
6855 std::make_shared<FakePointerController>();
6856 addConfigurationProperty("touch.deviceType", "touchScreen");
6857 prepareDisplay(ui::ROTATION_0);
6858 prepareButtons();
6859 prepareAxes(POSITION);
6860 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6861 mFakePolicy->setPointerController(fakePointerController);
6862 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singh56adebc2023-04-25 13:56:05 +00006863 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006864
6865 processKey(mapper, BTN_TOOL_PEN, 1);
6866 processMove(mapper, 100, 200);
6867 processSync(mapper);
6868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6869 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006870 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006871 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6872 ASSERT_TRUE(fakePointerController->isPointerShown());
6873 ASSERT_NO_FATAL_FAILURE(
6874 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6875}
6876
6877TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6878 std::shared_ptr<FakePointerController> fakePointerController =
6879 std::make_shared<FakePointerController>();
6880 addConfigurationProperty("touch.deviceType", "touchScreen");
6881 prepareDisplay(ui::ROTATION_0);
6882 prepareButtons();
6883 prepareAxes(POSITION);
6884 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6885 mFakePolicy->setPointerController(fakePointerController);
6886 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singh56adebc2023-04-25 13:56:05 +00006887 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006888
6889 processKey(mapper, BTN_TOOL_PEN, 1);
6890 processMove(mapper, 100, 200);
6891 processSync(mapper);
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6893 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006894 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006895 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6896 ASSERT_FALSE(fakePointerController->isPointerShown());
6897}
6898
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006899TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6900 // Initialize the device without setting device source to touch navigation.
6901 addConfigurationProperty("touch.deviceType", "touchScreen");
6902 prepareDisplay(ui::ROTATION_0);
6903 prepareButtons();
6904 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006905 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006906
6907 // Ensure that the device is created as a touchscreen, not touch navigation.
6908 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6909
6910 // Add device type association after the device was created.
6911 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6912
6913 // Send update to the mapper.
6914 std::list<NotifyArgs> unused2 =
6915 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006916 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006917
6918 // Check whether device type update was successful.
6919 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6920}
6921
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006922TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6923 // Initialize the device without setting device source to touch navigation.
6924 addConfigurationProperty("touch.deviceType", "touchScreen");
6925 prepareDisplay(ui::ROTATION_0);
6926 prepareButtons();
6927 prepareAxes(POSITION);
6928 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6929
6930 // Set a physical frame in the display viewport.
6931 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6932 viewport->physicalLeft = 0;
6933 viewport->physicalTop = 0;
6934 viewport->physicalRight = DISPLAY_WIDTH / 2;
6935 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6936 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006937 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006938
Arpit Singh56adebc2023-04-25 13:56:05 +00006939 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006940
6941 // Hovering inside the physical frame produces events.
6942 processKey(mapper, BTN_TOOL_PEN, 1);
6943 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6944 processSync(mapper);
6945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6946 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6948 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6949
6950 // Leaving the physical frame ends the hovering gesture.
6951 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6952 processSync(mapper);
6953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6954 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6955
6956 // Moving outside the physical frame does not produce events.
6957 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6958 processSync(mapper);
6959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6960
6961 // Re-entering the physical frame produces events.
6962 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6963 processSync(mapper);
6964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6965 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6967 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6968}
6969
Prabir Pradhan5632d622021-09-06 07:57:20 -07006970// --- TouchDisplayProjectionTest ---
6971
6972class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6973public:
6974 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6975 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6976 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006977 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6978 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6979 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006980 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006981 auto rotatedWidth = naturalDisplayWidth;
6982 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006983 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006984 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006985 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006986 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006987 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006988 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006989 inverseRotationFlags = ui::Transform::ROT_180;
6990 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006991 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006992 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006993 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006994 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006995 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006996 inverseRotationFlags = ui::Transform::ROT_0;
6997 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006998 }
6999
Prabir Pradhana9df3162022-12-05 23:57:27 +00007000 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007001 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7002
7003 std::optional<DisplayViewport> internalViewport =
7004 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7005 DisplayViewport& v = *internalViewport;
7006 v.displayId = DISPLAY_ID;
7007 v.orientation = orientation;
7008
7009 v.logicalLeft = 0;
7010 v.logicalTop = 0;
7011 v.logicalRight = 100;
7012 v.logicalBottom = 100;
7013
7014 v.physicalLeft = rotatedPhysicalDisplay.left;
7015 v.physicalTop = rotatedPhysicalDisplay.top;
7016 v.physicalRight = rotatedPhysicalDisplay.right;
7017 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7018
Prabir Pradhana9df3162022-12-05 23:57:27 +00007019 v.deviceWidth = rotatedWidth;
7020 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007021
7022 v.isActive = true;
7023 v.uniqueId = UNIQUE_ID;
7024 v.type = ViewportType::INTERNAL;
7025 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007026 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007027 }
7028
7029 void assertReceivedMove(const Point& point) {
7030 NotifyMotionArgs motionArgs;
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7032 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007033 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7035 1, 0, 0, 0, 0, 0, 0, 0));
7036 }
7037};
7038
7039TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7040 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007041 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007042
7043 prepareButtons();
7044 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00007045 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007046
7047 NotifyMotionArgs motionArgs;
7048
7049 // Configure the DisplayViewport such that the logical display maps to a subsection of
7050 // the display panel called the physical display. Here, the physical display is bounded by the
7051 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7052 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7053 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7054 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7055
Michael Wrighta9cf4192022-12-01 23:46:39 +00007056 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007057 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7058
7059 // Touches outside the physical display should be ignored, and should not generate any
7060 // events. Ensure touches at the following points that lie outside of the physical display
7061 // area do not generate any events.
7062 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7063 processDown(mapper, toRawX(point.x), toRawY(point.y));
7064 processSync(mapper);
7065 processUp(mapper);
7066 processSync(mapper);
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7068 << "Unexpected event generated for touch outside physical display at point: "
7069 << point.x << ", " << point.y;
7070 }
7071 }
7072}
7073
7074TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7075 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007076 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007077
7078 prepareButtons();
7079 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00007080 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007081
7082 NotifyMotionArgs motionArgs;
7083
7084 // Configure the DisplayViewport such that the logical display maps to a subsection of
7085 // the display panel called the physical display. Here, the physical display is bounded by the
7086 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7087 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7088
Michael Wrighta9cf4192022-12-01 23:46:39 +00007089 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007090 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7091
7092 // Touches that start outside the physical display should be ignored until it enters the
7093 // physical display bounds, at which point it should generate a down event. Start a touch at
7094 // the point (5, 100), which is outside the physical display bounds.
7095 static const Point kOutsidePoint{5, 100};
7096 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7097 processSync(mapper);
7098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7099
7100 // Move the touch into the physical display area. This should generate a pointer down.
7101 processMove(mapper, toRawX(11), toRawY(21));
7102 processSync(mapper);
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7104 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007105 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007106 ASSERT_NO_FATAL_FAILURE(
7107 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7108
7109 // Move the touch inside the physical display area. This should generate a pointer move.
7110 processMove(mapper, toRawX(69), toRawY(159));
7111 processSync(mapper);
7112 assertReceivedMove({69, 159});
7113
7114 // Move outside the physical display area. Since the pointer is already down, this should
7115 // now continue generating events.
7116 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7117 processSync(mapper);
7118 assertReceivedMove(kOutsidePoint);
7119
7120 // Release. This should generate a pointer up.
7121 processUp(mapper);
7122 processSync(mapper);
7123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7124 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7126 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7127
7128 // Ensure no more events were generated.
7129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7131 }
7132}
7133
Prabir Pradhana9df3162022-12-05 23:57:27 +00007134// --- TouchscreenPrecisionTests ---
7135
7136// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7137// in various orientations and with different display rotations. We configure the touchscreen to
7138// have a higher resolution than that of the display by an integer scale factor in each axis so that
7139// we can enforce that coordinates match precisely as expected.
7140class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7141 public ::testing::WithParamInterface<ui::Rotation> {
7142public:
7143 void SetUp() override {
7144 SingleTouchInputMapperTest::SetUp();
7145
7146 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7147 // four times the resolution of the display in the Y axis.
7148 prepareButtons();
7149 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007150 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7151 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007152 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007153 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7154 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007155 }
7156
7157 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7158 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7159 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7160 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7161
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007162 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7163 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7164
7165 static const int32_t PRECISION_RAW_X_FLAT = 16;
7166 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7167
7168 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7169 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7170
Prabir Pradhana9df3162022-12-05 23:57:27 +00007171 static const std::array<Point, 4> kRawCorners;
7172};
7173
7174const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7175 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7176 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7177 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7178 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7179}};
7180
7181// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7182// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7183// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7184TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7185 enum class Orientation {
7186 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7187 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7188 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7189 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7190 ftl_last = ORIENTATION_270,
7191 };
7192 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7193 Orientation::ORIENTATION_270;
7194 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7195 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7196 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7197 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7198 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7199 };
7200
7201 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7202
7203 // Configure the touchscreen as being installed in the one of the four different orientations
7204 // relative to the display.
7205 addConfigurationProperty("touch.deviceType", "touchScreen");
7206 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7207 prepareDisplay(ui::ROTATION_0);
7208
Arpit Singh56adebc2023-04-25 13:56:05 +00007209 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007210
7211 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7212 // orientations of either 90 or 270) this means the display's natural resolution will be
7213 // flipped.
7214 const bool displayRotated =
7215 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7216 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7217 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7218 const Rect physicalFrame{0, 0, width, height};
7219 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7220
7221 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7222 const float expectedPrecisionX = displayRotated ? 4 : 2;
7223 const float expectedPrecisionY = displayRotated ? 2 : 4;
7224
7225 // Test all four corners.
7226 for (int i = 0; i < 4; i++) {
7227 const auto& raw = kRawCorners[i];
7228 processDown(mapper, raw.x, raw.y);
7229 processSync(mapper);
7230 const auto& expected = expectedPoints[i];
7231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7232 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7233 WithCoords(expected.x, expected.y),
7234 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7235 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7236 << "with touchscreen orientation "
7237 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7238 << expected.x << ", " << expected.y << ").";
7239 processUp(mapper);
7240 processSync(mapper);
7241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7242 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7243 WithCoords(expected.x, expected.y))));
7244 }
7245}
7246
Prabir Pradhan82687402022-12-06 01:32:53 +00007247TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7248 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7249 kMappedCorners = {
7250 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7251 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7252 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7253 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7254 };
7255
7256 const ui::Rotation displayRotation = GetParam();
7257
7258 addConfigurationProperty("touch.deviceType", "touchScreen");
7259 prepareDisplay(displayRotation);
7260
Arpit Singh56adebc2023-04-25 13:56:05 +00007261 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007262
7263 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7264
7265 // Test all four corners.
7266 for (int i = 0; i < 4; i++) {
7267 const auto& expected = expectedPoints[i];
7268 const auto& raw = kRawCorners[i];
7269 processDown(mapper, raw.x, raw.y);
7270 processSync(mapper);
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7272 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7273 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7274 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7275 << "with display rotation " << ui::toCString(displayRotation)
7276 << ", expected point (" << expected.x << ", " << expected.y << ").";
7277 processUp(mapper);
7278 processSync(mapper);
7279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7280 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7281 WithCoords(expected.x, expected.y))));
7282 }
7283}
7284
Prabir Pradhan3e798762022-12-02 21:02:11 +00007285TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7286 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7287 kMappedCorners = {
7288 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7289 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7290 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7291 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7292 };
7293
7294 const ui::Rotation displayRotation = GetParam();
7295
7296 addConfigurationProperty("touch.deviceType", "touchScreen");
7297 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7298
Arpit Singh56adebc2023-04-25 13:56:05 +00007299 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007300
7301 // Ori 270, so width and height swapped
7302 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7303 prepareDisplay(displayRotation);
7304 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7305
7306 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7307
7308 // Test all four corners.
7309 for (int i = 0; i < 4; i++) {
7310 const auto& expected = expectedPoints[i];
7311 const auto& raw = kRawCorners[i];
7312 processDown(mapper, raw.x, raw.y);
7313 processSync(mapper);
7314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7315 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7316 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7317 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7318 << "with display rotation " << ui::toCString(displayRotation)
7319 << ", expected point (" << expected.x << ", " << expected.y << ").";
7320 processUp(mapper);
7321 processSync(mapper);
7322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7323 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7324 WithCoords(expected.x, expected.y))));
7325 }
7326}
7327
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007328TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7329 const ui::Rotation displayRotation = GetParam();
7330
7331 addConfigurationProperty("touch.deviceType", "touchScreen");
7332 prepareDisplay(displayRotation);
7333
7334 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singh56adebc2023-04-25 13:56:05 +00007335 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007336
7337 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7338 // MotionRanges use display pixels as their units
7339 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7340 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7341
7342 // The MotionRanges should be oriented in the rotated display's coordinate space
7343 const bool displayRotated =
7344 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7345
7346 constexpr float MAX_X = 479.5;
7347 constexpr float MAX_Y = 799.75;
7348 EXPECT_EQ(xRange->min, 0.f);
7349 EXPECT_EQ(yRange->min, 0.f);
7350 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7351 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7352
7353 EXPECT_EQ(xRange->flat, 8.f);
7354 EXPECT_EQ(yRange->flat, 8.f);
7355
7356 EXPECT_EQ(xRange->fuzz, 2.f);
7357 EXPECT_EQ(yRange->fuzz, 2.f);
7358
7359 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7360 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7361}
7362
Prabir Pradhana9df3162022-12-05 23:57:27 +00007363// Run the precision tests for all rotations.
7364INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7365 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7366 ui::ROTATION_270),
7367 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7368 return ftl::enum_string(testParamInfo.param);
7369 });
7370
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007371// --- ExternalStylusFusionTest ---
7372
7373class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7374public:
7375 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7376 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007377 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007378 prepareButtons();
7379 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00007380 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007381
7382 mStylusState.when = ARBITRARY_TIME;
7383 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007384 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007385 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007386 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007387 processExternalStylusState(mapper);
7388 return mapper;
7389 }
7390
7391 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7392 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7393 for (const NotifyArgs& args : generatedArgs) {
7394 mFakeListener->notify(args);
7395 }
7396 // Loop the reader to flush the input listener queue.
7397 mReader->loopOnce();
7398 return generatedArgs;
7399 }
7400
7401protected:
7402 StylusState mStylusState{};
7403 static constexpr uint32_t EXPECTED_SOURCE =
7404 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7405
7406 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7407 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007408 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007409
7410 // The first pointer is withheld.
7411 processDown(mapper, 100, 200);
7412 processSync(mapper);
7413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7414 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7415 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7416
7417 // The external stylus reports pressure. The withheld finger pointer is released as a
7418 // stylus.
7419 mStylusState.pressure = 1.f;
7420 processExternalStylusState(mapper);
7421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7422 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7423 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7424
7425 // Subsequent pointer events are not withheld.
7426 processMove(mapper, 101, 201);
7427 processSync(mapper);
7428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7429 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7430
7431 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7433 }
7434
7435 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7436 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7437
7438 // Releasing the touch pointer ends the gesture.
7439 processUp(mapper);
7440 processSync(mapper);
7441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7442 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007443 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007444
7445 mStylusState.pressure = 0.f;
7446 processExternalStylusState(mapper);
7447 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7449 }
7450
7451 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7452 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007453 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007454
7455 // The first pointer is withheld when an external stylus is connected,
7456 // and a timeout is requested.
7457 processDown(mapper, 100, 200);
7458 processSync(mapper);
7459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7460 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7461 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7462
7463 // If the timeout expires early, it is requested again.
7464 handleTimeout(mapper, ARBITRARY_TIME + 1);
7465 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7466 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7467
7468 // When the timeout expires, the withheld touch is released as a finger pointer.
7469 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7471 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7472
7473 // Subsequent pointer events are not withheld.
7474 processMove(mapper, 101, 201);
7475 processSync(mapper);
7476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7477 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7478 processUp(mapper);
7479 processSync(mapper);
7480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7481 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7482
7483 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7485 }
7486
7487private:
7488 InputDeviceInfo mExternalStylusDeviceInfo{};
7489};
7490
7491TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7492 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7493 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7494}
7495
7496TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7497 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7498 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7499}
7500
7501TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7502 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7503 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7504}
7505
7506// Test a successful stylus fusion gesture where the pressure is reported by the external
7507// before the touch is reported by the touchscreen.
7508TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7509 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7510 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007511 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007512
7513 // The external stylus reports pressure first. It is ignored for now.
7514 mStylusState.pressure = 1.f;
7515 processExternalStylusState(mapper);
7516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7517 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7518
7519 // When the touch goes down afterwards, it is reported as a stylus pointer.
7520 processDown(mapper, 100, 200);
7521 processSync(mapper);
7522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7523 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7524 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7525
7526 processMove(mapper, 101, 201);
7527 processSync(mapper);
7528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7529 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7530 processUp(mapper);
7531 processSync(mapper);
7532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7533 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7534
7535 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7537}
7538
7539TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7540 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7541
7542 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7543 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7544
7545 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7546 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7547 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7548 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7549}
7550
7551TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7552 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7553 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007554 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007555
7556 mStylusState.pressure = 0.8f;
7557 processExternalStylusState(mapper);
7558 processDown(mapper, 100, 200);
7559 processSync(mapper);
7560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7561 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7562 WithPressure(0.8f))));
7563 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7564
7565 // The external stylus reports a pressure change. We wait for some time for a touch event.
7566 mStylusState.pressure = 0.6f;
7567 processExternalStylusState(mapper);
7568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7569 ASSERT_NO_FATAL_FAILURE(
7570 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7571
7572 // If a touch is reported within the timeout, it reports the updated pressure.
7573 processMove(mapper, 101, 201);
7574 processSync(mapper);
7575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7576 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7577 WithPressure(0.6f))));
7578 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7579
7580 // There is another pressure change.
7581 mStylusState.pressure = 0.5f;
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 pressure.
7589 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7591 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7592 WithPressure(0.5f))));
7593
7594 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7595 // repeated indefinitely.
7596 mStylusState.pressure = 0.0f;
7597 processExternalStylusState(mapper);
7598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7599 ASSERT_NO_FATAL_FAILURE(
7600 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7601 processMove(mapper, 102, 202);
7602 processSync(mapper);
7603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7604 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7605 WithPressure(0.5f))));
7606 processMove(mapper, 103, 203);
7607 processSync(mapper);
7608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7609 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7610 WithPressure(0.5f))));
7611
7612 processUp(mapper);
7613 processSync(mapper);
7614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7615 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007616 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007617
7618 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7620}
7621
7622TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7623 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7624 auto source = WithSource(EXPECTED_SOURCE);
7625
7626 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007627 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007628 processExternalStylusState(mapper);
7629 processDown(mapper, 100, 200);
7630 processSync(mapper);
7631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7632 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007633 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007634 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7635
7636 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007637 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007638 processExternalStylusState(mapper);
7639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7640 ASSERT_NO_FATAL_FAILURE(
7641 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7642
7643 // If a touch is reported within the timeout, it reports the updated pressure.
7644 processMove(mapper, 101, 201);
7645 processSync(mapper);
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7647 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007648 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007649 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7650
7651 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007652 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007653 processExternalStylusState(mapper);
7654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7655 ASSERT_NO_FATAL_FAILURE(
7656 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7657
7658 // If a touch is not reported within the timeout, a move event is generated to report
7659 // the new tool type.
7660 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7662 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007663 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007664
7665 processUp(mapper);
7666 processSync(mapper);
7667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7668 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007669 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007670
7671 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7673}
7674
7675TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7676 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7677 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007678 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007679
7680 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7681
7682 // The external stylus reports a button change. We wait for some time for a touch event.
7683 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7684 processExternalStylusState(mapper);
7685 ASSERT_NO_FATAL_FAILURE(
7686 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7687
7688 // If a touch is reported within the timeout, it reports the updated button state.
7689 processMove(mapper, 101, 201);
7690 processSync(mapper);
7691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7692 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7693 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7695 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7696 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7697 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7698
7699 // The button is now released.
7700 mStylusState.buttons = 0;
7701 processExternalStylusState(mapper);
7702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7703 ASSERT_NO_FATAL_FAILURE(
7704 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7705
7706 // If a touch is not reported within the timeout, a move event is generated to report
7707 // the new button state.
7708 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7710 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7711 WithButtonState(0))));
7712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007713 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7714 WithButtonState(0))));
7715
7716 processUp(mapper);
7717 processSync(mapper);
7718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007719 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7720
7721 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7723}
7724
Michael Wrightd02c5b62014-02-10 15:10:22 -08007725// --- MultiTouchInputMapperTest ---
7726
7727class MultiTouchInputMapperTest : public TouchInputMapperTest {
7728protected:
7729 void prepareAxes(int axes);
7730
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007731 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7732 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7733 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7734 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7735 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7736 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7737 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7738 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7739 void processId(MultiTouchInputMapper& mapper, int32_t id);
7740 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7741 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7742 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007743 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007744 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007745 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7746 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007747};
7748
7749void MultiTouchInputMapperTest::prepareAxes(int axes) {
7750 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007751 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7752 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007753 }
7754 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007755 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7756 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007757 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007758 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7759 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007760 }
7761 }
7762 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007763 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7764 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007765 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007766 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007767 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007768 }
7769 }
7770 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007771 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7772 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007773 }
7774 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007775 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7776 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007777 }
7778 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007779 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7780 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007781 }
7782 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007783 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7784 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007785 }
7786 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007787 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7788 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007789 }
7790 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007791 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007792 }
7793}
7794
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007795void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7796 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007799}
7800
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007801void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7802 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007804}
7805
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007806void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7807 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007809}
7810
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007811void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007813}
7814
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007815void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007817}
7818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007819void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7820 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007822}
7823
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007824void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007826}
7827
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007828void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007830}
7831
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007832void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007834}
7835
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007836void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007838}
7839
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007840void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007842}
7843
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007844void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7845 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007847}
7848
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007849void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7850 int32_t value) {
7851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7852 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7853}
7854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007855void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007857}
7858
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007859void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7860 nsecs_t readTime) {
7861 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007862}
7863
Michael Wrightd02c5b62014-02-10 15:10:22 -08007864TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007865 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007866 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007867 prepareAxes(POSITION);
7868 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00007869 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007870
arthurhungdcef2dc2020-08-11 14:47:50 +08007871 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007872
7873 NotifyMotionArgs motionArgs;
7874
7875 // Two fingers down at once.
7876 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7877 processPosition(mapper, x1, y1);
7878 processMTSync(mapper);
7879 processPosition(mapper, x2, y2);
7880 processMTSync(mapper);
7881 processSync(mapper);
7882
7883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7884 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7885 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7886 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7887 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7888 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7889 ASSERT_EQ(0, motionArgs.flags);
7890 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7891 ASSERT_EQ(0, motionArgs.buttonState);
7892 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007893 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007894 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007895 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7897 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7898 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7899 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7900 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7901
7902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7903 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7904 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7905 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7906 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007907 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007908 ASSERT_EQ(0, motionArgs.flags);
7909 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7910 ASSERT_EQ(0, motionArgs.buttonState);
7911 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007912 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007913 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007914 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007915 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007916 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7918 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7920 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7921 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7922 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7923 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7924
7925 // Move.
7926 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7927 processPosition(mapper, x1, y1);
7928 processMTSync(mapper);
7929 processPosition(mapper, x2, y2);
7930 processMTSync(mapper);
7931 processSync(mapper);
7932
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7934 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7935 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7936 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7937 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7939 ASSERT_EQ(0, motionArgs.flags);
7940 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7941 ASSERT_EQ(0, motionArgs.buttonState);
7942 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007943 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007944 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007945 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007946 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007947 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7949 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7951 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7952 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7953 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7954 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7955
7956 // First finger up.
7957 x2 += 15; y2 -= 20;
7958 processPosition(mapper, x2, y2);
7959 processMTSync(mapper);
7960 processSync(mapper);
7961
7962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7963 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7964 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7965 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7966 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007967 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007968 ASSERT_EQ(0, motionArgs.flags);
7969 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7970 ASSERT_EQ(0, motionArgs.buttonState);
7971 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007972 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007973 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007974 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007975 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007976 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7978 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7980 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7981 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7982 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7983 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7984
7985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7986 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7987 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7988 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7989 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7991 ASSERT_EQ(0, motionArgs.flags);
7992 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7993 ASSERT_EQ(0, motionArgs.buttonState);
7994 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007995 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007996 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007997 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7999 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8000 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8001 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8002 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8003
8004 // Move.
8005 x2 += 20; y2 -= 25;
8006 processPosition(mapper, x2, y2);
8007 processMTSync(mapper);
8008 processSync(mapper);
8009
8010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8011 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8012 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8013 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8014 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8016 ASSERT_EQ(0, motionArgs.flags);
8017 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8018 ASSERT_EQ(0, motionArgs.buttonState);
8019 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008020 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008021 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008022 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8024 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8025 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8026 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8027 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8028
8029 // New finger down.
8030 int32_t x3 = 700, y3 = 300;
8031 processPosition(mapper, x2, y2);
8032 processMTSync(mapper);
8033 processPosition(mapper, x3, y3);
8034 processMTSync(mapper);
8035 processSync(mapper);
8036
8037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8038 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8039 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8040 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8041 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008042 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008043 ASSERT_EQ(0, motionArgs.flags);
8044 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8045 ASSERT_EQ(0, motionArgs.buttonState);
8046 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008047 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008048 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008049 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008050 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008051 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8053 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8055 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8056 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8057 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8058 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8059
8060 // Second finger up.
8061 x3 += 30; y3 -= 20;
8062 processPosition(mapper, x3, y3);
8063 processMTSync(mapper);
8064 processSync(mapper);
8065
8066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8067 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8068 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8069 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8070 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008071 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 ASSERT_EQ(0, motionArgs.flags);
8073 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8074 ASSERT_EQ(0, motionArgs.buttonState);
8075 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008076 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008078 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008079 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008080 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8082 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8084 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8085 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8086 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8087 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8088
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8090 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8091 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8092 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8093 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8095 ASSERT_EQ(0, motionArgs.flags);
8096 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8097 ASSERT_EQ(0, motionArgs.buttonState);
8098 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008099 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100 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_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8103 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8104 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8105 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8106 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8107
8108 // Last finger up.
8109 processMTSync(mapper);
8110 processSync(mapper);
8111
8112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8113 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8114 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8115 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8116 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8117 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8118 ASSERT_EQ(0, motionArgs.flags);
8119 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8120 ASSERT_EQ(0, motionArgs.buttonState);
8121 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008122 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008123 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008124 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8126 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8127 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8128 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8129 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8130
8131 // Should not have sent any more keys or motions.
8132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8134}
8135
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008136TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8137 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008138 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008139
8140 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8141 /*fuzz*/ 0, /*resolution*/ 10);
8142 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8143 /*fuzz*/ 0, /*resolution*/ 11);
8144 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8145 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8146 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8147 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8148 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8149 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8150 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8151 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8152
Arpit Singh56adebc2023-04-25 13:56:05 +00008153 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008154
8155 // X and Y axes
8156 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8157 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8158 // Touch major and minor
8159 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8160 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8161 // Tool major and minor
8162 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8163 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8164}
8165
8166TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8167 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008168 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008169
8170 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8171 /*fuzz*/ 0, /*resolution*/ 10);
8172 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8173 /*fuzz*/ 0, /*resolution*/ 11);
8174
8175 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8176
Arpit Singh56adebc2023-04-25 13:56:05 +00008177 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008178
8179 // Touch major and minor
8180 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8181 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8182 // Tool major and minor
8183 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8184 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8185}
8186
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008188 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008189 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008190 prepareAxes(POSITION | ID);
8191 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00008192 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008193
arthurhungdcef2dc2020-08-11 14:47:50 +08008194 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008195
8196 NotifyMotionArgs motionArgs;
8197
8198 // Two fingers down at once.
8199 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8200 processPosition(mapper, x1, y1);
8201 processId(mapper, 1);
8202 processMTSync(mapper);
8203 processPosition(mapper, x2, y2);
8204 processId(mapper, 2);
8205 processMTSync(mapper);
8206 processSync(mapper);
8207
8208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8209 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008210 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008211 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008212 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8214 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8215
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008217 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008218 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008219 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008220 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008221 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008222 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8224 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8226 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8227
8228 // Move.
8229 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8230 processPosition(mapper, x1, y1);
8231 processId(mapper, 1);
8232 processMTSync(mapper);
8233 processPosition(mapper, x2, y2);
8234 processId(mapper, 2);
8235 processMTSync(mapper);
8236 processSync(mapper);
8237
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008240 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008241 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008242 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008244 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8246 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8248 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8249
8250 // First finger up.
8251 x2 += 15; y2 -= 20;
8252 processPosition(mapper, x2, y2);
8253 processId(mapper, 2);
8254 processMTSync(mapper);
8255 processSync(mapper);
8256
8257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008258 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008259 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008260 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008261 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008262 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008263 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8265 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8267 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8268
8269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008271 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008272 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008273 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8275 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8276
8277 // Move.
8278 x2 += 20; y2 -= 25;
8279 processPosition(mapper, x2, y2);
8280 processId(mapper, 2);
8281 processMTSync(mapper);
8282 processSync(mapper);
8283
8284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008286 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008287 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008288 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8290 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8291
8292 // New finger down.
8293 int32_t x3 = 700, y3 = 300;
8294 processPosition(mapper, x2, y2);
8295 processId(mapper, 2);
8296 processMTSync(mapper);
8297 processPosition(mapper, x3, y3);
8298 processId(mapper, 3);
8299 processMTSync(mapper);
8300 processSync(mapper);
8301
8302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008303 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008304 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008305 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008306 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008307 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008308 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8310 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8311 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8312 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8313
8314 // Second finger up.
8315 x3 += 30; y3 -= 20;
8316 processPosition(mapper, x3, y3);
8317 processId(mapper, 3);
8318 processMTSync(mapper);
8319 processSync(mapper);
8320
8321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008322 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008323 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008324 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008325 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008326 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008327 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8329 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8331 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8332
8333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008335 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008336 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008337 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8339 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8340
8341 // Last finger up.
8342 processMTSync(mapper);
8343 processSync(mapper);
8344
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8346 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008347 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008348 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008349 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8351 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8352
8353 // Should not have sent any more keys or motions.
8354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8356}
8357
8358TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008359 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008360 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008361 prepareAxes(POSITION | ID | SLOT);
8362 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00008363 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008364
arthurhungdcef2dc2020-08-11 14:47:50 +08008365 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008366
8367 NotifyMotionArgs motionArgs;
8368
8369 // Two fingers down at once.
8370 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8371 processPosition(mapper, x1, y1);
8372 processId(mapper, 1);
8373 processSlot(mapper, 1);
8374 processPosition(mapper, x2, y2);
8375 processId(mapper, 2);
8376 processSync(mapper);
8377
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8379 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008380 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008381 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008382 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8384 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8385
8386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008387 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008388 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008389 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008390 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008391 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008392 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8394 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8396 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8397
8398 // Move.
8399 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8400 processSlot(mapper, 0);
8401 processPosition(mapper, x1, y1);
8402 processSlot(mapper, 1);
8403 processPosition(mapper, x2, y2);
8404 processSync(mapper);
8405
8406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008408 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008409 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008410 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008411 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008412 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8414 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8416 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8417
8418 // First finger up.
8419 x2 += 15; y2 -= 20;
8420 processSlot(mapper, 0);
8421 processId(mapper, -1);
8422 processSlot(mapper, 1);
8423 processPosition(mapper, x2, y2);
8424 processSync(mapper);
8425
8426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008427 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008428 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008429 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008430 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008431 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008432 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8434 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8436 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8437
8438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008440 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008441 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008442 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8444 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8445
8446 // Move.
8447 x2 += 20; y2 -= 25;
8448 processPosition(mapper, x2, y2);
8449 processSync(mapper);
8450
8451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008453 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008454 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008455 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8457 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8458
8459 // New finger down.
8460 int32_t x3 = 700, y3 = 300;
8461 processPosition(mapper, x2, y2);
8462 processSlot(mapper, 0);
8463 processId(mapper, 3);
8464 processPosition(mapper, x3, y3);
8465 processSync(mapper);
8466
8467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008468 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008469 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008470 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008471 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008472 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008473 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8475 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8477 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8478
8479 // Second finger up.
8480 x3 += 30; y3 -= 20;
8481 processSlot(mapper, 1);
8482 processId(mapper, -1);
8483 processSlot(mapper, 0);
8484 processPosition(mapper, x3, y3);
8485 processSync(mapper);
8486
8487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008488 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008489 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008490 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008491 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008492 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008493 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8495 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8497 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8498
8499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8500 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008501 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008502 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008503 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8505 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8506
8507 // Last finger up.
8508 processId(mapper, -1);
8509 processSync(mapper);
8510
8511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8512 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008513 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008514 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008515 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8517 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8518
8519 // Should not have sent any more keys or motions.
8520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8522}
8523
8524TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008525 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008526 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008527 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singh56adebc2023-04-25 13:56:05 +00008528 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008529
8530 // These calculations are based on the input device calibration documentation.
8531 int32_t rawX = 100;
8532 int32_t rawY = 200;
8533 int32_t rawTouchMajor = 7;
8534 int32_t rawTouchMinor = 6;
8535 int32_t rawToolMajor = 9;
8536 int32_t rawToolMinor = 8;
8537 int32_t rawPressure = 11;
8538 int32_t rawDistance = 0;
8539 int32_t rawOrientation = 3;
8540 int32_t id = 5;
8541
8542 float x = toDisplayX(rawX);
8543 float y = toDisplayY(rawY);
8544 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8545 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8546 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8547 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8548 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8549 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8550 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8551 float distance = float(rawDistance);
8552
8553 processPosition(mapper, rawX, rawY);
8554 processTouchMajor(mapper, rawTouchMajor);
8555 processTouchMinor(mapper, rawTouchMinor);
8556 processToolMajor(mapper, rawToolMajor);
8557 processToolMinor(mapper, rawToolMinor);
8558 processPressure(mapper, rawPressure);
8559 processOrientation(mapper, rawOrientation);
8560 processDistance(mapper, rawDistance);
8561 processId(mapper, id);
8562 processMTSync(mapper);
8563 processSync(mapper);
8564
8565 NotifyMotionArgs args;
8566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8567 ASSERT_EQ(0, args.pointerProperties[0].id);
8568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8569 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8570 orientation, distance));
8571}
8572
8573TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008574 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008575 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008576 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8577 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singh56adebc2023-04-25 13:56:05 +00008578 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008579
8580 // These calculations are based on the input device calibration documentation.
8581 int32_t rawX = 100;
8582 int32_t rawY = 200;
8583 int32_t rawTouchMajor = 140;
8584 int32_t rawTouchMinor = 120;
8585 int32_t rawToolMajor = 180;
8586 int32_t rawToolMinor = 160;
8587
8588 float x = toDisplayX(rawX);
8589 float y = toDisplayY(rawY);
8590 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8591 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8592 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8593 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8594 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8595
8596 processPosition(mapper, rawX, rawY);
8597 processTouchMajor(mapper, rawTouchMajor);
8598 processTouchMinor(mapper, rawTouchMinor);
8599 processToolMajor(mapper, rawToolMajor);
8600 processToolMinor(mapper, rawToolMinor);
8601 processMTSync(mapper);
8602 processSync(mapper);
8603
8604 NotifyMotionArgs args;
8605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8607 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8608}
8609
8610TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008611 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008612 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008613 prepareAxes(POSITION | TOUCH | TOOL);
8614 addConfigurationProperty("touch.size.calibration", "diameter");
8615 addConfigurationProperty("touch.size.scale", "10");
8616 addConfigurationProperty("touch.size.bias", "160");
8617 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singh56adebc2023-04-25 13:56:05 +00008618 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008619
8620 // These calculations are based on the input device calibration documentation.
8621 // Note: We only provide a single common touch/tool value because the device is assumed
8622 // not to emit separate values for each pointer (isSummed = 1).
8623 int32_t rawX = 100;
8624 int32_t rawY = 200;
8625 int32_t rawX2 = 150;
8626 int32_t rawY2 = 250;
8627 int32_t rawTouchMajor = 5;
8628 int32_t rawToolMajor = 8;
8629
8630 float x = toDisplayX(rawX);
8631 float y = toDisplayY(rawY);
8632 float x2 = toDisplayX(rawX2);
8633 float y2 = toDisplayY(rawY2);
8634 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8635 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8636 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8637
8638 processPosition(mapper, rawX, rawY);
8639 processTouchMajor(mapper, rawTouchMajor);
8640 processToolMajor(mapper, rawToolMajor);
8641 processMTSync(mapper);
8642 processPosition(mapper, rawX2, rawY2);
8643 processTouchMajor(mapper, rawTouchMajor);
8644 processToolMajor(mapper, rawToolMajor);
8645 processMTSync(mapper);
8646 processSync(mapper);
8647
8648 NotifyMotionArgs args;
8649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8650 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8651
8652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008653 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008654 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8656 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8658 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8659}
8660
8661TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008662 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008663 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008664 prepareAxes(POSITION | TOUCH | TOOL);
8665 addConfigurationProperty("touch.size.calibration", "area");
8666 addConfigurationProperty("touch.size.scale", "43");
8667 addConfigurationProperty("touch.size.bias", "3");
Arpit Singh56adebc2023-04-25 13:56:05 +00008668 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008669
8670 // These calculations are based on the input device calibration documentation.
8671 int32_t rawX = 100;
8672 int32_t rawY = 200;
8673 int32_t rawTouchMajor = 5;
8674 int32_t rawToolMajor = 8;
8675
8676 float x = toDisplayX(rawX);
8677 float y = toDisplayY(rawY);
8678 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8679 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8680 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8681
8682 processPosition(mapper, rawX, rawY);
8683 processTouchMajor(mapper, rawTouchMajor);
8684 processToolMajor(mapper, rawToolMajor);
8685 processMTSync(mapper);
8686 processSync(mapper);
8687
8688 NotifyMotionArgs args;
8689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8691 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8692}
8693
8694TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008695 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008696 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008697 prepareAxes(POSITION | PRESSURE);
8698 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8699 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singh56adebc2023-04-25 13:56:05 +00008700 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008701
Michael Wrightaa449c92017-12-13 21:21:43 +00008702 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008703 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008704 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8705 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8706 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8707
Michael Wrightd02c5b62014-02-10 15:10:22 -08008708 // These calculations are based on the input device calibration documentation.
8709 int32_t rawX = 100;
8710 int32_t rawY = 200;
8711 int32_t rawPressure = 60;
8712
8713 float x = toDisplayX(rawX);
8714 float y = toDisplayY(rawY);
8715 float pressure = float(rawPressure) * 0.01f;
8716
8717 processPosition(mapper, rawX, rawY);
8718 processPressure(mapper, rawPressure);
8719 processMTSync(mapper);
8720 processSync(mapper);
8721
8722 NotifyMotionArgs args;
8723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8725 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8726}
8727
8728TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008729 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008730 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008731 prepareAxes(POSITION | ID | SLOT);
Arpit Singh56adebc2023-04-25 13:56:05 +00008732 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008733
8734 NotifyMotionArgs motionArgs;
8735 NotifyKeyArgs keyArgs;
8736
8737 processId(mapper, 1);
8738 processPosition(mapper, 100, 200);
8739 processSync(mapper);
8740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8741 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8742 ASSERT_EQ(0, motionArgs.buttonState);
8743
8744 // press BTN_LEFT, release BTN_LEFT
8745 processKey(mapper, BTN_LEFT, 1);
8746 processSync(mapper);
8747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8749 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8750
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8752 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8753 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8754
Michael Wrightd02c5b62014-02-10 15:10:22 -08008755 processKey(mapper, BTN_LEFT, 0);
8756 processSync(mapper);
8757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008758 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008759 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008760
8761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008763 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008764
8765 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8766 processKey(mapper, BTN_RIGHT, 1);
8767 processKey(mapper, BTN_MIDDLE, 1);
8768 processSync(mapper);
8769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8770 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8771 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8772 motionArgs.buttonState);
8773
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8775 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8776 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8777
8778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8780 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8781 motionArgs.buttonState);
8782
Michael Wrightd02c5b62014-02-10 15:10:22 -08008783 processKey(mapper, BTN_RIGHT, 0);
8784 processSync(mapper);
8785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008786 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008787 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008788
8789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008791 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008792
8793 processKey(mapper, BTN_MIDDLE, 0);
8794 processSync(mapper);
8795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008796 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008797 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008798
8799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008801 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008802
8803 // press BTN_BACK, release BTN_BACK
8804 processKey(mapper, BTN_BACK, 1);
8805 processSync(mapper);
8806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8807 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8808 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008809
Michael Wrightd02c5b62014-02-10 15:10:22 -08008810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008812 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8813
8814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8815 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8816 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008817
8818 processKey(mapper, BTN_BACK, 0);
8819 processSync(mapper);
8820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008821 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008822 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008823
8824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008826 ASSERT_EQ(0, motionArgs.buttonState);
8827
Michael Wrightd02c5b62014-02-10 15:10:22 -08008828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8829 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8830 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8831
8832 // press BTN_SIDE, release BTN_SIDE
8833 processKey(mapper, BTN_SIDE, 1);
8834 processSync(mapper);
8835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8836 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8837 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008838
Michael Wrightd02c5b62014-02-10 15:10:22 -08008839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008841 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8842
8843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8844 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8845 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008846
8847 processKey(mapper, BTN_SIDE, 0);
8848 processSync(mapper);
8849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008850 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008851 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008852
8853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008855 ASSERT_EQ(0, motionArgs.buttonState);
8856
Michael Wrightd02c5b62014-02-10 15:10:22 -08008857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8858 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8859 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8860
8861 // press BTN_FORWARD, release BTN_FORWARD
8862 processKey(mapper, BTN_FORWARD, 1);
8863 processSync(mapper);
8864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8865 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8866 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008867
Michael Wrightd02c5b62014-02-10 15:10:22 -08008868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008869 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008870 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8871
8872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8873 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8874 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008875
8876 processKey(mapper, BTN_FORWARD, 0);
8877 processSync(mapper);
8878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008879 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008880 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008881
8882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008884 ASSERT_EQ(0, motionArgs.buttonState);
8885
Michael Wrightd02c5b62014-02-10 15:10:22 -08008886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8887 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8888 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8889
8890 // press BTN_EXTRA, release BTN_EXTRA
8891 processKey(mapper, BTN_EXTRA, 1);
8892 processSync(mapper);
8893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8894 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8895 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008896
Michael Wrightd02c5b62014-02-10 15:10:22 -08008897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008899 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8900
8901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8902 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8903 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008904
8905 processKey(mapper, BTN_EXTRA, 0);
8906 processSync(mapper);
8907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008908 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008909 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008910
8911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008913 ASSERT_EQ(0, motionArgs.buttonState);
8914
Michael Wrightd02c5b62014-02-10 15:10:22 -08008915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8916 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8917 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8918
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8920
Michael Wrightd02c5b62014-02-10 15:10:22 -08008921 // press BTN_STYLUS, release BTN_STYLUS
8922 processKey(mapper, BTN_STYLUS, 1);
8923 processSync(mapper);
8924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008926 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8927
8928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8929 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8930 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008931
8932 processKey(mapper, BTN_STYLUS, 0);
8933 processSync(mapper);
8934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008935 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008936 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008937
8938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008940 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008941
8942 // press BTN_STYLUS2, release BTN_STYLUS2
8943 processKey(mapper, BTN_STYLUS2, 1);
8944 processSync(mapper);
8945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008947 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8948
8949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8950 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8951 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008952
8953 processKey(mapper, BTN_STYLUS2, 0);
8954 processSync(mapper);
8955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008956 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008957 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008958
8959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008960 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008961 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008962
8963 // release touch
8964 processId(mapper, -1);
8965 processSync(mapper);
8966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8967 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8968 ASSERT_EQ(0, motionArgs.buttonState);
8969}
8970
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008971TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8972 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008973 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008974 prepareAxes(POSITION | ID | SLOT);
Arpit Singh56adebc2023-04-25 13:56:05 +00008975 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008976
8977 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8978 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8979
8980 // Touch down.
8981 processId(mapper, 1);
8982 processPosition(mapper, 100, 200);
8983 processSync(mapper);
8984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8985 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8986
8987 // Press and release button mapped to the primary stylus button.
8988 processKey(mapper, BTN_A, 1);
8989 processSync(mapper);
8990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8991 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8992 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8994 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8995 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8996
8997 processKey(mapper, BTN_A, 0);
8998 processSync(mapper);
8999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9000 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9002 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9003
9004 // Press and release the HID usage mapped to the secondary stylus button.
9005 processHidUsage(mapper, 0xabcd, 1);
9006 processSync(mapper);
9007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9008 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9009 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9011 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9012 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9013
9014 processHidUsage(mapper, 0xabcd, 0);
9015 processSync(mapper);
9016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9017 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9019 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9020
9021 // Release touch.
9022 processId(mapper, -1);
9023 processSync(mapper);
9024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9025 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9026}
9027
Michael Wrightd02c5b62014-02-10 15:10:22 -08009028TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009029 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009030 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009031 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009032 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009033
9034 NotifyMotionArgs motionArgs;
9035
9036 // default tool type is finger
9037 processId(mapper, 1);
9038 processPosition(mapper, 100, 200);
9039 processSync(mapper);
9040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9041 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009042 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009043
9044 // eraser
9045 processKey(mapper, BTN_TOOL_RUBBER, 1);
9046 processSync(mapper);
9047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009049 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009050
9051 // stylus
9052 processKey(mapper, BTN_TOOL_RUBBER, 0);
9053 processKey(mapper, BTN_TOOL_PEN, 1);
9054 processSync(mapper);
9055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009057 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009058
9059 // brush
9060 processKey(mapper, BTN_TOOL_PEN, 0);
9061 processKey(mapper, BTN_TOOL_BRUSH, 1);
9062 processSync(mapper);
9063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009065 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009066
9067 // pencil
9068 processKey(mapper, BTN_TOOL_BRUSH, 0);
9069 processKey(mapper, BTN_TOOL_PENCIL, 1);
9070 processSync(mapper);
9071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009073 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009074
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009075 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009076 processKey(mapper, BTN_TOOL_PENCIL, 0);
9077 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9078 processSync(mapper);
9079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009081 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009082
9083 // mouse
9084 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9085 processKey(mapper, BTN_TOOL_MOUSE, 1);
9086 processSync(mapper);
9087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009089 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009090
9091 // lens
9092 processKey(mapper, BTN_TOOL_MOUSE, 0);
9093 processKey(mapper, BTN_TOOL_LENS, 1);
9094 processSync(mapper);
9095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009097 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009098
9099 // double-tap
9100 processKey(mapper, BTN_TOOL_LENS, 0);
9101 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9102 processSync(mapper);
9103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009105 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009106
9107 // triple-tap
9108 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9109 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9110 processSync(mapper);
9111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009113 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009114
9115 // quad-tap
9116 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9117 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9118 processSync(mapper);
9119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009121 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009122
9123 // finger
9124 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9125 processKey(mapper, BTN_TOOL_FINGER, 1);
9126 processSync(mapper);
9127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009129 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009130
9131 // stylus trumps finger
9132 processKey(mapper, BTN_TOOL_PEN, 1);
9133 processSync(mapper);
9134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009136 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009137
9138 // eraser trumps stylus
9139 processKey(mapper, BTN_TOOL_RUBBER, 1);
9140 processSync(mapper);
9141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009143 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009144
9145 // mouse trumps eraser
9146 processKey(mapper, BTN_TOOL_MOUSE, 1);
9147 processSync(mapper);
9148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009150 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009151
9152 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9153 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9154 processSync(mapper);
9155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009157 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009158
9159 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9160 processToolType(mapper, MT_TOOL_PEN);
9161 processSync(mapper);
9162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009164 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009165
9166 // back to default tool type
9167 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9168 processKey(mapper, BTN_TOOL_MOUSE, 0);
9169 processKey(mapper, BTN_TOOL_RUBBER, 0);
9170 processKey(mapper, BTN_TOOL_PEN, 0);
9171 processKey(mapper, BTN_TOOL_FINGER, 0);
9172 processSync(mapper);
9173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009175 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009176}
9177
9178TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009179 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009180 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009181 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009182 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +00009183 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009184
9185 NotifyMotionArgs motionArgs;
9186
9187 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9188 processId(mapper, 1);
9189 processPosition(mapper, 100, 200);
9190 processSync(mapper);
9191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9192 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9194 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9195
9196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9197 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9199 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9200
9201 // move a little
9202 processPosition(mapper, 150, 250);
9203 processSync(mapper);
9204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9205 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9207 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9208
9209 // down when BTN_TOUCH is pressed, pressure defaults to 1
9210 processKey(mapper, BTN_TOUCH, 1);
9211 processSync(mapper);
9212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9213 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9215 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9216
9217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9218 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9220 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9221
9222 // up when BTN_TOUCH is released, hover restored
9223 processKey(mapper, BTN_TOUCH, 0);
9224 processSync(mapper);
9225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9226 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9228 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9229
9230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9231 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9233 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9234
9235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9236 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9238 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9239
9240 // exit hover when pointer goes away
9241 processId(mapper, -1);
9242 processSync(mapper);
9243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9244 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9246 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9247}
9248
9249TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009250 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009251 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009252 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009253 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009254
9255 NotifyMotionArgs motionArgs;
9256
9257 // initially hovering because pressure is 0
9258 processId(mapper, 1);
9259 processPosition(mapper, 100, 200);
9260 processPressure(mapper, 0);
9261 processSync(mapper);
9262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9263 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9265 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9266
9267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9268 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9270 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9271
9272 // move a little
9273 processPosition(mapper, 150, 250);
9274 processSync(mapper);
9275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9276 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9278 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9279
9280 // down when pressure becomes non-zero
9281 processPressure(mapper, RAW_PRESSURE_MAX);
9282 processSync(mapper);
9283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9284 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9286 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9287
9288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9289 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9291 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9292
9293 // up when pressure becomes 0, hover restored
9294 processPressure(mapper, 0);
9295 processSync(mapper);
9296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9297 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9299 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9300
9301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9302 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9304 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9305
9306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9307 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9309 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9310
9311 // exit hover when pointer goes away
9312 processId(mapper, -1);
9313 processSync(mapper);
9314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9315 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9317 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9318}
9319
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009320/**
9321 * Set the input device port <--> display port associations, and check that the
9322 * events are routed to the display that matches the display port.
9323 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9324 */
9325TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009326 const std::string usb2 = "USB2";
9327 const uint8_t hdmi1 = 0;
9328 const uint8_t hdmi2 = 1;
9329 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009330 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009331
9332 addConfigurationProperty("touch.deviceType", "touchScreen");
9333 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009334 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009335
9336 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9337 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9338
9339 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9340 // for this input device is specified, and the matching viewport is not present,
9341 // the input device should be disabled (at the mapper level).
9342
9343 // Add viewport for display 2 on hdmi2
9344 prepareSecondaryDisplay(type, hdmi2);
9345 // Send a touch event
9346 processPosition(mapper, 100, 100);
9347 processSync(mapper);
9348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9349
9350 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009351 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009352 // Send a touch event again
9353 processPosition(mapper, 100, 100);
9354 processSync(mapper);
9355
9356 NotifyMotionArgs args;
9357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9358 ASSERT_EQ(DISPLAY_ID, args.displayId);
9359}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009360
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009361TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9362 addConfigurationProperty("touch.deviceType", "touchScreen");
9363 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009364 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009365
9366 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9367
Michael Wrighta9cf4192022-12-01 23:46:39 +00009368 prepareDisplay(ui::ROTATION_0);
9369 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009370
9371 // Send a touch event
9372 processPosition(mapper, 100, 100);
9373 processSync(mapper);
9374
9375 NotifyMotionArgs args;
9376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9377 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9378}
9379
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009380TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009381 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009382 std::shared_ptr<FakePointerController> fakePointerController =
9383 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009384 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009385 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009386 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009387
Garfield Tan888a6a42020-01-09 11:39:16 -08009388 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009389 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009390
Michael Wrighta9cf4192022-12-01 23:46:39 +00009391 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009392 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009393 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009394
Josep del Río2d8c79a2023-01-23 19:33:50 +00009395 // Check source is mouse that would obtain the PointerController.
9396 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009397
9398 NotifyMotionArgs motionArgs;
9399 processPosition(mapper, 100, 100);
9400 processSync(mapper);
9401
9402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9403 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9404 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9405}
9406
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009407/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009408 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9409 */
9410TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9411 addConfigurationProperty("touch.deviceType", "touchScreen");
9412 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009413 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009414
Michael Wrighta9cf4192022-12-01 23:46:39 +00009415 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009416 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9417 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9418 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9419 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009420
9421 NotifyMotionArgs args;
9422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9423 ASSERT_EQ(26, args.readTime);
9424
Harry Cutts33476232023-01-30 19:57:29 +00009425 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9426 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9427 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009428
9429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9430 ASSERT_EQ(33, args.readTime);
9431}
9432
9433/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009434 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9435 * events should not be delivered to the listener.
9436 */
9437TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9438 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009439 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009440 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009441 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009442 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009443 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009444 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009445
9446 NotifyMotionArgs motionArgs;
9447 processPosition(mapper, 100, 100);
9448 processSync(mapper);
9449
9450 mFakeListener->assertNotifyMotionWasNotCalled();
9451}
9452
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009453/**
9454 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9455 * the touch mapper can process the events and the events can be delivered to the listener.
9456 */
9457TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9458 addConfigurationProperty("touch.deviceType", "touchScreen");
9459 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009460 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009461 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009462 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009463 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009464 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009465
9466 NotifyMotionArgs motionArgs;
9467 processPosition(mapper, 100, 100);
9468 processSync(mapper);
9469
9470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9471 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9472}
9473
Garfield Tanc734e4f2021-01-15 20:01:39 -08009474TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9475 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009476 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009477 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009478 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009479 std::optional<DisplayViewport> optionalDisplayViewport =
9480 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9481 ASSERT_TRUE(optionalDisplayViewport.has_value());
9482 DisplayViewport displayViewport = *optionalDisplayViewport;
9483
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009484 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009485 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009486 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009487
9488 // Finger down
9489 int32_t x = 100, y = 100;
9490 processPosition(mapper, x, y);
9491 processSync(mapper);
9492
9493 NotifyMotionArgs motionArgs;
9494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9495 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9496
9497 // Deactivate display viewport
9498 displayViewport.isActive = false;
9499 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009500 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009501
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009502 // The ongoing touch should be canceled immediately
9503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9504 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9505
9506 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009507 x += 10, y += 10;
9508 processPosition(mapper, x, y);
9509 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009511
9512 // Reactivate display viewport
9513 displayViewport.isActive = true;
9514 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009515 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009516
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009517 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009518 x += 10, y += 10;
9519 processPosition(mapper, x, y);
9520 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9522 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009523}
9524
Arthur Hung7c645402019-01-25 17:45:42 +08009525TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9526 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009527 prepareAxes(POSITION | ID | SLOT);
9528 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009529 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009530
9531 // Create the second touch screen device, and enable multi fingers.
9532 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009533 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009534 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009535 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009536 std::shared_ptr<InputDevice> device2 =
9537 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009538 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009539
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009540 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009541 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009542 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009543 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009544 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009545 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009546 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009547 /*flat=*/0, /*fuzz=*/0);
9548 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009549 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9550 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009551
9552 // Setup the second touch screen device.
Arpit Singh56adebc2023-04-25 13:56:05 +00009553 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9554 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9555 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009556 std::list<NotifyArgs> unused =
9557 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009558 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009559 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009560
9561 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009562 std::shared_ptr<FakePointerController> fakePointerController =
9563 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009564 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009565
9566 // Setup policy for associated displays and show touches.
9567 const uint8_t hdmi1 = 0;
9568 const uint8_t hdmi2 = 1;
9569 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9570 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9571 mFakePolicy->setShowTouches(true);
9572
9573 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009574 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009575 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009576
9577 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009578 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009579 InputReaderConfiguration::Change::DISPLAY_INFO |
9580 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009581
9582 // Two fingers down at default display.
9583 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9584 processPosition(mapper, x1, y1);
9585 processId(mapper, 1);
9586 processSlot(mapper, 1);
9587 processPosition(mapper, x2, y2);
9588 processId(mapper, 2);
9589 processSync(mapper);
9590
9591 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9592 fakePointerController->getSpots().find(DISPLAY_ID);
9593 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9594 ASSERT_EQ(size_t(2), iter->second.size());
9595
9596 // Two fingers down at second display.
9597 processPosition(mapper2, x1, y1);
9598 processId(mapper2, 1);
9599 processSlot(mapper2, 1);
9600 processPosition(mapper2, x2, y2);
9601 processId(mapper2, 2);
9602 processSync(mapper2);
9603
9604 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9605 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9606 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009607
9608 // Disable the show touches configuration and ensure the spots are cleared.
9609 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009610 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009611 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009612
9613 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009614}
9615
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009616TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009617 prepareAxes(POSITION);
9618 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009619 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00009620 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009621
9622 NotifyMotionArgs motionArgs;
9623 // Unrotated video frame
9624 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9625 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009626 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009627 processPosition(mapper, 100, 200);
9628 processSync(mapper);
9629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9630 ASSERT_EQ(frames, motionArgs.videoFrames);
9631
9632 // Subsequent touch events should not have any videoframes
9633 // This is implemented separately in FakeEventHub,
9634 // but that should match the behaviour of TouchVideoDevice.
9635 processPosition(mapper, 200, 200);
9636 processSync(mapper);
9637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9638 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9639}
9640
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009641TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009642 prepareAxes(POSITION);
9643 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009644 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009645 // Unrotated video frame
9646 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9647 NotifyMotionArgs motionArgs;
9648
9649 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009650 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009651 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9652 clearViewports();
9653 prepareDisplay(orientation);
9654 std::vector<TouchVideoFrame> frames{frame};
9655 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9656 processPosition(mapper, 100, 200);
9657 processSync(mapper);
9658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9659 ASSERT_EQ(frames, motionArgs.videoFrames);
9660 }
9661}
9662
9663TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9664 prepareAxes(POSITION);
9665 addConfigurationProperty("touch.deviceType", "touchScreen");
9666 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9667 // orientation-aware are affected by display rotation.
9668 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00009669 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009670 // Unrotated video frame
9671 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9672 NotifyMotionArgs motionArgs;
9673
9674 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009675 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009676 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9677 clearViewports();
9678 prepareDisplay(orientation);
9679 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009680 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009681 processPosition(mapper, 100, 200);
9682 processSync(mapper);
9683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009684 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9685 // compared to the display. This is so that when the window transform (which contains the
9686 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9687 // window's coordinate space.
9688 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009689 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009690
9691 // Release finger.
9692 processSync(mapper);
9693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009694 }
9695}
9696
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009697TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009698 prepareAxes(POSITION);
9699 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009700 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009701 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9702 // so mix these.
9703 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9704 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9705 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9706 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9707 NotifyMotionArgs motionArgs;
9708
Michael Wrighta9cf4192022-12-01 23:46:39 +00009709 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009710 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009711 processPosition(mapper, 100, 200);
9712 processSync(mapper);
9713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009714 ASSERT_EQ(frames, motionArgs.videoFrames);
9715}
9716
9717TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9718 prepareAxes(POSITION);
9719 addConfigurationProperty("touch.deviceType", "touchScreen");
9720 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9721 // orientation-aware are affected by display rotation.
9722 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00009723 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009724 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9725 // so mix these.
9726 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9727 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9728 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9729 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9730 NotifyMotionArgs motionArgs;
9731
Michael Wrighta9cf4192022-12-01 23:46:39 +00009732 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009733 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9734 processPosition(mapper, 100, 200);
9735 processSync(mapper);
9736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9737 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9738 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9739 // compared to the display. This is so that when the window transform (which contains the
9740 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9741 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009742 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009743 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009744 ASSERT_EQ(frames, motionArgs.videoFrames);
9745}
9746
Arthur Hung9da14732019-09-02 16:16:58 +08009747/**
9748 * If we had defined port associations, but the viewport is not ready, the touch device would be
9749 * expected to be disabled, and it should be enabled after the viewport has found.
9750 */
9751TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009752 constexpr uint8_t hdmi2 = 1;
9753 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009754 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009755
9756 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9757
9758 addConfigurationProperty("touch.deviceType", "touchScreen");
9759 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009760 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009761
9762 ASSERT_EQ(mDevice->isEnabled(), false);
9763
9764 // Add display on hdmi2, the device should be enabled and can receive touch event.
9765 prepareSecondaryDisplay(type, hdmi2);
9766 ASSERT_EQ(mDevice->isEnabled(), true);
9767
9768 // Send a touch event.
9769 processPosition(mapper, 100, 100);
9770 processSync(mapper);
9771
9772 NotifyMotionArgs args;
9773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9774 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9775}
9776
Arthur Hung421eb1c2020-01-16 00:09:42 +08009777TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009778 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009779 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009780 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009781 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009782
9783 NotifyMotionArgs motionArgs;
9784
9785 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9786 // finger down
9787 processId(mapper, 1);
9788 processPosition(mapper, x1, y1);
9789 processSync(mapper);
9790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9791 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009792 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009793
9794 // finger move
9795 processId(mapper, 1);
9796 processPosition(mapper, x2, y2);
9797 processSync(mapper);
9798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009800 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009801
9802 // finger up.
9803 processId(mapper, -1);
9804 processSync(mapper);
9805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9806 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009807 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009808
9809 // new finger down
9810 processId(mapper, 1);
9811 processPosition(mapper, x3, y3);
9812 processSync(mapper);
9813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9814 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009815 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009816}
9817
9818/**
arthurhungcc7f9802020-04-30 17:55:40 +08009819 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9820 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009821 */
arthurhungcc7f9802020-04-30 17:55:40 +08009822TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009823 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009824 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009825 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009826 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009827
9828 NotifyMotionArgs motionArgs;
9829
9830 // default tool type is finger
9831 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009832 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009833 processPosition(mapper, x1, y1);
9834 processSync(mapper);
9835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9836 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009837 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009838
9839 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9840 processToolType(mapper, MT_TOOL_PALM);
9841 processSync(mapper);
9842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9843 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9844
9845 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009846 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009847 processPosition(mapper, x2, y2);
9848 processSync(mapper);
9849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9850
9851 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009852 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009853 processSync(mapper);
9854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9855
9856 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009857 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009858 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009859 processPosition(mapper, x3, y3);
9860 processSync(mapper);
9861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9862 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009863 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009864}
9865
arthurhungbf89a482020-04-17 17:37:55 +08009866/**
arthurhungcc7f9802020-04-30 17:55:40 +08009867 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9868 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009869 */
arthurhungcc7f9802020-04-30 17:55:40 +08009870TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009871 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009872 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009873 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009874 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009875
9876 NotifyMotionArgs motionArgs;
9877
9878 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009879 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9880 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009881 processPosition(mapper, x1, y1);
9882 processSync(mapper);
9883 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);
arthurhungbf89a482020-04-17 17:37:55 +08009886
9887 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009888 processSlot(mapper, SECOND_SLOT);
9889 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009890 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009891 processSync(mapper);
9892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009893 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009894 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009895
9896 // If the tool type of the first finger changes to MT_TOOL_PALM,
9897 // we expect to receive ACTION_POINTER_UP with cancel flag.
9898 processSlot(mapper, FIRST_SLOT);
9899 processId(mapper, FIRST_TRACKING_ID);
9900 processToolType(mapper, MT_TOOL_PALM);
9901 processSync(mapper);
9902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009903 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009904 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9905
9906 // The following MOVE events of second finger should be processed.
9907 processSlot(mapper, SECOND_SLOT);
9908 processId(mapper, SECOND_TRACKING_ID);
9909 processPosition(mapper, x2 + 1, y2 + 1);
9910 processSync(mapper);
9911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009913 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009914
9915 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9916 // it. Second finger receive move.
9917 processSlot(mapper, FIRST_SLOT);
9918 processId(mapper, INVALID_TRACKING_ID);
9919 processSync(mapper);
9920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9921 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009922 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009923
9924 // Second finger keeps moving.
9925 processSlot(mapper, SECOND_SLOT);
9926 processId(mapper, SECOND_TRACKING_ID);
9927 processPosition(mapper, x2 + 2, y2 + 2);
9928 processSync(mapper);
9929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009931 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009932
9933 // Second finger up.
9934 processId(mapper, INVALID_TRACKING_ID);
9935 processSync(mapper);
9936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9937 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9938 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9939}
9940
9941/**
9942 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9943 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9944 */
9945TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9946 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009947 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009948 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009949 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009950
9951 NotifyMotionArgs motionArgs;
9952
9953 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9954 // First finger down.
9955 processId(mapper, FIRST_TRACKING_ID);
9956 processPosition(mapper, x1, y1);
9957 processSync(mapper);
9958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9959 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009960 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009961
9962 // Second finger down.
9963 processSlot(mapper, SECOND_SLOT);
9964 processId(mapper, SECOND_TRACKING_ID);
9965 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009966 processSync(mapper);
9967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009968 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009969 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009970
arthurhungcc7f9802020-04-30 17:55:40 +08009971 // If the tool type of the first finger changes to MT_TOOL_PALM,
9972 // we expect to receive ACTION_POINTER_UP with cancel flag.
9973 processSlot(mapper, FIRST_SLOT);
9974 processId(mapper, FIRST_TRACKING_ID);
9975 processToolType(mapper, MT_TOOL_PALM);
9976 processSync(mapper);
9977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009978 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009979 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9980
9981 // Second finger keeps moving.
9982 processSlot(mapper, SECOND_SLOT);
9983 processId(mapper, SECOND_TRACKING_ID);
9984 processPosition(mapper, x2 + 1, y2 + 1);
9985 processSync(mapper);
9986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9987 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9988
9989 // second finger becomes palm, receive cancel due to only 1 finger is active.
9990 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009991 processToolType(mapper, MT_TOOL_PALM);
9992 processSync(mapper);
9993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9994 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9995
arthurhungcc7f9802020-04-30 17:55:40 +08009996 // third finger down.
9997 processSlot(mapper, THIRD_SLOT);
9998 processId(mapper, THIRD_TRACKING_ID);
9999 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010000 processPosition(mapper, x3, y3);
10001 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10003 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010004 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010005 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010006
10007 // third finger move
10008 processId(mapper, THIRD_TRACKING_ID);
10009 processPosition(mapper, x3 + 1, y3 + 1);
10010 processSync(mapper);
10011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10012 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10013
10014 // first finger up, third finger receive move.
10015 processSlot(mapper, FIRST_SLOT);
10016 processId(mapper, INVALID_TRACKING_ID);
10017 processSync(mapper);
10018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010020 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010021
10022 // second finger up, third finger receive move.
10023 processSlot(mapper, SECOND_SLOT);
10024 processId(mapper, INVALID_TRACKING_ID);
10025 processSync(mapper);
10026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10027 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010028 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010029
10030 // third finger up.
10031 processSlot(mapper, THIRD_SLOT);
10032 processId(mapper, INVALID_TRACKING_ID);
10033 processSync(mapper);
10034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10035 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10036 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10037}
10038
10039/**
10040 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10041 * and the active finger could still be allowed to receive the events
10042 */
10043TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10044 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010045 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010046 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010047 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010048
10049 NotifyMotionArgs motionArgs;
10050
10051 // default tool type is finger
10052 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10053 processId(mapper, FIRST_TRACKING_ID);
10054 processPosition(mapper, x1, y1);
10055 processSync(mapper);
10056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10057 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010058 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010059
10060 // Second finger down.
10061 processSlot(mapper, SECOND_SLOT);
10062 processId(mapper, SECOND_TRACKING_ID);
10063 processPosition(mapper, x2, y2);
10064 processSync(mapper);
10065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010066 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010067 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010068
10069 // If the tool type of the second finger changes to MT_TOOL_PALM,
10070 // we expect to receive ACTION_POINTER_UP with cancel flag.
10071 processId(mapper, SECOND_TRACKING_ID);
10072 processToolType(mapper, MT_TOOL_PALM);
10073 processSync(mapper);
10074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010075 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010076 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10077
10078 // The following MOVE event should be processed.
10079 processSlot(mapper, FIRST_SLOT);
10080 processId(mapper, FIRST_TRACKING_ID);
10081 processPosition(mapper, x1 + 1, y1 + 1);
10082 processSync(mapper);
10083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010085 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010086
10087 // second finger up.
10088 processSlot(mapper, SECOND_SLOT);
10089 processId(mapper, INVALID_TRACKING_ID);
10090 processSync(mapper);
10091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10093
10094 // first finger keep moving
10095 processSlot(mapper, FIRST_SLOT);
10096 processId(mapper, FIRST_TRACKING_ID);
10097 processPosition(mapper, x1 + 2, y1 + 2);
10098 processSync(mapper);
10099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10101
10102 // first finger up.
10103 processId(mapper, INVALID_TRACKING_ID);
10104 processSync(mapper);
10105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10106 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10107 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010108}
10109
Arthur Hung9ad18942021-06-19 02:04:46 +000010110/**
10111 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10112 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10113 * cause slot be valid again.
10114 */
10115TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10116 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010117 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010118 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010119 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010120
10121 NotifyMotionArgs motionArgs;
10122
10123 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10124 // First finger down.
10125 processId(mapper, FIRST_TRACKING_ID);
10126 processPosition(mapper, x1, y1);
10127 processPressure(mapper, RAW_PRESSURE_MAX);
10128 processSync(mapper);
10129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10130 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010131 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010132
10133 // First finger move.
10134 processId(mapper, FIRST_TRACKING_ID);
10135 processPosition(mapper, x1 + 1, y1 + 1);
10136 processPressure(mapper, RAW_PRESSURE_MAX);
10137 processSync(mapper);
10138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010140 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010141
10142 // Second finger down.
10143 processSlot(mapper, SECOND_SLOT);
10144 processId(mapper, SECOND_TRACKING_ID);
10145 processPosition(mapper, x2, y2);
10146 processPressure(mapper, RAW_PRESSURE_MAX);
10147 processSync(mapper);
10148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010149 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010150 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010151
10152 // second finger up with some unexpected data.
10153 processSlot(mapper, SECOND_SLOT);
10154 processId(mapper, INVALID_TRACKING_ID);
10155 processPosition(mapper, x2, y2);
10156 processSync(mapper);
10157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010158 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010159 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010160
10161 // first finger up with some unexpected data.
10162 processSlot(mapper, FIRST_SLOT);
10163 processId(mapper, INVALID_TRACKING_ID);
10164 processPosition(mapper, x2, y2);
10165 processPressure(mapper, RAW_PRESSURE_MAX);
10166 processSync(mapper);
10167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10168 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010169 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010170}
10171
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010172TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10173 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010174 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010175 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010176 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010177
10178 // First finger down.
10179 processId(mapper, FIRST_TRACKING_ID);
10180 processPosition(mapper, 100, 200);
10181 processPressure(mapper, RAW_PRESSURE_MAX);
10182 processSync(mapper);
10183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10184 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10185
10186 // Second finger down.
10187 processSlot(mapper, SECOND_SLOT);
10188 processId(mapper, SECOND_TRACKING_ID);
10189 processPosition(mapper, 300, 400);
10190 processPressure(mapper, RAW_PRESSURE_MAX);
10191 processSync(mapper);
10192 ASSERT_NO_FATAL_FAILURE(
10193 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10194
10195 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010196 // preserved. Resetting should cancel the ongoing gesture.
10197 resetMapper(mapper, ARBITRARY_TIME);
10198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10199 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010200
10201 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10202 // the existing touch state to generate a down event.
10203 processPosition(mapper, 301, 302);
10204 processSync(mapper);
10205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10206 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10208 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10209
10210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10211}
10212
10213TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10214 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010215 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010216 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010217 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010218
10219 // First finger touches down and releases.
10220 processId(mapper, FIRST_TRACKING_ID);
10221 processPosition(mapper, 100, 200);
10222 processPressure(mapper, RAW_PRESSURE_MAX);
10223 processSync(mapper);
10224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10225 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10226 processId(mapper, INVALID_TRACKING_ID);
10227 processSync(mapper);
10228 ASSERT_NO_FATAL_FAILURE(
10229 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10230
10231 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10232 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010233 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10235
10236 // Send an empty sync frame. Since there are no pointers, no events are generated.
10237 processSync(mapper);
10238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10239}
10240
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010241TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010242 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010243 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010244 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010245 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010247
10248 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10249 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10250 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10251 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10252 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10253
10254 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010255 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010256 processId(mapper, FIRST_TRACKING_ID);
10257 processToolType(mapper, MT_TOOL_PEN);
10258 processPosition(mapper, 100, 200);
10259 processPressure(mapper, RAW_PRESSURE_MAX);
10260 processSync(mapper);
10261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10262 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10263 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010264 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010265
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010266 // Now that we know the device supports styluses, ensure that the device is re-configured with
10267 // the stylus source.
10268 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10269 {
10270 const auto& devices = mReader->getInputDevices();
10271 auto deviceInfo =
10272 std::find_if(devices.begin(), devices.end(),
10273 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10274 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10275 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10276 }
10277
10278 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10280
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010281 processId(mapper, INVALID_TRACKING_ID);
10282 processSync(mapper);
10283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10284 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10285 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010286 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010287}
10288
Seunghwan Choi356026c2023-02-01 14:37:25 +090010289TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10290 addConfigurationProperty("touch.deviceType", "touchScreen");
10291 prepareDisplay(ui::ROTATION_0);
10292 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10293 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10294 // indicate stylus presence dynamically.
10295 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10296 std::shared_ptr<FakePointerController> fakePointerController =
10297 std::make_shared<FakePointerController>();
10298 mFakePolicy->setPointerController(fakePointerController);
10299 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singh56adebc2023-04-25 13:56:05 +000010300 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010301
10302 processId(mapper, FIRST_TRACKING_ID);
10303 processPressure(mapper, RAW_PRESSURE_MIN);
10304 processPosition(mapper, 100, 200);
10305 processToolType(mapper, MT_TOOL_PEN);
10306 processSync(mapper);
10307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10308 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010309 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010310 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10311 ASSERT_TRUE(fakePointerController->isPointerShown());
10312 ASSERT_NO_FATAL_FAILURE(
10313 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10314}
10315
10316TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10317 addConfigurationProperty("touch.deviceType", "touchScreen");
10318 prepareDisplay(ui::ROTATION_0);
10319 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10320 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10321 // indicate stylus presence dynamically.
10322 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10323 std::shared_ptr<FakePointerController> fakePointerController =
10324 std::make_shared<FakePointerController>();
10325 mFakePolicy->setPointerController(fakePointerController);
10326 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singh56adebc2023-04-25 13:56:05 +000010327 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010328
10329 processId(mapper, FIRST_TRACKING_ID);
10330 processPressure(mapper, RAW_PRESSURE_MIN);
10331 processPosition(mapper, 100, 200);
10332 processToolType(mapper, MT_TOOL_PEN);
10333 processSync(mapper);
10334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10335 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010336 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010337 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10338 ASSERT_FALSE(fakePointerController->isPointerShown());
10339}
10340
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010341// --- MultiTouchInputMapperTest_ExternalDevice ---
10342
10343class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10344protected:
Chris Yea52ade12020-08-27 16:49:20 -070010345 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010346};
10347
10348/**
10349 * Expect fallback to internal viewport if device is external and external viewport is not present.
10350 */
10351TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10352 prepareAxes(POSITION);
10353 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010354 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +000010355 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010356
10357 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10358
10359 NotifyMotionArgs motionArgs;
10360
10361 // Expect the event to be sent to the internal viewport,
10362 // because an external viewport is not present.
10363 processPosition(mapper, 100, 100);
10364 processSync(mapper);
10365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10366 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10367
10368 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010369 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010370 processPosition(mapper, 100, 100);
10371 processSync(mapper);
10372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10373 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10374}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010375
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010376TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10377 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10378 std::shared_ptr<FakePointerController> fakePointerController =
10379 std::make_shared<FakePointerController>();
10380 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10381 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010382
10383 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010384 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010385 prepareAxes(POSITION | ID | SLOT);
10386 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10387 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10388 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010389 mFakePolicy->setPointerController(fakePointerController);
Arpit Singh56adebc2023-04-25 13:56:05 +000010390 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010391
10392 // captured touchpad should be a touchpad source
10393 NotifyDeviceResetArgs resetArgs;
10394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10395 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10396
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010397 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010398
10399 const InputDeviceInfo::MotionRange* relRangeX =
10400 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10401 ASSERT_NE(relRangeX, nullptr);
10402 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10403 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10404 const InputDeviceInfo::MotionRange* relRangeY =
10405 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10406 ASSERT_NE(relRangeY, nullptr);
10407 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10408 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10409
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010410 // run captured pointer tests - note that this is unscaled, so input listener events should be
10411 // identical to what the hardware sends (accounting for any
10412 // calibration).
10413 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010414 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010415 processId(mapper, 1);
10416 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10417 processKey(mapper, BTN_TOUCH, 1);
10418 processSync(mapper);
10419
10420 // expect coord[0] to contain initial location of touch 0
10421 NotifyMotionArgs args;
10422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10423 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010424 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010425 ASSERT_EQ(0, args.pointerProperties[0].id);
10426 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10427 ASSERT_NO_FATAL_FAILURE(
10428 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10429
10430 // FINGER 1 DOWN
10431 processSlot(mapper, 1);
10432 processId(mapper, 2);
10433 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10434 processSync(mapper);
10435
10436 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010438 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010439 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010440 ASSERT_EQ(0, args.pointerProperties[0].id);
10441 ASSERT_EQ(1, args.pointerProperties[1].id);
10442 ASSERT_NO_FATAL_FAILURE(
10443 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10444 ASSERT_NO_FATAL_FAILURE(
10445 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10446
10447 // FINGER 1 MOVE
10448 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10449 processSync(mapper);
10450
10451 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10452 // from move
10453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10455 ASSERT_NO_FATAL_FAILURE(
10456 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10457 ASSERT_NO_FATAL_FAILURE(
10458 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10459
10460 // FINGER 0 MOVE
10461 processSlot(mapper, 0);
10462 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10463 processSync(mapper);
10464
10465 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10468 ASSERT_NO_FATAL_FAILURE(
10469 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10470 ASSERT_NO_FATAL_FAILURE(
10471 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10472
10473 // BUTTON DOWN
10474 processKey(mapper, BTN_LEFT, 1);
10475 processSync(mapper);
10476
10477 // touchinputmapper design sends a move before button press
10478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10481 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10482
10483 // BUTTON UP
10484 processKey(mapper, BTN_LEFT, 0);
10485 processSync(mapper);
10486
10487 // touchinputmapper design sends a move after button release
10488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10489 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10492
10493 // FINGER 0 UP
10494 processId(mapper, -1);
10495 processSync(mapper);
10496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10497 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10498
10499 // FINGER 1 MOVE
10500 processSlot(mapper, 1);
10501 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10502 processSync(mapper);
10503
10504 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10506 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010507 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010508 ASSERT_EQ(1, args.pointerProperties[0].id);
10509 ASSERT_NO_FATAL_FAILURE(
10510 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10511
10512 // FINGER 1 UP
10513 processId(mapper, -1);
10514 processKey(mapper, BTN_TOUCH, 0);
10515 processSync(mapper);
10516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10517 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10518
Josep del Río2d8c79a2023-01-23 19:33:50 +000010519 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010520 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010521 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010523 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010524}
10525
10526TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10527 std::shared_ptr<FakePointerController> fakePointerController =
10528 std::make_shared<FakePointerController>();
10529 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10530 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010531
10532 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010533 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010534 prepareAxes(POSITION | ID | SLOT);
10535 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10536 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010537 mFakePolicy->setPointerController(fakePointerController);
Arpit Singh56adebc2023-04-25 13:56:05 +000010538 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010539 // run uncaptured pointer tests - pushes out generic events
10540 // FINGER 0 DOWN
10541 processId(mapper, 3);
10542 processPosition(mapper, 100, 100);
10543 processKey(mapper, BTN_TOUCH, 1);
10544 processSync(mapper);
10545
10546 // start at (100,100), cursor should be at (0,0) * scale
10547 NotifyMotionArgs args;
10548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10549 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10550 ASSERT_NO_FATAL_FAILURE(
10551 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10552
10553 // FINGER 0 MOVE
10554 processPosition(mapper, 200, 200);
10555 processSync(mapper);
10556
10557 // compute scaling to help with touch position checking
10558 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10559 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10560 float scale =
10561 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10562
10563 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10565 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10567 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010568
10569 // BUTTON DOWN
10570 processKey(mapper, BTN_LEFT, 1);
10571 processSync(mapper);
10572
10573 // touchinputmapper design sends a move before button press
10574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10575 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10577 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10578
10579 // BUTTON UP
10580 processKey(mapper, BTN_LEFT, 0);
10581 processSync(mapper);
10582
10583 // touchinputmapper design sends a move after button release
10584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10585 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10587 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010588}
10589
10590TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10591 std::shared_ptr<FakePointerController> fakePointerController =
10592 std::make_shared<FakePointerController>();
10593
Michael Wrighta9cf4192022-12-01 23:46:39 +000010594 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010595 prepareAxes(POSITION | ID | SLOT);
10596 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010597 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010598 mFakePolicy->setPointerCapture(false);
Arpit Singh56adebc2023-04-25 13:56:05 +000010599 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010600
Josep del Río2d8c79a2023-01-23 19:33:50 +000010601 // uncaptured touchpad should be a pointer device
10602 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010603
Josep del Río2d8c79a2023-01-23 19:33:50 +000010604 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010605 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010606 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010607 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10608}
10609
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010610// --- BluetoothMultiTouchInputMapperTest ---
10611
10612class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10613protected:
10614 void SetUp() override {
10615 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10616 }
10617};
10618
10619TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10620 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010621 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010622 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010623 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010624
10625 nsecs_t kernelEventTime = ARBITRARY_TIME;
10626 nsecs_t expectedEventTime = ARBITRARY_TIME;
10627 // Touch down.
10628 processId(mapper, FIRST_TRACKING_ID);
10629 processPosition(mapper, 100, 200);
10630 processPressure(mapper, RAW_PRESSURE_MAX);
10631 processSync(mapper, ARBITRARY_TIME);
10632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10633 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10634
10635 // Process several events that come in quick succession, according to their timestamps.
10636 for (int i = 0; i < 3; i++) {
10637 constexpr static nsecs_t delta = ms2ns(1);
10638 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10639 kernelEventTime += delta;
10640 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10641
10642 processPosition(mapper, 101 + i, 201 + i);
10643 processSync(mapper, kernelEventTime);
10644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10645 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10646 WithEventTime(expectedEventTime))));
10647 }
10648
10649 // Release the touch.
10650 processId(mapper, INVALID_TRACKING_ID);
10651 processPressure(mapper, RAW_PRESSURE_MIN);
10652 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10654 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10655 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10656}
10657
10658// --- MultiTouchPointerModeTest ---
10659
HQ Liue6983c72022-04-19 22:14:56 +000010660class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10661protected:
10662 float mPointerMovementScale;
10663 float mPointerXZoomScale;
10664 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10665 addConfigurationProperty("touch.deviceType", "pointer");
10666 std::shared_ptr<FakePointerController> fakePointerController =
10667 std::make_shared<FakePointerController>();
10668 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10669 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010670 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010671
10672 prepareAxes(POSITION);
10673 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10674 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10675 // needs to be disabled, and the pointer gesture needs to be enabled.
10676 mFakePolicy->setPointerCapture(false);
10677 mFakePolicy->setPointerGestureEnabled(true);
10678 mFakePolicy->setPointerController(fakePointerController);
10679
10680 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10681 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10682 mPointerMovementScale =
10683 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10684 mPointerXZoomScale =
10685 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10686 }
10687
10688 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10689 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10690 /*flat*/ 0,
10691 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10692 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10693 /*flat*/ 0,
10694 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10695 }
10696};
10697
10698/**
10699 * Two fingers down on a pointer mode touch pad. The width
10700 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10701 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10702 * be greater than the both value to be freeform gesture, so that after two
10703 * fingers start to move downwards, the gesture should be swipe.
10704 */
10705TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10706 // The min freeform gesture width is 25units/mm x 30mm = 750
10707 // which is greater than fraction of the diagnal length of the touchpad (349).
10708 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010709 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010710 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010711 NotifyMotionArgs motionArgs;
10712
10713 // Two fingers down at once.
10714 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10715 // Pointer's initial position is used the [0,0] coordinate.
10716 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10717
10718 processId(mapper, FIRST_TRACKING_ID);
10719 processPosition(mapper, x1, y1);
10720 processMTSync(mapper);
10721 processId(mapper, SECOND_TRACKING_ID);
10722 processPosition(mapper, x2, y2);
10723 processMTSync(mapper);
10724 processSync(mapper);
10725
10726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010727 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010728 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010729 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010730 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010731 ASSERT_NO_FATAL_FAILURE(
10732 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10733
10734 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10735 // that there should be 1 pointer.
10736 int32_t movingDistance = 200;
10737 y1 += movingDistance;
10738 y2 += movingDistance;
10739
10740 processId(mapper, FIRST_TRACKING_ID);
10741 processPosition(mapper, x1, y1);
10742 processMTSync(mapper);
10743 processId(mapper, SECOND_TRACKING_ID);
10744 processPosition(mapper, x2, y2);
10745 processMTSync(mapper);
10746 processSync(mapper);
10747
10748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010749 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010751 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010752 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10754 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10755 0, 0, 0, 0));
10756}
10757
10758/**
10759 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10760 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10761 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10762 * value to be freeform gesture, so that after two fingers start to move downwards,
10763 * the gesture should be swipe.
10764 */
10765TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10766 // The min freeform gesture width is 5units/mm x 30mm = 150
10767 // which is greater than fraction of the diagnal length of the touchpad (349).
10768 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010769 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singh56adebc2023-04-25 13:56:05 +000010770 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010771 NotifyMotionArgs motionArgs;
10772
10773 // Two fingers down at once.
10774 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10775 // Pointer's initial position is used the [0,0] coordinate.
10776 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10777
10778 processId(mapper, FIRST_TRACKING_ID);
10779 processPosition(mapper, x1, y1);
10780 processMTSync(mapper);
10781 processId(mapper, SECOND_TRACKING_ID);
10782 processPosition(mapper, x2, y2);
10783 processMTSync(mapper);
10784 processSync(mapper);
10785
10786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010787 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010788 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010789 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010790 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010791 ASSERT_NO_FATAL_FAILURE(
10792 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10793
10794 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10795 // and there should be 1 pointer.
10796 int32_t movingDistance = 200;
10797 y1 += movingDistance;
10798 y2 += movingDistance;
10799
10800 processId(mapper, FIRST_TRACKING_ID);
10801 processPosition(mapper, x1, y1);
10802 processMTSync(mapper);
10803 processId(mapper, SECOND_TRACKING_ID);
10804 processPosition(mapper, x2, y2);
10805 processMTSync(mapper);
10806 processSync(mapper);
10807
10808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010809 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010811 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010812 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010813 // New coordinate is the scaled relative coordinate from the initial coordinate.
10814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10815 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10816 0, 0, 0, 0));
10817}
10818
10819/**
10820 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10821 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10822 * freeform gestures after two fingers start to move downwards.
10823 */
10824TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010825 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010826 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010827
10828 NotifyMotionArgs motionArgs;
10829
10830 // Two fingers down at once. Wider than the max swipe width.
10831 // The gesture is expected to be PRESS, then transformed to FREEFORM
10832 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10833
10834 processId(mapper, FIRST_TRACKING_ID);
10835 processPosition(mapper, x1, y1);
10836 processMTSync(mapper);
10837 processId(mapper, SECOND_TRACKING_ID);
10838 processPosition(mapper, x2, y2);
10839 processMTSync(mapper);
10840 processSync(mapper);
10841
10842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010843 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010844 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010845 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010846 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010847 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10848 ASSERT_NO_FATAL_FAILURE(
10849 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10850
10851 int32_t movingDistance = 200;
10852
10853 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10854 // then two down events for two pointers.
10855 y1 += movingDistance;
10856 y2 += movingDistance;
10857
10858 processId(mapper, FIRST_TRACKING_ID);
10859 processPosition(mapper, x1, y1);
10860 processMTSync(mapper);
10861 processId(mapper, SECOND_TRACKING_ID);
10862 processPosition(mapper, x2, y2);
10863 processMTSync(mapper);
10864 processSync(mapper);
10865
10866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10867 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010868 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010869 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010871 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010872 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010873 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010875 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010876 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010877 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010878 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010879 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010880 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010881 // Two pointers' scaled relative coordinates from their initial centroid.
10882 // Initial y coordinates are 0 as y1 and y2 have the same value.
10883 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10884 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10885 // When pointers move, the new coordinates equal to the initial coordinates plus
10886 // scaled moving distance.
10887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10888 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10889 0, 0, 0, 0));
10890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10891 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10892 0, 0, 0, 0));
10893
10894 // Move two fingers down again, expect one MOVE motion event.
10895 y1 += movingDistance;
10896 y2 += movingDistance;
10897
10898 processId(mapper, FIRST_TRACKING_ID);
10899 processPosition(mapper, x1, y1);
10900 processMTSync(mapper);
10901 processId(mapper, SECOND_TRACKING_ID);
10902 processPosition(mapper, x2, y2);
10903 processMTSync(mapper);
10904 processSync(mapper);
10905
10906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010907 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010909 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010910 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10912 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10913 0, 0, 0, 0, 0));
10914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10915 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10916 0, 0, 0, 0, 0));
10917}
10918
Harry Cutts39b7ca22022-10-05 15:55:48 +000010919TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010920 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010921 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010922 NotifyMotionArgs motionArgs;
10923
10924 // Place two fingers down.
10925 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10926
10927 processId(mapper, FIRST_TRACKING_ID);
10928 processPosition(mapper, x1, y1);
10929 processMTSync(mapper);
10930 processId(mapper, SECOND_TRACKING_ID);
10931 processPosition(mapper, x2, y2);
10932 processMTSync(mapper);
10933 processSync(mapper);
10934
10935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010936 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010937 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10938 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10939 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10940 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10941
10942 // Move the two fingers down and to the left.
10943 int32_t movingDistance = 200;
10944 x1 -= movingDistance;
10945 y1 += movingDistance;
10946 x2 -= movingDistance;
10947 y2 += movingDistance;
10948
10949 processId(mapper, FIRST_TRACKING_ID);
10950 processPosition(mapper, x1, y1);
10951 processMTSync(mapper);
10952 processId(mapper, SECOND_TRACKING_ID);
10953 processPosition(mapper, x2, y2);
10954 processMTSync(mapper);
10955 processSync(mapper);
10956
10957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010958 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10960 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10961 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10962 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10963}
10964
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010965TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010966 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010967 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +000010968 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10970
10971 // Start a stylus gesture.
10972 processKey(mapper, BTN_TOOL_PEN, 1);
10973 processId(mapper, FIRST_TRACKING_ID);
10974 processPosition(mapper, 100, 200);
10975 processSync(mapper);
10976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10977 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10978 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010979 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010980 // TODO(b/257078296): Pointer mode generates extra event.
10981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10982 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10983 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010984 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10986
10987 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10988 // gesture should be disabled.
10989 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10990 viewport->isActive = false;
10991 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010992 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10994 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10995 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010996 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010997 // TODO(b/257078296): Pointer mode generates extra event.
10998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10999 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11000 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011001 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11003}
11004
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011005// --- JoystickInputMapperTest ---
11006
11007class JoystickInputMapperTest : public InputMapperTest {
11008protected:
11009 static const int32_t RAW_X_MIN;
11010 static const int32_t RAW_X_MAX;
11011 static const int32_t RAW_Y_MIN;
11012 static const int32_t RAW_Y_MAX;
11013
11014 void SetUp() override {
11015 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11016 }
11017 void prepareAxes() {
11018 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11019 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11020 }
11021
11022 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11024 }
11025
11026 void processSync(JoystickInputMapper& mapper) {
11027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11028 }
11029
Michael Wrighta9cf4192022-12-01 23:46:39 +000011030 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011031 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11032 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11033 NO_PORT, ViewportType::VIRTUAL);
11034 }
11035};
11036
11037const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11038const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11039const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11040const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11041
11042TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11043 prepareAxes();
Arpit Singh2be4a362023-04-26 14:16:50 +000011044 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011045
11046 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11047
Michael Wrighta9cf4192022-12-01 23:46:39 +000011048 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011049
11050 // Send an axis event
11051 processAxis(mapper, ABS_X, 100);
11052 processSync(mapper);
11053
11054 NotifyMotionArgs args;
11055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11056 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11057
11058 // Send another axis event
11059 processAxis(mapper, ABS_Y, 100);
11060 processSync(mapper);
11061
11062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11063 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11064}
11065
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011066// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011067
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011068class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011069protected:
11070 static const char* DEVICE_NAME;
11071 static const char* DEVICE_LOCATION;
11072 static const int32_t DEVICE_ID;
11073 static const int32_t DEVICE_GENERATION;
11074 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011075 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011076 static const int32_t EVENTHUB_ID;
11077
11078 std::shared_ptr<FakeEventHub> mFakeEventHub;
11079 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011080 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011081 std::unique_ptr<InstrumentedInputReader> mReader;
11082 std::shared_ptr<InputDevice> mDevice;
11083
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011084 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011085 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011086 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011087 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011088 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011089 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011090 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11091 }
11092
11093 void SetUp() override { SetUp(DEVICE_CLASSES); }
11094
11095 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011096 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011097 mFakePolicy.clear();
11098 }
11099
Chris Yee2b1e5c2021-03-10 22:45:12 -080011100 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11101 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011102 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011103 InputDeviceIdentifier identifier;
11104 identifier.name = name;
11105 identifier.location = location;
11106 std::shared_ptr<InputDevice> device =
11107 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11108 identifier);
11109 mReader->pushNextDevice(device);
11110 mFakeEventHub->addDevice(eventHubId, name, classes);
11111 mReader->loopOnce();
11112 return device;
11113 }
11114
11115 template <class T, typename... Args>
11116 T& addControllerAndConfigure(Args... args) {
11117 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11118
11119 return controller;
11120 }
11121};
11122
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011123const char* PeripheralControllerTest::DEVICE_NAME = "device";
11124const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11125const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11126const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11127const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011128const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11129 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011130const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011131
11132// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011133class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011134protected:
11135 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011136 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011137 }
11138};
11139
11140TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011141 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011142
Harry Cuttsa5b71292022-11-28 12:56:17 +000011143 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11144 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11145 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011146}
11147
11148TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011149 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011150
Harry Cuttsa5b71292022-11-28 12:56:17 +000011151 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11152 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11153 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011154}
11155
11156// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011157class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011158protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011159 void SetUp() override {
11160 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11161 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011162};
11163
Chris Ye85758332021-05-16 23:05:17 -070011164TEST_F(LightControllerTest, MonoLight) {
11165 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011166 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011167 .maxBrightness = 255,
11168 .flags = InputLightClass::BRIGHTNESS,
11169 .path = ""};
11170 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011171
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011172 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011173 InputDeviceInfo info;
11174 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011175 std::vector<InputDeviceLightInfo> lights = info.getLights();
11176 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011177 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11178 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11179
11180 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11181 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11182}
11183
11184TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11185 RawLightInfo infoMono = {.id = 1,
11186 .name = "mono_keyboard_backlight",
11187 .maxBrightness = 255,
11188 .flags = InputLightClass::BRIGHTNESS |
11189 InputLightClass::KEYBOARD_BACKLIGHT,
11190 .path = ""};
11191 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11192
11193 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11194 InputDeviceInfo info;
11195 controller.populateDeviceInfo(&info);
11196 std::vector<InputDeviceLightInfo> lights = info.getLights();
11197 ASSERT_EQ(1U, lights.size());
11198 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11199 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011200
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011201 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11202 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011203}
11204
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011205TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11206 RawLightInfo infoMono = {.id = 1,
11207 .name = "mono_light",
11208 .maxBrightness = 255,
11209 .flags = InputLightClass::BRIGHTNESS,
11210 .path = ""};
11211 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11212 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11213 "0,100,200");
11214
11215 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11216 std::list<NotifyArgs> unused =
11217 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11218 /*changes=*/{});
11219
11220 InputDeviceInfo info;
11221 controller.populateDeviceInfo(&info);
11222 std::vector<InputDeviceLightInfo> lights = info.getLights();
11223 ASSERT_EQ(1U, lights.size());
11224 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11225}
11226
11227TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11228 RawLightInfo infoMono = {.id = 1,
11229 .name = "mono_keyboard_backlight",
11230 .maxBrightness = 255,
11231 .flags = InputLightClass::BRIGHTNESS |
11232 InputLightClass::KEYBOARD_BACKLIGHT,
11233 .path = ""};
11234 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11235
11236 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11237 std::list<NotifyArgs> unused =
11238 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11239 /*changes=*/{});
11240
11241 InputDeviceInfo info;
11242 controller.populateDeviceInfo(&info);
11243 std::vector<InputDeviceLightInfo> lights = info.getLights();
11244 ASSERT_EQ(1U, lights.size());
11245 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11246}
11247
11248TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11249 RawLightInfo infoMono = {.id = 1,
11250 .name = "mono_keyboard_backlight",
11251 .maxBrightness = 255,
11252 .flags = InputLightClass::BRIGHTNESS |
11253 InputLightClass::KEYBOARD_BACKLIGHT,
11254 .path = ""};
11255 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11256 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11257 "0,100,200");
11258
11259 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11260 std::list<NotifyArgs> unused =
11261 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11262 /*changes=*/{});
11263
11264 InputDeviceInfo info;
11265 controller.populateDeviceInfo(&info);
11266 std::vector<InputDeviceLightInfo> lights = info.getLights();
11267 ASSERT_EQ(1U, lights.size());
11268 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
11269 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
11270 ASSERT_EQ(BrightnessLevel(0), *it);
11271 std::advance(it, 1);
11272 ASSERT_EQ(BrightnessLevel(100), *it);
11273 std::advance(it, 1);
11274 ASSERT_EQ(BrightnessLevel(200), *it);
11275}
11276
11277TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
11278 RawLightInfo infoMono = {.id = 1,
11279 .name = "mono_keyboard_backlight",
11280 .maxBrightness = 255,
11281 .flags = InputLightClass::BRIGHTNESS |
11282 InputLightClass::KEYBOARD_BACKLIGHT,
11283 .path = ""};
11284 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11285 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11286 "0,100,200,300,400,500");
11287
11288 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11289 std::list<NotifyArgs> unused =
11290 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11291 /*changes=*/{});
11292
11293 InputDeviceInfo info;
11294 controller.populateDeviceInfo(&info);
11295 std::vector<InputDeviceLightInfo> lights = info.getLights();
11296 ASSERT_EQ(1U, lights.size());
11297 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11298}
11299
Chris Yee2b1e5c2021-03-10 22:45:12 -080011300TEST_F(LightControllerTest, RGBLight) {
11301 RawLightInfo infoRed = {.id = 1,
11302 .name = "red",
11303 .maxBrightness = 255,
11304 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11305 .path = ""};
11306 RawLightInfo infoGreen = {.id = 2,
11307 .name = "green",
11308 .maxBrightness = 255,
11309 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11310 .path = ""};
11311 RawLightInfo infoBlue = {.id = 3,
11312 .name = "blue",
11313 .maxBrightness = 255,
11314 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11315 .path = ""};
11316 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11317 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11318 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11319
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011320 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011321 InputDeviceInfo info;
11322 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011323 std::vector<InputDeviceLightInfo> lights = info.getLights();
11324 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011325 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11326 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11327 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11328
11329 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11330 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11331}
11332
11333TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11334 RawLightInfo infoRed = {.id = 1,
11335 .name = "red_keyboard_backlight",
11336 .maxBrightness = 255,
11337 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11338 InputLightClass::KEYBOARD_BACKLIGHT,
11339 .path = ""};
11340 RawLightInfo infoGreen = {.id = 2,
11341 .name = "green_keyboard_backlight",
11342 .maxBrightness = 255,
11343 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11344 InputLightClass::KEYBOARD_BACKLIGHT,
11345 .path = ""};
11346 RawLightInfo infoBlue = {.id = 3,
11347 .name = "blue_keyboard_backlight",
11348 .maxBrightness = 255,
11349 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11350 InputLightClass::KEYBOARD_BACKLIGHT,
11351 .path = ""};
11352 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11353 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11354 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11355
11356 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11357 InputDeviceInfo info;
11358 controller.populateDeviceInfo(&info);
11359 std::vector<InputDeviceLightInfo> lights = info.getLights();
11360 ASSERT_EQ(1U, lights.size());
11361 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11362 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11363 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11364
11365 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11366 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11367}
11368
11369TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11370 RawLightInfo infoRed = {.id = 1,
11371 .name = "red",
11372 .maxBrightness = 255,
11373 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11374 .path = ""};
11375 RawLightInfo infoGreen = {.id = 2,
11376 .name = "green",
11377 .maxBrightness = 255,
11378 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11379 .path = ""};
11380 RawLightInfo infoBlue = {.id = 3,
11381 .name = "blue",
11382 .maxBrightness = 255,
11383 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11384 .path = ""};
11385 RawLightInfo infoGlobal = {.id = 3,
11386 .name = "global_keyboard_backlight",
11387 .maxBrightness = 255,
11388 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11389 InputLightClass::KEYBOARD_BACKLIGHT,
11390 .path = ""};
11391 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11392 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11393 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11394 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11395
11396 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11397 InputDeviceInfo info;
11398 controller.populateDeviceInfo(&info);
11399 std::vector<InputDeviceLightInfo> lights = info.getLights();
11400 ASSERT_EQ(1U, lights.size());
11401 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11402 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11403 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011404
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011405 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11406 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011407}
11408
11409TEST_F(LightControllerTest, MultiColorRGBLight) {
11410 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011411 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011412 .maxBrightness = 255,
11413 .flags = InputLightClass::BRIGHTNESS |
11414 InputLightClass::MULTI_INTENSITY |
11415 InputLightClass::MULTI_INDEX,
11416 .path = ""};
11417
11418 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11419
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011420 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011421 InputDeviceInfo info;
11422 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011423 std::vector<InputDeviceLightInfo> lights = info.getLights();
11424 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011425 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11426 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11427 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11428
11429 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11430 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11431}
11432
11433TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11434 RawLightInfo infoColor = {.id = 1,
11435 .name = "multi_color_keyboard_backlight",
11436 .maxBrightness = 255,
11437 .flags = InputLightClass::BRIGHTNESS |
11438 InputLightClass::MULTI_INTENSITY |
11439 InputLightClass::MULTI_INDEX |
11440 InputLightClass::KEYBOARD_BACKLIGHT,
11441 .path = ""};
11442
11443 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11444
11445 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11446 InputDeviceInfo info;
11447 controller.populateDeviceInfo(&info);
11448 std::vector<InputDeviceLightInfo> lights = info.getLights();
11449 ASSERT_EQ(1U, lights.size());
11450 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11451 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11452 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011453
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011454 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11455 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011456}
11457
11458TEST_F(LightControllerTest, PlayerIdLight) {
11459 RawLightInfo info1 = {.id = 1,
11460 .name = "player1",
11461 .maxBrightness = 255,
11462 .flags = InputLightClass::BRIGHTNESS,
11463 .path = ""};
11464 RawLightInfo info2 = {.id = 2,
11465 .name = "player2",
11466 .maxBrightness = 255,
11467 .flags = InputLightClass::BRIGHTNESS,
11468 .path = ""};
11469 RawLightInfo info3 = {.id = 3,
11470 .name = "player3",
11471 .maxBrightness = 255,
11472 .flags = InputLightClass::BRIGHTNESS,
11473 .path = ""};
11474 RawLightInfo info4 = {.id = 4,
11475 .name = "player4",
11476 .maxBrightness = 255,
11477 .flags = InputLightClass::BRIGHTNESS,
11478 .path = ""};
11479 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11480 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11481 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11482 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11483
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011484 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011485 InputDeviceInfo info;
11486 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011487 std::vector<InputDeviceLightInfo> lights = info.getLights();
11488 ASSERT_EQ(1U, lights.size());
11489 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011490 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11491 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011492
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011493 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11494 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11495 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011496}
11497
Michael Wrightd02c5b62014-02-10 15:10:22 -080011498} // namespace android