blob: d1c3f7d156425c8ae36b54ae9d9aaded1fd531f5 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
Harry Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020
Prabir Pradhan2770d242019-09-02 18:07:11 -070021#include <CursorInputMapper.h>
22#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080025#include <InputReaderBase.h>
26#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000027#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <KeyboardInputMapper.h>
29#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070031#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070032#include <SingleTouchInputMapper.h>
33#include <SwitchInputMapper.h>
34#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000035#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070036#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080037#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000038#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039#include <android-base/thread_annotations.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000040#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050042#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000043#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080044
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070045#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000046#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000047#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000048#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000049#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000050#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000051#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000052#include "input/DisplayViewport.h"
53#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010054
Michael Wrightd02c5b62014-02-10 15:10:22 -080055namespace android {
56
Dominik Laskowski2f01d772022-03-23 16:01:29 -070057using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000058using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070059using std::chrono_literals::operator""ms;
60
Michael Wrightd02c5b62014-02-10 15:10:22 -080061// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000063static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t DISPLAY_WIDTH = 480;
67static constexpr int32_t DISPLAY_HEIGHT = 800;
68static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
69static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
70static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070071static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070072static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080073
arthurhungcc7f9802020-04-30 17:55:40 +080074static constexpr int32_t FIRST_SLOT = 0;
75static constexpr int32_t SECOND_SLOT = 1;
76static constexpr int32_t THIRD_SLOT = 2;
77static constexpr int32_t INVALID_TRACKING_ID = -1;
78static constexpr int32_t FIRST_TRACKING_ID = 0;
79static constexpr int32_t SECOND_TRACKING_ID = 1;
80static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080081static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
82static constexpr int32_t LIGHT_COLOR = 0x7F448866;
83static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080084
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080085static constexpr int32_t ACTION_POINTER_0_DOWN =
86 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
87static constexpr int32_t ACTION_POINTER_0_UP =
88 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
89static constexpr int32_t ACTION_POINTER_1_DOWN =
90 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_1_UP =
92 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000094// Minimum timestamp separation between subsequent input events from a Bluetooth device.
95static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
96// Maximum smoothing time delta so that we don't generate events too far into the future.
97constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
98
Michael Wrightd02c5b62014-02-10 15:10:22 -080099template<typename T>
100static inline T min(T a, T b) {
101 return a < b ? a : b;
102}
103
104static inline float avg(float x, float y) {
105 return (x + y) / 2;
106}
107
Chris Ye3fdbfef2021-01-06 18:45:18 -0800108// Mapping for light color name and the light color
109const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
110 {"green", LightColor::GREEN},
111 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112
Michael Wrighta9cf4192022-12-01 23:46:39 +0000113static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700114 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000115 case ui::ROTATION_90:
116 return ui::ROTATION_270;
117 case ui::ROTATION_270:
118 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700119 default:
120 return orientation;
121 }
122}
123
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800124static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
125 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000126 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800127
128 const InputDeviceInfo::MotionRange* motionRange =
129 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
130 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
131}
132
133static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
134 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000135 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800136
137 const InputDeviceInfo::MotionRange* motionRange =
138 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
139 ASSERT_EQ(nullptr, motionRange);
140}
141
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700142[[maybe_unused]] static void dumpReader(InputReader& reader) {
143 std::string dump;
144 reader.dump(dump);
145 std::istringstream iss(dump);
146 for (std::string line; std::getline(iss, line);) {
147 ALOGE("%s", line.c_str());
148 std::this_thread::sleep_for(std::chrono::milliseconds(1));
149 }
150}
151
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152// --- FakeInputMapper ---
153
154class FakeInputMapper : public InputMapper {
155 uint32_t mSources;
156 int32_t mKeyboardType;
157 int32_t mMetaState;
158 KeyedVector<int32_t, int32_t> mKeyCodeStates;
159 KeyedVector<int32_t, int32_t> mScanCodeStates;
160 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100161 // fake mapping which would normally come from keyCharacterMap
162 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800163 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700165 std::mutex mLock;
166 std::condition_variable mStateChangedCondition;
167 bool mConfigureWasCalled GUARDED_BY(mLock);
168 bool mResetWasCalled GUARDED_BY(mLock);
169 bool mProcessWasCalled GUARDED_BY(mLock);
170 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171
Arthur Hungc23540e2018-11-29 20:42:11 +0800172 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000174 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
175 uint32_t sources)
176 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800177 mSources(sources),
178 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800180 mConfigureWasCalled(false),
181 mResetWasCalled(false),
182 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
Chris Yea52ade12020-08-27 16:49:20 -0700184 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185
186 void setKeyboardType(int32_t keyboardType) {
187 mKeyboardType = keyboardType;
188 }
189
190 void setMetaState(int32_t metaState) {
191 mMetaState = metaState;
192 }
193
194 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700195 std::unique_lock<std::mutex> lock(mLock);
196 base::ScopedLockAssertion assumeLocked(mLock);
197 const bool configureCalled =
198 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
199 return mConfigureWasCalled;
200 });
201 if (!configureCalled) {
202 FAIL() << "Expected configure() to have been called.";
203 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204 mConfigureWasCalled = false;
205 }
206
207 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208 std::unique_lock<std::mutex> lock(mLock);
209 base::ScopedLockAssertion assumeLocked(mLock);
210 const bool resetCalled =
211 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
212 return mResetWasCalled;
213 });
214 if (!resetCalled) {
215 FAIL() << "Expected reset() to have been called.";
216 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 mResetWasCalled = false;
218 }
219
Yi Kong9b14ac62018-07-17 13:48:38 -0700220 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 std::unique_lock<std::mutex> lock(mLock);
222 base::ScopedLockAssertion assumeLocked(mLock);
223 const bool processCalled =
224 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
225 return mProcessWasCalled;
226 });
227 if (!processCalled) {
228 FAIL() << "Expected process() to have been called.";
229 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 if (outLastEvent) {
231 *outLastEvent = mLastEvent;
232 }
233 mProcessWasCalled = false;
234 }
235
236 void setKeyCodeState(int32_t keyCode, int32_t state) {
237 mKeyCodeStates.replaceValueFor(keyCode, state);
238 }
239
240 void setScanCodeState(int32_t scanCode, int32_t state) {
241 mScanCodeStates.replaceValueFor(scanCode, state);
242 }
243
244 void setSwitchState(int32_t switchCode, int32_t state) {
245 mSwitchStates.replaceValueFor(switchCode, state);
246 }
247
248 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800249 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250 }
251
Philip Junker4af3b3d2021-12-14 10:36:55 +0100252 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
253 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
254 }
255
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100257 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258
Harry Cuttsd02ea102023-03-17 18:21:30 +0000259 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 InputMapper::populateDeviceInfo(deviceInfo);
261
262 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000263 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264 }
265 }
266
Arpit Singhed6c3de2023-04-05 19:24:37 +0000267 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000268 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700269 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800271
272 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800273 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000274 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000275 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800276 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700277
278 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700279 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280 }
281
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700282 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700283 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700285 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700286 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 }
288
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700289 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700290 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291 mLastEvent = *rawEvent;
292 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700293 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700294 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295 }
296
Chris Yea52ade12020-08-27 16:49:20 -0700297 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
299 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
300 }
301
Philip Junker4af3b3d2021-12-14 10:36:55 +0100302 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
303 auto it = mKeyCodeMapping.find(locationKeyCode);
304 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
305 }
306
Chris Yea52ade12020-08-27 16:49:20 -0700307 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
309 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
310 }
311
Chris Yea52ade12020-08-27 16:49:20 -0700312 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 ssize_t index = mSwitchStates.indexOfKey(switchCode);
314 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
315 }
316
Chris Yea52ade12020-08-27 16:49:20 -0700317 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700318 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700319 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700320 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
322 if (keyCodes[i] == mSupportedKeyCodes[j]) {
323 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800324 }
325 }
326 }
Chris Yea52ade12020-08-27 16:49:20 -0700327 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328 return result;
329 }
330
331 virtual int32_t getMetaState() {
332 return mMetaState;
333 }
334
335 virtual void fadePointer() {
336 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800337
338 virtual std::optional<int32_t> getAssociatedDisplay() {
339 if (mViewport) {
340 return std::make_optional(mViewport->displayId);
341 }
342 return std::nullopt;
343 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344};
345
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700346// --- InputReaderPolicyTest ---
347class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700348protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700349 sp<FakeInputReaderPolicy> mFakePolicy;
350
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700351 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700352 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700353};
354
355/**
356 * Check that empty set of viewports is an acceptable configuration.
357 * Also try to get internal viewport two different ways - by type and by uniqueId.
358 *
359 * There will be confusion if two viewports with empty uniqueId and identical type are present.
360 * Such configuration is not currently allowed.
361 */
362TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700363 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700364
365 // We didn't add any viewports yet, so there shouldn't be any.
366 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100367 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700368 ASSERT_FALSE(internalViewport);
369
370 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000371 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000372 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700373
374 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700375 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700376 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100377 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700378
379 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100380 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700381 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700382 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700383
384 mFakePolicy->clearViewports();
385 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700386 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700387 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100388 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700389 ASSERT_FALSE(internalViewport);
390}
391
392TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
393 const std::string internalUniqueId = "local:0";
394 const std::string externalUniqueId = "local:1";
395 const std::string virtualUniqueId1 = "virtual:2";
396 const std::string virtualUniqueId2 = "virtual:3";
397 constexpr int32_t virtualDisplayId1 = 2;
398 constexpr int32_t virtualDisplayId2 = 3;
399
400 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000401 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000402 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000403 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700404 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000405 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000406 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000407 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700408 // Add an virtual viewport
409 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000410 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000411 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700412 // Add another virtual viewport
413 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000414 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000415 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700416
417 // Check matching by type for internal
418 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100419 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700420 ASSERT_TRUE(internalViewport);
421 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
422
423 // Check matching by type for external
424 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100425 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700426 ASSERT_TRUE(externalViewport);
427 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
428
429 // Check matching by uniqueId for virtual viewport #1
430 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700431 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700432 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100433 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700434 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
435 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
436
437 // Check matching by uniqueId for virtual viewport #2
438 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700439 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700440 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100441 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700442 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
443 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
444}
445
446
447/**
448 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
449 * that lookup works by checking display id.
450 * Check that 2 viewports of each kind is possible, for all existing viewport types.
451 */
452TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
453 const std::string uniqueId1 = "uniqueId1";
454 const std::string uniqueId2 = "uniqueId2";
455 constexpr int32_t displayId1 = 2;
456 constexpr int32_t displayId2 = 3;
457
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100458 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
459 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700460 for (const ViewportType& type : types) {
461 mFakePolicy->clearViewports();
462 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000463 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000464 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700465 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000466 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000467 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700468
469 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700470 std::optional<DisplayViewport> viewport1 =
471 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700472 ASSERT_TRUE(viewport1);
473 ASSERT_EQ(displayId1, viewport1->displayId);
474 ASSERT_EQ(type, viewport1->type);
475
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700476 std::optional<DisplayViewport> viewport2 =
477 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700478 ASSERT_TRUE(viewport2);
479 ASSERT_EQ(displayId2, viewport2->displayId);
480 ASSERT_EQ(type, viewport2->type);
481
482 // When there are multiple viewports of the same kind, and uniqueId is not specified
483 // in the call to getDisplayViewport, then that situation is not supported.
484 // The viewports can be stored in any order, so we cannot rely on the order, since that
485 // is just implementation detail.
486 // However, we can check that it still returns *a* viewport, we just cannot assert
487 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700488 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700489 ASSERT_TRUE(someViewport);
490 }
491}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700493/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000494 * When we have multiple internal displays make sure we always return the default display when
495 * querying by type.
496 */
497TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
498 const std::string uniqueId1 = "uniqueId1";
499 const std::string uniqueId2 = "uniqueId2";
500 constexpr int32_t nonDefaultDisplayId = 2;
501 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
502 "Test display ID should not be ADISPLAY_ID_DEFAULT");
503
504 // Add the default display first and ensure it gets returned.
505 mFakePolicy->clearViewports();
506 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000507 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000508 ViewportType::INTERNAL);
509 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000510 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000511 ViewportType::INTERNAL);
512
513 std::optional<DisplayViewport> viewport =
514 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
515 ASSERT_TRUE(viewport);
516 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
517 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
518
519 // Add the default display second to make sure order doesn't matter.
520 mFakePolicy->clearViewports();
521 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000522 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000523 ViewportType::INTERNAL);
524 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000525 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000526 ViewportType::INTERNAL);
527
528 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
529 ASSERT_TRUE(viewport);
530 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
531 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
532}
533
534/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700535 * Check getDisplayViewportByPort
536 */
537TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100538 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700539 const std::string uniqueId1 = "uniqueId1";
540 const std::string uniqueId2 = "uniqueId2";
541 constexpr int32_t displayId1 = 1;
542 constexpr int32_t displayId2 = 2;
543 const uint8_t hdmi1 = 0;
544 const uint8_t hdmi2 = 1;
545 const uint8_t hdmi3 = 2;
546
547 mFakePolicy->clearViewports();
548 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000549 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000550 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700551 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000552 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000553 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700554
555 // Check that correct display viewport was returned by comparing the display ports.
556 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
557 ASSERT_TRUE(hdmi1Viewport);
558 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
559 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
560
561 // Check that we can still get the same viewport using the uniqueId
562 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
563 ASSERT_TRUE(hdmi1Viewport);
564 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
565 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
566 ASSERT_EQ(type, hdmi1Viewport->type);
567
568 // Check that we cannot find a port with "HDMI2", because we never added one
569 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
570 ASSERT_FALSE(hdmi2Viewport);
571}
572
Michael Wrightd02c5b62014-02-10 15:10:22 -0800573// --- InputReaderTest ---
574
575class InputReaderTest : public testing::Test {
576protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700577 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700579 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000580 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581
Chris Yea52ade12020-08-27 16:49:20 -0700582 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700583 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700584 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700585 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586
Prabir Pradhan28efc192019-11-05 01:10:04 +0000587 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700588 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589 }
590
Chris Yea52ade12020-08-27 16:49:20 -0700591 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700592 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594 }
595
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700596 void addDevice(int32_t eventHubId, const std::string& name,
597 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800598 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599
600 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800601 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 }
603 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000604 mReader->loopOnce();
605 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700606 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700608 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 }
610
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800611 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700612 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000613 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700614 }
615
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800616 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700617 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000618 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700619 }
620
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700622 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700623 ftl::Flags<InputDeviceClass> classes,
624 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800625 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800626 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000627 FakeInputMapper& mapper =
628 device->addMapper<FakeInputMapper>(eventHubId,
629 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800630 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800631 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 return mapper;
633 }
634};
635
Chris Ye98d3f532020-10-01 21:48:59 -0700636TEST_F(InputReaderTest, PolicyGetInputDevices) {
637 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700638 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700639 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640
641 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700642 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800644 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100645 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800646 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
647 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000648 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649}
650
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000651TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
652 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
653 mFakeEventHub->setSysfsRootPath(1, "xyz");
654
655 // Should also have received a notification describing the new input device.
656 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
657 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
658 ASSERT_EQ(0U, inputDevice.getLights().size());
659
660 RawLightInfo infoMonolight = {.id = 123,
661 .name = "mono_keyboard_backlight",
662 .maxBrightness = 255,
663 .flags = InputLightClass::BRIGHTNESS,
664 .path = ""};
665 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
666 mReader->sysfsNodeChanged("xyz");
667 mReader->loopOnce();
668
669 // Should also have received a notification describing the new recreated input device.
670 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
671 inputDevice = mFakePolicy->getInputDevices()[0];
672 ASSERT_EQ(1U, inputDevice.getLights().size());
673}
674
Chris Yee7310032020-09-22 15:36:28 -0700675TEST_F(InputReaderTest, GetMergedInputDevices) {
676 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
677 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
678 // Add two subdevices to device
679 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
680 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000681 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
682 AINPUT_SOURCE_KEYBOARD);
683 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
684 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700685
686 // Push same device instance for next device to be added, so they'll have same identifier.
687 mReader->pushNextDevice(device);
688 mReader->pushNextDevice(device);
689 ASSERT_NO_FATAL_FAILURE(
690 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
691 ASSERT_NO_FATAL_FAILURE(
692 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
693
694 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000695 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700696}
697
Chris Yee14523a2020-12-19 13:46:00 -0800698TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
700 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
701 // Add two subdevices to device
702 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
703 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000704 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
705 AINPUT_SOURCE_KEYBOARD);
706 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
707 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800708
709 // Push same device instance for next device to be added, so they'll have same identifier.
710 mReader->pushNextDevice(device);
711 mReader->pushNextDevice(device);
712 // Sensor device is initially disabled
713 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
714 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
715 nullptr));
716 // Device is disabled because the only sub device is a sensor device and disabled initially.
717 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
718 ASSERT_FALSE(device->isEnabled());
719 ASSERT_NO_FATAL_FAILURE(
720 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
721 // The merged device is enabled if any sub device is enabled
722 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
723 ASSERT_TRUE(device->isEnabled());
724}
725
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700726TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800727 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700728 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800729 constexpr int32_t eventHubId = 1;
730 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700731 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000732 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
733 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800734 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800735 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700736
Yi Kong9b14ac62018-07-17 13:48:38 -0700737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700738
739 NotifyDeviceResetArgs resetArgs;
740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700741 ASSERT_EQ(deviceId, resetArgs.deviceId);
742
743 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800744 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000745 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700746
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700748 ASSERT_EQ(deviceId, resetArgs.deviceId);
749 ASSERT_EQ(device->isEnabled(), false);
750
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800751 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000752 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700755 ASSERT_EQ(device->isEnabled(), false);
756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800757 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000758 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700760 ASSERT_EQ(deviceId, resetArgs.deviceId);
761 ASSERT_EQ(device->isEnabled(), true);
762}
763
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800765 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700766 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800767 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800768 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800769 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800770 AINPUT_SOURCE_KEYBOARD, nullptr);
771 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800772
773 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
774 AINPUT_SOURCE_ANY, AKEYCODE_A))
775 << "Should return unknown when the device id is >= 0 but unknown.";
776
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800777 ASSERT_EQ(AKEY_STATE_UNKNOWN,
778 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
779 << "Should return unknown when the device id is valid but the sources are not "
780 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800781
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800782 ASSERT_EQ(AKEY_STATE_DOWN,
783 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
784 AKEYCODE_A))
785 << "Should return value provided by mapper when device id is valid and the device "
786 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787
788 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
789 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
790 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
791
792 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
793 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
794 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
795}
796
Philip Junker4af3b3d2021-12-14 10:36:55 +0100797TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
798 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
799 constexpr int32_t eventHubId = 1;
800 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
801 InputDeviceClass::KEYBOARD,
802 AINPUT_SOURCE_KEYBOARD, nullptr);
803 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
804
805 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
806 << "Should return unknown when the device with the specified id is not found.";
807
808 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
809 << "Should return correct mapping when device id is valid and mapping exists.";
810
811 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
812 << "Should return the location key code when device id is valid and there's no "
813 "mapping.";
814}
815
816TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
817 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
818 constexpr int32_t eventHubId = 1;
819 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
820 InputDeviceClass::JOYSTICK,
821 AINPUT_SOURCE_GAMEPAD, nullptr);
822 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
823
824 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
825 << "Should return unknown when the device id is valid but there is no keyboard mapper";
826}
827
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800829 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700830 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800831 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800832 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800833 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800834 AINPUT_SOURCE_KEYBOARD, nullptr);
835 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836
837 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
838 AINPUT_SOURCE_ANY, KEY_A))
839 << "Should return unknown when the device id is >= 0 but unknown.";
840
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800841 ASSERT_EQ(AKEY_STATE_UNKNOWN,
842 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
843 << "Should return unknown when the device id is valid but the sources are not "
844 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800846 ASSERT_EQ(AKEY_STATE_DOWN,
847 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
848 KEY_A))
849 << "Should return value provided by mapper when device id is valid and the device "
850 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851
852 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
853 AINPUT_SOURCE_TRACKBALL, KEY_A))
854 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
855
856 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
857 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
858 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
859}
860
861TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800862 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700863 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800865 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800866 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800867 AINPUT_SOURCE_KEYBOARD, nullptr);
868 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869
870 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
871 AINPUT_SOURCE_ANY, SW_LID))
872 << "Should return unknown when the device id is >= 0 but unknown.";
873
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800874 ASSERT_EQ(AKEY_STATE_UNKNOWN,
875 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
876 << "Should return unknown when the device id is valid but the sources are not "
877 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800879 ASSERT_EQ(AKEY_STATE_DOWN,
880 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
881 SW_LID))
882 << "Should return value provided by mapper when device id is valid and the device "
883 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800884
885 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
886 AINPUT_SOURCE_TRACKBALL, SW_LID))
887 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
888
889 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
890 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
891 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
892}
893
894TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800895 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700896 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800897 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800898 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800899 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800900 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100901
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800902 mapper.addSupportedKeyCode(AKEYCODE_A);
903 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700905 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800906 uint8_t flags[4] = { 0, 0, 0, 1 };
907
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700908 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 << "Should return false when device id is >= 0 but unknown.";
910 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
911
912 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700913 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800914 << "Should return false when device id is valid but the sources are not supported by "
915 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
917
918 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700919 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800920 keyCodes, flags))
921 << "Should return value provided by mapper when device id is valid and the device "
922 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
924
925 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700926 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
927 << "Should return false when the device id is < 0 but the sources are not supported by "
928 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
930
931 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700932 ASSERT_TRUE(
933 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
934 << "Should return value provided by mapper when device id is < 0 and one of the "
935 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800936 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
937}
938
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000939TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800940 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700941 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942
943 NotifyConfigurationChangedArgs args;
944
945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
946 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
947}
948
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000949TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800950 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700951 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000952 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800953 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000954 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800955 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800956 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800957 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000959 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000960 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
962
963 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800964 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000965 ASSERT_EQ(when, event.when);
966 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800967 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 ASSERT_EQ(EV_KEY, event.type);
969 ASSERT_EQ(KEY_A, event.code);
970 ASSERT_EQ(1, event.value);
971}
972
Garfield Tan1c7bc862020-01-28 13:24:04 -0800973TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800974 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700975 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800976 constexpr int32_t eventHubId = 1;
977 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800978 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000979 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
980 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800981 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800982 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800983
984 NotifyDeviceResetArgs resetArgs;
985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800986 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800987
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800988 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000989 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800991 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800992 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800994 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000995 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800997 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800998 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001000 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001001 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001003 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001004 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001005}
1006
Garfield Tan1c7bc862020-01-28 13:24:04 -08001007TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1008 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001009 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001010 constexpr int32_t eventHubId = 1;
1011 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1012 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001013 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1014 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001015 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001016 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1017
1018 NotifyDeviceResetArgs resetArgs;
1019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1020 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1021}
1022
Arthur Hungc23540e2018-11-29 20:42:11 +08001023TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001024 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001025 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001026 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001027 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001028 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1029 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001030 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1031 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001032 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001033
1034 const uint8_t hdmi1 = 1;
1035
1036 // Associated touch screen with second display.
1037 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1038
1039 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001040 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001041 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001042 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001043 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001044 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001045 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001046 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001047 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001048
1049 // Add the device, and make sure all of the callbacks are triggered.
1050 // The device is added after the input port associations are processed since
1051 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001052 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001055 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001056
Arthur Hung2c9a3342019-07-23 14:18:59 +08001057 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001058 ASSERT_EQ(deviceId, device->getId());
1059 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1060 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001061
1062 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001063 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001064 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001065 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001066}
1067
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001068TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1069 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001070 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001071 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1072 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1073 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001074 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1075 AINPUT_SOURCE_KEYBOARD);
1076 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1077 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001078 mReader->pushNextDevice(device);
1079 mReader->pushNextDevice(device);
1080 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1081 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1082
1083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1084
1085 NotifyDeviceResetArgs resetArgs;
1086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1087 ASSERT_EQ(deviceId, resetArgs.deviceId);
1088 ASSERT_TRUE(device->isEnabled());
1089 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1090 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1091
1092 disableDevice(deviceId);
1093 mReader->loopOnce();
1094
1095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1096 ASSERT_EQ(deviceId, resetArgs.deviceId);
1097 ASSERT_FALSE(device->isEnabled());
1098 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1099 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1100
1101 enableDevice(deviceId);
1102 mReader->loopOnce();
1103
1104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1105 ASSERT_EQ(deviceId, resetArgs.deviceId);
1106 ASSERT_TRUE(device->isEnabled());
1107 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1108 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1109}
1110
1111TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1112 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001113 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001114 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1115 // Add two subdevices to device
1116 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1117 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001118 device->addMapper<FakeInputMapper>(eventHubIds[0],
1119 mFakePolicy->getReaderConfiguration(),
1120 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001121 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001122 device->addMapper<FakeInputMapper>(eventHubIds[1],
1123 mFakePolicy->getReaderConfiguration(),
1124 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001125 mReader->pushNextDevice(device);
1126 mReader->pushNextDevice(device);
1127 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1128 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1129
1130 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1131 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1132
1133 ASSERT_EQ(AKEY_STATE_DOWN,
1134 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1135 ASSERT_EQ(AKEY_STATE_DOWN,
1136 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1137 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1138 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1139}
1140
Prabir Pradhan7e186182020-11-10 13:56:45 -08001141TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1142 NotifyPointerCaptureChangedArgs args;
1143
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001144 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001145 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001146 mReader->loopOnce();
1147 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001148 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1149 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001150
1151 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001152 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001153 mReader->loopOnce();
1154 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001155 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001156
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001157 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001158 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001159 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001160 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001161 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001162}
1163
Chris Ye87143712020-11-10 05:05:58 +00001164class FakeVibratorInputMapper : public FakeInputMapper {
1165public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001166 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1167 const InputReaderConfiguration& readerConfig, uint32_t sources)
1168 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001169
1170 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1171};
1172
1173TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1174 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001175 ftl::Flags<InputDeviceClass> deviceClass =
1176 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001177 constexpr int32_t eventHubId = 1;
1178 const char* DEVICE_LOCATION = "BLUETOOTH";
1179 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1180 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001181 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1182 mFakePolicy->getReaderConfiguration(),
1183 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001184 mReader->pushNextDevice(device);
1185
1186 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1187 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1188
1189 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1190 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1191}
1192
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001193// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001194
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001195class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001196public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001197 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001198
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001199 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001200
Andy Chenf9f1a022022-08-29 20:07:10 -04001201 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1202
Chris Yee2b1e5c2021-03-10 22:45:12 -08001203 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1204
1205 void dump(std::string& dump) override {}
1206
1207 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1208 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001209 }
1210
Chris Yee2b1e5c2021-03-10 22:45:12 -08001211 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1212 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001213 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001214
1215 bool setLightColor(int32_t lightId, int32_t color) override {
1216 getDeviceContext().setLightBrightness(lightId, color >> 24);
1217 return true;
1218 }
1219
1220 std::optional<int32_t> getLightColor(int32_t lightId) override {
1221 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1222 if (!result.has_value()) {
1223 return std::nullopt;
1224 }
1225 return result.value() << 24;
1226 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001227
1228 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1229
1230 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1231
1232private:
1233 InputDeviceContext& mDeviceContext;
1234 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1235 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001236 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001237};
1238
Chris Yee2b1e5c2021-03-10 22:45:12 -08001239TEST_F(InputReaderTest, BatteryGetCapacity) {
1240 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001241 ftl::Flags<InputDeviceClass> deviceClass =
1242 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001243 constexpr int32_t eventHubId = 1;
1244 const char* DEVICE_LOCATION = "BLUETOOTH";
1245 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001246 FakePeripheralController& controller =
1247 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001248 mReader->pushNextDevice(device);
1249
1250 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1251
Harry Cuttsa5b71292022-11-28 12:56:17 +00001252 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1253 FakeEventHub::BATTERY_CAPACITY);
1254 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001255}
1256
1257TEST_F(InputReaderTest, BatteryGetStatus) {
1258 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001259 ftl::Flags<InputDeviceClass> deviceClass =
1260 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001261 constexpr int32_t eventHubId = 1;
1262 const char* DEVICE_LOCATION = "BLUETOOTH";
1263 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001264 FakePeripheralController& controller =
1265 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001266 mReader->pushNextDevice(device);
1267
1268 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1269
Harry Cuttsa5b71292022-11-28 12:56:17 +00001270 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1271 FakeEventHub::BATTERY_STATUS);
1272 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001273}
1274
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001275TEST_F(InputReaderTest, BatteryGetDevicePath) {
1276 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1277 ftl::Flags<InputDeviceClass> deviceClass =
1278 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1279 constexpr int32_t eventHubId = 1;
1280 const char* DEVICE_LOCATION = "BLUETOOTH";
1281 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1282 device->addController<FakePeripheralController>(eventHubId);
1283 mReader->pushNextDevice(device);
1284
1285 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1286
Harry Cuttsa5b71292022-11-28 12:56:17 +00001287 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001288}
1289
Chris Ye3fdbfef2021-01-06 18:45:18 -08001290TEST_F(InputReaderTest, LightGetColor) {
1291 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001292 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001293 constexpr int32_t eventHubId = 1;
1294 const char* DEVICE_LOCATION = "BLUETOOTH";
1295 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001296 FakePeripheralController& controller =
1297 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001298 mReader->pushNextDevice(device);
1299 RawLightInfo info = {.id = 1,
1300 .name = "Mono",
1301 .maxBrightness = 255,
1302 .flags = InputLightClass::BRIGHTNESS,
1303 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001304 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1305 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001306
1307 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001308
Harry Cutts33476232023-01-30 19:57:29 +00001309 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1310 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1311 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1312 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001313}
1314
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001315// --- InputReaderIntegrationTest ---
1316
1317// These tests create and interact with the InputReader only through its interface.
1318// The InputReader is started during SetUp(), which starts its processing in its own
1319// thread. The tests use linux uinput to emulate input devices.
1320// NOTE: Interacting with the physical device while these tests are running may cause
1321// the tests to fail.
1322class InputReaderIntegrationTest : public testing::Test {
1323protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001324 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001325 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001326 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001327
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001328 std::shared_ptr<FakePointerController> mFakePointerController;
1329
Chris Yea52ade12020-08-27 16:49:20 -07001330 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001331#if !defined(__ANDROID__)
1332 GTEST_SKIP();
1333#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001334 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001335 mFakePointerController = std::make_shared<FakePointerController>();
1336 mFakePolicy->setPointerController(mFakePointerController);
Harry Cutts33476232023-01-30 19:57:29 +00001337 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1338 /*eventDidNotHappenTimeout=*/30ms);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001339
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001340 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1341 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001342 ASSERT_EQ(mReader->start(), OK);
1343
1344 // Since this test is run on a real device, all the input devices connected
1345 // to the test device will show up in mReader. We wait for those input devices to
1346 // show up before beginning the tests.
1347 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +00001348 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1350 }
1351
Chris Yea52ade12020-08-27 16:49:20 -07001352 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001353#if !defined(__ANDROID__)
1354 return;
1355#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001356 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001357 mReader.reset();
1358 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001359 mFakePolicy.clear();
1360 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001361
1362 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1363 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1364 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1365 [&name](const InputDeviceInfo& info) {
1366 return info.getIdentifier().name == name;
1367 });
1368 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1369 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001370};
1371
1372TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1373 // An invalid input device that is only used for this test.
1374 class InvalidUinputDevice : public UinputDevice {
1375 public:
Harry Cutts33476232023-01-30 19:57:29 +00001376 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001377
1378 private:
1379 void configureDevice(int fd, uinput_user_dev* device) override {}
1380 };
1381
1382 const size_t numDevices = mFakePolicy->getInputDevices().size();
1383
1384 // UinputDevice does not set any event or key bits, so InputReader should not
1385 // consider it as a valid device.
1386 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1387 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1388 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1389 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1390
1391 invalidDevice.reset();
1392 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1394 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1395}
1396
1397TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1398 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1399
1400 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1401 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1402 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1403 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1404
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001405 const auto device = findDeviceByName(keyboard->getName());
1406 ASSERT_TRUE(device.has_value());
1407 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1408 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1409 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001410
1411 keyboard.reset();
1412 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1414 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1415}
1416
1417TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1418 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1419 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1420
1421 NotifyConfigurationChangedArgs configChangedArgs;
1422 ASSERT_NO_FATAL_FAILURE(
1423 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001424 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001425 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1426
1427 NotifyKeyArgs keyArgs;
1428 keyboard->pressAndReleaseHomeKey();
1429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1430 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001431 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001432 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001433 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001434 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001435 prevTimestamp = keyArgs.eventTime;
1436
1437 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1438 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001439 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001440 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001441 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001442}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001443
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001444TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1445 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1446 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1447
1448 const auto device = findDeviceByName(stylus->getName());
1449 ASSERT_TRUE(device.has_value());
1450
Prabir Pradhana3621852022-10-14 18:57:23 +00001451 // An external stylus with buttons should also be recognized as a keyboard.
1452 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001453 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1454 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1455
1456 const auto DOWN =
1457 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1458 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1459
1460 stylus->pressAndReleaseKey(BTN_STYLUS);
1461 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1462 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1463 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1464 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1465
1466 stylus->pressAndReleaseKey(BTN_STYLUS2);
1467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1468 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1469 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1470 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1471
1472 stylus->pressAndReleaseKey(BTN_STYLUS3);
1473 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1474 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1475 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1476 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1477}
1478
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001479/**
1480 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1481 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1482 * are passed to the listener.
1483 */
1484static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1485TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1486 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1487 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1488 NotifyKeyArgs keyArgs;
1489
1490 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1491 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1492 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1493 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1494
1495 controller->pressAndReleaseKey(BTN_GEAR_UP);
1496 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1497 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1498 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1499}
1500
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001501// --- TouchIntegrationTest ---
1502
Arthur Hungaab25622020-01-16 11:22:11 +08001503class TouchIntegrationTest : public InputReaderIntegrationTest {
1504protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001505 const std::string UNIQUE_ID = "local:0";
1506
Chris Yea52ade12020-08-27 16:49:20 -07001507 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001508#if !defined(__ANDROID__)
1509 GTEST_SKIP();
1510#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001511 InputReaderIntegrationTest::SetUp();
1512 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001513 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1514 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001515
1516 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1517 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1518 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001519 const auto info = findDeviceByName(mDevice->getName());
1520 ASSERT_TRUE(info);
1521 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001522 }
1523
1524 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001525 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001526 std::optional<uint8_t> physicalPort,
1527 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001528 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001529 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001530 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001531 }
1532
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001533 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1534 NotifyMotionArgs args;
1535 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1536 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001537 ASSERT_EQ(points.size(), args.getPointerCount());
1538 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001539 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1540 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1541 }
1542 }
1543
Arthur Hungaab25622020-01-16 11:22:11 +08001544 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001545 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001546};
1547
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001548TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1549 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1550 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1551 // presses).
1552 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1553 mDeviceInfo.getSources());
1554}
1555
Arthur Hungaab25622020-01-16 11:22:11 +08001556TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1557 NotifyMotionArgs args;
1558 const Point centerPoint = mDevice->getCenterPoint();
1559
1560 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001561 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001562 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001563 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001564 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1565 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1566
1567 // ACTION_MOVE
1568 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001569 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001570 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1572
1573 // ACTION_UP
1574 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001575 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001576 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1577 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1578}
1579
1580TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1581 NotifyMotionArgs args;
1582 const Point centerPoint = mDevice->getCenterPoint();
1583
1584 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001585 mDevice->sendSlot(FIRST_SLOT);
1586 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001587 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001588 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001589 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1590 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1591
1592 // ACTION_POINTER_DOWN (Second slot)
1593 const Point secondPoint = centerPoint + Point(100, 100);
1594 mDevice->sendSlot(SECOND_SLOT);
1595 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001596 mDevice->sendDown(secondPoint);
1597 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001598 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001599 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001600
1601 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001602 mDevice->sendMove(secondPoint + Point(1, 1));
1603 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001604 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1606
1607 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001608 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001609 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001610 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001611 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001612
1613 // ACTION_UP
1614 mDevice->sendSlot(FIRST_SLOT);
1615 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001616 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001617 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1618 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1619}
1620
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001621/**
1622 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1623 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1624 * data?
1625 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1626 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1627 * for Pointer 0 only is generated after.
1628 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1629 * events, we will not miss any information.
1630 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1631 * event generated afterwards that contains the newest movement of pointer 0.
1632 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1633 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1634 * losing information about non-palm pointers.
1635 */
1636TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1637 NotifyMotionArgs args;
1638 const Point centerPoint = mDevice->getCenterPoint();
1639
1640 // ACTION_DOWN
1641 mDevice->sendSlot(FIRST_SLOT);
1642 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1643 mDevice->sendDown(centerPoint);
1644 mDevice->sendSync();
1645 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1646
1647 // ACTION_POINTER_DOWN (Second slot)
1648 const Point secondPoint = centerPoint + Point(100, 100);
1649 mDevice->sendSlot(SECOND_SLOT);
1650 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1651 mDevice->sendDown(secondPoint);
1652 mDevice->sendSync();
1653 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1654
1655 // ACTION_MOVE (First slot)
1656 mDevice->sendSlot(FIRST_SLOT);
1657 mDevice->sendMove(centerPoint + Point(5, 5));
1658 // ACTION_POINTER_UP (Second slot)
1659 mDevice->sendSlot(SECOND_SLOT);
1660 mDevice->sendPointerUp();
1661 // Send a single sync for the above 2 pointer updates
1662 mDevice->sendSync();
1663
1664 // First, we should get POINTER_UP for the second pointer
1665 assertReceivedMotion(ACTION_POINTER_1_UP,
1666 {/*first pointer */ centerPoint + Point(5, 5),
1667 /*second pointer*/ secondPoint});
1668
1669 // Next, the MOVE event for the first pointer
1670 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1671}
1672
1673/**
1674 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1675 * move, and then it will go up, all in the same frame.
1676 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1677 * gets sent to the listener.
1678 */
1679TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1680 NotifyMotionArgs args;
1681 const Point centerPoint = mDevice->getCenterPoint();
1682
1683 // ACTION_DOWN
1684 mDevice->sendSlot(FIRST_SLOT);
1685 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1686 mDevice->sendDown(centerPoint);
1687 mDevice->sendSync();
1688 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1689
1690 // ACTION_POINTER_DOWN (Second slot)
1691 const Point secondPoint = centerPoint + Point(100, 100);
1692 mDevice->sendSlot(SECOND_SLOT);
1693 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1694 mDevice->sendDown(secondPoint);
1695 mDevice->sendSync();
1696 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1697
1698 // ACTION_MOVE (First slot)
1699 mDevice->sendSlot(FIRST_SLOT);
1700 mDevice->sendMove(centerPoint + Point(5, 5));
1701 // ACTION_POINTER_UP (Second slot)
1702 mDevice->sendSlot(SECOND_SLOT);
1703 mDevice->sendMove(secondPoint + Point(6, 6));
1704 mDevice->sendPointerUp();
1705 // Send a single sync for the above 2 pointer updates
1706 mDevice->sendSync();
1707
1708 // First, we should get POINTER_UP for the second pointer
1709 // The movement of the second pointer during the liftoff frame is ignored.
1710 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1711 assertReceivedMotion(ACTION_POINTER_1_UP,
1712 {/*first pointer */ centerPoint + Point(5, 5),
1713 /*second pointer*/ secondPoint});
1714
1715 // Next, the MOVE event for the first pointer
1716 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1717}
1718
Arthur Hungaab25622020-01-16 11:22:11 +08001719TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1720 NotifyMotionArgs args;
1721 const Point centerPoint = mDevice->getCenterPoint();
1722
1723 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001724 mDevice->sendSlot(FIRST_SLOT);
1725 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001726 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001727 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001728 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1729 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1730
arthurhungcc7f9802020-04-30 17:55:40 +08001731 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001732 const Point secondPoint = centerPoint + Point(100, 100);
1733 mDevice->sendSlot(SECOND_SLOT);
1734 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1735 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001736 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001737 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001738 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001739
arthurhungcc7f9802020-04-30 17:55:40 +08001740 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001741 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001742 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001743 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1745
arthurhungcc7f9802020-04-30 17:55:40 +08001746 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1747 // a palm event.
1748 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001749 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001750 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001751 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001752 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001753 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001754
arthurhungcc7f9802020-04-30 17:55:40 +08001755 // Send up to second slot, expect first slot send moving.
1756 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001757 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001758 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001760
arthurhungcc7f9802020-04-30 17:55:40 +08001761 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001762 mDevice->sendSlot(FIRST_SLOT);
1763 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001764 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001765
arthurhungcc7f9802020-04-30 17:55:40 +08001766 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1767 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001768}
1769
Prabir Pradhanda20b172022-09-26 17:01:18 +00001770TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1771 const Point centerPoint = mDevice->getCenterPoint();
1772
1773 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1774 mDevice->sendSlot(FIRST_SLOT);
1775 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1776 mDevice->sendToolType(MT_TOOL_PEN);
1777 mDevice->sendDown(centerPoint);
1778 mDevice->sendSync();
1779 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1780 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001781 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001782
1783 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1784
1785 // Release the stylus touch.
1786 mDevice->sendUp();
1787 mDevice->sendSync();
1788 ASSERT_NO_FATAL_FAILURE(
1789 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1790
1791 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1792
1793 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1794 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1795 mDevice->sendToolType(MT_TOOL_FINGER);
1796 mDevice->sendDown(centerPoint);
1797 mDevice->sendSync();
1798 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1799 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001800 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001801
1802 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1803
1804 mDevice->sendUp();
1805 mDevice->sendSync();
1806 ASSERT_NO_FATAL_FAILURE(
1807 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1808
1809 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1810 // The policy should be notified of the stylus presence.
1811 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1812 mDevice->sendToolType(MT_TOOL_PEN);
1813 mDevice->sendMove(centerPoint);
1814 mDevice->sendSync();
1815 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1816 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001817 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001818
1819 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1820}
1821
Prabir Pradhan124ea442022-10-28 20:27:44 +00001822// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001823
Prabir Pradhan124ea442022-10-28 20:27:44 +00001824// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1825// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1826// stylus.
1827template <typename UinputStylusDevice>
1828class StylusButtonIntegrationTest : public TouchIntegrationTest {
1829protected:
1830 void SetUp() override {
1831#if !defined(__ANDROID__)
1832 GTEST_SKIP();
1833#endif
1834 TouchIntegrationTest::SetUp();
1835 mTouchscreen = mDevice.get();
1836 mTouchscreenInfo = mDeviceInfo;
1837
1838 setUpStylusDevice();
1839 }
1840
1841 UinputStylusDevice* mStylus{nullptr};
1842 InputDeviceInfo mStylusInfo{};
1843
1844 UinputTouchScreen* mTouchscreen{nullptr};
1845 InputDeviceInfo mTouchscreenInfo{};
1846
1847private:
1848 // When we are attempting to test stylus button events that are sent from the touchscreen,
1849 // use the same Uinput device for the touchscreen and the stylus.
1850 template <typename T = UinputStylusDevice>
1851 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1852 mStylus = mDevice.get();
1853 mStylusInfo = mDeviceInfo;
1854 }
1855
1856 // When we are attempting to stylus buttons from an external stylus being merged with touches
1857 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1858 template <typename T = UinputStylusDevice>
1859 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1860 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1861 mStylus = mStylusDeviceLifecycleTracker.get();
1862 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1863 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1864 const auto info = findDeviceByName(mStylus->getName());
1865 ASSERT_TRUE(info);
1866 mStylusInfo = *info;
1867 }
1868
1869 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1870
1871 // Hide the base class's device to expose it with a different name for readability.
1872 using TouchIntegrationTest::mDevice;
1873 using TouchIntegrationTest::mDeviceInfo;
1874};
1875
1876using StylusButtonIntegrationTestTypes =
1877 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1878TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1879
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001880TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001881 const auto stylusId = TestFixture::mStylusInfo.getId();
1882
1883 TestFixture::mStylus->pressKey(BTN_STYLUS);
1884 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1885 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1886 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1887
1888 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1889 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001890 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001891 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001892}
1893
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001894TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001895 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1896 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1897 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001898
1899 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001900 TestFixture::mStylus->pressKey(BTN_STYLUS);
1901 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001902 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001903 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001904
1905 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001906 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1907 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1908 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1909 TestFixture::mTouchscreen->sendDown(centerPoint);
1910 TestFixture::mTouchscreen->sendSync();
1911 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001912 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001913 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001914 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1915 WithDeviceId(touchscreenId))));
1916 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001917 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001918 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001919 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1920 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001921
Prabir Pradhan124ea442022-10-28 20:27:44 +00001922 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1923 TestFixture::mTouchscreen->sendSync();
1924 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001925 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001926 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001927 WithDeviceId(touchscreenId))));
1928 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001929 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001930 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001931 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001932
1933 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001934 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1935 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001936 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001937 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001938}
1939
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001940TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001941 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1942 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1943 const auto stylusId = TestFixture::mStylusInfo.getId();
1944 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001945 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001946
1947 // Press the stylus button.
1948 TestFixture::mStylus->pressKey(BTN_STYLUS);
1949 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1950 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1951 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1952
1953 // Start hovering with the stylus.
1954 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1955 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1956 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1957 TestFixture::mTouchscreen->sendMove(centerPoint);
1958 TestFixture::mTouchscreen->sendSync();
1959 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1960 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1961 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1962 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1963 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1964 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1965 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1966 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1967 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1968
1969 // Touch down with the stylus.
1970 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1971 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1972 TestFixture::mTouchscreen->sendDown(centerPoint);
1973 TestFixture::mTouchscreen->sendSync();
1974 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1975 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1976 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1977
1978 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1979 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1980 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1981
1982 // Stop touching with the stylus, and start hovering.
1983 TestFixture::mTouchscreen->sendUp();
1984 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1985 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1986 TestFixture::mTouchscreen->sendMove(centerPoint);
1987 TestFixture::mTouchscreen->sendSync();
1988 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1989 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
1990 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1991 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1992 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1993 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1994 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1995 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1996 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1997
1998 // Stop hovering.
1999 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2000 TestFixture::mTouchscreen->sendSync();
2001 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2002 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2003 WithButtonState(0))));
2004 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2005 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2006 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2007 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2008
2009 // Release the stylus button.
2010 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2011 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2012 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2013 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2014}
2015
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002016TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002017 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2018 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2019 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002020
2021 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002022 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2023 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2024 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2025 TestFixture::mTouchscreen->sendDown(centerPoint);
2026 TestFixture::mTouchscreen->sendSync();
2027 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002028 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002029 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002030 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002031
2032 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002033 TestFixture::mStylus->pressKey(BTN_STYLUS);
2034 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002035 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002036 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2037 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002038 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002039 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002040 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2041 WithDeviceId(touchscreenId))));
2042 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002043 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002044 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002045 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2046 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002047
Prabir Pradhan124ea442022-10-28 20:27:44 +00002048 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2049 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002050 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002051 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2052 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002053 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002054 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002055 WithDeviceId(touchscreenId))));
2056 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002057 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002058 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002059 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002060
2061 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002062 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2063 TestFixture::mTouchscreen->sendSync();
2064 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002065 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002066 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002067 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002068}
2069
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002070TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002071 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2072 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002073 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002074
2075 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2076 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2077 const auto stylusId = TestFixture::mStylusInfo.getId();
2078
2079 // Start a stylus gesture. By the time this event is processed, the configuration change that
2080 // was requested is guaranteed to be completed.
2081 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2082 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2083 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2084 TestFixture::mTouchscreen->sendDown(centerPoint);
2085 TestFixture::mTouchscreen->sendSync();
2086 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2087 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002088 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002089 WithDeviceId(touchscreenId))));
2090
2091 // Press and release a stylus button. Each change only generates a MOVE motion event.
2092 // Key events are unaffected.
2093 TestFixture::mStylus->pressKey(BTN_STYLUS);
2094 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2095 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2096 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2097 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2098 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002099 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002100 WithDeviceId(touchscreenId))));
2101
2102 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2103 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2104 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2105 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2106 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2107 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002108 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002109 WithDeviceId(touchscreenId))));
2110
2111 // Finish the stylus gesture.
2112 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2113 TestFixture::mTouchscreen->sendSync();
2114 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2115 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002116 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002117 WithDeviceId(touchscreenId))));
2118}
2119
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002120// --- ExternalStylusIntegrationTest ---
2121
2122// Verify the behavior of an external stylus. An external stylus can report pressure or button
2123// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2124// ongoing stylus gesture that is being emitted by the touchscreen.
2125using ExternalStylusIntegrationTest = TouchIntegrationTest;
2126
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002127TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002128 const Point centerPoint = mDevice->getCenterPoint();
2129
2130 // Create an external stylus capable of reporting pressure data that
2131 // should be fused with a touch pointer.
2132 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2133 createUinputDevice<UinputExternalStylusWithPressure>();
2134 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2135 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2136 const auto stylusInfo = findDeviceByName(stylus->getName());
2137 ASSERT_TRUE(stylusInfo);
2138
2139 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2140
2141 const auto touchscreenId = mDeviceInfo.getId();
2142
2143 // Set a pressure value on the stylus. It doesn't generate any events.
2144 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2145 stylus->setPressure(100);
2146 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2147
2148 // Start a finger gesture, and ensure it shows up as stylus gesture
2149 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002150 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002151 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002152 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002153 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002154 mDevice->sendSync();
2155 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2156 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002157 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002158 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002159
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002160 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2161 // event with the updated pressure.
2162 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002163 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2164 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002165 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002166 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002167
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002168 // The external stylus did not generate any events.
2169 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2170 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2171}
2172
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002173TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002174 const Point centerPoint = mDevice->getCenterPoint();
2175
2176 // Create an external stylus capable of reporting pressure data that
2177 // should be fused with a touch pointer.
2178 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2179 createUinputDevice<UinputExternalStylusWithPressure>();
2180 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2181 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2182 const auto stylusInfo = findDeviceByName(stylus->getName());
2183 ASSERT_TRUE(stylusInfo);
2184
2185 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2186
2187 const auto touchscreenId = mDeviceInfo.getId();
2188
2189 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2190 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002191 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2192 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002193 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002194 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002195
2196 // Start a finger gesture. The touch device will withhold generating any touches for
2197 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2198 mDevice->sendSlot(FIRST_SLOT);
2199 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2200 mDevice->sendToolType(MT_TOOL_FINGER);
2201 mDevice->sendDown(centerPoint);
2202 auto waitUntil = std::chrono::system_clock::now() +
2203 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002204 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002205 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002206
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002207 // Since the external stylus did not report a pressure value within the timeout,
2208 // it shows up as a finger pointer.
2209 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2210 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002211 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002212 WithPressure(1.f))));
2213
2214 // Change the pressure on the external stylus. Since the pressure was not present at the start
2215 // of the gesture, it is ignored for now.
2216 stylus->setPressure(200);
2217 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2218
2219 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002220 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2221 mDevice->sendSync();
2222 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2223 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002224 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002225
2226 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2227 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2228 mDevice->sendToolType(MT_TOOL_FINGER);
2229 mDevice->sendDown(centerPoint);
2230 mDevice->sendSync();
2231 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2232 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002233 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002234 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2235
2236 // The external stylus did not generate any events.
2237 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2238 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002239}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002240
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002241TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002242 const Point centerPoint = mDevice->getCenterPoint();
2243
2244 // Create an external stylus device that does not support pressure. It should not affect any
2245 // touch pointers.
2246 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2247 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2248 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2249 const auto stylusInfo = findDeviceByName(stylus->getName());
2250 ASSERT_TRUE(stylusInfo);
2251
2252 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2253
2254 const auto touchscreenId = mDeviceInfo.getId();
2255
2256 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2257 // pressure data from the external stylus.
2258 mDevice->sendSlot(FIRST_SLOT);
2259 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2260 mDevice->sendToolType(MT_TOOL_FINGER);
2261 mDevice->sendDown(centerPoint);
2262 auto waitUntil = std::chrono::system_clock::now() +
2263 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2264 mDevice->sendSync();
2265 ASSERT_NO_FATAL_FAILURE(
2266 mTestListener
2267 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2268 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002269 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002270 WithButtonState(0),
2271 WithDeviceId(touchscreenId),
2272 WithPressure(1.f)),
2273 waitUntil));
2274
2275 // The external stylus did not generate any events.
2276 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2277 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2278}
2279
Michael Wrightd02c5b62014-02-10 15:10:22 -08002280// --- InputDeviceTest ---
2281class InputDeviceTest : public testing::Test {
2282protected:
2283 static const char* DEVICE_NAME;
2284 static const char* DEVICE_LOCATION;
2285 static const int32_t DEVICE_ID;
2286 static const int32_t DEVICE_GENERATION;
2287 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002288 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002289 static const int32_t EVENTHUB_ID;
2290 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2291
2292 std::shared_ptr<FakeEventHub> mFakeEventHub;
2293 sp<FakeInputReaderPolicy> mFakePolicy;
2294 std::unique_ptr<TestInputListener> mFakeListener;
2295 std::unique_ptr<InstrumentedInputReader> mReader;
2296 std::shared_ptr<InputDevice> mDevice;
2297
2298 void SetUp() override {
2299 mFakeEventHub = std::make_unique<FakeEventHub>();
2300 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2301 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002302 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002303 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002304 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002305 identifier.name = DEVICE_NAME;
2306 identifier.location = DEVICE_LOCATION;
2307 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2308 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2309 identifier);
2310 mReader->pushNextDevice(mDevice);
2311 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002312 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002313 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002314
2315 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002316 mFakeListener.reset();
2317 mFakePolicy.clear();
2318 }
2319};
2320
2321const char* InputDeviceTest::DEVICE_NAME = "device";
2322const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2323const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2324const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002325const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002326const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2327 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002328const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002329const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2330
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002331TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002332 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002333 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2334 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002335}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002336
Michael Wrightd02c5b62014-02-10 15:10:22 -08002337TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2338 ASSERT_EQ(mDevice->isEnabled(), false);
2339}
2340
2341TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2342 // Configuration.
2343 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002344 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002345
2346 // Reset.
2347 unused += mDevice->reset(ARBITRARY_TIME);
2348
2349 NotifyDeviceResetArgs resetArgs;
2350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2351 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2352 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2353
2354 // Metadata.
2355 ASSERT_TRUE(mDevice->isIgnored());
2356 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2357
2358 InputDeviceInfo info = mDevice->getDeviceInfo();
2359 ASSERT_EQ(DEVICE_ID, info.getId());
2360 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2361 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2362 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2363
2364 // State queries.
2365 ASSERT_EQ(0, mDevice->getMetaState());
2366
2367 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2368 << "Ignored device should return unknown key code state.";
2369 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2370 << "Ignored device should return unknown scan code state.";
2371 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2372 << "Ignored device should return unknown switch state.";
2373
2374 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2375 uint8_t flags[2] = { 0, 1 };
2376 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2377 << "Ignored device should never mark any key codes.";
2378 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2379 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2380}
2381
2382TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2383 // Configuration.
2384 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2385
2386 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002387 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2388 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2390 mapper1.setMetaState(AMETA_ALT_ON);
2391 mapper1.addSupportedKeyCode(AKEYCODE_A);
2392 mapper1.addSupportedKeyCode(AKEYCODE_B);
2393 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2394 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2395 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2396 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2397 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2398
2399 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002400 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2401 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002402 mapper2.setMetaState(AMETA_SHIFT_ON);
2403
2404 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002405 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002406
Harry Cuttsf13161a2023-03-08 14:15:49 +00002407 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2408 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002409 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002410 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002411
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002412 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2413 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414
2415 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002416 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002417 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2418 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002419
2420 NotifyDeviceResetArgs resetArgs;
2421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2422 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2423 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2424
2425 // Metadata.
2426 ASSERT_FALSE(mDevice->isIgnored());
2427 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2428
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002429 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002431 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2433 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2434
2435 // State queries.
2436 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2437 << "Should query mappers and combine meta states.";
2438
2439 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2440 << "Should return unknown key code state when source not supported.";
2441 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2442 << "Should return unknown scan code state when source not supported.";
2443 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2444 << "Should return unknown switch state when source not supported.";
2445
2446 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2447 << "Should query mapper when source is supported.";
2448 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2449 << "Should query mapper when source is supported.";
2450 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2451 << "Should query mapper when source is supported.";
2452
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002453 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002455 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002456 << "Should do nothing when source is unsupported.";
2457 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2458 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2459 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2460 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2461
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002462 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002463 << "Should query mapper when source is supported.";
2464 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2465 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2466 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2467 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2468
2469 // Event handling.
2470 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002471 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002472 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002474 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2475 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002476}
2477
Arthur Hung2c9a3342019-07-23 14:18:59 +08002478// A single input device is associated with a specific display. Check that:
2479// 1. Device is disabled if the viewport corresponding to the associated display is not found
2480// 2. Device is disabled when setEnabled API is called
2481TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002482 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2483 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002484
2485 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002486 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002487 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2488 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002489
2490 // Device should be enabled by default.
2491 ASSERT_TRUE(mDevice->isEnabled());
2492
2493 // Prepare associated info.
2494 constexpr uint8_t hdmi = 1;
2495 const std::string UNIQUE_ID = "local:1";
2496
2497 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002498 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002499 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002500 // Device should be disabled because it is associated with a specific display via
2501 // input port <-> display port association, but the corresponding display is not found
2502 ASSERT_FALSE(mDevice->isEnabled());
2503
2504 // Prepare displays.
2505 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002506 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002507 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002508 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002509 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002510 ASSERT_TRUE(mDevice->isEnabled());
2511
2512 // Device should be disabled after set disable.
2513 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002514 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002515 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002516 ASSERT_FALSE(mDevice->isEnabled());
2517
2518 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002519 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002520 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002521 ASSERT_FALSE(mDevice->isEnabled());
2522}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002523
Christine Franks1ba71cc2021-04-07 14:37:42 -07002524TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2525 // Device should be enabled by default.
2526 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002527 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2528 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002529 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002530 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2531 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002532 ASSERT_TRUE(mDevice->isEnabled());
2533
2534 // Device should be disabled because it is associated with a specific display, but the
2535 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002536 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002537 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002538 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002539 ASSERT_FALSE(mDevice->isEnabled());
2540
2541 // Device should be enabled when a display is found.
2542 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002543 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002544 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002545 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002546 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002547 ASSERT_TRUE(mDevice->isEnabled());
2548
2549 // Device should be disabled after set disable.
2550 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002551 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002552 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002553 ASSERT_FALSE(mDevice->isEnabled());
2554
2555 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002556 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002557 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002558 ASSERT_FALSE(mDevice->isEnabled());
2559}
2560
Christine Franks2a2293c2022-01-18 11:51:16 -08002561TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2562 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002563 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2564 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002565 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002566 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2567 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002568
Christine Franks2a2293c2022-01-18 11:51:16 -08002569 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2570 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002571 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002572 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002573 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002574 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002575 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2576}
2577
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002578/**
2579 * This test reproduces a crash caused by a dangling reference that remains after device is added
2580 * and removed. The reference is accessed in InputDevice::dump(..);
2581 */
2582TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2583 constexpr int32_t TEST_EVENTHUB_ID = 10;
2584 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2585
Harry Cutts33476232023-01-30 19:57:29 +00002586 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh8e6fb252023-04-06 11:49:17 +00002587 device.addEventHubDevice(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002588 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2589 std::string dumpStr, eventHubDevStr;
2590 device.dump(dumpStr, eventHubDevStr);
2591}
2592
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002593TEST_F(InputDeviceTest, GetBluetoothAddress) {
2594 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2595 ASSERT_TRUE(address);
2596 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2597}
2598
Michael Wrightd02c5b62014-02-10 15:10:22 -08002599// --- SwitchInputMapperTest ---
2600
2601class SwitchInputMapperTest : public InputMapperTest {
2602protected:
2603};
2604
2605TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002606 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002608 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002609}
2610
2611TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002612 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002613
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002614 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002615 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002617 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002618 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002619}
2620
2621TEST_F(SwitchInputMapperTest, Process) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002622 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002623 std::list<NotifyArgs> out;
2624 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2625 ASSERT_TRUE(out.empty());
2626 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2627 ASSERT_TRUE(out.empty());
2628 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2629 ASSERT_TRUE(out.empty());
2630 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002631
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002632 ASSERT_EQ(1u, out.size());
2633 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002634 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002635 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2636 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637 args.switchMask);
2638 ASSERT_EQ(uint32_t(0), args.policyFlags);
2639}
2640
Chris Ye87143712020-11-10 05:05:58 +00002641// --- VibratorInputMapperTest ---
2642class VibratorInputMapperTest : public InputMapperTest {
2643protected:
2644 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2645};
2646
2647TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singhc68f85b2023-04-26 16:23:13 +00002648 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002649
2650 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2651}
2652
2653TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singhc68f85b2023-04-26 16:23:13 +00002654 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002655
2656 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2657}
2658
2659TEST_F(VibratorInputMapperTest, Vibrate) {
2660 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002661 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singhc68f85b2023-04-26 16:23:13 +00002662 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002663
2664 VibrationElement pattern(2);
2665 VibrationSequence sequence(2);
2666 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002667 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2668 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002669 sequence.addElement(pattern);
2670 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002671 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2672 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002673 sequence.addElement(pattern);
2674
2675 std::vector<int64_t> timings = {0, 1};
2676 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2677
2678 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002679 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002680 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002681 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002682 // Verify vibrator state listener was notified.
2683 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002684 ASSERT_EQ(1u, out.size());
2685 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2686 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2687 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002688 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002689 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002690 ASSERT_FALSE(mapper.isVibrating());
2691 // Verify vibrator state listener was notified.
2692 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002693 ASSERT_EQ(1u, out.size());
2694 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2695 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2696 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002697}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002698
Chris Yef59a2f42020-10-16 12:55:26 -07002699// --- SensorInputMapperTest ---
2700
2701class SensorInputMapperTest : public InputMapperTest {
2702protected:
2703 static const int32_t ACCEL_RAW_MIN;
2704 static const int32_t ACCEL_RAW_MAX;
2705 static const int32_t ACCEL_RAW_FUZZ;
2706 static const int32_t ACCEL_RAW_FLAT;
2707 static const int32_t ACCEL_RAW_RESOLUTION;
2708
2709 static const int32_t GYRO_RAW_MIN;
2710 static const int32_t GYRO_RAW_MAX;
2711 static const int32_t GYRO_RAW_FUZZ;
2712 static const int32_t GYRO_RAW_FLAT;
2713 static const int32_t GYRO_RAW_RESOLUTION;
2714
2715 static const float GRAVITY_MS2_UNIT;
2716 static const float DEGREE_RADIAN_UNIT;
2717
2718 void prepareAccelAxes();
2719 void prepareGyroAxes();
2720 void setAccelProperties();
2721 void setGyroProperties();
2722 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2723};
2724
2725const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2726const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2727const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2728const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2729const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2730
2731const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2732const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2733const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2734const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2735const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2736
2737const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2738const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2739
2740void SensorInputMapperTest::prepareAccelAxes() {
2741 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2742 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2743 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2744 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2745 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2746 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2747}
2748
2749void SensorInputMapperTest::prepareGyroAxes() {
2750 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2751 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2752 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2753 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2754 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2755 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2756}
2757
2758void SensorInputMapperTest::setAccelProperties() {
2759 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2760 /* sensorDataIndex */ 0);
2761 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2762 /* sensorDataIndex */ 1);
2763 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2764 /* sensorDataIndex */ 2);
2765 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2766 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2767 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2768 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2769 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2770}
2771
2772void SensorInputMapperTest::setGyroProperties() {
2773 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2774 /* sensorDataIndex */ 0);
2775 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2776 /* sensorDataIndex */ 1);
2777 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2778 /* sensorDataIndex */ 2);
2779 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2780 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2781 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2782 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2783 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2784}
2785
2786TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singh61dd33e2023-04-26 15:07:55 +00002787 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002788
2789 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2790}
2791
2792TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2793 setAccelProperties();
2794 prepareAccelAxes();
Arpit Singh61dd33e2023-04-26 15:07:55 +00002795 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002796
2797 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2798 std::chrono::microseconds(10000),
2799 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002800 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2805 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002806
2807 NotifySensorArgs args;
2808 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2809 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2810 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2811
2812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2813 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2814 ASSERT_EQ(args.deviceId, DEVICE_ID);
2815 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2816 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2817 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2818 ASSERT_EQ(args.values, values);
2819 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2820}
2821
2822TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2823 setGyroProperties();
2824 prepareGyroAxes();
Arpit Singh61dd33e2023-04-26 15:07:55 +00002825 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002826
2827 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2828 std::chrono::microseconds(10000),
2829 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002830 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002836
2837 NotifySensorArgs args;
2838 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2839 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2840 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2841
2842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2843 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2844 ASSERT_EQ(args.deviceId, DEVICE_ID);
2845 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2846 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2847 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2848 ASSERT_EQ(args.values, values);
2849 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2850}
2851
Michael Wrightd02c5b62014-02-10 15:10:22 -08002852// --- KeyboardInputMapperTest ---
2853
2854class KeyboardInputMapperTest : public InputMapperTest {
2855protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002856 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002857 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002858 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002859
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002860 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002861 int32_t originalKeyCode, int32_t rotatedKeyCode,
2862 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002863};
2864
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002865/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2866 * orientation.
2867 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002868void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002869 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2870 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002871}
2872
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002873void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002874 int32_t originalScanCode, int32_t originalKeyCode,
2875 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002876 NotifyKeyArgs args;
2877
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2880 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2881 ASSERT_EQ(originalScanCode, args.scanCode);
2882 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002883 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002884
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2887 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2888 ASSERT_EQ(originalScanCode, args.scanCode);
2889 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002890 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002891}
2892
Michael Wrightd02c5b62014-02-10 15:10:22 -08002893TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002894 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00002895 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002896 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002897
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002898 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002899}
2900
2901TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2902 const int32_t USAGE_A = 0x070004;
2903 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002904 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2905 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002906 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2907 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2908 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002909
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002910 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00002911 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002912 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002913 // Initial metastate is AMETA_NONE.
2914 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915
2916 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002918 NotifyKeyArgs args;
2919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2920 ASSERT_EQ(DEVICE_ID, args.deviceId);
2921 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2922 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2923 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2924 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2925 ASSERT_EQ(KEY_HOME, args.scanCode);
2926 ASSERT_EQ(AMETA_NONE, args.metaState);
2927 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2928 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2929 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2930
2931 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002932 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2934 ASSERT_EQ(DEVICE_ID, args.deviceId);
2935 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2936 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2937 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2938 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2939 ASSERT_EQ(KEY_HOME, args.scanCode);
2940 ASSERT_EQ(AMETA_NONE, args.metaState);
2941 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2942 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2943 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2944
2945 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2949 ASSERT_EQ(DEVICE_ID, args.deviceId);
2950 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2951 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2952 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2953 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2954 ASSERT_EQ(0, args.scanCode);
2955 ASSERT_EQ(AMETA_NONE, args.metaState);
2956 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2957 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2958 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2959
2960 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2962 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2964 ASSERT_EQ(DEVICE_ID, args.deviceId);
2965 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2966 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2967 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2968 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2969 ASSERT_EQ(0, args.scanCode);
2970 ASSERT_EQ(AMETA_NONE, args.metaState);
2971 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2972 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2973 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2974
2975 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2979 ASSERT_EQ(DEVICE_ID, args.deviceId);
2980 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2981 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2982 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2983 ASSERT_EQ(0, args.keyCode);
2984 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2985 ASSERT_EQ(AMETA_NONE, args.metaState);
2986 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2987 ASSERT_EQ(0U, args.policyFlags);
2988 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2989
2990 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2992 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2994 ASSERT_EQ(DEVICE_ID, args.deviceId);
2995 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2996 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2997 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2998 ASSERT_EQ(0, args.keyCode);
2999 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3000 ASSERT_EQ(AMETA_NONE, args.metaState);
3001 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3002 ASSERT_EQ(0U, args.policyFlags);
3003 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3004}
3005
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003006TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3007 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3008 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3009 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3010
3011 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003012 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003013 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3014
3015 // Key down by scan code.
3016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3017 NotifyKeyArgs args;
3018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3019 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3020
3021 // Key up by scan code.
3022 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3024 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3025}
3026
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003027/**
3028 * Ensure that the readTime is set to the time when the EV_KEY is received.
3029 */
3030TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3031 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3032
3033 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003034 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003035 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3036 NotifyKeyArgs args;
3037
3038 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003039 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3041 ASSERT_EQ(12, args.readTime);
3042
3043 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003044 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3046 ASSERT_EQ(15, args.readTime);
3047}
3048
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003050 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3051 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003052 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3053 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3054 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003056 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003057 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003058 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059
Arthur Hung95f68612022-04-07 14:08:22 +08003060 // Initial metastate is AMETA_NONE.
3061 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062
3063 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 NotifyKeyArgs args;
3066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3067 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003068 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003069 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070
3071 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003072 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3074 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003075 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076
3077 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003078 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3080 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003081 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082
3083 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003084 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3086 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003087 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003088 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089}
3090
3091TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003092 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3093 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3094 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3095 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003097 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003098 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003099 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100
Michael Wrighta9cf4192022-12-01 23:46:39 +00003101 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3103 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3104 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3105 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3106 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3107 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3108 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3109 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3110}
3111
3112TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003113 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3114 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3115 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3116 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003117
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003119 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003120 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003121 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003122
Michael Wrighta9cf4192022-12-01 23:46:39 +00003123 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003124 ASSERT_NO_FATAL_FAILURE(
3125 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3126 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3127 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3128 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3129 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3130 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3131 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003133 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003134 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003135 ASSERT_NO_FATAL_FAILURE(
3136 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3137 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3138 AKEYCODE_DPAD_UP, DISPLAY_ID));
3139 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3140 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3141 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3142 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003144 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003145 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003146 ASSERT_NO_FATAL_FAILURE(
3147 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3148 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3149 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3150 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3151 AKEYCODE_DPAD_UP, DISPLAY_ID));
3152 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3153 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003154
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003155 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003156 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003157 ASSERT_NO_FATAL_FAILURE(
3158 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3159 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3160 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3161 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3162 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3163 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3164 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003165
3166 // Special case: if orientation changes while key is down, we still emit the same keycode
3167 // in the key up as we did in the key down.
3168 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003169 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003170 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3173 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3174 ASSERT_EQ(KEY_UP, args.scanCode);
3175 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3176
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003177 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003178 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3181 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3182 ASSERT_EQ(KEY_UP, args.scanCode);
3183 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3184}
3185
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003186TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3187 // If the keyboard is not orientation aware,
3188 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003189 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003190
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003191 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003192 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003193 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003194 NotifyKeyArgs args;
3195
3196 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3201 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3202
Michael Wrighta9cf4192022-12-01 23:46:39 +00003203 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3208 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3209}
3210
3211TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3212 // If the keyboard is orientation aware,
3213 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003214 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003215
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003216 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003217 KeyboardInputMapper& mapper =
Arpit 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);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003220 NotifyKeyArgs args;
3221
3222 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3223 // ^--- already checked by the previous test
3224
Michael Wrighta9cf4192022-12-01 23:46:39 +00003225 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003226 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3231 ASSERT_EQ(DISPLAY_ID, args.displayId);
3232
3233 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003234 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003235 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003236 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3241 ASSERT_EQ(newDisplayId, args.displayId);
3242}
3243
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003245 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003246 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003247 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003249 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003250 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003252 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003253 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254}
3255
Philip Junker4af3b3d2021-12-14 10:36:55 +01003256TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3257 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003258 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003259 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3260
3261 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3262 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3263 << "If a mapping is available, the result is equal to the mapping";
3264
3265 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3266 << "If no mapping is available, the result is the key location";
3267}
3268
Michael Wrightd02c5b62014-02-10 15:10:22 -08003269TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003270 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003271 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003272 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003274 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003275 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003277 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003278 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279}
3280
3281TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003282 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003283 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003284 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003286 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003289 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290 ASSERT_TRUE(flags[0]);
3291 ASSERT_FALSE(flags[1]);
3292}
3293
3294TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003295 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3296 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3297 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3298 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3299 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3300 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003302 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003303 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003304 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003305 // Initial metastate is AMETA_NONE.
3306 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307
3308 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003309 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3310 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3311 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312
3313 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003316 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3317 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3318 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003319 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320
3321 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003324 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3325 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3326 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003327 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328
3329 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003332 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3333 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3334 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003335 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336
3337 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003340 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3341 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3342 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003343 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344
3345 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003348 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3349 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3350 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003351 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352
3353 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003356 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3357 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3358 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003359 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360}
3361
Chris Yea52ade12020-08-27 16:49:20 -07003362TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3363 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3364 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3365 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3366 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3367
3368 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003369 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003370 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3371
Chris Yea52ade12020-08-27 16:49:20 -07003372 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003373 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003374 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3375 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3376 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3377 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3378
3379 NotifyKeyArgs args;
3380 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3383 ASSERT_EQ(AMETA_NONE, args.metaState);
3384 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3385 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3386 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3387
3388 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003389 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3391 ASSERT_EQ(AMETA_NONE, args.metaState);
3392 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3393 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3394 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3395}
3396
Arthur Hung2c9a3342019-07-23 14:18:59 +08003397TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3398 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003399 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3400 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3401 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3402 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003403
3404 // keyboard 2.
3405 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003406 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003407 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003408 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003409 std::shared_ptr<InputDevice> device2 =
3410 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003411 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003412
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003413 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3414 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3415 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3416 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003417
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003418 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003419 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003420 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003421
Arpit Singh033e3ec2023-04-26 14:43:16 +00003422 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003423 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003424 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3425 mFakePolicy
3426 ->getReaderConfiguration(),
3427 AINPUT_SOURCE_KEYBOARD,
3428 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003429 std::list<NotifyArgs> unused =
3430 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003431 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003432 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003433
3434 // Prepared displays and associated info.
3435 constexpr uint8_t hdmi1 = 0;
3436 constexpr uint8_t hdmi2 = 1;
3437 const std::string SECONDARY_UNIQUE_ID = "local:1";
3438
3439 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3440 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3441
3442 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003443 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003444 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003445 ASSERT_FALSE(device2->isEnabled());
3446
3447 // Prepare second display.
3448 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003449 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003450 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003451 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003452 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003453 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003454 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003455 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003456
3457 // Device should be enabled after the associated display is found.
3458 ASSERT_TRUE(mDevice->isEnabled());
3459 ASSERT_TRUE(device2->isEnabled());
3460
3461 // Test pad key events
3462 ASSERT_NO_FATAL_FAILURE(
3463 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3464 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3465 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3466 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3467 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3468 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3469 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3470
3471 ASSERT_NO_FATAL_FAILURE(
3472 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3473 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3474 AKEYCODE_DPAD_RIGHT, newDisplayId));
3475 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3476 AKEYCODE_DPAD_DOWN, newDisplayId));
3477 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3478 AKEYCODE_DPAD_LEFT, newDisplayId));
3479}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480
arthurhungc903df12020-08-11 15:08:42 +08003481TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3482 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3483 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3484 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3485 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3486 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3487 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3488
3489 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003490 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003491 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003492 // Initial metastate is AMETA_NONE.
3493 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003494
3495 // Initialization should have turned all of the lights off.
3496 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3497 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3498 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3499
3500 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003503 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3504 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3505
3506 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003509 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3510 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3511
3512 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003513 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3514 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003515 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3516 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3517
3518 mFakeEventHub->removeDevice(EVENTHUB_ID);
3519 mReader->loopOnce();
3520
3521 // keyboard 2 should default toggle keys.
3522 const std::string USB2 = "USB2";
3523 const std::string DEVICE_NAME2 = "KEYBOARD2";
3524 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3525 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3526 std::shared_ptr<InputDevice> device2 =
3527 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003528 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003529 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3530 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3531 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3532 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3533 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3534 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3535
Arpit Singh033e3ec2023-04-26 14:43:16 +00003536 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003537 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003538 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3539 mFakePolicy
3540 ->getReaderConfiguration(),
3541 AINPUT_SOURCE_KEYBOARD,
3542 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003543 std::list<NotifyArgs> unused =
3544 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003545 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003546 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003547
3548 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3549 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3550 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003551 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3552 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003553}
3554
Arthur Hungcb40a002021-08-03 14:31:01 +00003555TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3556 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3557 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3558 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3559
3560 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh033e3ec2023-04-26 14:43:16 +00003561 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003562 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3563 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003564 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003565 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003566 // Initial metastate is AMETA_NONE.
3567 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003568
3569 mReader->toggleCapsLockState(DEVICE_ID);
3570 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3571}
3572
Arthur Hungfb3cc112022-04-13 07:39:50 +00003573TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3574 // keyboard 1.
3575 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3576 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3577 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3578 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3579 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3580 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3581
3582 KeyboardInputMapper& mapper1 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003583 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003584 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3585
3586 // keyboard 2.
3587 const std::string USB2 = "USB2";
3588 const std::string DEVICE_NAME2 = "KEYBOARD2";
3589 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3590 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3591 std::shared_ptr<InputDevice> device2 =
3592 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3593 ftl::Flags<InputDeviceClass>(0));
3594 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3595 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3596 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3597 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3598 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3599 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3600
Arpit Singh033e3ec2023-04-26 14:43:16 +00003601 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003602 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003603 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3604 mFakePolicy
3605 ->getReaderConfiguration(),
3606 AINPUT_SOURCE_KEYBOARD,
3607 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003608 std::list<NotifyArgs> unused =
3609 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003610 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003611 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003612
Arthur Hung95f68612022-04-07 14:08:22 +08003613 // Initial metastate is AMETA_NONE.
3614 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3615 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3616
3617 // Toggle num lock on and off.
3618 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3619 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003620 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3621 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3622 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3623
3624 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3625 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3626 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3627 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3628 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3629
3630 // Toggle caps lock on and off.
3631 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3632 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3633 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3634 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3635 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3636
3637 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3638 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3639 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3640 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3641 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3642
3643 // Toggle scroll lock on and off.
3644 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3645 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3646 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3647 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3648 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3649
3650 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3651 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3652 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3653 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3654 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3655}
3656
Arthur Hung2141d542022-08-23 07:45:21 +00003657TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3658 const int32_t USAGE_A = 0x070004;
3659 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3660 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3661
3662 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003663 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003664 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3665 // Key down by scan code.
3666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3667 NotifyKeyArgs args;
3668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3669 ASSERT_EQ(DEVICE_ID, args.deviceId);
3670 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3671 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3672 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3673 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3674 ASSERT_EQ(KEY_HOME, args.scanCode);
3675 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3676
3677 // Disable device, it should synthesize cancellation events for down events.
3678 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003679 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003680
3681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3682 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3683 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3684 ASSERT_EQ(KEY_HOME, args.scanCode);
3685 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3686}
3687
Zixuan Qufecb6062022-11-12 04:44:31 +00003688TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh033e3ec2023-04-26 14:43:16 +00003689 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3690 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003691 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003692 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3693 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003694
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003695 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003696 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3697
3698 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003699 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003700
3701 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3702 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3703 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3704 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3705 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003706 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3707
3708 // Call change layout association with the same values: Generation shouldn't change
3709 generation = mReader->getContext()->getGeneration();
3710 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3711 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3712 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3713 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003714}
3715
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003716TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3717 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3718 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3719
3720 // Configuration
Arpit Singh033e3ec2023-04-26 14:43:16 +00003721 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003722 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3723 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003724 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003725
3726 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3727 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3728}
3729
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003730// --- KeyboardInputMapperTest_ExternalDevice ---
3731
3732class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3733protected:
Chris Yea52ade12020-08-27 16:49:20 -07003734 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003735};
3736
3737TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003738 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3739 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003740
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003741 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3742 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3743 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3744 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003745
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003746 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003747 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003748 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003749
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003750 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003751 NotifyKeyArgs args;
3752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3753 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3754
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003755 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3757 ASSERT_EQ(uint32_t(0), args.policyFlags);
3758
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003761 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003762
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003763 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3765 ASSERT_EQ(uint32_t(0), args.policyFlags);
3766
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3769 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3770
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003771 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3773 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3774}
3775
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003776TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003777 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003778
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003779 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3780 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3781 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003782
Powei Fengd041c5d2019-05-03 17:11:33 -07003783 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003784 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003785 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003786 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003787
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003789 NotifyKeyArgs args;
3790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3791 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3792
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003793 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3795 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3796
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3799 ASSERT_EQ(uint32_t(0), args.policyFlags);
3800
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003801 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3803 ASSERT_EQ(uint32_t(0), args.policyFlags);
3804
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003805 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3807 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3808
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003809 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3811 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3812}
3813
Michael Wrightd02c5b62014-02-10 15:10:22 -08003814// --- CursorInputMapperTest ---
3815
3816class CursorInputMapperTest : public InputMapperTest {
3817protected:
3818 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3819
Michael Wright17db18e2020-06-26 20:51:44 +01003820 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003821
Chris Yea52ade12020-08-27 16:49:20 -07003822 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003823 InputMapperTest::SetUp();
3824
Michael Wright17db18e2020-06-26 20:51:44 +01003825 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003826 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003827 }
3828
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003829 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3830 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003831
Michael Wrighta9cf4192022-12-01 23:46:39 +00003832 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003833 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3834 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3835 }
3836
3837 void prepareSecondaryDisplay() {
3838 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003839 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003840 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003841 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003842
3843 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3844 float pressure) {
3845 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3846 0.0f, 0.0f, 0.0f, EPSILON));
3847 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003848};
3849
3850const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3851
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003852void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3853 int32_t originalY, int32_t rotatedX,
3854 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003855 NotifyMotionArgs args;
3856
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3861 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003862 ASSERT_NO_FATAL_FAILURE(
3863 assertCursorPointerCoords(args.pointerCoords[0],
3864 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3865 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866}
3867
3868TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003869 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003870 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003871
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003872 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003873}
3874
3875TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003877 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003879 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003880}
3881
3882TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003883 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003884 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003885
3886 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003887 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003888
3889 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003890 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3891 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003892 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3893 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3894
3895 // When the bounds are set, then there should be a valid motion range.
3896 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3897
3898 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003899 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003900
3901 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3902 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3903 1, 800 - 1, 0.0f, 0.0f));
3904 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3905 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3906 2, 480 - 1, 0.0f, 0.0f));
3907 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3908 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3909 0.0f, 1.0f, 0.0f, 0.0f));
3910}
3911
3912TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003914 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003915
3916 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003917 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918
3919 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3920 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3921 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3922 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3923 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3924 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3925 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3926 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3927 0.0f, 1.0f, 0.0f, 0.0f));
3928}
3929
3930TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003932 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933
arthurhungdcef2dc2020-08-11 14:47:50 +08003934 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003935
3936 NotifyMotionArgs args;
3937
3938 // Button press.
3939 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003940 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3943 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3944 ASSERT_EQ(DEVICE_ID, args.deviceId);
3945 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3946 ASSERT_EQ(uint32_t(0), args.policyFlags);
3947 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3948 ASSERT_EQ(0, args.flags);
3949 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3950 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3951 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07003952 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003954 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003955 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003956 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3957 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3958 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3959
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3961 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3962 ASSERT_EQ(DEVICE_ID, args.deviceId);
3963 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3964 ASSERT_EQ(uint32_t(0), args.policyFlags);
3965 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3966 ASSERT_EQ(0, args.flags);
3967 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3968 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3969 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07003970 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003971 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003972 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003973 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003974 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3975 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3976 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3977
Michael Wrightd02c5b62014-02-10 15:10:22 -08003978 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003979 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3980 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3982 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3983 ASSERT_EQ(DEVICE_ID, args.deviceId);
3984 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3985 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003986 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3987 ASSERT_EQ(0, args.flags);
3988 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3989 ASSERT_EQ(0, args.buttonState);
3990 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07003991 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003992 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003993 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003994 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003995 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3996 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3997 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3998
3999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4000 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4001 ASSERT_EQ(DEVICE_ID, args.deviceId);
4002 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4003 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004004 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4005 ASSERT_EQ(0, args.flags);
4006 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4007 ASSERT_EQ(0, args.buttonState);
4008 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004009 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004011 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004012 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004013 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4014 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4015 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4016}
4017
4018TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004020 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021
4022 NotifyMotionArgs args;
4023
4024 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004029 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4030 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4031 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004032
4033 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004038 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4039 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004040}
4041
4042TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004044 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045
4046 NotifyMotionArgs args;
4047
4048 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4052 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
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
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4056 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004057 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004058
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004064 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004065
4066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004068 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004069}
4070
4071TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004073 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074
4075 NotifyMotionArgs args;
4076
4077 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4083 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004084 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4085 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4086 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4089 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004090 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4091 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4092 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004093
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004100 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4101 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4102 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103
4104 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004105 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004108 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004109 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004110
4111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004113 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114}
4115
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004116TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004117 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004118 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004119 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4120 // need to be rotated.
4121 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004122 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004123
Michael Wrighta9cf4192022-12-01 23:46:39 +00004124 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4130 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4133}
4134
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004135TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004136 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004138 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4139 // orientation-aware are affected by display rotation.
Arpit Singhd8510bd2023-04-27 12:48:15 +00004140 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004142 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004143 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4147 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4148 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4150 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4151 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4152
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004153 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004154 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004155 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4156 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4157 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4158 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4159 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4160 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4161 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4162 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004163
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004164 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004165 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4167 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4168 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4169 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4170 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4171 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4172 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4173 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4174
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004175 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004176 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004177 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4178 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4179 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4180 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4181 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4182 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4183 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185}
4186
4187TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004188 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004189 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004190
4191 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4192 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193
4194 NotifyMotionArgs motionArgs;
4195 NotifyKeyArgs keyArgs;
4196
4197 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004198 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4201 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4202 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004203 ASSERT_NO_FATAL_FAILURE(
4204 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4207 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4208 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004209 ASSERT_NO_FATAL_FAILURE(
4210 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004211
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004215 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004217 ASSERT_NO_FATAL_FAILURE(
4218 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219
4220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004221 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004223 ASSERT_NO_FATAL_FAILURE(
4224 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004225
4226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004228 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004229 ASSERT_NO_FATAL_FAILURE(
4230 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231
4232 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4237 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4238 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004239 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004240 ASSERT_NO_FATAL_FAILURE(
4241 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4244 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4245 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004246 ASSERT_NO_FATAL_FAILURE(
4247 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004248
4249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4250 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4251 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004252 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004253 ASSERT_NO_FATAL_FAILURE(
4254 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004255
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004259 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004261 ASSERT_NO_FATAL_FAILURE(
4262 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004263
4264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004266 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004267 ASSERT_NO_FATAL_FAILURE(
4268 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004273 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4274 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004275 ASSERT_NO_FATAL_FAILURE(
4276 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004279
4280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004283 ASSERT_NO_FATAL_FAILURE(
4284 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004285
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4287 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004289 ASSERT_NO_FATAL_FAILURE(
4290 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291
4292 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4296 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4297 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004298
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004302 ASSERT_NO_FATAL_FAILURE(
4303 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004304
4305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4306 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4307 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004308 ASSERT_NO_FATAL_FAILURE(
4309 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4312 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004314 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004315 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004316 ASSERT_NO_FATAL_FAILURE(
4317 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004318
4319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004321 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004322
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004323 ASSERT_NO_FATAL_FAILURE(
4324 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4326 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4327 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4328
4329 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4333 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4334 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004335
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004337 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004339 ASSERT_NO_FATAL_FAILURE(
4340 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004341
4342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4343 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4344 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004345 ASSERT_NO_FATAL_FAILURE(
4346 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004351 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004353 ASSERT_NO_FATAL_FAILURE(
4354 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355
4356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4357 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4358 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004359 ASSERT_NO_FATAL_FAILURE(
4360 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004361
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4363 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4364 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4365
4366 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4370 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4371 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004372
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004374 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004376 ASSERT_NO_FATAL_FAILURE(
4377 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004378
4379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4380 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4381 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004382 ASSERT_NO_FATAL_FAILURE(
4383 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004388 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004390 ASSERT_NO_FATAL_FAILURE(
4391 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004392
4393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4394 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4395 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004396 ASSERT_NO_FATAL_FAILURE(
4397 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004398
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4400 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4401 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4402
4403 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4407 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4408 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004409
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004411 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004413 ASSERT_NO_FATAL_FAILURE(
4414 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004415
4416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4417 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4418 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004419 ASSERT_NO_FATAL_FAILURE(
4420 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004425 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004427 ASSERT_NO_FATAL_FAILURE(
4428 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004429
4430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4431 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4432 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004433 ASSERT_NO_FATAL_FAILURE(
4434 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004435
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4437 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4438 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4439}
4440
4441TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004443 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444
4445 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4446 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447
4448 NotifyMotionArgs args;
4449
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004454 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4455 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4457 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 +00004458 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004459}
4460
4461TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004462 addConfigurationProperty("cursor.mode", "pointer");
4463 mFakePolicy->setPointerCapture(true);
Arpit Singhd8510bd2023-04-27 12:48:15 +00004464 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004465
4466 NotifyDeviceResetArgs resetArgs;
4467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4468 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4469 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4470
4471 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4472 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004473
4474 NotifyMotionArgs args;
4475
4476 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4481 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4484 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 +00004485 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004486
4487 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4491 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4492 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4494 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4496 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4499 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4500
4501 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004502 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4503 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4505 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4506 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4508 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4510 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4511 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4513 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4514
4515 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4520 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4523 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 +00004524 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004525
4526 // Disable pointer capture and check that the device generation got bumped
4527 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004528 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004529 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004530 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004531 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004532
4533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004534 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4535
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4538 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4540 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4543 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 +00004544 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545}
4546
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004547/**
4548 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4549 * pointer acceleration or speed processing should not be applied.
4550 */
4551TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4552 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004553 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4554 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004555 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhd8510bd2023-04-27 12:48:15 +00004556 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004557
4558 NotifyDeviceResetArgs resetArgs;
4559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4560 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4561 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4562
4563 NotifyMotionArgs args;
4564
4565 // Move and verify scale is applied.
4566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4570 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4571 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4572 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4573 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4574 ASSERT_GT(relX, 10);
4575 ASSERT_GT(relY, 20);
4576
4577 // Enable Pointer Capture
4578 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004579 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004580 NotifyPointerCaptureChangedArgs captureArgs;
4581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4582 ASSERT_TRUE(captureArgs.request.enable);
4583
4584 // Move and verify scale is not applied.
4585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4589 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4591 ASSERT_EQ(10, args.pointerCoords[0].getX());
4592 ASSERT_EQ(20, args.pointerCoords[0].getY());
4593}
4594
Prabir Pradhan208360b2022-06-24 18:37:04 +00004595TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4596 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004597 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004598
4599 NotifyDeviceResetArgs resetArgs;
4600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4601 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4602 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4603
4604 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004605 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004606
4607 NotifyMotionArgs args;
4608
4609 // Verify that the coordinates are rotated.
4610 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4611 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4614 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4615 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4616 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4617 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4618
4619 // Enable Pointer Capture.
4620 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004621 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004622 NotifyPointerCaptureChangedArgs captureArgs;
4623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4624 ASSERT_TRUE(captureArgs.request.enable);
4625
4626 // Move and verify rotation is not applied.
4627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4631 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4633 ASSERT_EQ(10, args.pointerCoords[0].getX());
4634 ASSERT_EQ(20, args.pointerCoords[0].getY());
4635}
4636
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004637TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004638 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004639
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004640 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004641 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004642
4643 // Set up the secondary display as the display on which the pointer should be shown.
4644 // The InputDevice is not associated with any display.
4645 prepareSecondaryDisplay();
4646 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004647 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004648
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004649 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004650 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004651
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004652 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004653 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004657 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4658 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4659 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004660 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004661}
4662
4663TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004664 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004665
4666 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004667 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004668
4669 // Set up the secondary display as the display on which the pointer should be shown,
4670 // and associate the InputDevice with the secondary display.
4671 prepareSecondaryDisplay();
4672 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4673 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004674 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004675
4676 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4677 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004678
4679 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4680 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004683 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4684 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4685 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004686 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004687}
4688
4689TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004690 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004691
4692 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004693 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004694 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4695
4696 // Associate the InputDevice with the secondary display.
4697 prepareSecondaryDisplay();
4698 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004699 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004700
4701 // The mapper should not generate any events because it is associated with a display that is
4702 // different from the pointer display.
4703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004707}
4708
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004709// --- BluetoothCursorInputMapperTest ---
4710
4711class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4712protected:
4713 void SetUp() override {
4714 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4715
4716 mFakePointerController = std::make_shared<FakePointerController>();
4717 mFakePolicy->setPointerController(mFakePointerController);
4718 }
4719};
4720
4721TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4722 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004723 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004724
4725 nsecs_t kernelEventTime = ARBITRARY_TIME;
4726 nsecs_t expectedEventTime = ARBITRARY_TIME;
4727 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4728 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4730 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4731 WithEventTime(expectedEventTime))));
4732
4733 // Process several events that come in quick succession, according to their timestamps.
4734 for (int i = 0; i < 3; i++) {
4735 constexpr static nsecs_t delta = ms2ns(1);
4736 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4737 kernelEventTime += delta;
4738 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4739
4740 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4741 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4743 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4744 WithEventTime(expectedEventTime))));
4745 }
4746}
4747
4748TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4749 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004750 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004751
4752 nsecs_t expectedEventTime = ARBITRARY_TIME;
4753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4756 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4757 WithEventTime(expectedEventTime))));
4758
4759 // Process several events with the same timestamp from the kernel.
4760 // Ensure that we do not generate events too far into the future.
4761 constexpr static int32_t numEvents =
4762 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4763 for (int i = 0; i < numEvents; i++) {
4764 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4765
4766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4769 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4770 WithEventTime(expectedEventTime))));
4771 }
4772
4773 // By processing more events with the same timestamp, we should not generate events with a
4774 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4775 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4776 for (int i = 0; i < 3; i++) {
4777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4780 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4781 WithEventTime(cappedEventTime))));
4782 }
4783}
4784
4785TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4786 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004787 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004788
4789 nsecs_t kernelEventTime = ARBITRARY_TIME;
4790 nsecs_t expectedEventTime = ARBITRARY_TIME;
4791 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4792 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4794 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4795 WithEventTime(expectedEventTime))));
4796
4797 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4798 // smoothening is not needed, its timestamp is not affected.
4799 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4800 expectedEventTime = kernelEventTime;
4801
4802 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4803 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4805 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4806 WithEventTime(expectedEventTime))));
4807}
4808
Michael Wrightd02c5b62014-02-10 15:10:22 -08004809// --- TouchInputMapperTest ---
4810
4811class TouchInputMapperTest : public InputMapperTest {
4812protected:
4813 static const int32_t RAW_X_MIN;
4814 static const int32_t RAW_X_MAX;
4815 static const int32_t RAW_Y_MIN;
4816 static const int32_t RAW_Y_MAX;
4817 static const int32_t RAW_TOUCH_MIN;
4818 static const int32_t RAW_TOUCH_MAX;
4819 static const int32_t RAW_TOOL_MIN;
4820 static const int32_t RAW_TOOL_MAX;
4821 static const int32_t RAW_PRESSURE_MIN;
4822 static const int32_t RAW_PRESSURE_MAX;
4823 static const int32_t RAW_ORIENTATION_MIN;
4824 static const int32_t RAW_ORIENTATION_MAX;
4825 static const int32_t RAW_DISTANCE_MIN;
4826 static const int32_t RAW_DISTANCE_MAX;
4827 static const int32_t RAW_TILT_MIN;
4828 static const int32_t RAW_TILT_MAX;
4829 static const int32_t RAW_ID_MIN;
4830 static const int32_t RAW_ID_MAX;
4831 static const int32_t RAW_SLOT_MIN;
4832 static const int32_t RAW_SLOT_MAX;
4833 static const float X_PRECISION;
4834 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004835 static const float X_PRECISION_VIRTUAL;
4836 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837
4838 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004839 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004840
4841 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4842
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004843 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004844 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004845
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846 enum Axes {
4847 POSITION = 1 << 0,
4848 TOUCH = 1 << 1,
4849 TOOL = 1 << 2,
4850 PRESSURE = 1 << 3,
4851 ORIENTATION = 1 << 4,
4852 MINOR = 1 << 5,
4853 ID = 1 << 6,
4854 DISTANCE = 1 << 7,
4855 TILT = 1 << 8,
4856 SLOT = 1 << 9,
4857 TOOL_TYPE = 1 << 10,
4858 };
4859
Michael Wrighta9cf4192022-12-01 23:46:39 +00004860 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004861 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004862 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004863 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004864 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865 int32_t toRawX(float displayX);
4866 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004867 int32_t toRotatedRawX(float displayX);
4868 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004869 float toCookedX(float rawX, float rawY);
4870 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004872 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004873 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004874 float toDisplayY(int32_t rawY, int32_t displayHeight);
4875
Michael Wrightd02c5b62014-02-10 15:10:22 -08004876};
4877
4878const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4879const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4880const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4881const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4882const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4883const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4884const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4885const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004886const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4887const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004888const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4889const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4890const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4891const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4892const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4893const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4894const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4895const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4896const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4897const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4898const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4899const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004900const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4901 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4902const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4903 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004904const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4905 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906
4907const float TouchInputMapperTest::GEOMETRIC_SCALE =
4908 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4909 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4910
4911const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4912 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4913 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4914};
4915
Michael Wrighta9cf4192022-12-01 23:46:39 +00004916void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004917 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4918 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004919}
4920
4921void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4922 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004923 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924}
4925
Michael Wrighta9cf4192022-12-01 23:46:39 +00004926void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004927 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4928 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4929 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004930}
4931
Michael Wrightd02c5b62014-02-10 15:10:22 -08004932void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004933 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4934 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4935 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4936 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004937}
4938
Jason Gerecke489fda82012-09-07 17:19:40 -07004939void TouchInputMapperTest::prepareLocationCalibration() {
4940 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4941}
4942
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943int32_t TouchInputMapperTest::toRawX(float displayX) {
4944 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4945}
4946
4947int32_t TouchInputMapperTest::toRawY(float displayY) {
4948 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4949}
4950
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004951int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4952 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4953}
4954
4955int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4956 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4957}
4958
Jason Gerecke489fda82012-09-07 17:19:40 -07004959float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4960 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4961 return rawX;
4962}
4963
4964float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4965 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4966 return rawY;
4967}
4968
Michael Wrightd02c5b62014-02-10 15:10:22 -08004969float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004970 return toDisplayX(rawX, DISPLAY_WIDTH);
4971}
4972
4973float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4974 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975}
4976
4977float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004978 return toDisplayY(rawY, DISPLAY_HEIGHT);
4979}
4980
4981float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4982 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983}
4984
4985
4986// --- SingleTouchInputMapperTest ---
4987
4988class SingleTouchInputMapperTest : public TouchInputMapperTest {
4989protected:
4990 void prepareButtons();
4991 void prepareAxes(int axes);
4992
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004993 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4994 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4995 void processUp(SingleTouchInputMapper& mappery);
4996 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4997 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4998 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4999 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5000 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5001 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002};
5003
5004void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005005 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005006}
5007
5008void SingleTouchInputMapperTest::prepareAxes(int axes) {
5009 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005010 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5011 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012 }
5013 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005014 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5015 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016 }
5017 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005018 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5019 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020 }
5021 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005022 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5023 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005024 }
5025 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005026 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5027 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005028 }
5029}
5030
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005031void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035}
5036
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005037void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040}
5041
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005042void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005044}
5045
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005046void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005048}
5049
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005050void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5051 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005053}
5054
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005055void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057}
5058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005059void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5060 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063}
5064
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005065void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5066 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068}
5069
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005070void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072}
5073
Michael Wrightd02c5b62014-02-10 15:10:22 -08005074TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075 prepareButtons();
5076 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00005077 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078
Josep del Río2d8c79a2023-01-23 19:33:50 +00005079 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080}
5081
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083 prepareButtons();
5084 prepareAxes(POSITION);
5085 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00005086 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005088 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005089}
5090
5091TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005092 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005093 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005094 prepareButtons();
5095 prepareAxes(POSITION);
5096 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005097 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005098
5099 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005100 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005101
5102 // Virtual key is down.
5103 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5104 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5105 processDown(mapper, x, y);
5106 processSync(mapper);
5107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5108
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005109 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110
5111 // Virtual key is up.
5112 processUp(mapper);
5113 processSync(mapper);
5114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5115
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005116 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005117}
5118
5119TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005120 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005121 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005122 prepareButtons();
5123 prepareAxes(POSITION);
5124 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005125 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005126
5127 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005128 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129
5130 // Virtual key is down.
5131 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5132 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5133 processDown(mapper, x, y);
5134 processSync(mapper);
5135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5136
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005137 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138
5139 // Virtual key is up.
5140 processUp(mapper);
5141 processSync(mapper);
5142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005144 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145}
5146
5147TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005149 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150 prepareButtons();
5151 prepareAxes(POSITION);
5152 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005153 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005154
Michael Wrightd02c5b62014-02-10 15:10:22 -08005155 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005156 ASSERT_TRUE(
5157 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005158 ASSERT_TRUE(flags[0]);
5159 ASSERT_FALSE(flags[1]);
5160}
5161
5162TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005163 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005164 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005165 prepareButtons();
5166 prepareAxes(POSITION);
5167 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005168 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169
arthurhungdcef2dc2020-08-11 14:47:50 +08005170 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005171
5172 NotifyKeyArgs args;
5173
5174 // Press virtual key.
5175 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5176 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5177 processDown(mapper, x, y);
5178 processSync(mapper);
5179
5180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5181 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5182 ASSERT_EQ(DEVICE_ID, args.deviceId);
5183 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5184 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5185 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5186 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5187 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5188 ASSERT_EQ(KEY_HOME, args.scanCode);
5189 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5190 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5191
5192 // Release virtual key.
5193 processUp(mapper);
5194 processSync(mapper);
5195
5196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5197 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5198 ASSERT_EQ(DEVICE_ID, args.deviceId);
5199 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5200 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5201 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5202 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5203 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5204 ASSERT_EQ(KEY_HOME, args.scanCode);
5205 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5206 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5207
5208 // Should not have sent any motions.
5209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5210}
5211
5212TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005213 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005214 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215 prepareButtons();
5216 prepareAxes(POSITION);
5217 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005218 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005219
arthurhungdcef2dc2020-08-11 14:47:50 +08005220 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005221
5222 NotifyKeyArgs keyArgs;
5223
5224 // Press virtual key.
5225 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5226 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5227 processDown(mapper, x, y);
5228 processSync(mapper);
5229
5230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5231 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5232 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5233 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5234 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5235 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5236 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5237 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5238 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5239 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5240 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5241
5242 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5243 // into the display area.
5244 y -= 100;
5245 processMove(mapper, x, y);
5246 processSync(mapper);
5247
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5249 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5250 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5251 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5252 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5253 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5254 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5255 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5256 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5257 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5258 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5259 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5260
5261 NotifyMotionArgs motionArgs;
5262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5263 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5264 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5265 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5266 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5267 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5268 ASSERT_EQ(0, motionArgs.flags);
5269 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5270 ASSERT_EQ(0, motionArgs.buttonState);
5271 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005272 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005273 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005274 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5276 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5277 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5278 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5279 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5280
5281 // Keep moving out of bounds. Should generate a pointer move.
5282 y -= 50;
5283 processMove(mapper, x, y);
5284 processSync(mapper);
5285
5286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5287 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5288 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5289 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5290 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5292 ASSERT_EQ(0, motionArgs.flags);
5293 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5294 ASSERT_EQ(0, motionArgs.buttonState);
5295 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005296 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005297 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005298 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5300 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5301 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5302 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5303 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5304
5305 // Release out of bounds. Should generate a pointer up.
5306 processUp(mapper);
5307 processSync(mapper);
5308
5309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5310 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5311 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5312 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5313 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5314 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5315 ASSERT_EQ(0, motionArgs.flags);
5316 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5317 ASSERT_EQ(0, motionArgs.buttonState);
5318 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005319 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005321 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5323 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5324 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5325 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5326 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5327
5328 // Should not have sent any more keys or motions.
5329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5331}
5332
5333TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005334 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005335 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005336 prepareButtons();
5337 prepareAxes(POSITION);
5338 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005339 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340
arthurhungdcef2dc2020-08-11 14:47:50 +08005341 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342
5343 NotifyMotionArgs motionArgs;
5344
5345 // Initially go down out of bounds.
5346 int32_t x = -10;
5347 int32_t y = -10;
5348 processDown(mapper, x, y);
5349 processSync(mapper);
5350
5351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5352
5353 // Move into the display area. Should generate a pointer down.
5354 x = 50;
5355 y = 75;
5356 processMove(mapper, x, y);
5357 processSync(mapper);
5358
5359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5360 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5361 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5362 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5363 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5364 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5365 ASSERT_EQ(0, motionArgs.flags);
5366 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5367 ASSERT_EQ(0, motionArgs.buttonState);
5368 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005369 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005370 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005371 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5373 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5374 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5375 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5376 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5377
5378 // Release. Should generate a pointer up.
5379 processUp(mapper);
5380 processSync(mapper);
5381
5382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5383 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5384 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5385 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5386 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5387 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5388 ASSERT_EQ(0, motionArgs.flags);
5389 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5390 ASSERT_EQ(0, motionArgs.buttonState);
5391 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005392 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005394 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5396 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5397 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5398 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5399 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5400
5401 // Should not have sent any more keys or motions.
5402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5404}
5405
Santos Cordonfa5cf462017-04-05 10:37:00 -07005406TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005407 addConfigurationProperty("touch.deviceType", "touchScreen");
5408 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5409
Michael Wrighta9cf4192022-12-01 23:46:39 +00005410 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005411 prepareButtons();
5412 prepareAxes(POSITION);
5413 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005414 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005415
arthurhungdcef2dc2020-08-11 14:47:50 +08005416 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005417
5418 NotifyMotionArgs motionArgs;
5419
5420 // Down.
5421 int32_t x = 100;
5422 int32_t y = 125;
5423 processDown(mapper, x, y);
5424 processSync(mapper);
5425
5426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5427 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5428 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5429 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5430 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5431 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5432 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5433 ASSERT_EQ(0, motionArgs.flags);
5434 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5435 ASSERT_EQ(0, motionArgs.buttonState);
5436 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005437 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005438 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005439 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5441 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5442 1, 0, 0, 0, 0, 0, 0, 0));
5443 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5444 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5445 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5446
5447 // Move.
5448 x += 50;
5449 y += 75;
5450 processMove(mapper, x, y);
5451 processSync(mapper);
5452
5453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5454 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5455 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5456 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5457 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5458 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5460 ASSERT_EQ(0, motionArgs.flags);
5461 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5462 ASSERT_EQ(0, motionArgs.buttonState);
5463 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005464 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005465 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005466 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5468 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5469 1, 0, 0, 0, 0, 0, 0, 0));
5470 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5471 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5472 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5473
5474 // Up.
5475 processUp(mapper);
5476 processSync(mapper);
5477
5478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5479 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5480 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5481 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5482 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5483 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5484 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5485 ASSERT_EQ(0, motionArgs.flags);
5486 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5487 ASSERT_EQ(0, motionArgs.buttonState);
5488 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005489 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005490 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005491 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5493 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5494 1, 0, 0, 0, 0, 0, 0, 0));
5495 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5496 ASSERT_NEAR(Y_PRECISION_VIRTUAL, 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08005504TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005506 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005507 prepareButtons();
5508 prepareAxes(POSITION);
5509 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005510 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511
arthurhungdcef2dc2020-08-11 14:47:50 +08005512 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005513
5514 NotifyMotionArgs motionArgs;
5515
5516 // Down.
5517 int32_t x = 100;
5518 int32_t y = 125;
5519 processDown(mapper, x, y);
5520 processSync(mapper);
5521
5522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5523 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5524 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5525 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5526 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5527 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5528 ASSERT_EQ(0, motionArgs.flags);
5529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5530 ASSERT_EQ(0, motionArgs.buttonState);
5531 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005532 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005533 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005534 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5536 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5537 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5538 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5539 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5540
5541 // Move.
5542 x += 50;
5543 y += 75;
5544 processMove(mapper, x, y);
5545 processSync(mapper);
5546
5547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5548 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5549 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5550 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5551 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5553 ASSERT_EQ(0, motionArgs.flags);
5554 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5555 ASSERT_EQ(0, motionArgs.buttonState);
5556 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005557 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005558 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005559 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5561 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5562 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5563 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5564 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5565
5566 // Up.
5567 processUp(mapper);
5568 processSync(mapper);
5569
5570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5571 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5572 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5573 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5574 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5575 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5576 ASSERT_EQ(0, motionArgs.flags);
5577 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5578 ASSERT_EQ(0, motionArgs.buttonState);
5579 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005580 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005581 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005582 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5584 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5585 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5586 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5587 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5588
5589 // Should not have sent any more keys or motions.
5590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5592}
5593
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005594TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005595 addConfigurationProperty("touch.deviceType", "touchScreen");
5596 prepareButtons();
5597 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005598 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5599 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singh56adebc2023-04-25 13:56:05 +00005600 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005601
5602 NotifyMotionArgs args;
5603
5604 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005605 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005606 processDown(mapper, toRawX(50), toRawY(75));
5607 processSync(mapper);
5608
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5610 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5611 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5612
5613 processUp(mapper);
5614 processSync(mapper);
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5616}
5617
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005618TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005619 addConfigurationProperty("touch.deviceType", "touchScreen");
5620 prepareButtons();
5621 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005622 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5623 // orientation-aware are affected by display rotation.
5624 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00005625 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005626
5627 NotifyMotionArgs args;
5628
5629 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005630 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005631 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005632 processDown(mapper, toRawX(50), toRawY(75));
5633 processSync(mapper);
5634
5635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5636 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5637 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5638
5639 processUp(mapper);
5640 processSync(mapper);
5641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5642
5643 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005644 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005645 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005646 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005647 processSync(mapper);
5648
5649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5650 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5651 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5652
5653 processUp(mapper);
5654 processSync(mapper);
5655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5656
5657 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005658 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005659 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005660 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5661 processSync(mapper);
5662
5663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5664 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5665 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5666
5667 processUp(mapper);
5668 processSync(mapper);
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5670
5671 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005672 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005673 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005674 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005675 processSync(mapper);
5676
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5678 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5679 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5680
5681 processUp(mapper);
5682 processSync(mapper);
5683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5684}
5685
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005686TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5687 addConfigurationProperty("touch.deviceType", "touchScreen");
5688 prepareButtons();
5689 prepareAxes(POSITION);
5690 addConfigurationProperty("touch.orientationAware", "1");
5691 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5692 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005693 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005694 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005695 NotifyMotionArgs args;
5696
5697 // Orientation 0.
5698 processDown(mapper, toRawX(50), toRawY(75));
5699 processSync(mapper);
5700
5701 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5702 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5703 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5704
5705 processUp(mapper);
5706 processSync(mapper);
5707 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5708}
5709
5710TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5711 addConfigurationProperty("touch.deviceType", "touchScreen");
5712 prepareButtons();
5713 prepareAxes(POSITION);
5714 addConfigurationProperty("touch.orientationAware", "1");
5715 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5716 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005717 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005718 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005719 NotifyMotionArgs args;
5720
5721 // Orientation 90.
5722 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5723 processSync(mapper);
5724
5725 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5726 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5727 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5728
5729 processUp(mapper);
5730 processSync(mapper);
5731 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5732}
5733
5734TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5735 addConfigurationProperty("touch.deviceType", "touchScreen");
5736 prepareButtons();
5737 prepareAxes(POSITION);
5738 addConfigurationProperty("touch.orientationAware", "1");
5739 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5740 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005741 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005742 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005743 NotifyMotionArgs args;
5744
5745 // Orientation 180.
5746 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5747 processSync(mapper);
5748
5749 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5750 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5751 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5752
5753 processUp(mapper);
5754 processSync(mapper);
5755 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5756}
5757
5758TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5759 addConfigurationProperty("touch.deviceType", "touchScreen");
5760 prepareButtons();
5761 prepareAxes(POSITION);
5762 addConfigurationProperty("touch.orientationAware", "1");
5763 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5764 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005765 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005766 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005767 NotifyMotionArgs args;
5768
5769 // Orientation 270.
5770 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5771 processSync(mapper);
5772
5773 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5774 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5775 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5776
5777 processUp(mapper);
5778 processSync(mapper);
5779 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5780}
5781
5782TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5783 addConfigurationProperty("touch.deviceType", "touchScreen");
5784 prepareButtons();
5785 prepareAxes(POSITION);
5786 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5787 // orientation-aware are affected by display rotation.
5788 addConfigurationProperty("touch.orientationAware", "0");
5789 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singh56adebc2023-04-25 13:56:05 +00005790 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005791
5792 NotifyMotionArgs args;
5793
5794 // Orientation 90, Rotation 0.
5795 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005796 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005797 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5798 processSync(mapper);
5799
5800 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5801 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5802 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5803
5804 processUp(mapper);
5805 processSync(mapper);
5806 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5807
5808 // Orientation 90, Rotation 90.
5809 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005810 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005811 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005812 processSync(mapper);
5813
5814 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5815 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5816 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5817
5818 processUp(mapper);
5819 processSync(mapper);
5820 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5821
5822 // Orientation 90, Rotation 180.
5823 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005824 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005825 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5826 processSync(mapper);
5827
5828 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5829 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5830 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5831
5832 processUp(mapper);
5833 processSync(mapper);
5834 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5835
5836 // Orientation 90, Rotation 270.
5837 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005838 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005839 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 -07005840 processSync(mapper);
5841
5842 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5843 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5844 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5845
5846 processUp(mapper);
5847 processSync(mapper);
5848 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5849}
5850
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005851TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5852 addConfigurationProperty("touch.deviceType", "touchScreen");
5853 prepareButtons();
5854 prepareAxes(POSITION);
5855 addConfigurationProperty("touch.orientationAware", "1");
5856 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005857 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005858
5859 // Set a physical frame in the display viewport.
5860 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5861 viewport->physicalLeft = 20;
5862 viewport->physicalTop = 600;
5863 viewport->physicalRight = 30;
5864 viewport->physicalBottom = 610;
5865 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005866 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005867
5868 // Start the touch.
5869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5870 processSync(mapper);
5871
5872 // Expect all input starting outside the physical frame to be ignored.
5873 const std::array<Point, 6> outsidePoints = {
5874 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5875 for (const auto& p : outsidePoints) {
5876 processMove(mapper, toRawX(p.x), toRawY(p.y));
5877 processSync(mapper);
5878 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5879 }
5880
5881 // Move the touch into the physical frame.
5882 processMove(mapper, toRawX(25), toRawY(605));
5883 processSync(mapper);
5884 NotifyMotionArgs args;
5885 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5886 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5887 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5888 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5889
5890 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5891 for (const auto& p : outsidePoints) {
5892 processMove(mapper, toRawX(p.x), toRawY(p.y));
5893 processSync(mapper);
5894 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5895 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5896 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5897 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5898 }
5899
5900 processUp(mapper);
5901 processSync(mapper);
5902 EXPECT_NO_FATAL_FAILURE(
5903 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5904}
5905
Harry Cutts1db43992023-06-19 17:05:07 +00005906TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
5907 std::shared_ptr<FakePointerController> fakePointerController =
5908 std::make_shared<FakePointerController>();
5909 mFakePolicy->setPointerController(fakePointerController);
5910
5911 addConfigurationProperty("touch.deviceType", "pointer");
5912 prepareAxes(POSITION);
5913 prepareDisplay(ui::ROTATION_0);
5914 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5915
5916 // Set a physical frame in the display viewport.
5917 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5918 viewport->physicalLeft = 20;
5919 viewport->physicalTop = 600;
5920 viewport->physicalRight = 30;
5921 viewport->physicalBottom = 610;
5922 mFakePolicy->updateViewport(*viewport);
5923 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5924
5925 // Start the touch.
5926 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5927 processSync(mapper);
5928
5929 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5930 // produced.
5931 const std::array<Point, 6> outsidePoints = {
5932 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5933 for (const auto& p : outsidePoints) {
5934 processMove(mapper, toRawX(p.x), toRawY(p.y));
5935 processSync(mapper);
5936 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5937 }
5938}
5939
Michael Wrightd02c5b62014-02-10 15:10:22 -08005940TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005942 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005943 prepareButtons();
5944 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singh56adebc2023-04-25 13:56:05 +00005945 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005946
5947 // These calculations are based on the input device calibration documentation.
5948 int32_t rawX = 100;
5949 int32_t rawY = 200;
5950 int32_t rawPressure = 10;
5951 int32_t rawToolMajor = 12;
5952 int32_t rawDistance = 2;
5953 int32_t rawTiltX = 30;
5954 int32_t rawTiltY = 110;
5955
5956 float x = toDisplayX(rawX);
5957 float y = toDisplayY(rawY);
5958 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5959 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5960 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5961 float distance = float(rawDistance);
5962
5963 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5964 float tiltScale = M_PI / 180;
5965 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5966 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5967 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5968 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5969
5970 processDown(mapper, rawX, rawY);
5971 processPressure(mapper, rawPressure);
5972 processToolMajor(mapper, rawToolMajor);
5973 processDistance(mapper, rawDistance);
5974 processTilt(mapper, rawTiltX, rawTiltY);
5975 processSync(mapper);
5976
5977 NotifyMotionArgs args;
5978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5980 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5981 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5982}
5983
Jason Gerecke489fda82012-09-07 17:19:40 -07005984TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005985 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005986 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005987 prepareLocationCalibration();
5988 prepareButtons();
5989 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00005990 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005991
5992 int32_t rawX = 100;
5993 int32_t rawY = 200;
5994
5995 float x = toDisplayX(toCookedX(rawX, rawY));
5996 float y = toDisplayY(toCookedY(rawX, rawY));
5997
5998 processDown(mapper, rawX, rawY);
5999 processSync(mapper);
6000
6001 NotifyMotionArgs args;
6002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6004 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6005}
6006
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006009 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006010 prepareButtons();
6011 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006012 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013
6014 NotifyMotionArgs motionArgs;
6015 NotifyKeyArgs keyArgs;
6016
6017 processDown(mapper, 100, 200);
6018 processSync(mapper);
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6020 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6021 ASSERT_EQ(0, motionArgs.buttonState);
6022
6023 // press BTN_LEFT, release BTN_LEFT
6024 processKey(mapper, BTN_LEFT, 1);
6025 processSync(mapper);
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6027 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6028 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6029
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6031 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6032 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6033
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034 processKey(mapper, BTN_LEFT, 0);
6035 processSync(mapper);
6036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006037 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006038 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006039
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006042 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006043
6044 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6045 processKey(mapper, BTN_RIGHT, 1);
6046 processKey(mapper, BTN_MIDDLE, 1);
6047 processSync(mapper);
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6050 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6051 motionArgs.buttonState);
6052
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6054 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6055 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6056
6057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6058 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6059 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6060 motionArgs.buttonState);
6061
Michael Wrightd02c5b62014-02-10 15:10:22 -08006062 processKey(mapper, BTN_RIGHT, 0);
6063 processSync(mapper);
6064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006065 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006066 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006067
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006070 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071
6072 processKey(mapper, BTN_MIDDLE, 0);
6073 processSync(mapper);
6074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006075 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006077
6078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006079 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006080 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081
6082 // press BTN_BACK, release BTN_BACK
6083 processKey(mapper, BTN_BACK, 1);
6084 processSync(mapper);
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6086 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6087 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006088
Michael Wrightd02c5b62014-02-10 15:10:22 -08006089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006091 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6092
6093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6094 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6095 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006096
6097 processKey(mapper, BTN_BACK, 0);
6098 processSync(mapper);
6099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006100 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006101 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006102
6103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006105 ASSERT_EQ(0, motionArgs.buttonState);
6106
Michael Wrightd02c5b62014-02-10 15:10:22 -08006107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6108 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6109 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6110
6111 // press BTN_SIDE, release BTN_SIDE
6112 processKey(mapper, BTN_SIDE, 1);
6113 processSync(mapper);
6114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6115 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6116 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006117
Michael Wrightd02c5b62014-02-10 15:10:22 -08006118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006119 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006120 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6121
6122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6123 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6124 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006125
6126 processKey(mapper, BTN_SIDE, 0);
6127 processSync(mapper);
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006129 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006130 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006131
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006134 ASSERT_EQ(0, motionArgs.buttonState);
6135
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6137 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6138 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6139
6140 // press BTN_FORWARD, release BTN_FORWARD
6141 processKey(mapper, BTN_FORWARD, 1);
6142 processSync(mapper);
6143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6144 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6145 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006146
Michael Wrightd02c5b62014-02-10 15:10:22 -08006147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006149 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6150
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6152 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6153 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006154
6155 processKey(mapper, BTN_FORWARD, 0);
6156 processSync(mapper);
6157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006158 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006159 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006160
6161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006162 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006163 ASSERT_EQ(0, motionArgs.buttonState);
6164
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6166 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6167 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6168
6169 // press BTN_EXTRA, release BTN_EXTRA
6170 processKey(mapper, BTN_EXTRA, 1);
6171 processSync(mapper);
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6173 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6174 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006175
Michael Wrightd02c5b62014-02-10 15:10:22 -08006176 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6179
6180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6181 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6182 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183
6184 processKey(mapper, BTN_EXTRA, 0);
6185 processSync(mapper);
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006187 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006188 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006189
6190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006192 ASSERT_EQ(0, motionArgs.buttonState);
6193
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6195 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6196 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6197
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6199
Michael Wrightd02c5b62014-02-10 15:10:22 -08006200 // press BTN_STYLUS, release BTN_STYLUS
6201 processKey(mapper, BTN_STYLUS, 1);
6202 processSync(mapper);
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006205 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6206
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6208 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6209 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210
6211 processKey(mapper, BTN_STYLUS, 0);
6212 processSync(mapper);
6213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006214 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006215 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006216
6217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006219 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220
6221 // press BTN_STYLUS2, release BTN_STYLUS2
6222 processKey(mapper, BTN_STYLUS2, 1);
6223 processSync(mapper);
6224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006226 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6227
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6229 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6230 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006231
6232 processKey(mapper, BTN_STYLUS2, 0);
6233 processSync(mapper);
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006235 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006237
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006240 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006241
6242 // release touch
6243 processUp(mapper);
6244 processSync(mapper);
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6246 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6247 ASSERT_EQ(0, motionArgs.buttonState);
6248}
6249
6250TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006251 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006252 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253 prepareButtons();
6254 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006255 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006256
6257 NotifyMotionArgs motionArgs;
6258
6259 // default tool type is finger
6260 processDown(mapper, 100, 200);
6261 processSync(mapper);
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6263 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006264 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006265
6266 // eraser
6267 processKey(mapper, BTN_TOOL_RUBBER, 1);
6268 processSync(mapper);
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006271 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272
6273 // stylus
6274 processKey(mapper, BTN_TOOL_RUBBER, 0);
6275 processKey(mapper, BTN_TOOL_PEN, 1);
6276 processSync(mapper);
6277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006279 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006280
6281 // brush
6282 processKey(mapper, BTN_TOOL_PEN, 0);
6283 processKey(mapper, BTN_TOOL_BRUSH, 1);
6284 processSync(mapper);
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006287 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288
6289 // pencil
6290 processKey(mapper, BTN_TOOL_BRUSH, 0);
6291 processKey(mapper, BTN_TOOL_PENCIL, 1);
6292 processSync(mapper);
6293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6294 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006295 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006297 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298 processKey(mapper, BTN_TOOL_PENCIL, 0);
6299 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6300 processSync(mapper);
6301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006303 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304
6305 // mouse
6306 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6307 processKey(mapper, BTN_TOOL_MOUSE, 1);
6308 processSync(mapper);
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006311 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006312
6313 // lens
6314 processKey(mapper, BTN_TOOL_MOUSE, 0);
6315 processKey(mapper, BTN_TOOL_LENS, 1);
6316 processSync(mapper);
6317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006319 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320
6321 // double-tap
6322 processKey(mapper, BTN_TOOL_LENS, 0);
6323 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6324 processSync(mapper);
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006327 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006328
6329 // triple-tap
6330 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6331 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6332 processSync(mapper);
6333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006335 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336
6337 // quad-tap
6338 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6339 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6340 processSync(mapper);
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006343 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344
6345 // finger
6346 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6347 processKey(mapper, BTN_TOOL_FINGER, 1);
6348 processSync(mapper);
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006351 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006352
6353 // stylus trumps finger
6354 processKey(mapper, BTN_TOOL_PEN, 1);
6355 processSync(mapper);
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006358 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359
6360 // eraser trumps stylus
6361 processKey(mapper, BTN_TOOL_RUBBER, 1);
6362 processSync(mapper);
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006365 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006366
6367 // mouse trumps eraser
6368 processKey(mapper, BTN_TOOL_MOUSE, 1);
6369 processSync(mapper);
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006372 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006373
6374 // back to default tool type
6375 processKey(mapper, BTN_TOOL_MOUSE, 0);
6376 processKey(mapper, BTN_TOOL_RUBBER, 0);
6377 processKey(mapper, BTN_TOOL_PEN, 0);
6378 processKey(mapper, BTN_TOOL_FINGER, 0);
6379 processSync(mapper);
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006382 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006383}
6384
6385TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006387 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006388 prepareButtons();
6389 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006390 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +00006391 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006392
6393 NotifyMotionArgs motionArgs;
6394
6395 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6396 processKey(mapper, BTN_TOOL_FINGER, 1);
6397 processMove(mapper, 100, 200);
6398 processSync(mapper);
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6400 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6402 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6403
6404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6405 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6407 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6408
6409 // move a little
6410 processMove(mapper, 150, 250);
6411 processSync(mapper);
6412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6413 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6415 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6416
6417 // down when BTN_TOUCH is pressed, pressure defaults to 1
6418 processKey(mapper, BTN_TOUCH, 1);
6419 processSync(mapper);
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6421 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6423 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6424
6425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6426 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6428 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6429
6430 // up when BTN_TOUCH is released, hover restored
6431 processKey(mapper, BTN_TOUCH, 0);
6432 processSync(mapper);
6433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6434 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6436 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6437
6438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6439 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6441 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6442
6443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6444 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6446 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6447
6448 // exit hover when pointer goes away
6449 processKey(mapper, BTN_TOOL_FINGER, 0);
6450 processSync(mapper);
6451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6452 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6454 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6455}
6456
6457TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006458 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006459 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460 prepareButtons();
6461 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006462 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006463
6464 NotifyMotionArgs motionArgs;
6465
6466 // initially hovering because pressure is 0
6467 processDown(mapper, 100, 200);
6468 processPressure(mapper, 0);
6469 processSync(mapper);
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6473 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6474
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6476 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6478 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6479
6480 // move a little
6481 processMove(mapper, 150, 250);
6482 processSync(mapper);
6483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6484 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6486 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6487
6488 // down when pressure is non-zero
6489 processPressure(mapper, RAW_PRESSURE_MAX);
6490 processSync(mapper);
6491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6492 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6494 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6495
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6497 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6499 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6500
6501 // up when pressure becomes 0, hover restored
6502 processPressure(mapper, 0);
6503 processSync(mapper);
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6507 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6508
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6510 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6512 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6513
6514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6515 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6517 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6518
6519 // exit hover when pointer goes away
6520 processUp(mapper);
6521 processSync(mapper);
6522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6523 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6525 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6526}
6527
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006528TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6529 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006530 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006531 prepareButtons();
6532 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006533 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006534
6535 // Touch down.
6536 processDown(mapper, 100, 200);
6537 processPressure(mapper, 1);
6538 processSync(mapper);
6539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6540 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6541
6542 // Reset the mapper. This should cancel the ongoing gesture.
6543 resetMapper(mapper, ARBITRARY_TIME);
6544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6545 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6546
6547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6548}
6549
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006550TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6551 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006552 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006553 prepareButtons();
6554 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006555 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006556
6557 // Set the initial state for the touch pointer.
6558 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6559 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6560 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6561 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6562
6563 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006564 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6565 // does not generate any events.
6566 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006567
6568 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6569 // the recreated touch state to generate a down event.
6570 processSync(mapper);
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6572 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6573
6574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6575}
6576
lilinnan687e58f2022-07-19 16:00:50 +08006577TEST_F(SingleTouchInputMapperTest,
6578 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6579 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006580 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006581 prepareButtons();
6582 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006583 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006584 NotifyMotionArgs motionArgs;
6585
6586 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006587 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006588 processSync(mapper);
6589
6590 // We should receive a down event
6591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6592 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6593
6594 // Change display id
6595 clearViewports();
6596 prepareSecondaryDisplay(ViewportType::INTERNAL);
6597
6598 // We should receive a cancel event
6599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6600 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6601 // Then receive reset called
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6603}
6604
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006605TEST_F(SingleTouchInputMapperTest,
6606 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6607 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006608 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006609 prepareButtons();
6610 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006611 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6613 NotifyMotionArgs motionArgs;
6614
6615 // Start a new gesture.
6616 processDown(mapper, 100, 200);
6617 processSync(mapper);
6618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6619 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6620
6621 // Make the viewport inactive. This will put the device in disabled mode.
6622 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6623 viewport->isActive = false;
6624 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006625 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006626
6627 // We should receive a cancel event for the ongoing gesture.
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6629 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6630 // Then we should be notified that the device was reset.
6631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6632
6633 // No events are generated while the viewport is inactive.
6634 processMove(mapper, 101, 201);
6635 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006636 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006637 processSync(mapper);
6638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6639
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006640 // Start a new gesture while the viewport is still inactive.
6641 processDown(mapper, 300, 400);
6642 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6643 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6644 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6645 processSync(mapper);
6646
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006647 // Make the viewport active again. The device should resume processing events.
6648 viewport->isActive = true;
6649 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006650 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006651
6652 // The device is reset because it changes back to direct mode, without generating any events.
6653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6655
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006656 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006657 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6659 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006660
6661 // No more events.
6662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6664}
6665
Prabir Pradhan211ba622022-10-31 21:09:21 +00006666TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6667 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006668 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006669 prepareButtons();
6670 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006671 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6673
6674 // Press a stylus button.
6675 processKey(mapper, BTN_STYLUS, 1);
6676 processSync(mapper);
6677
6678 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6679 processDown(mapper, 100, 200);
6680 processSync(mapper);
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6682 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6683 WithCoords(toDisplayX(100), toDisplayY(200)),
6684 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6686 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6687 WithCoords(toDisplayX(100), toDisplayY(200)),
6688 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6689
6690 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6691 // the button has not actually been released, since there will be no pointers through which the
6692 // button state can be reported. The event is generated at the location of the pointer before
6693 // it went up.
6694 processUp(mapper);
6695 processSync(mapper);
6696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6697 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6698 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6700 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6701 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6702}
6703
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006704TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6705 addConfigurationProperty("touch.deviceType", "touchScreen");
6706 prepareDisplay(ui::ROTATION_0);
6707 prepareButtons();
6708 prepareAxes(POSITION);
6709
6710 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6711
Arpit Singh56adebc2023-04-25 13:56:05 +00006712 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6714
6715 // Press a stylus button.
6716 processKey(mapper, BTN_STYLUS, 1);
6717 processSync(mapper);
6718
6719 // Start a touch gesture and ensure that the stylus button is not reported.
6720 processDown(mapper, 100, 200);
6721 processSync(mapper);
6722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6723 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6724
6725 // Release and press the stylus button again.
6726 processKey(mapper, BTN_STYLUS, 0);
6727 processSync(mapper);
6728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6729 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6730 processKey(mapper, BTN_STYLUS, 1);
6731 processSync(mapper);
6732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6733 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6734
6735 // Release the touch gesture.
6736 processUp(mapper);
6737 processSync(mapper);
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6739 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6740
6741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6742}
6743
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006744TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6745 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6746 prepareDisplay(ui::ROTATION_0);
6747 prepareButtons();
6748 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006749 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6751
6752 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6753}
6754
Seunghwan Choi356026c2023-02-01 14:37:25 +09006755TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6756 std::shared_ptr<FakePointerController> fakePointerController =
6757 std::make_shared<FakePointerController>();
6758 addConfigurationProperty("touch.deviceType", "touchScreen");
6759 prepareDisplay(ui::ROTATION_0);
6760 prepareButtons();
6761 prepareAxes(POSITION);
6762 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6763 mFakePolicy->setPointerController(fakePointerController);
6764 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singh56adebc2023-04-25 13:56:05 +00006765 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006766
6767 processKey(mapper, BTN_TOOL_PEN, 1);
6768 processMove(mapper, 100, 200);
6769 processSync(mapper);
6770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6771 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006772 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006773 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6774 ASSERT_TRUE(fakePointerController->isPointerShown());
6775 ASSERT_NO_FATAL_FAILURE(
6776 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6777}
6778
6779TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6780 std::shared_ptr<FakePointerController> fakePointerController =
6781 std::make_shared<FakePointerController>();
6782 addConfigurationProperty("touch.deviceType", "touchScreen");
6783 prepareDisplay(ui::ROTATION_0);
6784 prepareButtons();
6785 prepareAxes(POSITION);
6786 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6787 mFakePolicy->setPointerController(fakePointerController);
6788 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singh56adebc2023-04-25 13:56:05 +00006789 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006790
6791 processKey(mapper, BTN_TOOL_PEN, 1);
6792 processMove(mapper, 100, 200);
6793 processSync(mapper);
6794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6795 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006796 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006797 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6798 ASSERT_FALSE(fakePointerController->isPointerShown());
6799}
6800
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006801TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6802 // Initialize the device without setting device source to touch navigation.
6803 addConfigurationProperty("touch.deviceType", "touchScreen");
6804 prepareDisplay(ui::ROTATION_0);
6805 prepareButtons();
6806 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006807 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006808
6809 // Ensure that the device is created as a touchscreen, not touch navigation.
6810 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6811
6812 // Add device type association after the device was created.
6813 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6814
6815 // Send update to the mapper.
6816 std::list<NotifyArgs> unused2 =
6817 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006818 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006819
6820 // Check whether device type update was successful.
6821 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6822}
6823
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006824TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6825 // Initialize the device without setting device source to touch navigation.
6826 addConfigurationProperty("touch.deviceType", "touchScreen");
6827 prepareDisplay(ui::ROTATION_0);
6828 prepareButtons();
6829 prepareAxes(POSITION);
6830 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6831
6832 // Set a physical frame in the display viewport.
6833 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6834 viewport->physicalLeft = 0;
6835 viewport->physicalTop = 0;
6836 viewport->physicalRight = DISPLAY_WIDTH / 2;
6837 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6838 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006839 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006840
Arpit Singh56adebc2023-04-25 13:56:05 +00006841 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006842
6843 // Hovering inside the physical frame produces events.
6844 processKey(mapper, BTN_TOOL_PEN, 1);
6845 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6846 processSync(mapper);
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6848 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6850 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6851
6852 // Leaving the physical frame ends the hovering gesture.
6853 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6854 processSync(mapper);
6855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6856 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6857
6858 // Moving outside the physical frame does not produce events.
6859 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6860 processSync(mapper);
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6862
6863 // Re-entering the physical frame produces events.
6864 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6865 processSync(mapper);
6866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6867 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6869 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6870}
6871
Prabir Pradhan5632d622021-09-06 07:57:20 -07006872// --- TouchDisplayProjectionTest ---
6873
6874class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6875public:
6876 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6877 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6878 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006879 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6880 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6881 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006882 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006883 auto rotatedWidth = naturalDisplayWidth;
6884 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006885 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006886 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006887 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006888 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006889 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006890 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006891 inverseRotationFlags = ui::Transform::ROT_180;
6892 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006893 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006894 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006895 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006896 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006897 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006898 inverseRotationFlags = ui::Transform::ROT_0;
6899 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006900 }
6901
Prabir Pradhana9df3162022-12-05 23:57:27 +00006902 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006903 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6904
6905 std::optional<DisplayViewport> internalViewport =
6906 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6907 DisplayViewport& v = *internalViewport;
6908 v.displayId = DISPLAY_ID;
6909 v.orientation = orientation;
6910
6911 v.logicalLeft = 0;
6912 v.logicalTop = 0;
6913 v.logicalRight = 100;
6914 v.logicalBottom = 100;
6915
6916 v.physicalLeft = rotatedPhysicalDisplay.left;
6917 v.physicalTop = rotatedPhysicalDisplay.top;
6918 v.physicalRight = rotatedPhysicalDisplay.right;
6919 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6920
Prabir Pradhana9df3162022-12-05 23:57:27 +00006921 v.deviceWidth = rotatedWidth;
6922 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006923
6924 v.isActive = true;
6925 v.uniqueId = UNIQUE_ID;
6926 v.type = ViewportType::INTERNAL;
6927 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006928 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006929 }
6930
6931 void assertReceivedMove(const Point& point) {
6932 NotifyMotionArgs motionArgs;
6933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006935 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6937 1, 0, 0, 0, 0, 0, 0, 0));
6938 }
6939};
6940
6941TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6942 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006943 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006944
6945 prepareButtons();
6946 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006947 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006948
6949 NotifyMotionArgs motionArgs;
6950
6951 // Configure the DisplayViewport such that the logical display maps to a subsection of
6952 // the display panel called the physical display. Here, the physical display is bounded by the
6953 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6954 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6955 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6956 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6957
Michael Wrighta9cf4192022-12-01 23:46:39 +00006958 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006959 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6960
6961 // Touches outside the physical display should be ignored, and should not generate any
6962 // events. Ensure touches at the following points that lie outside of the physical display
6963 // area do not generate any events.
6964 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6965 processDown(mapper, toRawX(point.x), toRawY(point.y));
6966 processSync(mapper);
6967 processUp(mapper);
6968 processSync(mapper);
6969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6970 << "Unexpected event generated for touch outside physical display at point: "
6971 << point.x << ", " << point.y;
6972 }
6973 }
6974}
6975
6976TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6977 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006978 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006979
6980 prepareButtons();
6981 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006982 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006983
6984 NotifyMotionArgs motionArgs;
6985
6986 // Configure the DisplayViewport such that the logical display maps to a subsection of
6987 // the display panel called the physical display. Here, the physical display is bounded by the
6988 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6989 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6990
Michael Wrighta9cf4192022-12-01 23:46:39 +00006991 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006992 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6993
6994 // Touches that start outside the physical display should be ignored until it enters the
6995 // physical display bounds, at which point it should generate a down event. Start a touch at
6996 // the point (5, 100), which is outside the physical display bounds.
6997 static const Point kOutsidePoint{5, 100};
6998 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6999 processSync(mapper);
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7001
7002 // Move the touch into the physical display area. This should generate a pointer down.
7003 processMove(mapper, toRawX(11), toRawY(21));
7004 processSync(mapper);
7005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7006 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007007 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007008 ASSERT_NO_FATAL_FAILURE(
7009 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7010
7011 // Move the touch inside the physical display area. This should generate a pointer move.
7012 processMove(mapper, toRawX(69), toRawY(159));
7013 processSync(mapper);
7014 assertReceivedMove({69, 159});
7015
7016 // Move outside the physical display area. Since the pointer is already down, this should
7017 // now continue generating events.
7018 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7019 processSync(mapper);
7020 assertReceivedMove(kOutsidePoint);
7021
7022 // Release. This should generate a pointer up.
7023 processUp(mapper);
7024 processSync(mapper);
7025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7026 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7028 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7029
7030 // Ensure no more events were generated.
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7033 }
7034}
7035
Prabir Pradhana9df3162022-12-05 23:57:27 +00007036// --- TouchscreenPrecisionTests ---
7037
7038// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7039// in various orientations and with different display rotations. We configure the touchscreen to
7040// have a higher resolution than that of the display by an integer scale factor in each axis so that
7041// we can enforce that coordinates match precisely as expected.
7042class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7043 public ::testing::WithParamInterface<ui::Rotation> {
7044public:
7045 void SetUp() override {
7046 SingleTouchInputMapperTest::SetUp();
7047
7048 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7049 // four times the resolution of the display in the Y axis.
7050 prepareButtons();
7051 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007052 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7053 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007054 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007055 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7056 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007057 }
7058
7059 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7060 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7061 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7062 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7063
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007064 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7065 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7066
7067 static const int32_t PRECISION_RAW_X_FLAT = 16;
7068 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7069
7070 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7071 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7072
Prabir Pradhana9df3162022-12-05 23:57:27 +00007073 static const std::array<Point, 4> kRawCorners;
7074};
7075
7076const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7077 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7078 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7079 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7080 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7081}};
7082
7083// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7084// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7085// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7086TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7087 enum class Orientation {
7088 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7089 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7090 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7091 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7092 ftl_last = ORIENTATION_270,
7093 };
7094 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7095 Orientation::ORIENTATION_270;
7096 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7097 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7098 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7099 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7100 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7101 };
7102
7103 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7104
7105 // Configure the touchscreen as being installed in the one of the four different orientations
7106 // relative to the display.
7107 addConfigurationProperty("touch.deviceType", "touchScreen");
7108 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7109 prepareDisplay(ui::ROTATION_0);
7110
Arpit Singh56adebc2023-04-25 13:56:05 +00007111 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007112
7113 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7114 // orientations of either 90 or 270) this means the display's natural resolution will be
7115 // flipped.
7116 const bool displayRotated =
7117 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7118 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7119 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7120 const Rect physicalFrame{0, 0, width, height};
7121 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7122
7123 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7124 const float expectedPrecisionX = displayRotated ? 4 : 2;
7125 const float expectedPrecisionY = displayRotated ? 2 : 4;
7126
7127 // Test all four corners.
7128 for (int i = 0; i < 4; i++) {
7129 const auto& raw = kRawCorners[i];
7130 processDown(mapper, raw.x, raw.y);
7131 processSync(mapper);
7132 const auto& expected = expectedPoints[i];
7133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7134 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7135 WithCoords(expected.x, expected.y),
7136 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7137 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7138 << "with touchscreen orientation "
7139 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7140 << expected.x << ", " << expected.y << ").";
7141 processUp(mapper);
7142 processSync(mapper);
7143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7144 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7145 WithCoords(expected.x, expected.y))));
7146 }
7147}
7148
Prabir Pradhan82687402022-12-06 01:32:53 +00007149TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7150 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7151 kMappedCorners = {
7152 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7153 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7154 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7155 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7156 };
7157
7158 const ui::Rotation displayRotation = GetParam();
7159
7160 addConfigurationProperty("touch.deviceType", "touchScreen");
7161 prepareDisplay(displayRotation);
7162
Arpit Singh56adebc2023-04-25 13:56:05 +00007163 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007164
7165 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7166
7167 // Test all four corners.
7168 for (int i = 0; i < 4; i++) {
7169 const auto& expected = expectedPoints[i];
7170 const auto& raw = kRawCorners[i];
7171 processDown(mapper, raw.x, raw.y);
7172 processSync(mapper);
7173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7174 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7175 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7176 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7177 << "with display rotation " << ui::toCString(displayRotation)
7178 << ", expected point (" << expected.x << ", " << expected.y << ").";
7179 processUp(mapper);
7180 processSync(mapper);
7181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7182 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7183 WithCoords(expected.x, expected.y))));
7184 }
7185}
7186
Prabir Pradhan3e798762022-12-02 21:02:11 +00007187TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7188 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7189 kMappedCorners = {
7190 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7191 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7192 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7193 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7194 };
7195
7196 const ui::Rotation displayRotation = GetParam();
7197
7198 addConfigurationProperty("touch.deviceType", "touchScreen");
7199 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7200
Arpit Singh56adebc2023-04-25 13:56:05 +00007201 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007202
7203 // Ori 270, so width and height swapped
7204 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7205 prepareDisplay(displayRotation);
7206 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7207
7208 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7209
7210 // Test all four corners.
7211 for (int i = 0; i < 4; i++) {
7212 const auto& expected = expectedPoints[i];
7213 const auto& raw = kRawCorners[i];
7214 processDown(mapper, raw.x, raw.y);
7215 processSync(mapper);
7216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7217 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7218 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7219 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7220 << "with display rotation " << ui::toCString(displayRotation)
7221 << ", expected point (" << expected.x << ", " << expected.y << ").";
7222 processUp(mapper);
7223 processSync(mapper);
7224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7225 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7226 WithCoords(expected.x, expected.y))));
7227 }
7228}
7229
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007230TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7231 const ui::Rotation displayRotation = GetParam();
7232
7233 addConfigurationProperty("touch.deviceType", "touchScreen");
7234 prepareDisplay(displayRotation);
7235
7236 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singh56adebc2023-04-25 13:56:05 +00007237 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007238
7239 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7240 // MotionRanges use display pixels as their units
7241 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7242 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7243
7244 // The MotionRanges should be oriented in the rotated display's coordinate space
7245 const bool displayRotated =
7246 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7247
7248 constexpr float MAX_X = 479.5;
7249 constexpr float MAX_Y = 799.75;
7250 EXPECT_EQ(xRange->min, 0.f);
7251 EXPECT_EQ(yRange->min, 0.f);
7252 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7253 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7254
7255 EXPECT_EQ(xRange->flat, 8.f);
7256 EXPECT_EQ(yRange->flat, 8.f);
7257
7258 EXPECT_EQ(xRange->fuzz, 2.f);
7259 EXPECT_EQ(yRange->fuzz, 2.f);
7260
7261 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7262 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7263}
7264
Prabir Pradhana9df3162022-12-05 23:57:27 +00007265// Run the precision tests for all rotations.
7266INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7267 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7268 ui::ROTATION_270),
7269 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7270 return ftl::enum_string(testParamInfo.param);
7271 });
7272
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007273// --- ExternalStylusFusionTest ---
7274
7275class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7276public:
7277 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7278 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007279 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007280 prepareButtons();
7281 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00007282 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007283
7284 mStylusState.when = ARBITRARY_TIME;
7285 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007286 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007287 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007288 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007289 processExternalStylusState(mapper);
7290 return mapper;
7291 }
7292
7293 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7294 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7295 for (const NotifyArgs& args : generatedArgs) {
7296 mFakeListener->notify(args);
7297 }
7298 // Loop the reader to flush the input listener queue.
7299 mReader->loopOnce();
7300 return generatedArgs;
7301 }
7302
7303protected:
7304 StylusState mStylusState{};
7305 static constexpr uint32_t EXPECTED_SOURCE =
7306 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7307
7308 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7309 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007310 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007311
7312 // The first pointer is withheld.
7313 processDown(mapper, 100, 200);
7314 processSync(mapper);
7315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7316 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7317 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7318
7319 // The external stylus reports pressure. The withheld finger pointer is released as a
7320 // stylus.
7321 mStylusState.pressure = 1.f;
7322 processExternalStylusState(mapper);
7323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7324 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7325 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7326
7327 // Subsequent pointer events are not withheld.
7328 processMove(mapper, 101, 201);
7329 processSync(mapper);
7330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7331 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7332
7333 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7335 }
7336
7337 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7338 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7339
7340 // Releasing the touch pointer ends the gesture.
7341 processUp(mapper);
7342 processSync(mapper);
7343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7344 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007345 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007346
7347 mStylusState.pressure = 0.f;
7348 processExternalStylusState(mapper);
7349 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7351 }
7352
7353 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7354 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007355 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007356
7357 // The first pointer is withheld when an external stylus is connected,
7358 // and a timeout is requested.
7359 processDown(mapper, 100, 200);
7360 processSync(mapper);
7361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7362 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7363 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7364
7365 // If the timeout expires early, it is requested again.
7366 handleTimeout(mapper, ARBITRARY_TIME + 1);
7367 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7368 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7369
7370 // When the timeout expires, the withheld touch is released as a finger pointer.
7371 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7373 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7374
7375 // Subsequent pointer events are not withheld.
7376 processMove(mapper, 101, 201);
7377 processSync(mapper);
7378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7379 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7380 processUp(mapper);
7381 processSync(mapper);
7382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7383 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7384
7385 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7387 }
7388
7389private:
7390 InputDeviceInfo mExternalStylusDeviceInfo{};
7391};
7392
7393TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7394 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7395 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7396}
7397
7398TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7399 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7400 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7401}
7402
7403TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7404 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7405 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7406}
7407
7408// Test a successful stylus fusion gesture where the pressure is reported by the external
7409// before the touch is reported by the touchscreen.
7410TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7411 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7412 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007413 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007414
7415 // The external stylus reports pressure first. It is ignored for now.
7416 mStylusState.pressure = 1.f;
7417 processExternalStylusState(mapper);
7418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7419 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7420
7421 // When the touch goes down afterwards, it is reported as a stylus pointer.
7422 processDown(mapper, 100, 200);
7423 processSync(mapper);
7424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7425 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7426 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7427
7428 processMove(mapper, 101, 201);
7429 processSync(mapper);
7430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7431 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7432 processUp(mapper);
7433 processSync(mapper);
7434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7435 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7436
7437 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7439}
7440
7441TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7442 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7443
7444 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7445 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7446
7447 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7448 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7449 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7450 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7451}
7452
7453TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7454 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7455 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007456 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007457
7458 mStylusState.pressure = 0.8f;
7459 processExternalStylusState(mapper);
7460 processDown(mapper, 100, 200);
7461 processSync(mapper);
7462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7463 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7464 WithPressure(0.8f))));
7465 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7466
7467 // The external stylus reports a pressure change. We wait for some time for a touch event.
7468 mStylusState.pressure = 0.6f;
7469 processExternalStylusState(mapper);
7470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7471 ASSERT_NO_FATAL_FAILURE(
7472 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7473
7474 // If a touch is reported within the timeout, it reports the updated pressure.
7475 processMove(mapper, 101, 201);
7476 processSync(mapper);
7477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7478 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7479 WithPressure(0.6f))));
7480 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7481
7482 // There is another pressure change.
7483 mStylusState.pressure = 0.5f;
7484 processExternalStylusState(mapper);
7485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7486 ASSERT_NO_FATAL_FAILURE(
7487 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7488
7489 // If a touch is not reported within the timeout, a move event is generated to report
7490 // the new pressure.
7491 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7493 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7494 WithPressure(0.5f))));
7495
7496 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7497 // repeated indefinitely.
7498 mStylusState.pressure = 0.0f;
7499 processExternalStylusState(mapper);
7500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7501 ASSERT_NO_FATAL_FAILURE(
7502 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7503 processMove(mapper, 102, 202);
7504 processSync(mapper);
7505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7506 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7507 WithPressure(0.5f))));
7508 processMove(mapper, 103, 203);
7509 processSync(mapper);
7510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7511 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7512 WithPressure(0.5f))));
7513
7514 processUp(mapper);
7515 processSync(mapper);
7516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7517 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007518 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007519
7520 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7522}
7523
7524TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7525 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7526 auto source = WithSource(EXPECTED_SOURCE);
7527
7528 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007529 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007530 processExternalStylusState(mapper);
7531 processDown(mapper, 100, 200);
7532 processSync(mapper);
7533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7534 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007535 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007536 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7537
7538 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007539 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007540 processExternalStylusState(mapper);
7541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7542 ASSERT_NO_FATAL_FAILURE(
7543 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7544
7545 // If a touch is reported within the timeout, it reports the updated pressure.
7546 processMove(mapper, 101, 201);
7547 processSync(mapper);
7548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7549 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007550 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007551 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7552
7553 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007554 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007555 processExternalStylusState(mapper);
7556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7557 ASSERT_NO_FATAL_FAILURE(
7558 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7559
7560 // If a touch is not reported within the timeout, a move event is generated to report
7561 // the new tool type.
7562 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7564 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007565 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007566
7567 processUp(mapper);
7568 processSync(mapper);
7569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7570 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007571 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007572
7573 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7575}
7576
7577TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7578 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7579 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007580 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007581
7582 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7583
7584 // The external stylus reports a button change. We wait for some time for a touch event.
7585 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7586 processExternalStylusState(mapper);
7587 ASSERT_NO_FATAL_FAILURE(
7588 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7589
7590 // If a touch is reported within the timeout, it reports the updated button state.
7591 processMove(mapper, 101, 201);
7592 processSync(mapper);
7593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7594 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7595 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7597 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7598 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7599 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7600
7601 // The button is now released.
7602 mStylusState.buttons = 0;
7603 processExternalStylusState(mapper);
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7605 ASSERT_NO_FATAL_FAILURE(
7606 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7607
7608 // If a touch is not reported within the timeout, a move event is generated to report
7609 // the new button state.
7610 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7612 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7613 WithButtonState(0))));
7614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007615 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7616 WithButtonState(0))));
7617
7618 processUp(mapper);
7619 processSync(mapper);
7620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007621 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7622
7623 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7625}
7626
Michael Wrightd02c5b62014-02-10 15:10:22 -08007627// --- MultiTouchInputMapperTest ---
7628
7629class MultiTouchInputMapperTest : public TouchInputMapperTest {
7630protected:
7631 void prepareAxes(int axes);
7632
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007633 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7634 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7635 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7636 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7637 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7638 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7639 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7640 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7641 void processId(MultiTouchInputMapper& mapper, int32_t id);
7642 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7643 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7644 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007645 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007646 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007647 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7648 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007649};
7650
7651void MultiTouchInputMapperTest::prepareAxes(int axes) {
7652 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007653 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7654 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007655 }
7656 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007657 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7658 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007659 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007660 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7661 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007662 }
7663 }
7664 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007665 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7666 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007667 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007668 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007669 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007670 }
7671 }
7672 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007673 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7674 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007675 }
7676 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007677 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7678 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007679 }
7680 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007681 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7682 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007683 }
7684 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007685 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7686 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007687 }
7688 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007689 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7690 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007691 }
7692 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007693 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007694 }
7695}
7696
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007697void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7698 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007701}
7702
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007703void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7704 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007706}
7707
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007708void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7709 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007711}
7712
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007713void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007715}
7716
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007717void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007719}
7720
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007721void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7722 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007724}
7725
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007726void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007728}
7729
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007730void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007732}
7733
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007734void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007735 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007736}
7737
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007738void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007740}
7741
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007742void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007743 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007744}
7745
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007746void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7747 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007749}
7750
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007751void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7752 int32_t value) {
7753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7755}
7756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007757void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007759}
7760
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007761void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7762 nsecs_t readTime) {
7763 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007764}
7765
Michael Wrightd02c5b62014-02-10 15:10:22 -08007766TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007767 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007768 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007769 prepareAxes(POSITION);
7770 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00007771 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007772
arthurhungdcef2dc2020-08-11 14:47:50 +08007773 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007774
7775 NotifyMotionArgs motionArgs;
7776
7777 // Two fingers down at once.
7778 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7779 processPosition(mapper, x1, y1);
7780 processMTSync(mapper);
7781 processPosition(mapper, x2, y2);
7782 processMTSync(mapper);
7783 processSync(mapper);
7784
7785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7786 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7787 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7788 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7789 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7790 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7791 ASSERT_EQ(0, motionArgs.flags);
7792 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7793 ASSERT_EQ(0, motionArgs.buttonState);
7794 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007795 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007796 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007797 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7799 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7800 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7801 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7802 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7803
7804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7805 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7806 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7807 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7808 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007809 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007810 ASSERT_EQ(0, motionArgs.flags);
7811 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7812 ASSERT_EQ(0, motionArgs.buttonState);
7813 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007814 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007815 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007816 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007817 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007818 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7820 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7822 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7823 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7824 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7825 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7826
7827 // Move.
7828 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7829 processPosition(mapper, x1, y1);
7830 processMTSync(mapper);
7831 processPosition(mapper, x2, y2);
7832 processMTSync(mapper);
7833 processSync(mapper);
7834
7835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7836 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7837 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7838 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7839 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7841 ASSERT_EQ(0, motionArgs.flags);
7842 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7843 ASSERT_EQ(0, motionArgs.buttonState);
7844 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007845 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007846 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007847 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007848 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007849 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7851 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7853 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7854 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7855 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7856 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7857
7858 // First finger up.
7859 x2 += 15; y2 -= 20;
7860 processPosition(mapper, x2, y2);
7861 processMTSync(mapper);
7862 processSync(mapper);
7863
7864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7865 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7866 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7867 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7868 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007869 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007870 ASSERT_EQ(0, motionArgs.flags);
7871 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7872 ASSERT_EQ(0, motionArgs.buttonState);
7873 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007874 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007875 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007876 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007877 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007878 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7880 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7881 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7882 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7883 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7884 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7885 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7886
7887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7888 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7889 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7890 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7891 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7893 ASSERT_EQ(0, motionArgs.flags);
7894 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7895 ASSERT_EQ(0, motionArgs.buttonState);
7896 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007897 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007898 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007899 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7901 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7902 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7903 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7904 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7905
7906 // Move.
7907 x2 += 20; y2 -= 25;
7908 processPosition(mapper, x2, y2);
7909 processMTSync(mapper);
7910 processSync(mapper);
7911
7912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7913 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7914 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7915 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7916 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7918 ASSERT_EQ(0, motionArgs.flags);
7919 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7920 ASSERT_EQ(0, motionArgs.buttonState);
7921 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007922 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007923 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007924 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7926 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7927 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7928 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7929 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7930
7931 // New finger down.
7932 int32_t x3 = 700, y3 = 300;
7933 processPosition(mapper, x2, y2);
7934 processMTSync(mapper);
7935 processPosition(mapper, x3, y3);
7936 processMTSync(mapper);
7937 processSync(mapper);
7938
7939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7940 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7941 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7942 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7943 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007944 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007945 ASSERT_EQ(0, motionArgs.flags);
7946 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7947 ASSERT_EQ(0, motionArgs.buttonState);
7948 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007949 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007950 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007951 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007952 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007953 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7955 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7957 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7958 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7959 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7960 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7961
7962 // Second finger up.
7963 x3 += 30; y3 -= 20;
7964 processPosition(mapper, x3, y3);
7965 processMTSync(mapper);
7966 processSync(mapper);
7967
7968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7969 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7970 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7971 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7972 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007973 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007974 ASSERT_EQ(0, motionArgs.flags);
7975 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7976 ASSERT_EQ(0, motionArgs.buttonState);
7977 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007978 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007979 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007980 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007981 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007982 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7984 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7986 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7987 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7988 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7989 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7990
7991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7992 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7993 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7994 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7995 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7997 ASSERT_EQ(0, motionArgs.flags);
7998 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7999 ASSERT_EQ(0, motionArgs.buttonState);
8000 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008001 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008002 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008003 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8005 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8006 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8007 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8008 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8009
8010 // Last finger up.
8011 processMTSync(mapper);
8012 processSync(mapper);
8013
8014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8015 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8016 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8017 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8018 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8019 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8020 ASSERT_EQ(0, motionArgs.flags);
8021 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8022 ASSERT_EQ(0, motionArgs.buttonState);
8023 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008024 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008025 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008026 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8028 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8029 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8030 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8031 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8032
8033 // Should not have sent any more keys or motions.
8034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8036}
8037
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008038TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8039 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008040 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008041
8042 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8043 /*fuzz*/ 0, /*resolution*/ 10);
8044 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8045 /*fuzz*/ 0, /*resolution*/ 11);
8046 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8047 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8048 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8049 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8050 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8051 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8052 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8053 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8054
Arpit Singh56adebc2023-04-25 13:56:05 +00008055 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008056
8057 // X and Y axes
8058 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8059 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8060 // Touch major and minor
8061 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8062 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8063 // Tool major and minor
8064 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8065 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8066}
8067
8068TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8069 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008070 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008071
8072 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8073 /*fuzz*/ 0, /*resolution*/ 10);
8074 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8075 /*fuzz*/ 0, /*resolution*/ 11);
8076
8077 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8078
Arpit Singh56adebc2023-04-25 13:56:05 +00008079 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008080
8081 // Touch major and minor
8082 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8083 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8084 // Tool major and minor
8085 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8086 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8087}
8088
Michael Wrightd02c5b62014-02-10 15:10:22 -08008089TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008091 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092 prepareAxes(POSITION | ID);
8093 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00008094 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095
arthurhungdcef2dc2020-08-11 14:47:50 +08008096 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097
8098 NotifyMotionArgs motionArgs;
8099
8100 // Two fingers down at once.
8101 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8102 processPosition(mapper, x1, y1);
8103 processId(mapper, 1);
8104 processMTSync(mapper);
8105 processPosition(mapper, x2, y2);
8106 processId(mapper, 2);
8107 processMTSync(mapper);
8108 processSync(mapper);
8109
8110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8111 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008112 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008113 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008114 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008115 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8116 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8117
8118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008119 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008120 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008121 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008122 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008123 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008124 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8126 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8128 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8129
8130 // Move.
8131 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8132 processPosition(mapper, x1, y1);
8133 processId(mapper, 1);
8134 processMTSync(mapper);
8135 processPosition(mapper, x2, y2);
8136 processId(mapper, 2);
8137 processMTSync(mapper);
8138 processSync(mapper);
8139
8140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008142 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008143 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008144 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008145 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008146 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8148 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8149 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8150 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8151
8152 // First finger up.
8153 x2 += 15; y2 -= 20;
8154 processPosition(mapper, x2, y2);
8155 processId(mapper, 2);
8156 processMTSync(mapper);
8157 processSync(mapper);
8158
8159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008160 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008161 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008162 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008163 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008164 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008165 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8167 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8169 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8170
8171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008173 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008174 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008175 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8177 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8178
8179 // Move.
8180 x2 += 20; y2 -= 25;
8181 processPosition(mapper, x2, y2);
8182 processId(mapper, 2);
8183 processMTSync(mapper);
8184 processSync(mapper);
8185
8186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008188 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008189 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008190 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8192 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8193
8194 // New finger down.
8195 int32_t x3 = 700, y3 = 300;
8196 processPosition(mapper, x2, y2);
8197 processId(mapper, 2);
8198 processMTSync(mapper);
8199 processPosition(mapper, x3, y3);
8200 processId(mapper, 3);
8201 processMTSync(mapper);
8202 processSync(mapper);
8203
8204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008205 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008206 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008208 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008209 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008210 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8212 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8214 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8215
8216 // Second finger up.
8217 x3 += 30; y3 -= 20;
8218 processPosition(mapper, x3, y3);
8219 processId(mapper, 3);
8220 processMTSync(mapper);
8221 processSync(mapper);
8222
8223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008224 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008225 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008226 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008227 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008228 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008229 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8231 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8233 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8234
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008237 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008238 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008239 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8241 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8242
8243 // Last finger up.
8244 processMTSync(mapper);
8245 processSync(mapper);
8246
8247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8248 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008249 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008250 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008251 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8253 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8254
8255 // Should not have sent any more keys or motions.
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8258}
8259
8260TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008261 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008262 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008263 prepareAxes(POSITION | ID | SLOT);
8264 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00008265 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008266
arthurhungdcef2dc2020-08-11 14:47:50 +08008267 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008268
8269 NotifyMotionArgs motionArgs;
8270
8271 // Two fingers down at once.
8272 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8273 processPosition(mapper, x1, y1);
8274 processId(mapper, 1);
8275 processSlot(mapper, 1);
8276 processPosition(mapper, x2, y2);
8277 processId(mapper, 2);
8278 processSync(mapper);
8279
8280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8281 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008282 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008283 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008284 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8286 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8287
8288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008289 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008290 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008291 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008292 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008293 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008294 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8296 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8298 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8299
8300 // Move.
8301 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8302 processSlot(mapper, 0);
8303 processPosition(mapper, x1, y1);
8304 processSlot(mapper, 1);
8305 processPosition(mapper, x2, y2);
8306 processSync(mapper);
8307
8308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008310 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008311 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008312 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008313 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008314 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8316 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8318 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8319
8320 // First finger up.
8321 x2 += 15; y2 -= 20;
8322 processSlot(mapper, 0);
8323 processId(mapper, -1);
8324 processSlot(mapper, 1);
8325 processPosition(mapper, x2, y2);
8326 processSync(mapper);
8327
8328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008329 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008330 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008331 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008332 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008333 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008334 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8336 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8338 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8339
8340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008342 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008343 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008344 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8346 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8347
8348 // Move.
8349 x2 += 20; y2 -= 25;
8350 processPosition(mapper, x2, y2);
8351 processSync(mapper);
8352
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008355 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008356 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008357 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8359 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8360
8361 // New finger down.
8362 int32_t x3 = 700, y3 = 300;
8363 processPosition(mapper, x2, y2);
8364 processSlot(mapper, 0);
8365 processId(mapper, 3);
8366 processPosition(mapper, x3, y3);
8367 processSync(mapper);
8368
8369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008370 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008371 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008372 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008373 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008374 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008375 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8377 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8379 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8380
8381 // Second finger up.
8382 x3 += 30; y3 -= 20;
8383 processSlot(mapper, 1);
8384 processId(mapper, -1);
8385 processSlot(mapper, 0);
8386 processPosition(mapper, x3, y3);
8387 processSync(mapper);
8388
8389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008390 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008391 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008392 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008393 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008394 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008395 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8397 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8399 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8400
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008403 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008404 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008405 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8407 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8408
8409 // Last finger up.
8410 processId(mapper, -1);
8411 processSync(mapper);
8412
8413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8414 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008415 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008416 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008417 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8419 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8420
8421 // Should not have sent any more keys or motions.
8422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8424}
8425
8426TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008427 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008428 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008429 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singh56adebc2023-04-25 13:56:05 +00008430 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008431
8432 // These calculations are based on the input device calibration documentation.
8433 int32_t rawX = 100;
8434 int32_t rawY = 200;
8435 int32_t rawTouchMajor = 7;
8436 int32_t rawTouchMinor = 6;
8437 int32_t rawToolMajor = 9;
8438 int32_t rawToolMinor = 8;
8439 int32_t rawPressure = 11;
8440 int32_t rawDistance = 0;
8441 int32_t rawOrientation = 3;
8442 int32_t id = 5;
8443
8444 float x = toDisplayX(rawX);
8445 float y = toDisplayY(rawY);
8446 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8447 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8448 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8449 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8450 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8451 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8452 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8453 float distance = float(rawDistance);
8454
8455 processPosition(mapper, rawX, rawY);
8456 processTouchMajor(mapper, rawTouchMajor);
8457 processTouchMinor(mapper, rawTouchMinor);
8458 processToolMajor(mapper, rawToolMajor);
8459 processToolMinor(mapper, rawToolMinor);
8460 processPressure(mapper, rawPressure);
8461 processOrientation(mapper, rawOrientation);
8462 processDistance(mapper, rawDistance);
8463 processId(mapper, id);
8464 processMTSync(mapper);
8465 processSync(mapper);
8466
8467 NotifyMotionArgs args;
8468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8469 ASSERT_EQ(0, args.pointerProperties[0].id);
8470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8471 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8472 orientation, distance));
8473}
8474
8475TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008476 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008477 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008478 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8479 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singh56adebc2023-04-25 13:56:05 +00008480 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008481
8482 // These calculations are based on the input device calibration documentation.
8483 int32_t rawX = 100;
8484 int32_t rawY = 200;
8485 int32_t rawTouchMajor = 140;
8486 int32_t rawTouchMinor = 120;
8487 int32_t rawToolMajor = 180;
8488 int32_t rawToolMinor = 160;
8489
8490 float x = toDisplayX(rawX);
8491 float y = toDisplayY(rawY);
8492 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8493 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8494 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8495 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8496 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8497
8498 processPosition(mapper, rawX, rawY);
8499 processTouchMajor(mapper, rawTouchMajor);
8500 processTouchMinor(mapper, rawTouchMinor);
8501 processToolMajor(mapper, rawToolMajor);
8502 processToolMinor(mapper, rawToolMinor);
8503 processMTSync(mapper);
8504 processSync(mapper);
8505
8506 NotifyMotionArgs args;
8507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8509 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8510}
8511
8512TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008513 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008514 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008515 prepareAxes(POSITION | TOUCH | TOOL);
8516 addConfigurationProperty("touch.size.calibration", "diameter");
8517 addConfigurationProperty("touch.size.scale", "10");
8518 addConfigurationProperty("touch.size.bias", "160");
8519 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singh56adebc2023-04-25 13:56:05 +00008520 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008521
8522 // These calculations are based on the input device calibration documentation.
8523 // Note: We only provide a single common touch/tool value because the device is assumed
8524 // not to emit separate values for each pointer (isSummed = 1).
8525 int32_t rawX = 100;
8526 int32_t rawY = 200;
8527 int32_t rawX2 = 150;
8528 int32_t rawY2 = 250;
8529 int32_t rawTouchMajor = 5;
8530 int32_t rawToolMajor = 8;
8531
8532 float x = toDisplayX(rawX);
8533 float y = toDisplayY(rawY);
8534 float x2 = toDisplayX(rawX2);
8535 float y2 = toDisplayY(rawY2);
8536 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8537 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8538 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8539
8540 processPosition(mapper, rawX, rawY);
8541 processTouchMajor(mapper, rawTouchMajor);
8542 processToolMajor(mapper, rawToolMajor);
8543 processMTSync(mapper);
8544 processPosition(mapper, rawX2, rawY2);
8545 processTouchMajor(mapper, rawTouchMajor);
8546 processToolMajor(mapper, rawToolMajor);
8547 processMTSync(mapper);
8548 processSync(mapper);
8549
8550 NotifyMotionArgs args;
8551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8552 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8553
8554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008555 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008556 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8558 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8560 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8561}
8562
8563TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008564 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008565 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008566 prepareAxes(POSITION | TOUCH | TOOL);
8567 addConfigurationProperty("touch.size.calibration", "area");
8568 addConfigurationProperty("touch.size.scale", "43");
8569 addConfigurationProperty("touch.size.bias", "3");
Arpit Singh56adebc2023-04-25 13:56:05 +00008570 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008571
8572 // These calculations are based on the input device calibration documentation.
8573 int32_t rawX = 100;
8574 int32_t rawY = 200;
8575 int32_t rawTouchMajor = 5;
8576 int32_t rawToolMajor = 8;
8577
8578 float x = toDisplayX(rawX);
8579 float y = toDisplayY(rawY);
8580 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8581 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8582 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8583
8584 processPosition(mapper, rawX, rawY);
8585 processTouchMajor(mapper, rawTouchMajor);
8586 processToolMajor(mapper, rawToolMajor);
8587 processMTSync(mapper);
8588 processSync(mapper);
8589
8590 NotifyMotionArgs args;
8591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8593 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8594}
8595
8596TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008597 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008598 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008599 prepareAxes(POSITION | PRESSURE);
8600 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8601 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singh56adebc2023-04-25 13:56:05 +00008602 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008603
Michael Wrightaa449c92017-12-13 21:21:43 +00008604 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008605 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008606 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8607 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8608 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8609
Michael Wrightd02c5b62014-02-10 15:10:22 -08008610 // These calculations are based on the input device calibration documentation.
8611 int32_t rawX = 100;
8612 int32_t rawY = 200;
8613 int32_t rawPressure = 60;
8614
8615 float x = toDisplayX(rawX);
8616 float y = toDisplayY(rawY);
8617 float pressure = float(rawPressure) * 0.01f;
8618
8619 processPosition(mapper, rawX, rawY);
8620 processPressure(mapper, rawPressure);
8621 processMTSync(mapper);
8622 processSync(mapper);
8623
8624 NotifyMotionArgs args;
8625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8627 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8628}
8629
8630TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008631 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008632 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008633 prepareAxes(POSITION | ID | SLOT);
Arpit Singh56adebc2023-04-25 13:56:05 +00008634 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008635
8636 NotifyMotionArgs motionArgs;
8637 NotifyKeyArgs keyArgs;
8638
8639 processId(mapper, 1);
8640 processPosition(mapper, 100, 200);
8641 processSync(mapper);
8642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8643 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8644 ASSERT_EQ(0, motionArgs.buttonState);
8645
8646 // press BTN_LEFT, release BTN_LEFT
8647 processKey(mapper, BTN_LEFT, 1);
8648 processSync(mapper);
8649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8651 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8652
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8654 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8655 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8656
Michael Wrightd02c5b62014-02-10 15:10:22 -08008657 processKey(mapper, BTN_LEFT, 0);
8658 processSync(mapper);
8659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008660 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008661 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008662
8663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008665 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008666
8667 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8668 processKey(mapper, BTN_RIGHT, 1);
8669 processKey(mapper, BTN_MIDDLE, 1);
8670 processSync(mapper);
8671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8673 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8674 motionArgs.buttonState);
8675
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8677 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8678 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8679
8680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8681 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8682 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8683 motionArgs.buttonState);
8684
Michael Wrightd02c5b62014-02-10 15:10:22 -08008685 processKey(mapper, BTN_RIGHT, 0);
8686 processSync(mapper);
8687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008688 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008689 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008690
8691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008693 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008694
8695 processKey(mapper, BTN_MIDDLE, 0);
8696 processSync(mapper);
8697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008698 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008699 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008700
8701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008702 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008703 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008704
8705 // press BTN_BACK, release BTN_BACK
8706 processKey(mapper, BTN_BACK, 1);
8707 processSync(mapper);
8708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8709 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8710 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008711
Michael Wrightd02c5b62014-02-10 15:10:22 -08008712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008714 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8715
8716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8717 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8718 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008719
8720 processKey(mapper, BTN_BACK, 0);
8721 processSync(mapper);
8722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008723 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008724 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008725
8726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008728 ASSERT_EQ(0, motionArgs.buttonState);
8729
Michael Wrightd02c5b62014-02-10 15:10:22 -08008730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8731 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8732 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8733
8734 // press BTN_SIDE, release BTN_SIDE
8735 processKey(mapper, BTN_SIDE, 1);
8736 processSync(mapper);
8737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8738 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8739 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008740
Michael Wrightd02c5b62014-02-10 15:10:22 -08008741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008743 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8744
8745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8746 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8747 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008748
8749 processKey(mapper, BTN_SIDE, 0);
8750 processSync(mapper);
8751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008752 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008753 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008754
8755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008756 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008757 ASSERT_EQ(0, motionArgs.buttonState);
8758
Michael Wrightd02c5b62014-02-10 15:10:22 -08008759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8760 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8761 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8762
8763 // press BTN_FORWARD, release BTN_FORWARD
8764 processKey(mapper, BTN_FORWARD, 1);
8765 processSync(mapper);
8766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8767 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8768 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008769
Michael Wrightd02c5b62014-02-10 15:10:22 -08008770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008771 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008772 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8773
8774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8775 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8776 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008777
8778 processKey(mapper, BTN_FORWARD, 0);
8779 processSync(mapper);
8780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008781 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008782 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008783
8784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008786 ASSERT_EQ(0, motionArgs.buttonState);
8787
Michael Wrightd02c5b62014-02-10 15:10:22 -08008788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8789 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8790 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8791
8792 // press BTN_EXTRA, release BTN_EXTRA
8793 processKey(mapper, BTN_EXTRA, 1);
8794 processSync(mapper);
8795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8796 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8797 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008798
Michael Wrightd02c5b62014-02-10 15:10:22 -08008799 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8802
8803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8804 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8805 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008806
8807 processKey(mapper, BTN_EXTRA, 0);
8808 processSync(mapper);
8809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008810 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008811 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008812
8813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008814 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008815 ASSERT_EQ(0, motionArgs.buttonState);
8816
Michael Wrightd02c5b62014-02-10 15:10:22 -08008817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8818 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8819 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8820
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8822
Michael Wrightd02c5b62014-02-10 15:10:22 -08008823 // press BTN_STYLUS, release BTN_STYLUS
8824 processKey(mapper, BTN_STYLUS, 1);
8825 processSync(mapper);
8826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8827 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008828 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8829
8830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8831 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8832 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008833
8834 processKey(mapper, BTN_STYLUS, 0);
8835 processSync(mapper);
8836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008837 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008838 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008839
8840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008842 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008843
8844 // press BTN_STYLUS2, release BTN_STYLUS2
8845 processKey(mapper, BTN_STYLUS2, 1);
8846 processSync(mapper);
8847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008849 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8850
8851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8853 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008854
8855 processKey(mapper, BTN_STYLUS2, 0);
8856 processSync(mapper);
8857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008859 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008860
8861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008863 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008864
8865 // release touch
8866 processId(mapper, -1);
8867 processSync(mapper);
8868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8869 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8870 ASSERT_EQ(0, motionArgs.buttonState);
8871}
8872
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008873TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8874 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008875 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008876 prepareAxes(POSITION | ID | SLOT);
Arpit Singh56adebc2023-04-25 13:56:05 +00008877 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008878
8879 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8880 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8881
8882 // Touch down.
8883 processId(mapper, 1);
8884 processPosition(mapper, 100, 200);
8885 processSync(mapper);
8886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8887 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8888
8889 // Press and release button mapped to the primary stylus button.
8890 processKey(mapper, BTN_A, 1);
8891 processSync(mapper);
8892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8893 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8894 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8896 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8897 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8898
8899 processKey(mapper, BTN_A, 0);
8900 processSync(mapper);
8901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8902 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8904 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8905
8906 // Press and release the HID usage mapped to the secondary stylus button.
8907 processHidUsage(mapper, 0xabcd, 1);
8908 processSync(mapper);
8909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8910 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8911 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8913 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8914 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8915
8916 processHidUsage(mapper, 0xabcd, 0);
8917 processSync(mapper);
8918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8919 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8921 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8922
8923 // Release touch.
8924 processId(mapper, -1);
8925 processSync(mapper);
8926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8927 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8928}
8929
Michael Wrightd02c5b62014-02-10 15:10:22 -08008930TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008931 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008932 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008933 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00008934 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008935
8936 NotifyMotionArgs motionArgs;
8937
8938 // default tool type is finger
8939 processId(mapper, 1);
8940 processPosition(mapper, 100, 200);
8941 processSync(mapper);
8942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8943 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008944 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008945
8946 // eraser
8947 processKey(mapper, BTN_TOOL_RUBBER, 1);
8948 processSync(mapper);
8949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008951 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008952
8953 // stylus
8954 processKey(mapper, BTN_TOOL_RUBBER, 0);
8955 processKey(mapper, BTN_TOOL_PEN, 1);
8956 processSync(mapper);
8957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008959 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008960
8961 // brush
8962 processKey(mapper, BTN_TOOL_PEN, 0);
8963 processKey(mapper, BTN_TOOL_BRUSH, 1);
8964 processSync(mapper);
8965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008967 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008968
8969 // pencil
8970 processKey(mapper, BTN_TOOL_BRUSH, 0);
8971 processKey(mapper, BTN_TOOL_PENCIL, 1);
8972 processSync(mapper);
8973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008975 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008976
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008977 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008978 processKey(mapper, BTN_TOOL_PENCIL, 0);
8979 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8980 processSync(mapper);
8981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008983 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008984
8985 // mouse
8986 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8987 processKey(mapper, BTN_TOOL_MOUSE, 1);
8988 processSync(mapper);
8989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008991 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008992
8993 // lens
8994 processKey(mapper, BTN_TOOL_MOUSE, 0);
8995 processKey(mapper, BTN_TOOL_LENS, 1);
8996 processSync(mapper);
8997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8998 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008999 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009000
9001 // double-tap
9002 processKey(mapper, BTN_TOOL_LENS, 0);
9003 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9004 processSync(mapper);
9005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009007 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009008
9009 // triple-tap
9010 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9011 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9012 processSync(mapper);
9013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009015 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009016
9017 // quad-tap
9018 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9019 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9020 processSync(mapper);
9021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009023 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009024
9025 // finger
9026 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9027 processKey(mapper, BTN_TOOL_FINGER, 1);
9028 processSync(mapper);
9029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9030 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009031 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009032
9033 // stylus trumps finger
9034 processKey(mapper, BTN_TOOL_PEN, 1);
9035 processSync(mapper);
9036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009038 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009039
9040 // eraser trumps stylus
9041 processKey(mapper, BTN_TOOL_RUBBER, 1);
9042 processSync(mapper);
9043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009045 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009046
9047 // mouse trumps eraser
9048 processKey(mapper, BTN_TOOL_MOUSE, 1);
9049 processSync(mapper);
9050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009052 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009053
9054 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9055 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9056 processSync(mapper);
9057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009059 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009060
9061 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9062 processToolType(mapper, MT_TOOL_PEN);
9063 processSync(mapper);
9064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009066 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009067
9068 // back to default tool type
9069 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9070 processKey(mapper, BTN_TOOL_MOUSE, 0);
9071 processKey(mapper, BTN_TOOL_RUBBER, 0);
9072 processKey(mapper, BTN_TOOL_PEN, 0);
9073 processKey(mapper, BTN_TOOL_FINGER, 0);
9074 processSync(mapper);
9075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009077 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009078}
9079
9080TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009081 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009082 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009083 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009084 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +00009085 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009086
9087 NotifyMotionArgs motionArgs;
9088
9089 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9090 processId(mapper, 1);
9091 processPosition(mapper, 100, 200);
9092 processSync(mapper);
9093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9094 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9096 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9097
9098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9099 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9100 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9101 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9102
9103 // move a little
9104 processPosition(mapper, 150, 250);
9105 processSync(mapper);
9106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9107 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9109 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9110
9111 // down when BTN_TOUCH is pressed, pressure defaults to 1
9112 processKey(mapper, BTN_TOUCH, 1);
9113 processSync(mapper);
9114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9115 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9117 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9118
9119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9120 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9122 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9123
9124 // up when BTN_TOUCH is released, hover restored
9125 processKey(mapper, BTN_TOUCH, 0);
9126 processSync(mapper);
9127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9128 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9129 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9130 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9131
9132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9133 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9135 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9136
9137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9138 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9140 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9141
9142 // exit hover when pointer goes away
9143 processId(mapper, -1);
9144 processSync(mapper);
9145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9146 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9148 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9149}
9150
9151TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009152 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009153 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009154 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009155 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009156
9157 NotifyMotionArgs motionArgs;
9158
9159 // initially hovering because pressure is 0
9160 processId(mapper, 1);
9161 processPosition(mapper, 100, 200);
9162 processPressure(mapper, 0);
9163 processSync(mapper);
9164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9165 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9167 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9168
9169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9170 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9172 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9173
9174 // move a little
9175 processPosition(mapper, 150, 250);
9176 processSync(mapper);
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9178 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9180 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9181
9182 // down when pressure becomes non-zero
9183 processPressure(mapper, RAW_PRESSURE_MAX);
9184 processSync(mapper);
9185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9186 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9188 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9189
9190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9191 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9193 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9194
9195 // up when pressure becomes 0, hover restored
9196 processPressure(mapper, 0);
9197 processSync(mapper);
9198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9199 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9201 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9202
9203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9204 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9206 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9207
9208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9209 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9211 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9212
9213 // exit hover when pointer goes away
9214 processId(mapper, -1);
9215 processSync(mapper);
9216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9217 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9219 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9220}
9221
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009222/**
9223 * Set the input device port <--> display port associations, and check that the
9224 * events are routed to the display that matches the display port.
9225 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9226 */
9227TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009228 const std::string usb2 = "USB2";
9229 const uint8_t hdmi1 = 0;
9230 const uint8_t hdmi2 = 1;
9231 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009232 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009233
9234 addConfigurationProperty("touch.deviceType", "touchScreen");
9235 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009236 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009237
9238 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9239 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9240
9241 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9242 // for this input device is specified, and the matching viewport is not present,
9243 // the input device should be disabled (at the mapper level).
9244
9245 // Add viewport for display 2 on hdmi2
9246 prepareSecondaryDisplay(type, hdmi2);
9247 // Send a touch event
9248 processPosition(mapper, 100, 100);
9249 processSync(mapper);
9250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9251
9252 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009253 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009254 // Send a touch event again
9255 processPosition(mapper, 100, 100);
9256 processSync(mapper);
9257
9258 NotifyMotionArgs args;
9259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9260 ASSERT_EQ(DISPLAY_ID, args.displayId);
9261}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009262
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009263TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9264 addConfigurationProperty("touch.deviceType", "touchScreen");
9265 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009266 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009267
9268 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9269
Michael Wrighta9cf4192022-12-01 23:46:39 +00009270 prepareDisplay(ui::ROTATION_0);
9271 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009272
9273 // Send a touch event
9274 processPosition(mapper, 100, 100);
9275 processSync(mapper);
9276
9277 NotifyMotionArgs args;
9278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9279 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9280}
9281
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009282TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009283 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009284 std::shared_ptr<FakePointerController> fakePointerController =
9285 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009286 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009287 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009288 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009289
Garfield Tan888a6a42020-01-09 11:39:16 -08009290 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009291 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009292
Michael Wrighta9cf4192022-12-01 23:46:39 +00009293 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009294 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009295 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009296
Josep del Río2d8c79a2023-01-23 19:33:50 +00009297 // Check source is mouse that would obtain the PointerController.
9298 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009299
9300 NotifyMotionArgs motionArgs;
9301 processPosition(mapper, 100, 100);
9302 processSync(mapper);
9303
9304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9305 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9306 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9307}
9308
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009309/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009310 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9311 */
9312TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9313 addConfigurationProperty("touch.deviceType", "touchScreen");
9314 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009315 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009316
Michael Wrighta9cf4192022-12-01 23:46:39 +00009317 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009318 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9319 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9320 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9321 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009322
9323 NotifyMotionArgs args;
9324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9325 ASSERT_EQ(26, args.readTime);
9326
Harry Cutts33476232023-01-30 19:57:29 +00009327 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9328 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9329 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009330
9331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9332 ASSERT_EQ(33, args.readTime);
9333}
9334
9335/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009336 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9337 * events should not be delivered to the listener.
9338 */
9339TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9340 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009341 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009342 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009343 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009344 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009345 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009346 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009347
9348 NotifyMotionArgs motionArgs;
9349 processPosition(mapper, 100, 100);
9350 processSync(mapper);
9351
9352 mFakeListener->assertNotifyMotionWasNotCalled();
9353}
9354
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009355/**
9356 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9357 * the touch mapper can process the events and the events can be delivered to the listener.
9358 */
9359TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9360 addConfigurationProperty("touch.deviceType", "touchScreen");
9361 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009362 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009363 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009364 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009365 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009366 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009367
9368 NotifyMotionArgs motionArgs;
9369 processPosition(mapper, 100, 100);
9370 processSync(mapper);
9371
9372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9373 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9374}
9375
Garfield Tanc734e4f2021-01-15 20:01:39 -08009376TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9377 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009378 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009379 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009380 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009381 std::optional<DisplayViewport> optionalDisplayViewport =
9382 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9383 ASSERT_TRUE(optionalDisplayViewport.has_value());
9384 DisplayViewport displayViewport = *optionalDisplayViewport;
9385
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009386 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009387 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009388 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009389
9390 // Finger down
9391 int32_t x = 100, y = 100;
9392 processPosition(mapper, x, y);
9393 processSync(mapper);
9394
9395 NotifyMotionArgs motionArgs;
9396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9397 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9398
9399 // Deactivate display viewport
9400 displayViewport.isActive = false;
9401 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009402 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009403
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009404 // The ongoing touch should be canceled immediately
9405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9406 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9407
9408 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009409 x += 10, y += 10;
9410 processPosition(mapper, x, y);
9411 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009413
9414 // Reactivate display viewport
9415 displayViewport.isActive = true;
9416 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009417 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009418
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009419 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009420 x += 10, y += 10;
9421 processPosition(mapper, x, y);
9422 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9424 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009425}
9426
Arthur Hung7c645402019-01-25 17:45:42 +08009427TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9428 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009429 prepareAxes(POSITION | ID | SLOT);
9430 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009431 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009432
9433 // Create the second touch screen device, and enable multi fingers.
9434 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009435 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009436 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009437 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009438 std::shared_ptr<InputDevice> device2 =
9439 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009440 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009441
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009442 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009443 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009444 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009445 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009446 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009447 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009448 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009449 /*flat=*/0, /*fuzz=*/0);
9450 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009451 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9452 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009453
9454 // Setup the second touch screen device.
Arpit Singh56adebc2023-04-25 13:56:05 +00009455 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9456 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9457 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009458 std::list<NotifyArgs> unused =
9459 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009460 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009461 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009462
9463 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009464 std::shared_ptr<FakePointerController> fakePointerController =
9465 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009466 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009467
9468 // Setup policy for associated displays and show touches.
9469 const uint8_t hdmi1 = 0;
9470 const uint8_t hdmi2 = 1;
9471 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9472 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9473 mFakePolicy->setShowTouches(true);
9474
9475 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009476 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009477 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009478
9479 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009480 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009481 InputReaderConfiguration::Change::DISPLAY_INFO |
9482 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009483
9484 // Two fingers down at default display.
9485 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9486 processPosition(mapper, x1, y1);
9487 processId(mapper, 1);
9488 processSlot(mapper, 1);
9489 processPosition(mapper, x2, y2);
9490 processId(mapper, 2);
9491 processSync(mapper);
9492
9493 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9494 fakePointerController->getSpots().find(DISPLAY_ID);
9495 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9496 ASSERT_EQ(size_t(2), iter->second.size());
9497
9498 // Two fingers down at second display.
9499 processPosition(mapper2, x1, y1);
9500 processId(mapper2, 1);
9501 processSlot(mapper2, 1);
9502 processPosition(mapper2, x2, y2);
9503 processId(mapper2, 2);
9504 processSync(mapper2);
9505
9506 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9507 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9508 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009509
9510 // Disable the show touches configuration and ensure the spots are cleared.
9511 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009512 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009513 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009514
9515 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009516}
9517
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009518TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009519 prepareAxes(POSITION);
9520 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009521 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00009522 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009523
9524 NotifyMotionArgs motionArgs;
9525 // Unrotated video frame
9526 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9527 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009528 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009529 processPosition(mapper, 100, 200);
9530 processSync(mapper);
9531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9532 ASSERT_EQ(frames, motionArgs.videoFrames);
9533
9534 // Subsequent touch events should not have any videoframes
9535 // This is implemented separately in FakeEventHub,
9536 // but that should match the behaviour of TouchVideoDevice.
9537 processPosition(mapper, 200, 200);
9538 processSync(mapper);
9539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9540 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9541}
9542
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009543TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009544 prepareAxes(POSITION);
9545 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009546 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009547 // Unrotated video frame
9548 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9549 NotifyMotionArgs motionArgs;
9550
9551 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009552 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009553 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9554 clearViewports();
9555 prepareDisplay(orientation);
9556 std::vector<TouchVideoFrame> frames{frame};
9557 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9558 processPosition(mapper, 100, 200);
9559 processSync(mapper);
9560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9561 ASSERT_EQ(frames, motionArgs.videoFrames);
9562 }
9563}
9564
9565TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9566 prepareAxes(POSITION);
9567 addConfigurationProperty("touch.deviceType", "touchScreen");
9568 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9569 // orientation-aware are affected by display rotation.
9570 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00009571 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009572 // Unrotated video frame
9573 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9574 NotifyMotionArgs motionArgs;
9575
9576 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009577 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009578 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9579 clearViewports();
9580 prepareDisplay(orientation);
9581 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009582 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009583 processPosition(mapper, 100, 200);
9584 processSync(mapper);
9585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009586 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9587 // compared to the display. This is so that when the window transform (which contains the
9588 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9589 // window's coordinate space.
9590 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009591 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009592
9593 // Release finger.
9594 processSync(mapper);
9595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009596 }
9597}
9598
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009599TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009600 prepareAxes(POSITION);
9601 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009602 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009603 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9604 // so mix these.
9605 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9606 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9607 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9608 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9609 NotifyMotionArgs motionArgs;
9610
Michael Wrighta9cf4192022-12-01 23:46:39 +00009611 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009612 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009613 processPosition(mapper, 100, 200);
9614 processSync(mapper);
9615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009616 ASSERT_EQ(frames, motionArgs.videoFrames);
9617}
9618
9619TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9620 prepareAxes(POSITION);
9621 addConfigurationProperty("touch.deviceType", "touchScreen");
9622 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9623 // orientation-aware are affected by display rotation.
9624 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00009625 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009626 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9627 // so mix these.
9628 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9629 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9630 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9631 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9632 NotifyMotionArgs motionArgs;
9633
Michael Wrighta9cf4192022-12-01 23:46:39 +00009634 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009635 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9636 processPosition(mapper, 100, 200);
9637 processSync(mapper);
9638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9639 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9640 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9641 // compared to the display. This is so that when the window transform (which contains the
9642 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9643 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009644 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009645 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009646 ASSERT_EQ(frames, motionArgs.videoFrames);
9647}
9648
Arthur Hung9da14732019-09-02 16:16:58 +08009649/**
9650 * If we had defined port associations, but the viewport is not ready, the touch device would be
9651 * expected to be disabled, and it should be enabled after the viewport has found.
9652 */
9653TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009654 constexpr uint8_t hdmi2 = 1;
9655 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009656 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009657
9658 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9659
9660 addConfigurationProperty("touch.deviceType", "touchScreen");
9661 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009662 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009663
9664 ASSERT_EQ(mDevice->isEnabled(), false);
9665
9666 // Add display on hdmi2, the device should be enabled and can receive touch event.
9667 prepareSecondaryDisplay(type, hdmi2);
9668 ASSERT_EQ(mDevice->isEnabled(), true);
9669
9670 // Send a touch event.
9671 processPosition(mapper, 100, 100);
9672 processSync(mapper);
9673
9674 NotifyMotionArgs args;
9675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9676 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9677}
9678
Arthur Hung421eb1c2020-01-16 00:09:42 +08009679TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009680 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009681 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009682 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009683 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009684
9685 NotifyMotionArgs motionArgs;
9686
9687 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9688 // finger down
9689 processId(mapper, 1);
9690 processPosition(mapper, x1, y1);
9691 processSync(mapper);
9692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9693 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009694 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009695
9696 // finger move
9697 processId(mapper, 1);
9698 processPosition(mapper, x2, y2);
9699 processSync(mapper);
9700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009702 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009703
9704 // finger up.
9705 processId(mapper, -1);
9706 processSync(mapper);
9707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9708 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009709 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009710
9711 // new finger down
9712 processId(mapper, 1);
9713 processPosition(mapper, x3, y3);
9714 processSync(mapper);
9715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9716 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009717 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009718}
9719
9720/**
arthurhungcc7f9802020-04-30 17:55:40 +08009721 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9722 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009723 */
arthurhungcc7f9802020-04-30 17:55:40 +08009724TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009725 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009726 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009727 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009728 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009729
9730 NotifyMotionArgs motionArgs;
9731
9732 // default tool type is finger
9733 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009734 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009735 processPosition(mapper, x1, y1);
9736 processSync(mapper);
9737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9738 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009739 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009740
9741 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9742 processToolType(mapper, MT_TOOL_PALM);
9743 processSync(mapper);
9744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9745 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9746
9747 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009748 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009749 processPosition(mapper, x2, y2);
9750 processSync(mapper);
9751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9752
9753 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009754 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009755 processSync(mapper);
9756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9757
9758 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009759 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009760 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009761 processPosition(mapper, x3, y3);
9762 processSync(mapper);
9763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9764 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009765 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009766}
9767
arthurhungbf89a482020-04-17 17:37:55 +08009768/**
arthurhungcc7f9802020-04-30 17:55:40 +08009769 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9770 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009771 */
arthurhungcc7f9802020-04-30 17:55:40 +08009772TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009773 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009774 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009775 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009776 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009777
9778 NotifyMotionArgs motionArgs;
9779
9780 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009781 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9782 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009783 processPosition(mapper, x1, y1);
9784 processSync(mapper);
9785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9786 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009787 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009788
9789 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009790 processSlot(mapper, SECOND_SLOT);
9791 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009792 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009793 processSync(mapper);
9794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009795 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009796 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009797
9798 // If the tool type of the first finger changes to MT_TOOL_PALM,
9799 // we expect to receive ACTION_POINTER_UP with cancel flag.
9800 processSlot(mapper, FIRST_SLOT);
9801 processId(mapper, FIRST_TRACKING_ID);
9802 processToolType(mapper, MT_TOOL_PALM);
9803 processSync(mapper);
9804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009805 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009806 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9807
9808 // The following MOVE events of second finger should be processed.
9809 processSlot(mapper, SECOND_SLOT);
9810 processId(mapper, SECOND_TRACKING_ID);
9811 processPosition(mapper, x2 + 1, y2 + 1);
9812 processSync(mapper);
9813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9814 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009815 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009816
9817 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9818 // it. Second finger receive move.
9819 processSlot(mapper, FIRST_SLOT);
9820 processId(mapper, INVALID_TRACKING_ID);
9821 processSync(mapper);
9822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9823 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009824 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009825
9826 // Second finger keeps moving.
9827 processSlot(mapper, SECOND_SLOT);
9828 processId(mapper, SECOND_TRACKING_ID);
9829 processPosition(mapper, x2 + 2, y2 + 2);
9830 processSync(mapper);
9831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009833 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009834
9835 // Second finger up.
9836 processId(mapper, INVALID_TRACKING_ID);
9837 processSync(mapper);
9838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9839 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9840 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9841}
9842
9843/**
9844 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9845 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9846 */
9847TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9848 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009849 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009850 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009851 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009852
9853 NotifyMotionArgs motionArgs;
9854
9855 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9856 // First finger down.
9857 processId(mapper, FIRST_TRACKING_ID);
9858 processPosition(mapper, x1, y1);
9859 processSync(mapper);
9860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9861 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009862 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009863
9864 // Second finger down.
9865 processSlot(mapper, SECOND_SLOT);
9866 processId(mapper, SECOND_TRACKING_ID);
9867 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009868 processSync(mapper);
9869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009870 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009871 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009872
arthurhungcc7f9802020-04-30 17:55:40 +08009873 // If the tool type of the first finger changes to MT_TOOL_PALM,
9874 // we expect to receive ACTION_POINTER_UP with cancel flag.
9875 processSlot(mapper, FIRST_SLOT);
9876 processId(mapper, FIRST_TRACKING_ID);
9877 processToolType(mapper, MT_TOOL_PALM);
9878 processSync(mapper);
9879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009880 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009881 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9882
9883 // Second finger keeps moving.
9884 processSlot(mapper, SECOND_SLOT);
9885 processId(mapper, SECOND_TRACKING_ID);
9886 processPosition(mapper, x2 + 1, y2 + 1);
9887 processSync(mapper);
9888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9890
9891 // second finger becomes palm, receive cancel due to only 1 finger is active.
9892 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009893 processToolType(mapper, MT_TOOL_PALM);
9894 processSync(mapper);
9895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9896 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9897
arthurhungcc7f9802020-04-30 17:55:40 +08009898 // third finger down.
9899 processSlot(mapper, THIRD_SLOT);
9900 processId(mapper, THIRD_TRACKING_ID);
9901 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009902 processPosition(mapper, x3, y3);
9903 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9905 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009906 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009907 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009908
9909 // third finger move
9910 processId(mapper, THIRD_TRACKING_ID);
9911 processPosition(mapper, x3 + 1, y3 + 1);
9912 processSync(mapper);
9913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9914 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9915
9916 // first finger up, third 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 up, third finger receive move.
9925 processSlot(mapper, SECOND_SLOT);
9926 processId(mapper, INVALID_TRACKING_ID);
9927 processSync(mapper);
9928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009930 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009931
9932 // third finger up.
9933 processSlot(mapper, THIRD_SLOT);
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 from some finger,
9943 * and the active finger could still be allowed to receive the events
9944 */
9945TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
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 // default tool type is finger
9954 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
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);
9966 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);
arthurhungcc7f9802020-04-30 17:55:40 +08009970
9971 // If the tool type of the second finger changes to MT_TOOL_PALM,
9972 // we expect to receive ACTION_POINTER_UP with cancel flag.
9973 processId(mapper, SECOND_TRACKING_ID);
9974 processToolType(mapper, MT_TOOL_PALM);
9975 processSync(mapper);
9976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009977 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009978 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9979
9980 // The following MOVE event should be processed.
9981 processSlot(mapper, FIRST_SLOT);
9982 processId(mapper, FIRST_TRACKING_ID);
9983 processPosition(mapper, x1 + 1, y1 + 1);
9984 processSync(mapper);
9985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9986 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009987 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009988
9989 // second finger up.
9990 processSlot(mapper, SECOND_SLOT);
9991 processId(mapper, INVALID_TRACKING_ID);
9992 processSync(mapper);
9993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9995
9996 // first finger keep moving
9997 processSlot(mapper, FIRST_SLOT);
9998 processId(mapper, FIRST_TRACKING_ID);
9999 processPosition(mapper, x1 + 2, y1 + 2);
10000 processSync(mapper);
10001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10002 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10003
10004 // first finger up.
10005 processId(mapper, INVALID_TRACKING_ID);
10006 processSync(mapper);
10007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10008 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10009 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010010}
10011
Arthur Hung9ad18942021-06-19 02:04:46 +000010012/**
10013 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10014 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10015 * cause slot be valid again.
10016 */
10017TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10018 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010019 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010020 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010021 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010022
10023 NotifyMotionArgs motionArgs;
10024
10025 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10026 // First finger down.
10027 processId(mapper, FIRST_TRACKING_ID);
10028 processPosition(mapper, x1, y1);
10029 processPressure(mapper, RAW_PRESSURE_MAX);
10030 processSync(mapper);
10031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10032 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010033 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010034
10035 // First finger move.
10036 processId(mapper, FIRST_TRACKING_ID);
10037 processPosition(mapper, x1 + 1, y1 + 1);
10038 processPressure(mapper, RAW_PRESSURE_MAX);
10039 processSync(mapper);
10040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10041 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010042 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010043
10044 // Second finger down.
10045 processSlot(mapper, SECOND_SLOT);
10046 processId(mapper, SECOND_TRACKING_ID);
10047 processPosition(mapper, x2, y2);
10048 processPressure(mapper, RAW_PRESSURE_MAX);
10049 processSync(mapper);
10050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010051 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010052 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010053
10054 // second finger up with some unexpected data.
10055 processSlot(mapper, SECOND_SLOT);
10056 processId(mapper, INVALID_TRACKING_ID);
10057 processPosition(mapper, x2, y2);
10058 processSync(mapper);
10059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010060 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010061 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010062
10063 // first finger up with some unexpected data.
10064 processSlot(mapper, FIRST_SLOT);
10065 processId(mapper, INVALID_TRACKING_ID);
10066 processPosition(mapper, x2, y2);
10067 processPressure(mapper, RAW_PRESSURE_MAX);
10068 processSync(mapper);
10069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10070 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010071 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010072}
10073
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010074TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10075 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010076 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010077 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010078 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010079
10080 // First finger down.
10081 processId(mapper, FIRST_TRACKING_ID);
10082 processPosition(mapper, 100, 200);
10083 processPressure(mapper, RAW_PRESSURE_MAX);
10084 processSync(mapper);
10085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10086 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10087
10088 // Second finger down.
10089 processSlot(mapper, SECOND_SLOT);
10090 processId(mapper, SECOND_TRACKING_ID);
10091 processPosition(mapper, 300, 400);
10092 processPressure(mapper, RAW_PRESSURE_MAX);
10093 processSync(mapper);
10094 ASSERT_NO_FATAL_FAILURE(
10095 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10096
10097 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010098 // preserved. Resetting should cancel the ongoing gesture.
10099 resetMapper(mapper, ARBITRARY_TIME);
10100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10101 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010102
10103 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10104 // the existing touch state to generate a down event.
10105 processPosition(mapper, 301, 302);
10106 processSync(mapper);
10107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10108 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10110 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10111
10112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10113}
10114
10115TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10116 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010117 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010118 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010119 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010120
10121 // First finger touches down and releases.
10122 processId(mapper, FIRST_TRACKING_ID);
10123 processPosition(mapper, 100, 200);
10124 processPressure(mapper, RAW_PRESSURE_MAX);
10125 processSync(mapper);
10126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10127 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10128 processId(mapper, INVALID_TRACKING_ID);
10129 processSync(mapper);
10130 ASSERT_NO_FATAL_FAILURE(
10131 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10132
10133 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10134 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010135 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10137
10138 // Send an empty sync frame. Since there are no pointers, no events are generated.
10139 processSync(mapper);
10140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10141}
10142
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010143TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010144 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010145 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010146 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010147 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010149
10150 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10151 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10152 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10153 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10154 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10155
10156 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010157 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010158 processId(mapper, FIRST_TRACKING_ID);
10159 processToolType(mapper, MT_TOOL_PEN);
10160 processPosition(mapper, 100, 200);
10161 processPressure(mapper, RAW_PRESSURE_MAX);
10162 processSync(mapper);
10163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10164 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10165 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010166 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010167
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010168 // Now that we know the device supports styluses, ensure that the device is re-configured with
10169 // the stylus source.
10170 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10171 {
10172 const auto& devices = mReader->getInputDevices();
10173 auto deviceInfo =
10174 std::find_if(devices.begin(), devices.end(),
10175 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10176 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10177 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10178 }
10179
10180 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10182
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010183 processId(mapper, INVALID_TRACKING_ID);
10184 processSync(mapper);
10185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10186 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10187 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010188 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010189}
10190
Seunghwan Choi356026c2023-02-01 14:37:25 +090010191TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10192 addConfigurationProperty("touch.deviceType", "touchScreen");
10193 prepareDisplay(ui::ROTATION_0);
10194 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10195 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10196 // indicate stylus presence dynamically.
10197 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10198 std::shared_ptr<FakePointerController> fakePointerController =
10199 std::make_shared<FakePointerController>();
10200 mFakePolicy->setPointerController(fakePointerController);
10201 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singh56adebc2023-04-25 13:56:05 +000010202 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010203
10204 processId(mapper, FIRST_TRACKING_ID);
10205 processPressure(mapper, RAW_PRESSURE_MIN);
10206 processPosition(mapper, 100, 200);
10207 processToolType(mapper, MT_TOOL_PEN);
10208 processSync(mapper);
10209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10210 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010211 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010212 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10213 ASSERT_TRUE(fakePointerController->isPointerShown());
10214 ASSERT_NO_FATAL_FAILURE(
10215 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10216}
10217
10218TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10219 addConfigurationProperty("touch.deviceType", "touchScreen");
10220 prepareDisplay(ui::ROTATION_0);
10221 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10222 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10223 // indicate stylus presence dynamically.
10224 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10225 std::shared_ptr<FakePointerController> fakePointerController =
10226 std::make_shared<FakePointerController>();
10227 mFakePolicy->setPointerController(fakePointerController);
10228 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singh56adebc2023-04-25 13:56:05 +000010229 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010230
10231 processId(mapper, FIRST_TRACKING_ID);
10232 processPressure(mapper, RAW_PRESSURE_MIN);
10233 processPosition(mapper, 100, 200);
10234 processToolType(mapper, MT_TOOL_PEN);
10235 processSync(mapper);
10236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10237 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010238 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010239 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10240 ASSERT_FALSE(fakePointerController->isPointerShown());
10241}
10242
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010243// --- MultiTouchInputMapperTest_ExternalDevice ---
10244
10245class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10246protected:
Chris Yea52ade12020-08-27 16:49:20 -070010247 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010248};
10249
10250/**
10251 * Expect fallback to internal viewport if device is external and external viewport is not present.
10252 */
10253TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10254 prepareAxes(POSITION);
10255 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010256 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +000010257 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010258
10259 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10260
10261 NotifyMotionArgs motionArgs;
10262
10263 // Expect the event to be sent to the internal viewport,
10264 // because an external viewport is not present.
10265 processPosition(mapper, 100, 100);
10266 processSync(mapper);
10267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10268 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10269
10270 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010271 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010272 processPosition(mapper, 100, 100);
10273 processSync(mapper);
10274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10275 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10276}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010277
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010278TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10279 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10280 std::shared_ptr<FakePointerController> fakePointerController =
10281 std::make_shared<FakePointerController>();
10282 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10283 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010284
10285 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010286 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010287 prepareAxes(POSITION | ID | SLOT);
10288 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10289 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10290 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010291 mFakePolicy->setPointerController(fakePointerController);
Arpit Singh56adebc2023-04-25 13:56:05 +000010292 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010293
10294 // captured touchpad should be a touchpad source
10295 NotifyDeviceResetArgs resetArgs;
10296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10297 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10298
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010299 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010300
10301 const InputDeviceInfo::MotionRange* relRangeX =
10302 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10303 ASSERT_NE(relRangeX, nullptr);
10304 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10305 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10306 const InputDeviceInfo::MotionRange* relRangeY =
10307 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10308 ASSERT_NE(relRangeY, nullptr);
10309 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10310 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10311
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010312 // run captured pointer tests - note that this is unscaled, so input listener events should be
10313 // identical to what the hardware sends (accounting for any
10314 // calibration).
10315 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010316 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010317 processId(mapper, 1);
10318 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10319 processKey(mapper, BTN_TOUCH, 1);
10320 processSync(mapper);
10321
10322 // expect coord[0] to contain initial location of touch 0
10323 NotifyMotionArgs args;
10324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10325 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010326 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010327 ASSERT_EQ(0, args.pointerProperties[0].id);
10328 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10329 ASSERT_NO_FATAL_FAILURE(
10330 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10331
10332 // FINGER 1 DOWN
10333 processSlot(mapper, 1);
10334 processId(mapper, 2);
10335 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10336 processSync(mapper);
10337
10338 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010340 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010341 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010342 ASSERT_EQ(0, args.pointerProperties[0].id);
10343 ASSERT_EQ(1, args.pointerProperties[1].id);
10344 ASSERT_NO_FATAL_FAILURE(
10345 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10346 ASSERT_NO_FATAL_FAILURE(
10347 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10348
10349 // FINGER 1 MOVE
10350 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10351 processSync(mapper);
10352
10353 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10354 // from move
10355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10357 ASSERT_NO_FATAL_FAILURE(
10358 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10359 ASSERT_NO_FATAL_FAILURE(
10360 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10361
10362 // FINGER 0 MOVE
10363 processSlot(mapper, 0);
10364 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10365 processSync(mapper);
10366
10367 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10370 ASSERT_NO_FATAL_FAILURE(
10371 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10372 ASSERT_NO_FATAL_FAILURE(
10373 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10374
10375 // BUTTON DOWN
10376 processKey(mapper, BTN_LEFT, 1);
10377 processSync(mapper);
10378
10379 // touchinputmapper design sends a move before button press
10380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10383 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10384
10385 // BUTTON UP
10386 processKey(mapper, BTN_LEFT, 0);
10387 processSync(mapper);
10388
10389 // touchinputmapper design sends a move after button release
10390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10391 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10394
10395 // FINGER 0 UP
10396 processId(mapper, -1);
10397 processSync(mapper);
10398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10399 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10400
10401 // FINGER 1 MOVE
10402 processSlot(mapper, 1);
10403 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10404 processSync(mapper);
10405
10406 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010409 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010410 ASSERT_EQ(1, args.pointerProperties[0].id);
10411 ASSERT_NO_FATAL_FAILURE(
10412 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10413
10414 // FINGER 1 UP
10415 processId(mapper, -1);
10416 processKey(mapper, BTN_TOUCH, 0);
10417 processSync(mapper);
10418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10419 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10420
Josep del Río2d8c79a2023-01-23 19:33:50 +000010421 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010422 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010423 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010425 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010426}
10427
10428TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10429 std::shared_ptr<FakePointerController> fakePointerController =
10430 std::make_shared<FakePointerController>();
10431 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10432 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010433
10434 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010435 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010436 prepareAxes(POSITION | ID | SLOT);
10437 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10438 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010439 mFakePolicy->setPointerController(fakePointerController);
Arpit Singh56adebc2023-04-25 13:56:05 +000010440 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010441 // run uncaptured pointer tests - pushes out generic events
10442 // FINGER 0 DOWN
10443 processId(mapper, 3);
10444 processPosition(mapper, 100, 100);
10445 processKey(mapper, BTN_TOUCH, 1);
10446 processSync(mapper);
10447
10448 // start at (100,100), cursor should be at (0,0) * scale
10449 NotifyMotionArgs args;
10450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10451 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10452 ASSERT_NO_FATAL_FAILURE(
10453 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10454
10455 // FINGER 0 MOVE
10456 processPosition(mapper, 200, 200);
10457 processSync(mapper);
10458
10459 // compute scaling to help with touch position checking
10460 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10461 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10462 float scale =
10463 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10464
10465 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10467 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10469 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010470
10471 // BUTTON DOWN
10472 processKey(mapper, BTN_LEFT, 1);
10473 processSync(mapper);
10474
10475 // touchinputmapper design sends a move before button press
10476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10477 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10479 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10480
10481 // BUTTON UP
10482 processKey(mapper, BTN_LEFT, 0);
10483 processSync(mapper);
10484
10485 // touchinputmapper design sends a move after button release
10486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10487 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10489 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010490}
10491
10492TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10493 std::shared_ptr<FakePointerController> fakePointerController =
10494 std::make_shared<FakePointerController>();
10495
Michael Wrighta9cf4192022-12-01 23:46:39 +000010496 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010497 prepareAxes(POSITION | ID | SLOT);
10498 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010499 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010500 mFakePolicy->setPointerCapture(false);
Arpit Singh56adebc2023-04-25 13:56:05 +000010501 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010502
Josep del Río2d8c79a2023-01-23 19:33:50 +000010503 // uncaptured touchpad should be a pointer device
10504 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010505
Josep del Río2d8c79a2023-01-23 19:33:50 +000010506 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010507 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010508 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010509 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10510}
10511
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010512// --- BluetoothMultiTouchInputMapperTest ---
10513
10514class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10515protected:
10516 void SetUp() override {
10517 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10518 }
10519};
10520
10521TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10522 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010523 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010524 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010525 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010526
10527 nsecs_t kernelEventTime = ARBITRARY_TIME;
10528 nsecs_t expectedEventTime = ARBITRARY_TIME;
10529 // Touch down.
10530 processId(mapper, FIRST_TRACKING_ID);
10531 processPosition(mapper, 100, 200);
10532 processPressure(mapper, RAW_PRESSURE_MAX);
10533 processSync(mapper, ARBITRARY_TIME);
10534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10535 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10536
10537 // Process several events that come in quick succession, according to their timestamps.
10538 for (int i = 0; i < 3; i++) {
10539 constexpr static nsecs_t delta = ms2ns(1);
10540 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10541 kernelEventTime += delta;
10542 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10543
10544 processPosition(mapper, 101 + i, 201 + i);
10545 processSync(mapper, kernelEventTime);
10546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10547 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10548 WithEventTime(expectedEventTime))));
10549 }
10550
10551 // Release the touch.
10552 processId(mapper, INVALID_TRACKING_ID);
10553 processPressure(mapper, RAW_PRESSURE_MIN);
10554 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10556 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10557 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10558}
10559
10560// --- MultiTouchPointerModeTest ---
10561
HQ Liue6983c72022-04-19 22:14:56 +000010562class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10563protected:
10564 float mPointerMovementScale;
10565 float mPointerXZoomScale;
10566 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10567 addConfigurationProperty("touch.deviceType", "pointer");
10568 std::shared_ptr<FakePointerController> fakePointerController =
10569 std::make_shared<FakePointerController>();
10570 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10571 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010572 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010573
10574 prepareAxes(POSITION);
10575 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10576 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10577 // needs to be disabled, and the pointer gesture needs to be enabled.
10578 mFakePolicy->setPointerCapture(false);
10579 mFakePolicy->setPointerGestureEnabled(true);
10580 mFakePolicy->setPointerController(fakePointerController);
10581
10582 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10583 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10584 mPointerMovementScale =
10585 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10586 mPointerXZoomScale =
10587 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10588 }
10589
10590 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10591 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10592 /*flat*/ 0,
10593 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10594 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10595 /*flat*/ 0,
10596 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10597 }
10598};
10599
10600/**
10601 * Two fingers down on a pointer mode touch pad. The width
10602 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10603 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10604 * be greater than the both value to be freeform gesture, so that after two
10605 * fingers start to move downwards, the gesture should be swipe.
10606 */
10607TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10608 // The min freeform gesture width is 25units/mm x 30mm = 750
10609 // which is greater than fraction of the diagnal length of the touchpad (349).
10610 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010611 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010612 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010613 NotifyMotionArgs motionArgs;
10614
10615 // Two fingers down at once.
10616 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10617 // Pointer's initial position is used the [0,0] coordinate.
10618 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10619
10620 processId(mapper, FIRST_TRACKING_ID);
10621 processPosition(mapper, x1, y1);
10622 processMTSync(mapper);
10623 processId(mapper, SECOND_TRACKING_ID);
10624 processPosition(mapper, x2, y2);
10625 processMTSync(mapper);
10626 processSync(mapper);
10627
10628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010629 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010630 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010631 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010632 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010633 ASSERT_NO_FATAL_FAILURE(
10634 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10635
10636 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10637 // that there should be 1 pointer.
10638 int32_t movingDistance = 200;
10639 y1 += movingDistance;
10640 y2 += movingDistance;
10641
10642 processId(mapper, FIRST_TRACKING_ID);
10643 processPosition(mapper, x1, y1);
10644 processMTSync(mapper);
10645 processId(mapper, SECOND_TRACKING_ID);
10646 processPosition(mapper, x2, y2);
10647 processMTSync(mapper);
10648 processSync(mapper);
10649
10650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010651 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010653 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010654 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10656 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10657 0, 0, 0, 0));
10658}
10659
10660/**
10661 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10662 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10663 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10664 * value to be freeform gesture, so that after two fingers start to move downwards,
10665 * the gesture should be swipe.
10666 */
10667TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10668 // The min freeform gesture width is 5units/mm x 30mm = 150
10669 // which is greater than fraction of the diagnal length of the touchpad (349).
10670 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010671 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singh56adebc2023-04-25 13:56:05 +000010672 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010673 NotifyMotionArgs motionArgs;
10674
10675 // Two fingers down at once.
10676 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10677 // Pointer's initial position is used the [0,0] coordinate.
10678 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10679
10680 processId(mapper, FIRST_TRACKING_ID);
10681 processPosition(mapper, x1, y1);
10682 processMTSync(mapper);
10683 processId(mapper, SECOND_TRACKING_ID);
10684 processPosition(mapper, x2, y2);
10685 processMTSync(mapper);
10686 processSync(mapper);
10687
10688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010689 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010690 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010691 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010692 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010693 ASSERT_NO_FATAL_FAILURE(
10694 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10695
10696 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10697 // and there should be 1 pointer.
10698 int32_t movingDistance = 200;
10699 y1 += movingDistance;
10700 y2 += movingDistance;
10701
10702 processId(mapper, FIRST_TRACKING_ID);
10703 processPosition(mapper, x1, y1);
10704 processMTSync(mapper);
10705 processId(mapper, SECOND_TRACKING_ID);
10706 processPosition(mapper, x2, y2);
10707 processMTSync(mapper);
10708 processSync(mapper);
10709
10710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010711 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010713 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010714 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010715 // New coordinate is the scaled relative coordinate from the initial coordinate.
10716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10717 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10718 0, 0, 0, 0));
10719}
10720
10721/**
10722 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10723 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10724 * freeform gestures after two fingers start to move downwards.
10725 */
10726TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010727 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010728 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010729
10730 NotifyMotionArgs motionArgs;
10731
10732 // Two fingers down at once. Wider than the max swipe width.
10733 // The gesture is expected to be PRESS, then transformed to FREEFORM
10734 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10735
10736 processId(mapper, FIRST_TRACKING_ID);
10737 processPosition(mapper, x1, y1);
10738 processMTSync(mapper);
10739 processId(mapper, SECOND_TRACKING_ID);
10740 processPosition(mapper, x2, y2);
10741 processMTSync(mapper);
10742 processSync(mapper);
10743
10744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010745 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010746 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010747 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010748 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010749 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10750 ASSERT_NO_FATAL_FAILURE(
10751 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10752
10753 int32_t movingDistance = 200;
10754
10755 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10756 // then two down events for two pointers.
10757 y1 += movingDistance;
10758 y2 += movingDistance;
10759
10760 processId(mapper, FIRST_TRACKING_ID);
10761 processPosition(mapper, x1, y1);
10762 processMTSync(mapper);
10763 processId(mapper, SECOND_TRACKING_ID);
10764 processPosition(mapper, x2, y2);
10765 processMTSync(mapper);
10766 processSync(mapper);
10767
10768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10769 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010770 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010771 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010773 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010774 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010775 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010777 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010778 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010779 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010780 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010781 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010782 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010783 // Two pointers' scaled relative coordinates from their initial centroid.
10784 // Initial y coordinates are 0 as y1 and y2 have the same value.
10785 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10786 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10787 // When pointers move, the new coordinates equal to the initial coordinates plus
10788 // scaled moving distance.
10789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10790 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10791 0, 0, 0, 0));
10792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10793 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10794 0, 0, 0, 0));
10795
10796 // Move two fingers down again, expect one MOVE motion event.
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(2U, 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::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10814 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10815 0, 0, 0, 0, 0));
10816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10817 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10818 0, 0, 0, 0, 0));
10819}
10820
Harry Cutts39b7ca22022-10-05 15:55:48 +000010821TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010822 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010823 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010824 NotifyMotionArgs motionArgs;
10825
10826 // Place two fingers down.
10827 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10828
10829 processId(mapper, FIRST_TRACKING_ID);
10830 processPosition(mapper, x1, y1);
10831 processMTSync(mapper);
10832 processId(mapper, SECOND_TRACKING_ID);
10833 processPosition(mapper, x2, y2);
10834 processMTSync(mapper);
10835 processSync(mapper);
10836
10837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010838 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010839 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10840 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10841 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10842 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10843
10844 // Move the two fingers down and to the left.
10845 int32_t movingDistance = 200;
10846 x1 -= movingDistance;
10847 y1 += movingDistance;
10848 x2 -= movingDistance;
10849 y2 += movingDistance;
10850
10851 processId(mapper, FIRST_TRACKING_ID);
10852 processPosition(mapper, x1, y1);
10853 processMTSync(mapper);
10854 processId(mapper, SECOND_TRACKING_ID);
10855 processPosition(mapper, x2, y2);
10856 processMTSync(mapper);
10857 processSync(mapper);
10858
10859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010860 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010861 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10862 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10863 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10864 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10865}
10866
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010867TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010868 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010869 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +000010870 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10872
10873 // Start a stylus gesture.
10874 processKey(mapper, BTN_TOOL_PEN, 1);
10875 processId(mapper, FIRST_TRACKING_ID);
10876 processPosition(mapper, 100, 200);
10877 processSync(mapper);
10878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10879 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10880 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010881 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010882 // TODO(b/257078296): Pointer mode generates extra event.
10883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10884 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10885 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010886 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10888
10889 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10890 // gesture should be disabled.
10891 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10892 viewport->isActive = false;
10893 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010894 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10896 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10897 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010898 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010899 // TODO(b/257078296): Pointer mode generates extra event.
10900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10901 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10902 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010903 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10905}
10906
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010907// --- JoystickInputMapperTest ---
10908
10909class JoystickInputMapperTest : public InputMapperTest {
10910protected:
10911 static const int32_t RAW_X_MIN;
10912 static const int32_t RAW_X_MAX;
10913 static const int32_t RAW_Y_MIN;
10914 static const int32_t RAW_Y_MAX;
10915
10916 void SetUp() override {
10917 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10918 }
10919 void prepareAxes() {
10920 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10921 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10922 }
10923
10924 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10926 }
10927
10928 void processSync(JoystickInputMapper& mapper) {
10929 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10930 }
10931
Michael Wrighta9cf4192022-12-01 23:46:39 +000010932 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010933 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10934 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10935 NO_PORT, ViewportType::VIRTUAL);
10936 }
10937};
10938
10939const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10940const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10941const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10942const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10943
10944TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10945 prepareAxes();
Arpit Singh2be4a362023-04-26 14:16:50 +000010946 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010947
10948 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10949
Michael Wrighta9cf4192022-12-01 23:46:39 +000010950 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010951
10952 // Send an axis event
10953 processAxis(mapper, ABS_X, 100);
10954 processSync(mapper);
10955
10956 NotifyMotionArgs args;
10957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10958 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10959
10960 // Send another axis event
10961 processAxis(mapper, ABS_Y, 100);
10962 processSync(mapper);
10963
10964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10965 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10966}
10967
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010968// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010969
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010970class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010971protected:
10972 static const char* DEVICE_NAME;
10973 static const char* DEVICE_LOCATION;
10974 static const int32_t DEVICE_ID;
10975 static const int32_t DEVICE_GENERATION;
10976 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010977 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010978 static const int32_t EVENTHUB_ID;
10979
10980 std::shared_ptr<FakeEventHub> mFakeEventHub;
10981 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010982 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010983 std::unique_ptr<InstrumentedInputReader> mReader;
10984 std::shared_ptr<InputDevice> mDevice;
10985
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010986 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010987 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010988 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010989 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010990 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010991 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010992 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10993 }
10994
10995 void SetUp() override { SetUp(DEVICE_CLASSES); }
10996
10997 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010998 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010999 mFakePolicy.clear();
11000 }
11001
Chris Yee2b1e5c2021-03-10 22:45:12 -080011002 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11003 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011004 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011005 InputDeviceIdentifier identifier;
11006 identifier.name = name;
11007 identifier.location = location;
11008 std::shared_ptr<InputDevice> device =
11009 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11010 identifier);
11011 mReader->pushNextDevice(device);
11012 mFakeEventHub->addDevice(eventHubId, name, classes);
11013 mReader->loopOnce();
11014 return device;
11015 }
11016
11017 template <class T, typename... Args>
11018 T& addControllerAndConfigure(Args... args) {
11019 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11020
11021 return controller;
11022 }
11023};
11024
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011025const char* PeripheralControllerTest::DEVICE_NAME = "device";
11026const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11027const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11028const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11029const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011030const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11031 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011032const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011033
11034// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011035class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011036protected:
11037 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011038 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011039 }
11040};
11041
11042TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011043 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011044
Harry Cuttsa5b71292022-11-28 12:56:17 +000011045 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11046 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11047 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011048}
11049
11050TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011051 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011052
Harry Cuttsa5b71292022-11-28 12:56:17 +000011053 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11054 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11055 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011056}
11057
11058// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011059class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011060protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011061 void SetUp() override {
11062 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11063 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011064};
11065
Chris Ye85758332021-05-16 23:05:17 -070011066TEST_F(LightControllerTest, MonoLight) {
11067 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011068 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011069 .maxBrightness = 255,
11070 .flags = InputLightClass::BRIGHTNESS,
11071 .path = ""};
11072 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011073
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011074 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011075 InputDeviceInfo info;
11076 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011077 std::vector<InputDeviceLightInfo> lights = info.getLights();
11078 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011079 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11080 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11081
11082 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11083 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11084}
11085
11086TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11087 RawLightInfo infoMono = {.id = 1,
11088 .name = "mono_keyboard_backlight",
11089 .maxBrightness = 255,
11090 .flags = InputLightClass::BRIGHTNESS |
11091 InputLightClass::KEYBOARD_BACKLIGHT,
11092 .path = ""};
11093 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11094
11095 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11096 InputDeviceInfo info;
11097 controller.populateDeviceInfo(&info);
11098 std::vector<InputDeviceLightInfo> lights = info.getLights();
11099 ASSERT_EQ(1U, lights.size());
11100 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11101 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011102
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011103 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11104 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011105}
11106
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011107TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11108 RawLightInfo infoMono = {.id = 1,
11109 .name = "mono_light",
11110 .maxBrightness = 255,
11111 .flags = InputLightClass::BRIGHTNESS,
11112 .path = ""};
11113 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11114 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11115 "0,100,200");
11116
11117 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11118 std::list<NotifyArgs> unused =
11119 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11120 /*changes=*/{});
11121
11122 InputDeviceInfo info;
11123 controller.populateDeviceInfo(&info);
11124 std::vector<InputDeviceLightInfo> lights = info.getLights();
11125 ASSERT_EQ(1U, lights.size());
11126 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11127}
11128
11129TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11130 RawLightInfo infoMono = {.id = 1,
11131 .name = "mono_keyboard_backlight",
11132 .maxBrightness = 255,
11133 .flags = InputLightClass::BRIGHTNESS |
11134 InputLightClass::KEYBOARD_BACKLIGHT,
11135 .path = ""};
11136 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11137
11138 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11139 std::list<NotifyArgs> unused =
11140 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11141 /*changes=*/{});
11142
11143 InputDeviceInfo info;
11144 controller.populateDeviceInfo(&info);
11145 std::vector<InputDeviceLightInfo> lights = info.getLights();
11146 ASSERT_EQ(1U, lights.size());
11147 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11148}
11149
11150TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11151 RawLightInfo infoMono = {.id = 1,
11152 .name = "mono_keyboard_backlight",
11153 .maxBrightness = 255,
11154 .flags = InputLightClass::BRIGHTNESS |
11155 InputLightClass::KEYBOARD_BACKLIGHT,
11156 .path = ""};
11157 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11158 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11159 "0,100,200");
11160
11161 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11162 std::list<NotifyArgs> unused =
11163 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11164 /*changes=*/{});
11165
11166 InputDeviceInfo info;
11167 controller.populateDeviceInfo(&info);
11168 std::vector<InputDeviceLightInfo> lights = info.getLights();
11169 ASSERT_EQ(1U, lights.size());
11170 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
11171 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
11172 ASSERT_EQ(BrightnessLevel(0), *it);
11173 std::advance(it, 1);
11174 ASSERT_EQ(BrightnessLevel(100), *it);
11175 std::advance(it, 1);
11176 ASSERT_EQ(BrightnessLevel(200), *it);
11177}
11178
11179TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
11180 RawLightInfo infoMono = {.id = 1,
11181 .name = "mono_keyboard_backlight",
11182 .maxBrightness = 255,
11183 .flags = InputLightClass::BRIGHTNESS |
11184 InputLightClass::KEYBOARD_BACKLIGHT,
11185 .path = ""};
11186 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11187 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11188 "0,100,200,300,400,500");
11189
11190 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11191 std::list<NotifyArgs> unused =
11192 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11193 /*changes=*/{});
11194
11195 InputDeviceInfo info;
11196 controller.populateDeviceInfo(&info);
11197 std::vector<InputDeviceLightInfo> lights = info.getLights();
11198 ASSERT_EQ(1U, lights.size());
11199 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11200}
11201
Chris Yee2b1e5c2021-03-10 22:45:12 -080011202TEST_F(LightControllerTest, RGBLight) {
11203 RawLightInfo infoRed = {.id = 1,
11204 .name = "red",
11205 .maxBrightness = 255,
11206 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11207 .path = ""};
11208 RawLightInfo infoGreen = {.id = 2,
11209 .name = "green",
11210 .maxBrightness = 255,
11211 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11212 .path = ""};
11213 RawLightInfo infoBlue = {.id = 3,
11214 .name = "blue",
11215 .maxBrightness = 255,
11216 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11217 .path = ""};
11218 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11219 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11220 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11221
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011222 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011223 InputDeviceInfo info;
11224 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011225 std::vector<InputDeviceLightInfo> lights = info.getLights();
11226 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011227 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11228 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11229 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11230
11231 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11232 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11233}
11234
11235TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11236 RawLightInfo infoRed = {.id = 1,
11237 .name = "red_keyboard_backlight",
11238 .maxBrightness = 255,
11239 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11240 InputLightClass::KEYBOARD_BACKLIGHT,
11241 .path = ""};
11242 RawLightInfo infoGreen = {.id = 2,
11243 .name = "green_keyboard_backlight",
11244 .maxBrightness = 255,
11245 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11246 InputLightClass::KEYBOARD_BACKLIGHT,
11247 .path = ""};
11248 RawLightInfo infoBlue = {.id = 3,
11249 .name = "blue_keyboard_backlight",
11250 .maxBrightness = 255,
11251 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11252 InputLightClass::KEYBOARD_BACKLIGHT,
11253 .path = ""};
11254 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11255 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11256 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11257
11258 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11259 InputDeviceInfo info;
11260 controller.populateDeviceInfo(&info);
11261 std::vector<InputDeviceLightInfo> lights = info.getLights();
11262 ASSERT_EQ(1U, lights.size());
11263 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11264 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11265 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11266
11267 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11268 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11269}
11270
11271TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11272 RawLightInfo infoRed = {.id = 1,
11273 .name = "red",
11274 .maxBrightness = 255,
11275 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11276 .path = ""};
11277 RawLightInfo infoGreen = {.id = 2,
11278 .name = "green",
11279 .maxBrightness = 255,
11280 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11281 .path = ""};
11282 RawLightInfo infoBlue = {.id = 3,
11283 .name = "blue",
11284 .maxBrightness = 255,
11285 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11286 .path = ""};
11287 RawLightInfo infoGlobal = {.id = 3,
11288 .name = "global_keyboard_backlight",
11289 .maxBrightness = 255,
11290 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11291 InputLightClass::KEYBOARD_BACKLIGHT,
11292 .path = ""};
11293 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11294 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11295 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11296 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11297
11298 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11299 InputDeviceInfo info;
11300 controller.populateDeviceInfo(&info);
11301 std::vector<InputDeviceLightInfo> lights = info.getLights();
11302 ASSERT_EQ(1U, lights.size());
11303 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11304 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11305 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011306
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011307 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11308 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011309}
11310
11311TEST_F(LightControllerTest, MultiColorRGBLight) {
11312 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011313 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011314 .maxBrightness = 255,
11315 .flags = InputLightClass::BRIGHTNESS |
11316 InputLightClass::MULTI_INTENSITY |
11317 InputLightClass::MULTI_INDEX,
11318 .path = ""};
11319
11320 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11321
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011322 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011323 InputDeviceInfo info;
11324 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011325 std::vector<InputDeviceLightInfo> lights = info.getLights();
11326 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011327 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11328 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11329 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11330
11331 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11332 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11333}
11334
11335TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11336 RawLightInfo infoColor = {.id = 1,
11337 .name = "multi_color_keyboard_backlight",
11338 .maxBrightness = 255,
11339 .flags = InputLightClass::BRIGHTNESS |
11340 InputLightClass::MULTI_INTENSITY |
11341 InputLightClass::MULTI_INDEX |
11342 InputLightClass::KEYBOARD_BACKLIGHT,
11343 .path = ""};
11344
11345 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11346
11347 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11348 InputDeviceInfo info;
11349 controller.populateDeviceInfo(&info);
11350 std::vector<InputDeviceLightInfo> lights = info.getLights();
11351 ASSERT_EQ(1U, lights.size());
11352 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11353 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11354 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011355
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011356 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11357 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011358}
11359
11360TEST_F(LightControllerTest, PlayerIdLight) {
11361 RawLightInfo info1 = {.id = 1,
11362 .name = "player1",
11363 .maxBrightness = 255,
11364 .flags = InputLightClass::BRIGHTNESS,
11365 .path = ""};
11366 RawLightInfo info2 = {.id = 2,
11367 .name = "player2",
11368 .maxBrightness = 255,
11369 .flags = InputLightClass::BRIGHTNESS,
11370 .path = ""};
11371 RawLightInfo info3 = {.id = 3,
11372 .name = "player3",
11373 .maxBrightness = 255,
11374 .flags = InputLightClass::BRIGHTNESS,
11375 .path = ""};
11376 RawLightInfo info4 = {.id = 4,
11377 .name = "player4",
11378 .maxBrightness = 255,
11379 .flags = InputLightClass::BRIGHTNESS,
11380 .path = ""};
11381 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11382 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11383 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11384 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11385
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011386 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011387 InputDeviceInfo info;
11388 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011389 std::vector<InputDeviceLightInfo> lights = info.getLights();
11390 ASSERT_EQ(1U, lights.size());
11391 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011392 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11393 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011394
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011395 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11396 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11397 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011398}
11399
Michael Wrightd02c5b62014-02-10 15:10:22 -080011400} // namespace android