blob: 335f5240325f9307102b000fb450dfdf20def47f [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
Harry Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020
Prabir Pradhan2770d242019-09-02 18:07:11 -070021#include <CursorInputMapper.h>
22#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080025#include <InputReaderBase.h>
26#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000027#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <KeyboardInputMapper.h>
29#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070031#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070032#include <SingleTouchInputMapper.h>
33#include <SwitchInputMapper.h>
34#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000035#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070036#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080037#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000038#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039#include <android-base/thread_annotations.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000040#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050042#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000043#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080044
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070045#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000046#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000047#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000048#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000049#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000050#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000051#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000052#include "input/DisplayViewport.h"
53#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010054
Michael Wrightd02c5b62014-02-10 15:10:22 -080055namespace android {
56
Dominik Laskowski2f01d772022-03-23 16:01:29 -070057using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000058using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070059using std::chrono_literals::operator""ms;
60
Michael Wrightd02c5b62014-02-10 15:10:22 -080061// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000063static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t DISPLAY_WIDTH = 480;
67static constexpr int32_t DISPLAY_HEIGHT = 800;
68static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
69static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
70static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070071static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070072static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080073
arthurhungcc7f9802020-04-30 17:55:40 +080074static constexpr int32_t FIRST_SLOT = 0;
75static constexpr int32_t SECOND_SLOT = 1;
76static constexpr int32_t THIRD_SLOT = 2;
77static constexpr int32_t INVALID_TRACKING_ID = -1;
78static constexpr int32_t FIRST_TRACKING_ID = 0;
79static constexpr int32_t SECOND_TRACKING_ID = 1;
80static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080081static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
82static constexpr int32_t LIGHT_COLOR = 0x7F448866;
83static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080084
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080085static constexpr int32_t ACTION_POINTER_0_DOWN =
86 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
87static constexpr int32_t ACTION_POINTER_0_UP =
88 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
89static constexpr int32_t ACTION_POINTER_1_DOWN =
90 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_1_UP =
92 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000094// Minimum timestamp separation between subsequent input events from a Bluetooth device.
95static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
96// Maximum smoothing time delta so that we don't generate events too far into the future.
97constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
98
Michael Wrightd02c5b62014-02-10 15:10:22 -080099template<typename T>
100static inline T min(T a, T b) {
101 return a < b ? a : b;
102}
103
104static inline float avg(float x, float y) {
105 return (x + y) / 2;
106}
107
Chris Ye3fdbfef2021-01-06 18:45:18 -0800108// Mapping for light color name and the light color
109const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
110 {"green", LightColor::GREEN},
111 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112
Michael Wrighta9cf4192022-12-01 23:46:39 +0000113static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700114 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000115 case ui::ROTATION_90:
116 return ui::ROTATION_270;
117 case ui::ROTATION_270:
118 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700119 default:
120 return orientation;
121 }
122}
123
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800124static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
125 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000126 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800127
128 const InputDeviceInfo::MotionRange* motionRange =
129 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
130 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
131}
132
133static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
134 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000135 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800136
137 const InputDeviceInfo::MotionRange* motionRange =
138 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
139 ASSERT_EQ(nullptr, motionRange);
140}
141
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700142[[maybe_unused]] static void dumpReader(InputReader& reader) {
143 std::string dump;
144 reader.dump(dump);
145 std::istringstream iss(dump);
146 for (std::string line; std::getline(iss, line);) {
147 ALOGE("%s", line.c_str());
148 std::this_thread::sleep_for(std::chrono::milliseconds(1));
149 }
150}
151
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152// --- FakeInputMapper ---
153
154class FakeInputMapper : public InputMapper {
155 uint32_t mSources;
156 int32_t mKeyboardType;
157 int32_t mMetaState;
158 KeyedVector<int32_t, int32_t> mKeyCodeStates;
159 KeyedVector<int32_t, int32_t> mScanCodeStates;
160 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100161 // fake mapping which would normally come from keyCharacterMap
162 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800163 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700165 std::mutex mLock;
166 std::condition_variable mStateChangedCondition;
167 bool mConfigureWasCalled GUARDED_BY(mLock);
168 bool mResetWasCalled GUARDED_BY(mLock);
169 bool mProcessWasCalled GUARDED_BY(mLock);
170 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171
Arthur Hungc23540e2018-11-29 20:42:11 +0800172 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000174 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
175 uint32_t sources)
176 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800177 mSources(sources),
178 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800180 mConfigureWasCalled(false),
181 mResetWasCalled(false),
182 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
Chris Yea52ade12020-08-27 16:49:20 -0700184 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185
186 void setKeyboardType(int32_t keyboardType) {
187 mKeyboardType = keyboardType;
188 }
189
190 void setMetaState(int32_t metaState) {
191 mMetaState = metaState;
192 }
193
194 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700195 std::unique_lock<std::mutex> lock(mLock);
196 base::ScopedLockAssertion assumeLocked(mLock);
197 const bool configureCalled =
198 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
199 return mConfigureWasCalled;
200 });
201 if (!configureCalled) {
202 FAIL() << "Expected configure() to have been called.";
203 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204 mConfigureWasCalled = false;
205 }
206
207 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208 std::unique_lock<std::mutex> lock(mLock);
209 base::ScopedLockAssertion assumeLocked(mLock);
210 const bool resetCalled =
211 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
212 return mResetWasCalled;
213 });
214 if (!resetCalled) {
215 FAIL() << "Expected reset() to have been called.";
216 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 mResetWasCalled = false;
218 }
219
Yi Kong9b14ac62018-07-17 13:48:38 -0700220 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 std::unique_lock<std::mutex> lock(mLock);
222 base::ScopedLockAssertion assumeLocked(mLock);
223 const bool processCalled =
224 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
225 return mProcessWasCalled;
226 });
227 if (!processCalled) {
228 FAIL() << "Expected process() to have been called.";
229 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 if (outLastEvent) {
231 *outLastEvent = mLastEvent;
232 }
233 mProcessWasCalled = false;
234 }
235
236 void setKeyCodeState(int32_t keyCode, int32_t state) {
237 mKeyCodeStates.replaceValueFor(keyCode, state);
238 }
239
240 void setScanCodeState(int32_t scanCode, int32_t state) {
241 mScanCodeStates.replaceValueFor(scanCode, state);
242 }
243
244 void setSwitchState(int32_t switchCode, int32_t state) {
245 mSwitchStates.replaceValueFor(switchCode, state);
246 }
247
248 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800249 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250 }
251
Philip Junker4af3b3d2021-12-14 10:36:55 +0100252 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
253 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
254 }
255
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100257 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258
Harry Cuttsd02ea102023-03-17 18:21:30 +0000259 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 InputMapper::populateDeviceInfo(deviceInfo);
261
262 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000263 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264 }
265 }
266
Arpit Singhed6c3de2023-04-05 19:24:37 +0000267 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000268 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700269 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800271
272 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800273 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000274 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000275 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800276 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700277
278 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700279 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280 }
281
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700282 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700283 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700285 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700286 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 }
288
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700289 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700290 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291 mLastEvent = *rawEvent;
292 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700293 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700294 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295 }
296
Chris Yea52ade12020-08-27 16:49:20 -0700297 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
299 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
300 }
301
Philip Junker4af3b3d2021-12-14 10:36:55 +0100302 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
303 auto it = mKeyCodeMapping.find(locationKeyCode);
304 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
305 }
306
Chris Yea52ade12020-08-27 16:49:20 -0700307 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
309 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
310 }
311
Chris Yea52ade12020-08-27 16:49:20 -0700312 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 ssize_t index = mSwitchStates.indexOfKey(switchCode);
314 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
315 }
316
Chris Yea52ade12020-08-27 16:49:20 -0700317 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700318 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700319 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700320 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
322 if (keyCodes[i] == mSupportedKeyCodes[j]) {
323 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800324 }
325 }
326 }
Chris Yea52ade12020-08-27 16:49:20 -0700327 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328 return result;
329 }
330
331 virtual int32_t getMetaState() {
332 return mMetaState;
333 }
334
335 virtual void fadePointer() {
336 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800337
338 virtual std::optional<int32_t> getAssociatedDisplay() {
339 if (mViewport) {
340 return std::make_optional(mViewport->displayId);
341 }
342 return std::nullopt;
343 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344};
345
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700346// --- InputReaderPolicyTest ---
347class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700348protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700349 sp<FakeInputReaderPolicy> mFakePolicy;
350
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700351 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700352 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700353};
354
355/**
356 * Check that empty set of viewports is an acceptable configuration.
357 * Also try to get internal viewport two different ways - by type and by uniqueId.
358 *
359 * There will be confusion if two viewports with empty uniqueId and identical type are present.
360 * Such configuration is not currently allowed.
361 */
362TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700363 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700364
365 // We didn't add any viewports yet, so there shouldn't be any.
366 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100367 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700368 ASSERT_FALSE(internalViewport);
369
370 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000371 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000372 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700373
374 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700375 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700376 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100377 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700378
379 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100380 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700381 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700382 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700383
384 mFakePolicy->clearViewports();
385 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700386 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700387 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100388 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700389 ASSERT_FALSE(internalViewport);
390}
391
392TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
393 const std::string internalUniqueId = "local:0";
394 const std::string externalUniqueId = "local:1";
395 const std::string virtualUniqueId1 = "virtual:2";
396 const std::string virtualUniqueId2 = "virtual:3";
397 constexpr int32_t virtualDisplayId1 = 2;
398 constexpr int32_t virtualDisplayId2 = 3;
399
400 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000401 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000402 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000403 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700404 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000405 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000406 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000407 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700408 // Add an virtual viewport
409 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000410 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000411 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700412 // Add another virtual viewport
413 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000414 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000415 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700416
417 // Check matching by type for internal
418 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100419 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700420 ASSERT_TRUE(internalViewport);
421 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
422
423 // Check matching by type for external
424 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100425 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700426 ASSERT_TRUE(externalViewport);
427 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
428
429 // Check matching by uniqueId for virtual viewport #1
430 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700431 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700432 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100433 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700434 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
435 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
436
437 // Check matching by uniqueId for virtual viewport #2
438 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700439 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700440 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100441 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700442 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
443 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
444}
445
446
447/**
448 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
449 * that lookup works by checking display id.
450 * Check that 2 viewports of each kind is possible, for all existing viewport types.
451 */
452TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
453 const std::string uniqueId1 = "uniqueId1";
454 const std::string uniqueId2 = "uniqueId2";
455 constexpr int32_t displayId1 = 2;
456 constexpr int32_t displayId2 = 3;
457
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100458 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
459 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700460 for (const ViewportType& type : types) {
461 mFakePolicy->clearViewports();
462 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000463 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000464 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700465 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000466 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000467 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700468
469 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700470 std::optional<DisplayViewport> viewport1 =
471 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700472 ASSERT_TRUE(viewport1);
473 ASSERT_EQ(displayId1, viewport1->displayId);
474 ASSERT_EQ(type, viewport1->type);
475
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700476 std::optional<DisplayViewport> viewport2 =
477 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700478 ASSERT_TRUE(viewport2);
479 ASSERT_EQ(displayId2, viewport2->displayId);
480 ASSERT_EQ(type, viewport2->type);
481
482 // When there are multiple viewports of the same kind, and uniqueId is not specified
483 // in the call to getDisplayViewport, then that situation is not supported.
484 // The viewports can be stored in any order, so we cannot rely on the order, since that
485 // is just implementation detail.
486 // However, we can check that it still returns *a* viewport, we just cannot assert
487 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700488 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700489 ASSERT_TRUE(someViewport);
490 }
491}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700493/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000494 * When we have multiple internal displays make sure we always return the default display when
495 * querying by type.
496 */
497TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
498 const std::string uniqueId1 = "uniqueId1";
499 const std::string uniqueId2 = "uniqueId2";
500 constexpr int32_t nonDefaultDisplayId = 2;
501 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
502 "Test display ID should not be ADISPLAY_ID_DEFAULT");
503
504 // Add the default display first and ensure it gets returned.
505 mFakePolicy->clearViewports();
506 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000507 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000508 ViewportType::INTERNAL);
509 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000510 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000511 ViewportType::INTERNAL);
512
513 std::optional<DisplayViewport> viewport =
514 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
515 ASSERT_TRUE(viewport);
516 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
517 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
518
519 // Add the default display second to make sure order doesn't matter.
520 mFakePolicy->clearViewports();
521 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000522 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000523 ViewportType::INTERNAL);
524 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000525 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000526 ViewportType::INTERNAL);
527
528 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
529 ASSERT_TRUE(viewport);
530 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
531 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
532}
533
534/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700535 * Check getDisplayViewportByPort
536 */
537TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100538 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700539 const std::string uniqueId1 = "uniqueId1";
540 const std::string uniqueId2 = "uniqueId2";
541 constexpr int32_t displayId1 = 1;
542 constexpr int32_t displayId2 = 2;
543 const uint8_t hdmi1 = 0;
544 const uint8_t hdmi2 = 1;
545 const uint8_t hdmi3 = 2;
546
547 mFakePolicy->clearViewports();
548 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000549 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000550 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700551 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000552 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000553 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700554
555 // Check that correct display viewport was returned by comparing the display ports.
556 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
557 ASSERT_TRUE(hdmi1Viewport);
558 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
559 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
560
561 // Check that we can still get the same viewport using the uniqueId
562 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
563 ASSERT_TRUE(hdmi1Viewport);
564 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
565 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
566 ASSERT_EQ(type, hdmi1Viewport->type);
567
568 // Check that we cannot find a port with "HDMI2", because we never added one
569 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
570 ASSERT_FALSE(hdmi2Viewport);
571}
572
Michael Wrightd02c5b62014-02-10 15:10:22 -0800573// --- InputReaderTest ---
574
575class InputReaderTest : public testing::Test {
576protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700577 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700579 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000580 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581
Chris Yea52ade12020-08-27 16:49:20 -0700582 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700583 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700584 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700585 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586
Prabir Pradhan28efc192019-11-05 01:10:04 +0000587 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700588 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589 }
590
Chris Yea52ade12020-08-27 16:49:20 -0700591 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700592 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594 }
595
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700596 void addDevice(int32_t eventHubId, const std::string& name,
597 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800598 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599
600 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800601 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 }
603 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000604 mReader->loopOnce();
605 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700606 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700608 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 }
610
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800611 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700612 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000613 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700614 }
615
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800616 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700617 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000618 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700619 }
620
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700622 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700623 ftl::Flags<InputDeviceClass> classes,
624 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800625 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800626 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000627 FakeInputMapper& mapper =
628 device->addMapper<FakeInputMapper>(eventHubId,
629 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800630 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800631 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 return mapper;
633 }
634};
635
Chris Ye98d3f532020-10-01 21:48:59 -0700636TEST_F(InputReaderTest, PolicyGetInputDevices) {
637 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700638 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700639 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640
641 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700642 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800644 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100645 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800646 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
647 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000648 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649}
650
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000651TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
652 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
653 mFakeEventHub->setSysfsRootPath(1, "xyz");
654
655 // Should also have received a notification describing the new input device.
656 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
657 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
658 ASSERT_EQ(0U, inputDevice.getLights().size());
659
660 RawLightInfo infoMonolight = {.id = 123,
661 .name = "mono_keyboard_backlight",
662 .maxBrightness = 255,
663 .flags = InputLightClass::BRIGHTNESS,
664 .path = ""};
665 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
666 mReader->sysfsNodeChanged("xyz");
667 mReader->loopOnce();
668
669 // Should also have received a notification describing the new recreated input device.
670 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
671 inputDevice = mFakePolicy->getInputDevices()[0];
672 ASSERT_EQ(1U, inputDevice.getLights().size());
673}
674
Chris Yee7310032020-09-22 15:36:28 -0700675TEST_F(InputReaderTest, GetMergedInputDevices) {
676 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
677 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
678 // Add two subdevices to device
679 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
680 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000681 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
682 AINPUT_SOURCE_KEYBOARD);
683 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
684 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700685
686 // Push same device instance for next device to be added, so they'll have same identifier.
687 mReader->pushNextDevice(device);
688 mReader->pushNextDevice(device);
689 ASSERT_NO_FATAL_FAILURE(
690 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
691 ASSERT_NO_FATAL_FAILURE(
692 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
693
694 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000695 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700696}
697
Chris Yee14523a2020-12-19 13:46:00 -0800698TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
700 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
701 // Add two subdevices to device
702 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
703 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000704 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
705 AINPUT_SOURCE_KEYBOARD);
706 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
707 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800708
709 // Push same device instance for next device to be added, so they'll have same identifier.
710 mReader->pushNextDevice(device);
711 mReader->pushNextDevice(device);
712 // Sensor device is initially disabled
713 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
714 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
715 nullptr));
716 // Device is disabled because the only sub device is a sensor device and disabled initially.
717 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
718 ASSERT_FALSE(device->isEnabled());
719 ASSERT_NO_FATAL_FAILURE(
720 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
721 // The merged device is enabled if any sub device is enabled
722 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
723 ASSERT_TRUE(device->isEnabled());
724}
725
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700726TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800727 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700728 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800729 constexpr int32_t eventHubId = 1;
730 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700731 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000732 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
733 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800734 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800735 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700736
Yi Kong9b14ac62018-07-17 13:48:38 -0700737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700738
739 NotifyDeviceResetArgs resetArgs;
740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700741 ASSERT_EQ(deviceId, resetArgs.deviceId);
742
743 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800744 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000745 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700746
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700748 ASSERT_EQ(deviceId, resetArgs.deviceId);
749 ASSERT_EQ(device->isEnabled(), false);
750
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800751 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000752 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700755 ASSERT_EQ(device->isEnabled(), false);
756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800757 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000758 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700760 ASSERT_EQ(deviceId, resetArgs.deviceId);
761 ASSERT_EQ(device->isEnabled(), true);
762}
763
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800765 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700766 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800767 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800768 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800769 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800770 AINPUT_SOURCE_KEYBOARD, nullptr);
771 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800772
773 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
774 AINPUT_SOURCE_ANY, AKEYCODE_A))
775 << "Should return unknown when the device id is >= 0 but unknown.";
776
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800777 ASSERT_EQ(AKEY_STATE_UNKNOWN,
778 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
779 << "Should return unknown when the device id is valid but the sources are not "
780 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800781
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800782 ASSERT_EQ(AKEY_STATE_DOWN,
783 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
784 AKEYCODE_A))
785 << "Should return value provided by mapper when device id is valid and the device "
786 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787
788 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
789 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
790 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
791
792 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
793 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
794 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
795}
796
Philip Junker4af3b3d2021-12-14 10:36:55 +0100797TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
798 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
799 constexpr int32_t eventHubId = 1;
800 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
801 InputDeviceClass::KEYBOARD,
802 AINPUT_SOURCE_KEYBOARD, nullptr);
803 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
804
805 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
806 << "Should return unknown when the device with the specified id is not found.";
807
808 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
809 << "Should return correct mapping when device id is valid and mapping exists.";
810
811 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
812 << "Should return the location key code when device id is valid and there's no "
813 "mapping.";
814}
815
816TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
817 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
818 constexpr int32_t eventHubId = 1;
819 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
820 InputDeviceClass::JOYSTICK,
821 AINPUT_SOURCE_GAMEPAD, nullptr);
822 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
823
824 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
825 << "Should return unknown when the device id is valid but there is no keyboard mapper";
826}
827
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800829 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700830 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800831 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800832 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800833 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800834 AINPUT_SOURCE_KEYBOARD, nullptr);
835 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836
837 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
838 AINPUT_SOURCE_ANY, KEY_A))
839 << "Should return unknown when the device id is >= 0 but unknown.";
840
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800841 ASSERT_EQ(AKEY_STATE_UNKNOWN,
842 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
843 << "Should return unknown when the device id is valid but the sources are not "
844 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800846 ASSERT_EQ(AKEY_STATE_DOWN,
847 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
848 KEY_A))
849 << "Should return value provided by mapper when device id is valid and the device "
850 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851
852 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
853 AINPUT_SOURCE_TRACKBALL, KEY_A))
854 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
855
856 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
857 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
858 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
859}
860
861TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800862 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700863 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800865 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800866 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800867 AINPUT_SOURCE_KEYBOARD, nullptr);
868 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869
870 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
871 AINPUT_SOURCE_ANY, SW_LID))
872 << "Should return unknown when the device id is >= 0 but unknown.";
873
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800874 ASSERT_EQ(AKEY_STATE_UNKNOWN,
875 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
876 << "Should return unknown when the device id is valid but the sources are not "
877 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800879 ASSERT_EQ(AKEY_STATE_DOWN,
880 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
881 SW_LID))
882 << "Should return value provided by mapper when device id is valid and the device "
883 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800884
885 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
886 AINPUT_SOURCE_TRACKBALL, SW_LID))
887 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
888
889 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
890 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
891 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
892}
893
894TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800895 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700896 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800897 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800898 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800899 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800900 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100901
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800902 mapper.addSupportedKeyCode(AKEYCODE_A);
903 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700905 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800906 uint8_t flags[4] = { 0, 0, 0, 1 };
907
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700908 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 << "Should return false when device id is >= 0 but unknown.";
910 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
911
912 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700913 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800914 << "Should return false when device id is valid but the sources are not supported by "
915 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
917
918 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700919 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800920 keyCodes, flags))
921 << "Should return value provided by mapper when device id is valid and the device "
922 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
924
925 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700926 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
927 << "Should return false when the device id is < 0 but the sources are not supported by "
928 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
930
931 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700932 ASSERT_TRUE(
933 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
934 << "Should return value provided by mapper when device id is < 0 and one of the "
935 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800936 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
937}
938
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000939TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800940 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700941 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942
943 NotifyConfigurationChangedArgs args;
944
945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
946 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
947}
948
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000949TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800950 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700951 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000952 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800953 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000954 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800955 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800956 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800957 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000959 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000960 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
962
963 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800964 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000965 ASSERT_EQ(when, event.when);
966 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800967 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 ASSERT_EQ(EV_KEY, event.type);
969 ASSERT_EQ(KEY_A, event.code);
970 ASSERT_EQ(1, event.value);
971}
972
Garfield Tan1c7bc862020-01-28 13:24:04 -0800973TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800974 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700975 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800976 constexpr int32_t eventHubId = 1;
977 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800978 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000979 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
980 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800981 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800982 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800983
984 NotifyDeviceResetArgs resetArgs;
985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800986 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800987
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800988 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000989 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800991 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800992 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800994 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000995 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800997 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800998 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001000 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001001 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001003 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001004 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001005}
1006
Garfield Tan1c7bc862020-01-28 13:24:04 -08001007TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1008 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001009 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001010 constexpr int32_t eventHubId = 1;
1011 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1012 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001013 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1014 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001015 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001016 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1017
1018 NotifyDeviceResetArgs resetArgs;
1019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1020 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1021}
1022
Arthur Hungc23540e2018-11-29 20:42:11 +08001023TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001024 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001025 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001026 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001027 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001028 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1029 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001030 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1031 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001032 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001033
1034 const uint8_t hdmi1 = 1;
1035
1036 // Associated touch screen with second display.
1037 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1038
1039 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001040 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001041 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001042 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001043 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001044 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001045 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001046 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001047 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001048
1049 // Add the device, and make sure all of the callbacks are triggered.
1050 // The device is added after the input port associations are processed since
1051 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001052 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001055 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001056
Arthur Hung2c9a3342019-07-23 14:18:59 +08001057 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001058 ASSERT_EQ(deviceId, device->getId());
1059 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1060 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001061
1062 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001063 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001064 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001065 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001066}
1067
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001068TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1069 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001070 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001071 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1072 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1073 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001074 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1075 AINPUT_SOURCE_KEYBOARD);
1076 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1077 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001078 mReader->pushNextDevice(device);
1079 mReader->pushNextDevice(device);
1080 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1081 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1082
1083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1084
1085 NotifyDeviceResetArgs resetArgs;
1086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1087 ASSERT_EQ(deviceId, resetArgs.deviceId);
1088 ASSERT_TRUE(device->isEnabled());
1089 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1090 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1091
1092 disableDevice(deviceId);
1093 mReader->loopOnce();
1094
1095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1096 ASSERT_EQ(deviceId, resetArgs.deviceId);
1097 ASSERT_FALSE(device->isEnabled());
1098 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1099 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1100
1101 enableDevice(deviceId);
1102 mReader->loopOnce();
1103
1104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1105 ASSERT_EQ(deviceId, resetArgs.deviceId);
1106 ASSERT_TRUE(device->isEnabled());
1107 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1108 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1109}
1110
1111TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1112 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001113 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001114 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1115 // Add two subdevices to device
1116 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1117 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001118 device->addMapper<FakeInputMapper>(eventHubIds[0],
1119 mFakePolicy->getReaderConfiguration(),
1120 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001121 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001122 device->addMapper<FakeInputMapper>(eventHubIds[1],
1123 mFakePolicy->getReaderConfiguration(),
1124 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001125 mReader->pushNextDevice(device);
1126 mReader->pushNextDevice(device);
1127 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1128 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1129
1130 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1131 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1132
1133 ASSERT_EQ(AKEY_STATE_DOWN,
1134 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1135 ASSERT_EQ(AKEY_STATE_DOWN,
1136 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1137 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1138 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1139}
1140
Prabir Pradhan7e186182020-11-10 13:56:45 -08001141TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1142 NotifyPointerCaptureChangedArgs args;
1143
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001144 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001145 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001146 mReader->loopOnce();
1147 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001148 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1149 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001150
1151 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001152 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001153 mReader->loopOnce();
1154 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001155 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001156
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001157 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001158 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001159 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001160 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001161 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001162}
1163
Chris Ye87143712020-11-10 05:05:58 +00001164class FakeVibratorInputMapper : public FakeInputMapper {
1165public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001166 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1167 const InputReaderConfiguration& readerConfig, uint32_t sources)
1168 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001169
1170 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1171};
1172
1173TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1174 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001175 ftl::Flags<InputDeviceClass> deviceClass =
1176 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001177 constexpr int32_t eventHubId = 1;
1178 const char* DEVICE_LOCATION = "BLUETOOTH";
1179 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1180 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001181 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1182 mFakePolicy->getReaderConfiguration(),
1183 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001184 mReader->pushNextDevice(device);
1185
1186 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1187 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1188
1189 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1190 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1191}
1192
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001193// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001194
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001195class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001196public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001197 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001198
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001199 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001200
Andy Chenf9f1a022022-08-29 20:07:10 -04001201 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1202
Chris Yee2b1e5c2021-03-10 22:45:12 -08001203 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1204
1205 void dump(std::string& dump) override {}
1206
1207 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1208 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001209 }
1210
Chris Yee2b1e5c2021-03-10 22:45:12 -08001211 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1212 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001213 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001214
1215 bool setLightColor(int32_t lightId, int32_t color) override {
1216 getDeviceContext().setLightBrightness(lightId, color >> 24);
1217 return true;
1218 }
1219
1220 std::optional<int32_t> getLightColor(int32_t lightId) override {
1221 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1222 if (!result.has_value()) {
1223 return std::nullopt;
1224 }
1225 return result.value() << 24;
1226 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001227
1228 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1229
1230 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1231
1232private:
1233 InputDeviceContext& mDeviceContext;
1234 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1235 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001236 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001237};
1238
Chris Yee2b1e5c2021-03-10 22:45:12 -08001239TEST_F(InputReaderTest, BatteryGetCapacity) {
1240 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001241 ftl::Flags<InputDeviceClass> deviceClass =
1242 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001243 constexpr int32_t eventHubId = 1;
1244 const char* DEVICE_LOCATION = "BLUETOOTH";
1245 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001246 FakePeripheralController& controller =
1247 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001248 mReader->pushNextDevice(device);
1249
1250 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1251
Harry Cuttsa5b71292022-11-28 12:56:17 +00001252 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1253 FakeEventHub::BATTERY_CAPACITY);
1254 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001255}
1256
1257TEST_F(InputReaderTest, BatteryGetStatus) {
1258 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001259 ftl::Flags<InputDeviceClass> deviceClass =
1260 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001261 constexpr int32_t eventHubId = 1;
1262 const char* DEVICE_LOCATION = "BLUETOOTH";
1263 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001264 FakePeripheralController& controller =
1265 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001266 mReader->pushNextDevice(device);
1267
1268 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1269
Harry Cuttsa5b71292022-11-28 12:56:17 +00001270 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1271 FakeEventHub::BATTERY_STATUS);
1272 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001273}
1274
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001275TEST_F(InputReaderTest, BatteryGetDevicePath) {
1276 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1277 ftl::Flags<InputDeviceClass> deviceClass =
1278 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1279 constexpr int32_t eventHubId = 1;
1280 const char* DEVICE_LOCATION = "BLUETOOTH";
1281 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1282 device->addController<FakePeripheralController>(eventHubId);
1283 mReader->pushNextDevice(device);
1284
1285 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1286
Harry Cuttsa5b71292022-11-28 12:56:17 +00001287 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001288}
1289
Chris Ye3fdbfef2021-01-06 18:45:18 -08001290TEST_F(InputReaderTest, LightGetColor) {
1291 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001292 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001293 constexpr int32_t eventHubId = 1;
1294 const char* DEVICE_LOCATION = "BLUETOOTH";
1295 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001296 FakePeripheralController& controller =
1297 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001298 mReader->pushNextDevice(device);
1299 RawLightInfo info = {.id = 1,
1300 .name = "Mono",
1301 .maxBrightness = 255,
1302 .flags = InputLightClass::BRIGHTNESS,
1303 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001304 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1305 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001306
1307 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001308
Harry Cutts33476232023-01-30 19:57:29 +00001309 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1310 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1311 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1312 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001313}
1314
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001315// --- InputReaderIntegrationTest ---
1316
1317// These tests create and interact with the InputReader only through its interface.
1318// The InputReader is started during SetUp(), which starts its processing in its own
1319// thread. The tests use linux uinput to emulate input devices.
1320// NOTE: Interacting with the physical device while these tests are running may cause
1321// the tests to fail.
1322class InputReaderIntegrationTest : public testing::Test {
1323protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001324 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001325 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001326 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001327
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001328 std::shared_ptr<FakePointerController> mFakePointerController;
1329
Chris Yea52ade12020-08-27 16:49:20 -07001330 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001331#if !defined(__ANDROID__)
1332 GTEST_SKIP();
1333#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001334 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001335 mFakePointerController = std::make_shared<FakePointerController>();
1336 mFakePolicy->setPointerController(mFakePointerController);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001337
Arpit Singha11fc942023-08-09 09:23:43 +00001338 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001339 }
1340
Chris Yea52ade12020-08-27 16:49:20 -07001341 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001342#if !defined(__ANDROID__)
1343 return;
1344#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001345 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001346 mReader.reset();
1347 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001348 mFakePolicy.clear();
1349 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001350
1351 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1352 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1353 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1354 [&name](const InputDeviceInfo& info) {
1355 return info.getIdentifier().name == name;
1356 });
1357 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1358 }
Arpit Singha11fc942023-08-09 09:23:43 +00001359
1360 void setupInputReader() {
1361 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1362 /*eventDidNotHappenTimeout=*/30ms);
1363
1364 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1365 *mTestListener);
1366 ASSERT_EQ(mReader->start(), OK);
1367
1368 // Since this test is run on a real device, all the input devices connected
1369 // to the test device will show up in mReader. We wait for those input devices to
1370 // show up before beginning the tests.
1371 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1372 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1373 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1374 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001375};
1376
1377TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1378 // An invalid input device that is only used for this test.
1379 class InvalidUinputDevice : public UinputDevice {
1380 public:
Harry Cutts33476232023-01-30 19:57:29 +00001381 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001382
1383 private:
1384 void configureDevice(int fd, uinput_user_dev* device) override {}
1385 };
1386
1387 const size_t numDevices = mFakePolicy->getInputDevices().size();
1388
1389 // UinputDevice does not set any event or key bits, so InputReader should not
1390 // consider it as a valid device.
1391 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1392 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1394 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1395
1396 invalidDevice.reset();
1397 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1398 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1399 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1400}
1401
1402TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1403 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1404
1405 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1406 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1407 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1408 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1409
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001410 const auto device = findDeviceByName(keyboard->getName());
1411 ASSERT_TRUE(device.has_value());
1412 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1413 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1414 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001415
1416 keyboard.reset();
1417 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1418 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1419 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1420}
1421
1422TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1423 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1424 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1425
1426 NotifyConfigurationChangedArgs configChangedArgs;
1427 ASSERT_NO_FATAL_FAILURE(
1428 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001429 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001430 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1431
1432 NotifyKeyArgs keyArgs;
1433 keyboard->pressAndReleaseHomeKey();
1434 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1435 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001436 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001437 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001438 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001439 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001440 prevTimestamp = keyArgs.eventTime;
1441
1442 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1443 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001444 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001445 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001446 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001447}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001448
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001449TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1450 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1451 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1452
1453 const auto device = findDeviceByName(stylus->getName());
1454 ASSERT_TRUE(device.has_value());
1455
Prabir Pradhana3621852022-10-14 18:57:23 +00001456 // An external stylus with buttons should also be recognized as a keyboard.
1457 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001458 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1459 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1460
1461 const auto DOWN =
1462 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1463 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1464
1465 stylus->pressAndReleaseKey(BTN_STYLUS);
1466 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1467 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1468 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1469 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1470
1471 stylus->pressAndReleaseKey(BTN_STYLUS2);
1472 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1473 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1474 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1475 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1476
1477 stylus->pressAndReleaseKey(BTN_STYLUS3);
1478 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1479 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1480 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1481 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1482}
1483
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001484/**
1485 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1486 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1487 * are passed to the listener.
1488 */
1489static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1490TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1491 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1492 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1493 NotifyKeyArgs keyArgs;
1494
1495 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
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_DOWN, keyArgs.scanCode);
1499
1500 controller->pressAndReleaseKey(BTN_GEAR_UP);
1501 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1502 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1503 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1504}
1505
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001506// --- TouchIntegrationTest ---
1507
Arpit Singha11fc942023-08-09 09:23:43 +00001508class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001509protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001510 const std::string UNIQUE_ID = "local:0";
1511
Chris Yea52ade12020-08-27 16:49:20 -07001512 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001513#if !defined(__ANDROID__)
1514 GTEST_SKIP();
1515#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001516 InputReaderIntegrationTest::SetUp();
1517 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001518 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1519 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001520
1521 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1522 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1523 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001524 const auto info = findDeviceByName(mDevice->getName());
1525 ASSERT_TRUE(info);
1526 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001527 }
1528
1529 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001530 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001531 std::optional<uint8_t> physicalPort,
1532 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001533 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001534 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001535 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001536 }
1537
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001538 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1539 NotifyMotionArgs args;
1540 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1541 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001542 ASSERT_EQ(points.size(), args.getPointerCount());
1543 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001544 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1545 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1546 }
1547 }
1548
Arthur Hungaab25622020-01-16 11:22:11 +08001549 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001550 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001551};
1552
Arpit Singha11fc942023-08-09 09:23:43 +00001553enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1554
1555class TouchIntegrationTest : public BaseTouchIntegrationTest,
1556 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1557protected:
1558 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1559 const std::string INPUT_PORT = "uinput_touch/input0";
1560
1561 void SetUp() override {
1562#if !defined(__ANDROID__)
1563 GTEST_SKIP();
1564#endif
1565 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1566 BaseTouchIntegrationTest::SetUp();
1567 return;
1568 }
1569
1570 // setup policy with a input-port or UniqueId association to the display
1571 bool isInputPortAssociation =
1572 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1573
1574 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1575 if (isInputPortAssociation) {
1576 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1577 } else {
1578 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1579 }
1580 mFakePointerController = std::make_shared<FakePointerController>();
1581 mFakePolicy->setPointerController(mFakePointerController);
1582
1583 InputReaderIntegrationTest::setupInputReader();
1584
1585 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1586 INPUT_PORT);
1587 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1588
1589 // Add a display linked to a physical port or UniqueId.
1590 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1591 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1592 ViewportType::INTERNAL);
1593 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1594 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1595 const auto info = findDeviceByName(mDevice->getName());
1596 ASSERT_TRUE(info);
1597 mDeviceInfo = *info;
1598 }
1599};
1600
1601TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001602 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1603 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1604 // presses).
1605 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1606 mDeviceInfo.getSources());
1607}
1608
Arpit Singha11fc942023-08-09 09:23:43 +00001609TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001610 NotifyMotionArgs args;
1611 const Point centerPoint = mDevice->getCenterPoint();
1612
1613 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001614 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001615 mDevice->sendDown(centerPoint);
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_DOWN, args.action);
1619
1620 // ACTION_MOVE
1621 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001622 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001623 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1625
1626 // ACTION_UP
1627 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001628 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001629 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1630 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1631}
1632
Arpit Singha11fc942023-08-09 09:23:43 +00001633TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001634 NotifyMotionArgs args;
1635 const Point centerPoint = mDevice->getCenterPoint();
1636
1637 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001638 mDevice->sendSlot(FIRST_SLOT);
1639 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001640 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001641 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001642 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1643 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1644
1645 // ACTION_POINTER_DOWN (Second slot)
1646 const Point secondPoint = centerPoint + Point(100, 100);
1647 mDevice->sendSlot(SECOND_SLOT);
1648 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001649 mDevice->sendDown(secondPoint);
1650 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001651 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001652 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001653
1654 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001655 mDevice->sendMove(secondPoint + Point(1, 1));
1656 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001657 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1659
1660 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001661 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001662 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001663 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001664 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001665
1666 // ACTION_UP
1667 mDevice->sendSlot(FIRST_SLOT);
1668 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001669 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001670 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1671 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1672}
1673
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001674/**
1675 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1676 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1677 * data?
1678 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1679 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1680 * for Pointer 0 only is generated after.
1681 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1682 * events, we will not miss any information.
1683 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1684 * event generated afterwards that contains the newest movement of pointer 0.
1685 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1686 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1687 * losing information about non-palm pointers.
1688 */
Arpit Singha11fc942023-08-09 09:23:43 +00001689TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001690 NotifyMotionArgs args;
1691 const Point centerPoint = mDevice->getCenterPoint();
1692
1693 // ACTION_DOWN
1694 mDevice->sendSlot(FIRST_SLOT);
1695 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1696 mDevice->sendDown(centerPoint);
1697 mDevice->sendSync();
1698 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1699
1700 // ACTION_POINTER_DOWN (Second slot)
1701 const Point secondPoint = centerPoint + Point(100, 100);
1702 mDevice->sendSlot(SECOND_SLOT);
1703 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1704 mDevice->sendDown(secondPoint);
1705 mDevice->sendSync();
1706 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1707
1708 // ACTION_MOVE (First slot)
1709 mDevice->sendSlot(FIRST_SLOT);
1710 mDevice->sendMove(centerPoint + Point(5, 5));
1711 // ACTION_POINTER_UP (Second slot)
1712 mDevice->sendSlot(SECOND_SLOT);
1713 mDevice->sendPointerUp();
1714 // Send a single sync for the above 2 pointer updates
1715 mDevice->sendSync();
1716
1717 // First, we should get POINTER_UP for the second pointer
1718 assertReceivedMotion(ACTION_POINTER_1_UP,
1719 {/*first pointer */ centerPoint + Point(5, 5),
1720 /*second pointer*/ secondPoint});
1721
1722 // Next, the MOVE event for the first pointer
1723 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1724}
1725
1726/**
1727 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1728 * move, and then it will go up, all in the same frame.
1729 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1730 * gets sent to the listener.
1731 */
Arpit Singha11fc942023-08-09 09:23:43 +00001732TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001733 NotifyMotionArgs args;
1734 const Point centerPoint = mDevice->getCenterPoint();
1735
1736 // ACTION_DOWN
1737 mDevice->sendSlot(FIRST_SLOT);
1738 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1739 mDevice->sendDown(centerPoint);
1740 mDevice->sendSync();
1741 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1742
1743 // ACTION_POINTER_DOWN (Second slot)
1744 const Point secondPoint = centerPoint + Point(100, 100);
1745 mDevice->sendSlot(SECOND_SLOT);
1746 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1747 mDevice->sendDown(secondPoint);
1748 mDevice->sendSync();
1749 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1750
1751 // ACTION_MOVE (First slot)
1752 mDevice->sendSlot(FIRST_SLOT);
1753 mDevice->sendMove(centerPoint + Point(5, 5));
1754 // ACTION_POINTER_UP (Second slot)
1755 mDevice->sendSlot(SECOND_SLOT);
1756 mDevice->sendMove(secondPoint + Point(6, 6));
1757 mDevice->sendPointerUp();
1758 // Send a single sync for the above 2 pointer updates
1759 mDevice->sendSync();
1760
1761 // First, we should get POINTER_UP for the second pointer
1762 // The movement of the second pointer during the liftoff frame is ignored.
1763 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1764 assertReceivedMotion(ACTION_POINTER_1_UP,
1765 {/*first pointer */ centerPoint + Point(5, 5),
1766 /*second pointer*/ secondPoint});
1767
1768 // Next, the MOVE event for the first pointer
1769 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1770}
1771
Arpit Singha11fc942023-08-09 09:23:43 +00001772TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001773 NotifyMotionArgs args;
1774 const Point centerPoint = mDevice->getCenterPoint();
1775
1776 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001777 mDevice->sendSlot(FIRST_SLOT);
1778 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001779 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001780 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001781 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1782 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1783
arthurhungcc7f9802020-04-30 17:55:40 +08001784 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001785 const Point secondPoint = centerPoint + Point(100, 100);
1786 mDevice->sendSlot(SECOND_SLOT);
1787 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1788 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001789 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001790 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001791 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001792
arthurhungcc7f9802020-04-30 17:55:40 +08001793 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001794 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001795 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001796 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1798
arthurhungcc7f9802020-04-30 17:55:40 +08001799 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1800 // a palm event.
1801 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001802 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001803 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001804 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001805 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001806 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001807
arthurhungcc7f9802020-04-30 17:55:40 +08001808 // Send up to second slot, expect first slot send moving.
1809 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001810 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001811 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1812 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001813
arthurhungcc7f9802020-04-30 17:55:40 +08001814 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001815 mDevice->sendSlot(FIRST_SLOT);
1816 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001817 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001818
arthurhungcc7f9802020-04-30 17:55:40 +08001819 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1820 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001821}
1822
Arpit Singha11fc942023-08-09 09:23:43 +00001823TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001824 const Point centerPoint = mDevice->getCenterPoint();
1825
1826 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1827 mDevice->sendSlot(FIRST_SLOT);
1828 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1829 mDevice->sendToolType(MT_TOOL_PEN);
1830 mDevice->sendDown(centerPoint);
1831 mDevice->sendSync();
1832 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1833 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001834 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001835
1836 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1837
1838 // Release the stylus touch.
1839 mDevice->sendUp();
1840 mDevice->sendSync();
1841 ASSERT_NO_FATAL_FAILURE(
1842 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1843
1844 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1845
1846 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1847 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1848 mDevice->sendToolType(MT_TOOL_FINGER);
1849 mDevice->sendDown(centerPoint);
1850 mDevice->sendSync();
1851 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1852 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001853 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001854
1855 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1856
1857 mDevice->sendUp();
1858 mDevice->sendSync();
1859 ASSERT_NO_FATAL_FAILURE(
1860 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1861
1862 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1863 // The policy should be notified of the stylus presence.
1864 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1865 mDevice->sendToolType(MT_TOOL_PEN);
1866 mDevice->sendMove(centerPoint);
1867 mDevice->sendSync();
1868 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1869 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001870 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001871
1872 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1873}
1874
Arpit Singha11fc942023-08-09 09:23:43 +00001875INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
1876 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
1877 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
1878 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
1879
Prabir Pradhan124ea442022-10-28 20:27:44 +00001880// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001881
Prabir Pradhan124ea442022-10-28 20:27:44 +00001882// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1883// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1884// stylus.
1885template <typename UinputStylusDevice>
Arpit Singha11fc942023-08-09 09:23:43 +00001886class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001887protected:
1888 void SetUp() override {
1889#if !defined(__ANDROID__)
1890 GTEST_SKIP();
1891#endif
Arpit Singha11fc942023-08-09 09:23:43 +00001892 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00001893 mTouchscreen = mDevice.get();
1894 mTouchscreenInfo = mDeviceInfo;
1895
1896 setUpStylusDevice();
1897 }
1898
1899 UinputStylusDevice* mStylus{nullptr};
1900 InputDeviceInfo mStylusInfo{};
1901
1902 UinputTouchScreen* mTouchscreen{nullptr};
1903 InputDeviceInfo mTouchscreenInfo{};
1904
1905private:
1906 // When we are attempting to test stylus button events that are sent from the touchscreen,
1907 // use the same Uinput device for the touchscreen and the stylus.
1908 template <typename T = UinputStylusDevice>
1909 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1910 mStylus = mDevice.get();
1911 mStylusInfo = mDeviceInfo;
1912 }
1913
1914 // When we are attempting to stylus buttons from an external stylus being merged with touches
1915 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1916 template <typename T = UinputStylusDevice>
1917 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1918 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1919 mStylus = mStylusDeviceLifecycleTracker.get();
1920 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1921 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1922 const auto info = findDeviceByName(mStylus->getName());
1923 ASSERT_TRUE(info);
1924 mStylusInfo = *info;
1925 }
1926
1927 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1928
1929 // Hide the base class's device to expose it with a different name for readability.
Arpit Singha11fc942023-08-09 09:23:43 +00001930 using BaseTouchIntegrationTest::mDevice;
1931 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00001932};
1933
1934using StylusButtonIntegrationTestTypes =
1935 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1936TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1937
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001938TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001939 const auto stylusId = TestFixture::mStylusInfo.getId();
1940
1941 TestFixture::mStylus->pressKey(BTN_STYLUS);
1942 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1943 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1944 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1945
1946 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1947 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001948 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001949 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001950}
1951
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001952TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001953 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1954 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1955 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001956
1957 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001958 TestFixture::mStylus->pressKey(BTN_STYLUS);
1959 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001960 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001961 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001962
1963 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001964 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1965 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1966 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1967 TestFixture::mTouchscreen->sendDown(centerPoint);
1968 TestFixture::mTouchscreen->sendSync();
1969 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001970 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001971 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001972 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1973 WithDeviceId(touchscreenId))));
1974 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001975 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001976 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001977 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1978 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001979
Prabir Pradhan124ea442022-10-28 20:27:44 +00001980 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1981 TestFixture::mTouchscreen->sendSync();
1982 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001983 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001984 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001985 WithDeviceId(touchscreenId))));
1986 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001987 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001988 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001989 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001990
1991 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001992 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1993 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001994 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001995 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001996}
1997
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001998TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001999 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2000 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2001 const auto stylusId = TestFixture::mStylusInfo.getId();
2002 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002003 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002004
2005 // Press the stylus button.
2006 TestFixture::mStylus->pressKey(BTN_STYLUS);
2007 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2008 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2009 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2010
2011 // Start hovering with the stylus.
2012 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2013 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2014 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2015 TestFixture::mTouchscreen->sendMove(centerPoint);
2016 TestFixture::mTouchscreen->sendSync();
2017 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2018 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2019 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2020 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2021 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2022 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2023 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2024 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2025 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2026
2027 // Touch down with the stylus.
2028 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2029 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2030 TestFixture::mTouchscreen->sendDown(centerPoint);
2031 TestFixture::mTouchscreen->sendSync();
2032 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2033 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2034 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2035
2036 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2037 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2038 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2039
2040 // Stop touching with the stylus, and start hovering.
2041 TestFixture::mTouchscreen->sendUp();
2042 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2043 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2044 TestFixture::mTouchscreen->sendMove(centerPoint);
2045 TestFixture::mTouchscreen->sendSync();
2046 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2047 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2048 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2049 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2050 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2051 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2052 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2053 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2054 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2055
2056 // Stop hovering.
2057 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2058 TestFixture::mTouchscreen->sendSync();
2059 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2060 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2061 WithButtonState(0))));
2062 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2063 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2064 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2065 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2066
2067 // Release the stylus button.
2068 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2069 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2070 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2071 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2072}
2073
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002074TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002075 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2076 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2077 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002078
2079 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002080 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2081 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2082 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2083 TestFixture::mTouchscreen->sendDown(centerPoint);
2084 TestFixture::mTouchscreen->sendSync();
2085 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002086 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002087 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002088 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002089
2090 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002091 TestFixture::mStylus->pressKey(BTN_STYLUS);
2092 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002093 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002094 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2095 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002096 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002097 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002098 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2099 WithDeviceId(touchscreenId))));
2100 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002101 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002102 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002103 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2104 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002105
Prabir Pradhan124ea442022-10-28 20:27:44 +00002106 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2107 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002108 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002109 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2110 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002111 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002112 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002113 WithDeviceId(touchscreenId))));
2114 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002115 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002116 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002117 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002118
2119 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002120 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2121 TestFixture::mTouchscreen->sendSync();
2122 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002123 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002124 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002125 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002126}
2127
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002128TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002129 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2130 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002131 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002132
2133 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2134 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2135 const auto stylusId = TestFixture::mStylusInfo.getId();
2136
2137 // Start a stylus gesture. By the time this event is processed, the configuration change that
2138 // was requested is guaranteed to be completed.
2139 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2140 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2141 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2142 TestFixture::mTouchscreen->sendDown(centerPoint);
2143 TestFixture::mTouchscreen->sendSync();
2144 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2145 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002146 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002147 WithDeviceId(touchscreenId))));
2148
2149 // Press and release a stylus button. Each change only generates a MOVE motion event.
2150 // Key events are unaffected.
2151 TestFixture::mStylus->pressKey(BTN_STYLUS);
2152 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2153 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2154 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2155 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2156 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002157 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002158 WithDeviceId(touchscreenId))));
2159
2160 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2161 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2162 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2163 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2164 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2165 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002166 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002167 WithDeviceId(touchscreenId))));
2168
2169 // Finish the stylus gesture.
2170 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2171 TestFixture::mTouchscreen->sendSync();
2172 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2173 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002174 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002175 WithDeviceId(touchscreenId))));
2176}
2177
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002178// --- ExternalStylusIntegrationTest ---
2179
2180// Verify the behavior of an external stylus. An external stylus can report pressure or button
2181// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2182// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singha11fc942023-08-09 09:23:43 +00002183using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002184
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002185TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002186 const Point centerPoint = mDevice->getCenterPoint();
2187
2188 // Create an external stylus capable of reporting pressure data that
2189 // should be fused with a touch pointer.
2190 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2191 createUinputDevice<UinputExternalStylusWithPressure>();
2192 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2193 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2194 const auto stylusInfo = findDeviceByName(stylus->getName());
2195 ASSERT_TRUE(stylusInfo);
2196
2197 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2198
2199 const auto touchscreenId = mDeviceInfo.getId();
2200
2201 // Set a pressure value on the stylus. It doesn't generate any events.
2202 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2203 stylus->setPressure(100);
2204 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2205
2206 // Start a finger gesture, and ensure it shows up as stylus gesture
2207 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002208 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002209 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002210 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002211 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002212 mDevice->sendSync();
2213 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2214 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002215 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002216 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002217
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002218 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2219 // event with the updated pressure.
2220 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002221 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2222 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002223 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002224 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002225
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002226 // The external stylus did not generate any events.
2227 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2228 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2229}
2230
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002231TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002232 const Point centerPoint = mDevice->getCenterPoint();
2233
2234 // Create an external stylus capable of reporting pressure data that
2235 // should be fused with a touch pointer.
2236 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2237 createUinputDevice<UinputExternalStylusWithPressure>();
2238 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2239 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2240 const auto stylusInfo = findDeviceByName(stylus->getName());
2241 ASSERT_TRUE(stylusInfo);
2242
2243 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2244
2245 const auto touchscreenId = mDeviceInfo.getId();
2246
2247 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2248 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002249 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2250 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002251 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002252 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002253
2254 // Start a finger gesture. The touch device will withhold generating any touches for
2255 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2256 mDevice->sendSlot(FIRST_SLOT);
2257 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2258 mDevice->sendToolType(MT_TOOL_FINGER);
2259 mDevice->sendDown(centerPoint);
2260 auto waitUntil = std::chrono::system_clock::now() +
2261 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002262 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002263 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002264
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002265 // Since the external stylus did not report a pressure value within the timeout,
2266 // it shows up as a finger pointer.
2267 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2268 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002269 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002270 WithPressure(1.f))));
2271
2272 // Change the pressure on the external stylus. Since the pressure was not present at the start
2273 // of the gesture, it is ignored for now.
2274 stylus->setPressure(200);
2275 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2276
2277 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002278 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2279 mDevice->sendSync();
2280 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2281 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002282 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002283
2284 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2285 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2286 mDevice->sendToolType(MT_TOOL_FINGER);
2287 mDevice->sendDown(centerPoint);
2288 mDevice->sendSync();
2289 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2290 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002291 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002292 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2293
2294 // The external stylus did not generate any events.
2295 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2296 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002297}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002298
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002299TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002300 const Point centerPoint = mDevice->getCenterPoint();
2301
2302 // Create an external stylus device that does not support pressure. It should not affect any
2303 // touch pointers.
2304 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2305 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2306 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2307 const auto stylusInfo = findDeviceByName(stylus->getName());
2308 ASSERT_TRUE(stylusInfo);
2309
2310 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2311
2312 const auto touchscreenId = mDeviceInfo.getId();
2313
2314 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2315 // pressure data from the external stylus.
2316 mDevice->sendSlot(FIRST_SLOT);
2317 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2318 mDevice->sendToolType(MT_TOOL_FINGER);
2319 mDevice->sendDown(centerPoint);
2320 auto waitUntil = std::chrono::system_clock::now() +
2321 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2322 mDevice->sendSync();
2323 ASSERT_NO_FATAL_FAILURE(
2324 mTestListener
2325 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2326 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002327 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002328 WithButtonState(0),
2329 WithDeviceId(touchscreenId),
2330 WithPressure(1.f)),
2331 waitUntil));
2332
2333 // The external stylus did not generate any events.
2334 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2335 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2336}
2337
Michael Wrightd02c5b62014-02-10 15:10:22 -08002338// --- InputDeviceTest ---
2339class InputDeviceTest : public testing::Test {
2340protected:
2341 static const char* DEVICE_NAME;
2342 static const char* DEVICE_LOCATION;
2343 static const int32_t DEVICE_ID;
2344 static const int32_t DEVICE_GENERATION;
2345 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002346 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002347 static const int32_t EVENTHUB_ID;
2348 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2349
2350 std::shared_ptr<FakeEventHub> mFakeEventHub;
2351 sp<FakeInputReaderPolicy> mFakePolicy;
2352 std::unique_ptr<TestInputListener> mFakeListener;
2353 std::unique_ptr<InstrumentedInputReader> mReader;
2354 std::shared_ptr<InputDevice> mDevice;
2355
2356 void SetUp() override {
2357 mFakeEventHub = std::make_unique<FakeEventHub>();
2358 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2359 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002360 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002361 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002362 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002363 identifier.name = DEVICE_NAME;
2364 identifier.location = DEVICE_LOCATION;
2365 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2366 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2367 identifier);
2368 mReader->pushNextDevice(mDevice);
2369 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002370 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002371 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002372
2373 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002374 mFakeListener.reset();
2375 mFakePolicy.clear();
2376 }
2377};
2378
2379const char* InputDeviceTest::DEVICE_NAME = "device";
2380const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2381const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2382const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002383const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002384const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2385 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002386const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002387const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2388
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002389TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002390 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002391 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2392 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002393}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002394
Michael Wrightd02c5b62014-02-10 15:10:22 -08002395TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2396 ASSERT_EQ(mDevice->isEnabled(), false);
2397}
2398
2399TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2400 // Configuration.
2401 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002402 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002403
2404 // Reset.
2405 unused += mDevice->reset(ARBITRARY_TIME);
2406
2407 NotifyDeviceResetArgs resetArgs;
2408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2409 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2410 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2411
2412 // Metadata.
2413 ASSERT_TRUE(mDevice->isIgnored());
2414 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2415
2416 InputDeviceInfo info = mDevice->getDeviceInfo();
2417 ASSERT_EQ(DEVICE_ID, info.getId());
2418 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2419 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2420 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2421
2422 // State queries.
2423 ASSERT_EQ(0, mDevice->getMetaState());
2424
2425 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2426 << "Ignored device should return unknown key code state.";
2427 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2428 << "Ignored device should return unknown scan code state.";
2429 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2430 << "Ignored device should return unknown switch state.";
2431
2432 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2433 uint8_t flags[2] = { 0, 1 };
2434 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2435 << "Ignored device should never mark any key codes.";
2436 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2437 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2438}
2439
2440TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2441 // Configuration.
2442 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2443
2444 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002445 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2446 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002447 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2448 mapper1.setMetaState(AMETA_ALT_ON);
2449 mapper1.addSupportedKeyCode(AKEYCODE_A);
2450 mapper1.addSupportedKeyCode(AKEYCODE_B);
2451 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2452 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2453 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2454 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2455 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2456
2457 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002458 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2459 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002460 mapper2.setMetaState(AMETA_SHIFT_ON);
2461
2462 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002463 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002464
Harry Cuttsf13161a2023-03-08 14:15:49 +00002465 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2466 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002467 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002468 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002470 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2471 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472
2473 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002474 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002475 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2476 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002477
2478 NotifyDeviceResetArgs resetArgs;
2479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2480 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2481 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2482
2483 // Metadata.
2484 ASSERT_FALSE(mDevice->isIgnored());
2485 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2486
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002487 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002488 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002489 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002490 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2491 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2492
2493 // State queries.
2494 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2495 << "Should query mappers and combine meta states.";
2496
2497 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2498 << "Should return unknown key code state when source not supported.";
2499 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2500 << "Should return unknown scan code state when source not supported.";
2501 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2502 << "Should return unknown switch state when source not supported.";
2503
2504 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2505 << "Should query mapper when source is supported.";
2506 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2507 << "Should query mapper when source is supported.";
2508 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2509 << "Should query mapper when source is supported.";
2510
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002511 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002512 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002513 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002514 << "Should do nothing when source is unsupported.";
2515 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2516 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2517 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2518 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2519
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002520 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002521 << "Should query mapper when source is supported.";
2522 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2523 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2524 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2525 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2526
2527 // Event handling.
2528 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002529 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002530 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002531
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002532 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2533 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002534}
2535
Arthur Hung2c9a3342019-07-23 14:18:59 +08002536// A single input device is associated with a specific display. Check that:
2537// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh3e56f7e2023-07-07 13:12:37 +00002538// 2. Device is disabled when setEnabled API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002539TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002540 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2541 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002542
2543 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002544 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002545 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2546 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002547
2548 // Device should be enabled by default.
2549 ASSERT_TRUE(mDevice->isEnabled());
2550
2551 // Prepare associated info.
2552 constexpr uint8_t hdmi = 1;
2553 const std::string UNIQUE_ID = "local:1";
2554
2555 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
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);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002558 // Device should be disabled because it is associated with a specific display via
2559 // input port <-> display port association, but the corresponding display is not found
2560 ASSERT_FALSE(mDevice->isEnabled());
2561
2562 // Prepare displays.
2563 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002564 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002565 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002566 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002567 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002568 ASSERT_TRUE(mDevice->isEnabled());
2569
2570 // Device should be disabled after set disable.
2571 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002572 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002573 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002574 ASSERT_FALSE(mDevice->isEnabled());
2575
2576 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002577 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002578 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002579 ASSERT_FALSE(mDevice->isEnabled());
2580}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002581
Christine Franks1ba71cc2021-04-07 14:37:42 -07002582TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2583 // Device should be enabled by default.
2584 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002585 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2586 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002587 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002588 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2589 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002590 ASSERT_TRUE(mDevice->isEnabled());
2591
2592 // Device should be disabled because it is associated with a specific display, but the
2593 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002594 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002595 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002596 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002597 ASSERT_FALSE(mDevice->isEnabled());
2598
2599 // Device should be enabled when a display is found.
2600 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002601 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002602 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002603 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002604 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002605 ASSERT_TRUE(mDevice->isEnabled());
2606
2607 // Device should be disabled after set disable.
2608 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002609 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002610 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002611 ASSERT_FALSE(mDevice->isEnabled());
2612
2613 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002614 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002615 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002616 ASSERT_FALSE(mDevice->isEnabled());
2617}
2618
Christine Franks2a2293c2022-01-18 11:51:16 -08002619TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2620 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002621 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2622 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002623 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002624 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2625 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002626
Christine Franks2a2293c2022-01-18 11:51:16 -08002627 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2628 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002629 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002630 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002631 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002632 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002633 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2634}
2635
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002636/**
2637 * This test reproduces a crash caused by a dangling reference that remains after device is added
2638 * and removed. The reference is accessed in InputDevice::dump(..);
2639 */
2640TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2641 constexpr int32_t TEST_EVENTHUB_ID = 10;
2642 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2643
Harry Cutts33476232023-01-30 19:57:29 +00002644 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh7f1765e2023-07-07 13:12:37 +00002645 device.addEventHubDevice(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002646 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2647 std::string dumpStr, eventHubDevStr;
2648 device.dump(dumpStr, eventHubDevStr);
2649}
2650
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002651TEST_F(InputDeviceTest, GetBluetoothAddress) {
2652 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2653 ASSERT_TRUE(address);
2654 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2655}
2656
Michael Wrightd02c5b62014-02-10 15:10:22 -08002657// --- SwitchInputMapperTest ---
2658
2659class SwitchInputMapperTest : public InputMapperTest {
2660protected:
2661};
2662
2663TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002664 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002666 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667}
2668
2669TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002670 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002672 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002673 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002675 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002676 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677}
2678
2679TEST_F(SwitchInputMapperTest, Process) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002680 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002681 std::list<NotifyArgs> out;
2682 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2683 ASSERT_TRUE(out.empty());
2684 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2685 ASSERT_TRUE(out.empty());
2686 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2687 ASSERT_TRUE(out.empty());
2688 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002690 ASSERT_EQ(1u, out.size());
2691 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002692 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002693 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2694 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695 args.switchMask);
2696 ASSERT_EQ(uint32_t(0), args.policyFlags);
2697}
2698
Chris Ye87143712020-11-10 05:05:58 +00002699// --- VibratorInputMapperTest ---
2700class VibratorInputMapperTest : public InputMapperTest {
2701protected:
2702 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2703};
2704
2705TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singhc68f85b2023-04-26 16:23:13 +00002706 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002707
2708 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2709}
2710
2711TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singhc68f85b2023-04-26 16:23:13 +00002712 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002713
2714 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2715}
2716
2717TEST_F(VibratorInputMapperTest, Vibrate) {
2718 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002719 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singhc68f85b2023-04-26 16:23:13 +00002720 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002721
2722 VibrationElement pattern(2);
2723 VibrationSequence sequence(2);
2724 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002725 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2726 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002727 sequence.addElement(pattern);
2728 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002729 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2730 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002731 sequence.addElement(pattern);
2732
2733 std::vector<int64_t> timings = {0, 1};
2734 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2735
2736 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002737 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002738 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002739 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002740 // Verify vibrator state listener was notified.
2741 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002742 ASSERT_EQ(1u, out.size());
2743 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2744 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2745 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002746 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002747 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002748 ASSERT_FALSE(mapper.isVibrating());
2749 // Verify vibrator state listener was notified.
2750 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002751 ASSERT_EQ(1u, out.size());
2752 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2753 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2754 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002755}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002756
Chris Yef59a2f42020-10-16 12:55:26 -07002757// --- SensorInputMapperTest ---
2758
2759class SensorInputMapperTest : public InputMapperTest {
2760protected:
2761 static const int32_t ACCEL_RAW_MIN;
2762 static const int32_t ACCEL_RAW_MAX;
2763 static const int32_t ACCEL_RAW_FUZZ;
2764 static const int32_t ACCEL_RAW_FLAT;
2765 static const int32_t ACCEL_RAW_RESOLUTION;
2766
2767 static const int32_t GYRO_RAW_MIN;
2768 static const int32_t GYRO_RAW_MAX;
2769 static const int32_t GYRO_RAW_FUZZ;
2770 static const int32_t GYRO_RAW_FLAT;
2771 static const int32_t GYRO_RAW_RESOLUTION;
2772
2773 static const float GRAVITY_MS2_UNIT;
2774 static const float DEGREE_RADIAN_UNIT;
2775
2776 void prepareAccelAxes();
2777 void prepareGyroAxes();
2778 void setAccelProperties();
2779 void setGyroProperties();
2780 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2781};
2782
2783const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2784const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2785const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2786const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2787const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2788
2789const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2790const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2791const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2792const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2793const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2794
2795const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2796const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2797
2798void SensorInputMapperTest::prepareAccelAxes() {
2799 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2800 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2801 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2802 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2803 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2804 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2805}
2806
2807void SensorInputMapperTest::prepareGyroAxes() {
2808 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2809 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2810 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2811 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2812 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2813 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2814}
2815
2816void SensorInputMapperTest::setAccelProperties() {
2817 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2818 /* sensorDataIndex */ 0);
2819 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2820 /* sensorDataIndex */ 1);
2821 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2822 /* sensorDataIndex */ 2);
2823 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2824 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2825 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2826 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2827 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2828}
2829
2830void SensorInputMapperTest::setGyroProperties() {
2831 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2832 /* sensorDataIndex */ 0);
2833 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2834 /* sensorDataIndex */ 1);
2835 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2836 /* sensorDataIndex */ 2);
2837 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2838 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2839 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2840 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2841 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2842}
2843
2844TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singh61dd33e2023-04-26 15:07:55 +00002845 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002846
2847 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2848}
2849
2850TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2851 setAccelProperties();
2852 prepareAccelAxes();
Arpit Singh61dd33e2023-04-26 15:07:55 +00002853 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002854
2855 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2856 std::chrono::microseconds(10000),
2857 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002858 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2863 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002864
2865 NotifySensorArgs args;
2866 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2867 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2868 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2869
2870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2871 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2872 ASSERT_EQ(args.deviceId, DEVICE_ID);
2873 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2874 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2875 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2876 ASSERT_EQ(args.values, values);
2877 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2878}
2879
2880TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2881 setGyroProperties();
2882 prepareGyroAxes();
Arpit Singh61dd33e2023-04-26 15:07:55 +00002883 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002884
2885 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2886 std::chrono::microseconds(10000),
2887 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002888 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2890 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002894
2895 NotifySensorArgs args;
2896 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2897 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2898 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2899
2900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2901 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2902 ASSERT_EQ(args.deviceId, DEVICE_ID);
2903 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2904 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2905 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2906 ASSERT_EQ(args.values, values);
2907 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2908}
2909
Michael Wrightd02c5b62014-02-10 15:10:22 -08002910// --- KeyboardInputMapperTest ---
2911
2912class KeyboardInputMapperTest : public InputMapperTest {
2913protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002914 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002915 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002916 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002917
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002918 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002919 int32_t originalKeyCode, int32_t rotatedKeyCode,
2920 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002921};
2922
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002923/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2924 * orientation.
2925 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002926void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002927 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2928 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002929}
2930
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002931void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002932 int32_t originalScanCode, int32_t originalKeyCode,
2933 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934 NotifyKeyArgs args;
2935
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2938 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2939 ASSERT_EQ(originalScanCode, args.scanCode);
2940 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002941 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002942
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2945 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2946 ASSERT_EQ(originalScanCode, args.scanCode);
2947 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002948 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002949}
2950
Michael Wrightd02c5b62014-02-10 15:10:22 -08002951TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002952 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00002953 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002954 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002956 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002957}
2958
2959TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2960 const int32_t USAGE_A = 0x070004;
2961 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002962 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2963 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002964 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2965 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2966 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002967
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002968 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00002969 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002970 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002971 // Initial metastate is AMETA_NONE.
2972 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002973
2974 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002976 NotifyKeyArgs args;
2977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2978 ASSERT_EQ(DEVICE_ID, args.deviceId);
2979 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2980 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2981 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2982 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2983 ASSERT_EQ(KEY_HOME, args.scanCode);
2984 ASSERT_EQ(AMETA_NONE, args.metaState);
2985 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2986 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2987 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2988
2989 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002990 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2992 ASSERT_EQ(DEVICE_ID, args.deviceId);
2993 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2994 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2995 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2996 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2997 ASSERT_EQ(KEY_HOME, args.scanCode);
2998 ASSERT_EQ(AMETA_NONE, args.metaState);
2999 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3000 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3001 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3002
3003 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3005 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3007 ASSERT_EQ(DEVICE_ID, args.deviceId);
3008 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3009 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3010 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3011 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3012 ASSERT_EQ(0, args.scanCode);
3013 ASSERT_EQ(AMETA_NONE, args.metaState);
3014 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3015 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3016 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3017
3018 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3020 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3022 ASSERT_EQ(DEVICE_ID, args.deviceId);
3023 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3024 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3025 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3026 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3027 ASSERT_EQ(0, args.scanCode);
3028 ASSERT_EQ(AMETA_NONE, args.metaState);
3029 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3030 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3031 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3032
3033 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3037 ASSERT_EQ(DEVICE_ID, args.deviceId);
3038 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3039 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3040 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3041 ASSERT_EQ(0, args.keyCode);
3042 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3043 ASSERT_EQ(AMETA_NONE, args.metaState);
3044 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3045 ASSERT_EQ(0U, args.policyFlags);
3046 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3047
3048 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3050 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3052 ASSERT_EQ(DEVICE_ID, args.deviceId);
3053 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3054 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3055 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3056 ASSERT_EQ(0, args.keyCode);
3057 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3058 ASSERT_EQ(AMETA_NONE, args.metaState);
3059 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3060 ASSERT_EQ(0U, args.policyFlags);
3061 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3062}
3063
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003064TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3065 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3066 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3067 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3068
3069 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003070 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003071 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3072
3073 // Key down by scan code.
3074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3075 NotifyKeyArgs args;
3076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3077 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3078
3079 // Key up by scan code.
3080 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3082 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3083}
3084
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003085/**
3086 * Ensure that the readTime is set to the time when the EV_KEY is received.
3087 */
3088TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3089 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3090
3091 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003092 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003093 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3094 NotifyKeyArgs args;
3095
3096 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003097 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3099 ASSERT_EQ(12, args.readTime);
3100
3101 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003102 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3104 ASSERT_EQ(15, args.readTime);
3105}
3106
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003108 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3109 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003110 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3111 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3112 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003114 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003115 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003116 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003117
Arthur Hung95f68612022-04-07 14:08:22 +08003118 // Initial metastate is AMETA_NONE.
3119 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120
3121 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123 NotifyKeyArgs args;
3124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3125 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003126 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003127 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128
3129 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003130 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3132 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003133 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003134
3135 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003136 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3138 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003139 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003140
3141 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003142 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3144 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003145 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003146 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003147}
3148
3149TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003150 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3151 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3152 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3153 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003154
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003155 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003156 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003157 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003158
Michael Wrighta9cf4192022-12-01 23:46:39 +00003159 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3161 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3162 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3163 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3164 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3165 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3166 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3167 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3168}
3169
3170TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003171 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3172 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3173 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3174 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003175
Michael Wrightd02c5b62014-02-10 15:10:22 -08003176 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003177 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003178 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003179 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003180
Michael Wrighta9cf4192022-12-01 23:46:39 +00003181 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003182 ASSERT_NO_FATAL_FAILURE(
3183 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3184 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3185 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3186 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3187 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3188 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3189 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003191 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003192 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003193 ASSERT_NO_FATAL_FAILURE(
3194 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3195 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3196 AKEYCODE_DPAD_UP, DISPLAY_ID));
3197 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3198 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3199 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3200 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003201
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003202 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003203 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003204 ASSERT_NO_FATAL_FAILURE(
3205 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3206 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3207 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3208 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3209 AKEYCODE_DPAD_UP, DISPLAY_ID));
3210 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3211 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003212
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003213 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003214 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003215 ASSERT_NO_FATAL_FAILURE(
3216 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3217 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3218 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3219 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3220 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3221 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3222 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003223
3224 // Special case: if orientation changes while key is down, we still emit the same keycode
3225 // in the key up as we did in the key down.
3226 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003227 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003228 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3231 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3232 ASSERT_EQ(KEY_UP, args.scanCode);
3233 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3234
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003235 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003236 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3239 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3240 ASSERT_EQ(KEY_UP, args.scanCode);
3241 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3242}
3243
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003244TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3245 // If the keyboard is not orientation aware,
3246 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003247 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003248
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003249 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003250 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003251 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003252 NotifyKeyArgs args;
3253
3254 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3259 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3260
Michael Wrighta9cf4192022-12-01 23:46:39 +00003261 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3266 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3267}
3268
3269TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3270 // If the keyboard is orientation aware,
3271 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003272 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003273
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003274 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003275 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003276 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003277 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003278 NotifyKeyArgs args;
3279
3280 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3281 // ^--- already checked by the previous test
3282
Michael Wrighta9cf4192022-12-01 23:46:39 +00003283 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003284 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3289 ASSERT_EQ(DISPLAY_ID, args.displayId);
3290
3291 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003292 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003293 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003294 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3299 ASSERT_EQ(newDisplayId, args.displayId);
3300}
3301
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003303 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003304 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003305 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003307 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003308 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003309
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003310 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003311 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312}
3313
Philip Junker4af3b3d2021-12-14 10:36:55 +01003314TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3315 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003316 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003317 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3318
3319 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3320 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3321 << "If a mapping is available, the result is equal to the mapping";
3322
3323 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3324 << "If no mapping is available, the result is the key location";
3325}
3326
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003328 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003329 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003330 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003331
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003332 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003333 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003335 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003336 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003337}
3338
3339TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003340 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003341 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003342 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003344 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003347 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_TRUE(flags[0]);
3349 ASSERT_FALSE(flags[1]);
3350}
3351
3352TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003353 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3354 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3355 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3356 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3357 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3358 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003360 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003361 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003362 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003363 // Initial metastate is AMETA_NONE.
3364 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003365
3366 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003367 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3368 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3369 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003370
3371 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003374 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3375 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3376 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003377 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003378
3379 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003380 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003382 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3383 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3384 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003385 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003386
3387 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003390 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3391 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3392 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003393 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394
3395 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003398 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3399 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3400 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003401 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402
3403 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003406 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3407 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3408 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003409 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410
3411 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003414 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3415 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3416 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003417 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418}
3419
Chris Yea52ade12020-08-27 16:49:20 -07003420TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3421 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3422 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3423 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3424 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3425
3426 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003427 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003428 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3429
Chris Yea52ade12020-08-27 16:49:20 -07003430 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003431 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003432 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3433 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3434 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3435 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3436
3437 NotifyKeyArgs args;
3438 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3441 ASSERT_EQ(AMETA_NONE, args.metaState);
3442 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3443 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3444 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3445
3446 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003447 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3449 ASSERT_EQ(AMETA_NONE, args.metaState);
3450 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3451 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3452 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3453}
3454
Arthur Hung2c9a3342019-07-23 14:18:59 +08003455TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3456 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003457 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3458 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3459 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3460 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003461
3462 // keyboard 2.
3463 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003464 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003465 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003466 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003467 std::shared_ptr<InputDevice> device2 =
3468 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003469 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003470
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003471 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3472 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3473 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3474 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003475
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003476 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003477 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003478 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003479
Arpit Singh033e3ec2023-04-26 14:43:16 +00003480 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003481 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003482 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3483 mFakePolicy
3484 ->getReaderConfiguration(),
3485 AINPUT_SOURCE_KEYBOARD,
3486 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003487 std::list<NotifyArgs> unused =
3488 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003489 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003490 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003491
3492 // Prepared displays and associated info.
3493 constexpr uint8_t hdmi1 = 0;
3494 constexpr uint8_t hdmi2 = 1;
3495 const std::string SECONDARY_UNIQUE_ID = "local:1";
3496
3497 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3498 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3499
3500 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003501 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003502 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003503 ASSERT_FALSE(device2->isEnabled());
3504
3505 // Prepare second display.
3506 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003507 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003508 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003509 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003510 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003511 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003512 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003513 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003514
3515 // Device should be enabled after the associated display is found.
3516 ASSERT_TRUE(mDevice->isEnabled());
3517 ASSERT_TRUE(device2->isEnabled());
3518
3519 // Test pad key events
3520 ASSERT_NO_FATAL_FAILURE(
3521 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3522 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3523 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3524 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3525 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3526 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3527 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3528
3529 ASSERT_NO_FATAL_FAILURE(
3530 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3531 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3532 AKEYCODE_DPAD_RIGHT, newDisplayId));
3533 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3534 AKEYCODE_DPAD_DOWN, newDisplayId));
3535 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3536 AKEYCODE_DPAD_LEFT, newDisplayId));
3537}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003538
arthurhungc903df12020-08-11 15:08:42 +08003539TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3540 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3541 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3542 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3543 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3544 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3545 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3546
3547 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003548 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003549 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003550 // Initial metastate is AMETA_NONE.
3551 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003552
3553 // Initialization should have turned all of the lights off.
3554 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3555 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3556 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3557
3558 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003561 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3562 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3563
3564 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003567 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3568 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3569
3570 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003573 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3574 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3575
3576 mFakeEventHub->removeDevice(EVENTHUB_ID);
3577 mReader->loopOnce();
3578
3579 // keyboard 2 should default toggle keys.
3580 const std::string USB2 = "USB2";
3581 const std::string DEVICE_NAME2 = "KEYBOARD2";
3582 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3583 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3584 std::shared_ptr<InputDevice> device2 =
3585 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003586 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003587 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3588 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3589 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3590 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3591 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3592 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3593
Arpit Singh033e3ec2023-04-26 14:43:16 +00003594 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003595 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003596 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3597 mFakePolicy
3598 ->getReaderConfiguration(),
3599 AINPUT_SOURCE_KEYBOARD,
3600 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003601 std::list<NotifyArgs> unused =
3602 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003603 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003604 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003605
3606 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3607 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3608 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003609 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3610 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003611}
3612
Arthur Hungcb40a002021-08-03 14:31:01 +00003613TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3614 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3615 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3616 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3617
3618 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh033e3ec2023-04-26 14:43:16 +00003619 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003620 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3621 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003622 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003623 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003624 // Initial metastate is AMETA_NONE.
3625 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003626
3627 mReader->toggleCapsLockState(DEVICE_ID);
3628 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3629}
3630
Arthur Hungfb3cc112022-04-13 07:39:50 +00003631TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3632 // keyboard 1.
3633 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3634 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3635 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3636 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3637 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3638 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3639
3640 KeyboardInputMapper& mapper1 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003641 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003642 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3643
3644 // keyboard 2.
3645 const std::string USB2 = "USB2";
3646 const std::string DEVICE_NAME2 = "KEYBOARD2";
3647 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3648 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3649 std::shared_ptr<InputDevice> device2 =
3650 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3651 ftl::Flags<InputDeviceClass>(0));
3652 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3653 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3654 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3655 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3656 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3657 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3658
Arpit Singh033e3ec2023-04-26 14:43:16 +00003659 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003660 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003661 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3662 mFakePolicy
3663 ->getReaderConfiguration(),
3664 AINPUT_SOURCE_KEYBOARD,
3665 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003666 std::list<NotifyArgs> unused =
3667 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003668 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003669 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003670
Arthur Hung95f68612022-04-07 14:08:22 +08003671 // Initial metastate is AMETA_NONE.
3672 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3673 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3674
3675 // Toggle num lock on and off.
3676 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3677 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003678 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3679 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3680 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3681
3682 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3683 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3684 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3685 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3686 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3687
3688 // Toggle caps lock on and off.
3689 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3690 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3691 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3692 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3693 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3694
3695 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3696 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3697 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3698 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3699 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3700
3701 // Toggle scroll lock on and off.
3702 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3703 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3704 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3705 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3706 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3707
3708 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3709 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3710 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3711 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3712 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3713}
3714
Arthur Hung2141d542022-08-23 07:45:21 +00003715TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3716 const int32_t USAGE_A = 0x070004;
3717 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3718 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3719
3720 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003721 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003722 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3723 // Key down by scan code.
3724 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3725 NotifyKeyArgs args;
3726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3727 ASSERT_EQ(DEVICE_ID, args.deviceId);
3728 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3729 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3730 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3731 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3732 ASSERT_EQ(KEY_HOME, args.scanCode);
3733 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3734
3735 // Disable device, it should synthesize cancellation events for down events.
3736 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003737 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003738
3739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3740 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3741 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3742 ASSERT_EQ(KEY_HOME, args.scanCode);
3743 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3744}
3745
Zixuan Qufecb6062022-11-12 04:44:31 +00003746TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh033e3ec2023-04-26 14:43:16 +00003747 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3748 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003749 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003750 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3751 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003752
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003753 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003754 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3755
3756 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003757 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003758
3759 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3760 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3761 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3762 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3763 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003764 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3765
3766 // Call change layout association with the same values: Generation shouldn't change
3767 generation = mReader->getContext()->getGeneration();
3768 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3769 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3770 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3771 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003772}
3773
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003774TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3775 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3776 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3777
3778 // Configuration
Arpit Singh033e3ec2023-04-26 14:43:16 +00003779 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003780 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3781 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003782 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003783
3784 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3785 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3786}
3787
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003788// --- KeyboardInputMapperTest_ExternalDevice ---
3789
3790class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3791protected:
Chris Yea52ade12020-08-27 16:49:20 -07003792 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003793};
3794
3795TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003796 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3797 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003798
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003799 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3800 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3801 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3802 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003803
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003804 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003805 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003806 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003807
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003809 NotifyKeyArgs args;
3810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3811 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3812
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003813 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3815 ASSERT_EQ(uint32_t(0), args.policyFlags);
3816
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003819 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003820
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003821 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3823 ASSERT_EQ(uint32_t(0), args.policyFlags);
3824
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3827 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3828
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003829 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3831 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3832}
3833
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003834TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003835 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003836
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003837 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3838 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3839 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003840
Powei Fengd041c5d2019-05-03 17:11:33 -07003841 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003842 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003843 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003844 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003845
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003847 NotifyKeyArgs args;
3848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3849 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3850
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003851 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3853 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3854
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3857 ASSERT_EQ(uint32_t(0), args.policyFlags);
3858
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003859 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3861 ASSERT_EQ(uint32_t(0), args.policyFlags);
3862
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003863 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3865 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3866
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003867 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3869 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3870}
3871
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872// --- CursorInputMapperTest ---
3873
3874class CursorInputMapperTest : public InputMapperTest {
3875protected:
3876 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3877
Michael Wright17db18e2020-06-26 20:51:44 +01003878 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879
Chris Yea52ade12020-08-27 16:49:20 -07003880 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881 InputMapperTest::SetUp();
3882
Michael Wright17db18e2020-06-26 20:51:44 +01003883 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003884 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003885 }
3886
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003887 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3888 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003889
Michael Wrighta9cf4192022-12-01 23:46:39 +00003890 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003891 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3892 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3893 }
3894
3895 void prepareSecondaryDisplay() {
3896 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003897 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003898 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003899 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003900
3901 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3902 float pressure) {
3903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3904 0.0f, 0.0f, 0.0f, EPSILON));
3905 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906};
3907
3908const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3909
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003910void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3911 int32_t originalY, int32_t rotatedX,
3912 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913 NotifyMotionArgs args;
3914
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3916 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003920 ASSERT_NO_FATAL_FAILURE(
3921 assertCursorPointerCoords(args.pointerCoords[0],
3922 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3923 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924}
3925
3926TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003928 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003930 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931}
3932
3933TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003934 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003935 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003937 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003938}
3939
3940TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003941 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003942 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003943
3944 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003945 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003946
3947 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003948 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3949 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003950 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3951 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3952
3953 // When the bounds are set, then there should be a valid motion range.
3954 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3955
3956 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003957 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958
3959 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3960 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3961 1, 800 - 1, 0.0f, 0.0f));
3962 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3963 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3964 2, 480 - 1, 0.0f, 0.0f));
3965 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3966 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3967 0.0f, 1.0f, 0.0f, 0.0f));
3968}
3969
3970TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003971 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003972 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973
3974 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003975 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003976
3977 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3978 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3979 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3980 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3981 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3982 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3983 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3984 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3985 0.0f, 1.0f, 0.0f, 0.0f));
3986}
3987
3988TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003989 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003990 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003991
arthurhungdcef2dc2020-08-11 14:47:50 +08003992 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003993
3994 NotifyMotionArgs args;
3995
3996 // Button press.
3997 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4001 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4002 ASSERT_EQ(DEVICE_ID, args.deviceId);
4003 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4004 ASSERT_EQ(uint32_t(0), args.policyFlags);
4005 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4006 ASSERT_EQ(0, args.flags);
4007 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4008 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4009 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004010 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004012 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004013 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004014 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4015 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4016 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4017
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4019 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4020 ASSERT_EQ(DEVICE_ID, args.deviceId);
4021 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4022 ASSERT_EQ(uint32_t(0), args.policyFlags);
4023 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4024 ASSERT_EQ(0, args.flags);
4025 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4026 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4027 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004028 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004029 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004030 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004031 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004032 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4033 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4034 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4035
Michael Wrightd02c5b62014-02-10 15:10:22 -08004036 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004037 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4038 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4040 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4041 ASSERT_EQ(DEVICE_ID, args.deviceId);
4042 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4043 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004044 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4045 ASSERT_EQ(0, args.flags);
4046 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4047 ASSERT_EQ(0, args.buttonState);
4048 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004049 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004050 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004051 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004052 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004053 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4054 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4055 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4056
4057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4058 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4059 ASSERT_EQ(DEVICE_ID, args.deviceId);
4060 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4061 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4063 ASSERT_EQ(0, args.flags);
4064 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4065 ASSERT_EQ(0, args.buttonState);
4066 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004067 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004069 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004070 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4072 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4073 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4074}
4075
4076TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004077 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004078 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004079
4080 NotifyMotionArgs args;
4081
4082 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4084 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004087 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4088 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4089 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090
4091 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4095 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004096 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4097 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004098}
4099
4100TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004102 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004103
4104 NotifyMotionArgs args;
4105
4106 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4110 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004111 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4114 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004115 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004116
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004122 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004123
4124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004126 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127}
4128
4129TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004131 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132
4133 NotifyMotionArgs args;
4134
4135 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4141 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004142 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4143 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4144 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004145
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4147 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004148 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4149 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4150 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004151
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004158 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4159 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4160 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004161
4162 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004166 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004167 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004168
4169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004171 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172}
4173
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004174TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004175 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004177 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4178 // need to be rotated.
4179 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004180 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181
Michael Wrighta9cf4192022-12-01 23:46:39 +00004182 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004183 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4187 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4188 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4189 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4190 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4191}
4192
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004193TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004194 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004196 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4197 // orientation-aware are affected by display rotation.
Arpit Singhd8510bd2023-04-27 12:48:15 +00004198 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004200 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004201 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4203 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4204 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4205 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4206 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4207 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4208 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4209 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4210
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004211 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004212 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004213 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4214 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4215 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4216 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4217 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4218 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4219 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4220 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004222 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004223 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4225 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4226 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4227 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4228 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4229 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4230 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4231 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4232
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004233 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004234 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004235 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4236 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4237 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4238 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4239 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4240 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4241 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4242 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243}
4244
4245TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004247 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248
4249 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4250 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251
4252 NotifyMotionArgs motionArgs;
4253 NotifyKeyArgs keyArgs;
4254
4255 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
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));
4259 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4260 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4266 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004269
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 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);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277
4278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004279 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004281 ASSERT_NO_FATAL_FAILURE(
4282 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004283
4284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004285 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004286 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004287 ASSERT_NO_FATAL_FAILURE(
4288 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289
4290 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4295 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4296 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004297 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004298 ASSERT_NO_FATAL_FAILURE(
4299 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4302 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4303 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004304 ASSERT_NO_FATAL_FAILURE(
4305 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004306
4307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4308 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4309 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004310 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004311 ASSERT_NO_FATAL_FAILURE(
4312 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004313
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004317 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004319 ASSERT_NO_FATAL_FAILURE(
4320 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004321
4322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004324 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004325 ASSERT_NO_FATAL_FAILURE(
4326 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004331 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4332 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004333 ASSERT_NO_FATAL_FAILURE(
4334 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004337
4338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004341 ASSERT_NO_FATAL_FAILURE(
4342 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004343
Michael Wrightd02c5b62014-02-10 15:10:22 -08004344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4345 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004347 ASSERT_NO_FATAL_FAILURE(
4348 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349
4350 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4354 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4355 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004356
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004358 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004360 ASSERT_NO_FATAL_FAILURE(
4361 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004362
4363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4365 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004366 ASSERT_NO_FATAL_FAILURE(
4367 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004372 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004374 ASSERT_NO_FATAL_FAILURE(
4375 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004376
4377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004379 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004380
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004381 ASSERT_NO_FATAL_FAILURE(
4382 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4384 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4385 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4386
4387 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4391 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4392 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004393
Michael Wrightd02c5b62014-02-10 15:10:22 -08004394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004395 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004396 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004397 ASSERT_NO_FATAL_FAILURE(
4398 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004399
4400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4401 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4402 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004403 ASSERT_NO_FATAL_FAILURE(
4404 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004409 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004411 ASSERT_NO_FATAL_FAILURE(
4412 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004413
4414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4415 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4416 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004417 ASSERT_NO_FATAL_FAILURE(
4418 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004419
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4421 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4422 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4423
4424 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4428 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4429 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004430
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004432 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004434 ASSERT_NO_FATAL_FAILURE(
4435 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004436
4437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4439 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004440 ASSERT_NO_FATAL_FAILURE(
4441 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004446 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004448 ASSERT_NO_FATAL_FAILURE(
4449 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004450
4451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4452 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4453 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004454 ASSERT_NO_FATAL_FAILURE(
4455 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004456
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4458 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4459 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4460
4461 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4465 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4466 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004467
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004469 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004471 ASSERT_NO_FATAL_FAILURE(
4472 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004473
4474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4475 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4476 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004477 ASSERT_NO_FATAL_FAILURE(
4478 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004483 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004485 ASSERT_NO_FATAL_FAILURE(
4486 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004487
4488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4489 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4490 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004491 ASSERT_NO_FATAL_FAILURE(
4492 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004493
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4495 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4496 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4497}
4498
4499TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004501 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502
4503 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4504 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004505
4506 NotifyMotionArgs args;
4507
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004512 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4513 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4515 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 +00004516 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004517}
4518
4519TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004520 addConfigurationProperty("cursor.mode", "pointer");
4521 mFakePolicy->setPointerCapture(true);
Arpit Singhd8510bd2023-04-27 12:48:15 +00004522 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004523
4524 NotifyDeviceResetArgs resetArgs;
4525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4526 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4527 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4528
4529 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4530 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004531
4532 NotifyMotionArgs args;
4533
4534 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4539 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4540 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4542 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 +00004543 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004544
4545 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4549 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4550 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4552 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4554 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4555 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4557 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4558
4559 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004560 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4561 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4563 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4564 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4566 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4568 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4569 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4571 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4572
4573 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004574 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4578 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4581 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 +00004582 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004583
4584 // Disable pointer capture and check that the device generation got bumped
4585 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004586 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004587 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004588 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004589 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004590
4591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004592 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4593
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4598 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4601 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 +00004602 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004603}
4604
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004605/**
4606 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4607 * pointer acceleration or speed processing should not be applied.
4608 */
4609TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4610 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004611 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4612 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004613 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhd8510bd2023-04-27 12:48:15 +00004614 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004615
4616 NotifyDeviceResetArgs resetArgs;
4617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4618 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4619 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4620
4621 NotifyMotionArgs args;
4622
4623 // Move and verify scale is applied.
4624 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4628 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4629 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4630 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4631 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4632 ASSERT_GT(relX, 10);
4633 ASSERT_GT(relY, 20);
4634
4635 // Enable Pointer Capture
4636 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004637 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004638 NotifyPointerCaptureChangedArgs captureArgs;
4639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4640 ASSERT_TRUE(captureArgs.request.enable);
4641
4642 // Move and verify scale is not applied.
4643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4644 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4647 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4648 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4649 ASSERT_EQ(10, args.pointerCoords[0].getX());
4650 ASSERT_EQ(20, args.pointerCoords[0].getY());
4651}
4652
Prabir Pradhan208360b2022-06-24 18:37:04 +00004653TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4654 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004655 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004656
4657 NotifyDeviceResetArgs resetArgs;
4658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4659 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4660 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4661
4662 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004663 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004664
4665 NotifyMotionArgs args;
4666
4667 // Verify that the coordinates are rotated.
4668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4672 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4673 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4674 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4675 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4676
4677 // Enable Pointer Capture.
4678 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004679 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004680 NotifyPointerCaptureChangedArgs captureArgs;
4681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4682 ASSERT_TRUE(captureArgs.request.enable);
4683
4684 // Move and verify rotation is not applied.
4685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4689 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4691 ASSERT_EQ(10, args.pointerCoords[0].getX());
4692 ASSERT_EQ(20, args.pointerCoords[0].getY());
4693}
4694
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004695TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004696 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004697
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004698 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004699 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004700
4701 // Set up the secondary display as the display on which the pointer should be shown.
4702 // The InputDevice is not associated with any display.
4703 prepareSecondaryDisplay();
4704 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004705 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004706
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004707 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004708 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004709
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004710 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004715 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4716 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4717 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004718 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004719}
4720
4721TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004722 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004723
4724 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004725 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004726
4727 // Set up the secondary display as the display on which the pointer should be shown,
4728 // and associate the InputDevice with the secondary display.
4729 prepareSecondaryDisplay();
4730 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4731 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004732 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004733
4734 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4735 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004736
4737 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004741 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4742 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4743 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004744 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004745}
4746
4747TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004748 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004749
4750 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004751 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004752 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4753
4754 // Associate the InputDevice with the secondary display.
4755 prepareSecondaryDisplay();
4756 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004757 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004758
4759 // The mapper should not generate any events because it is associated with a display that is
4760 // different from the pointer display.
4761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004765}
4766
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004767// --- BluetoothCursorInputMapperTest ---
4768
4769class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4770protected:
4771 void SetUp() override {
4772 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4773
4774 mFakePointerController = std::make_shared<FakePointerController>();
4775 mFakePolicy->setPointerController(mFakePointerController);
4776 }
4777};
4778
4779TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4780 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004781 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004782
4783 nsecs_t kernelEventTime = ARBITRARY_TIME;
4784 nsecs_t expectedEventTime = ARBITRARY_TIME;
4785 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4786 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4788 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4789 WithEventTime(expectedEventTime))));
4790
4791 // Process several events that come in quick succession, according to their timestamps.
4792 for (int i = 0; i < 3; i++) {
4793 constexpr static nsecs_t delta = ms2ns(1);
4794 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4795 kernelEventTime += delta;
4796 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4797
4798 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4799 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4801 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4802 WithEventTime(expectedEventTime))));
4803 }
4804}
4805
4806TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4807 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004808 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004809
4810 nsecs_t expectedEventTime = ARBITRARY_TIME;
4811 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4814 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4815 WithEventTime(expectedEventTime))));
4816
4817 // Process several events with the same timestamp from the kernel.
4818 // Ensure that we do not generate events too far into the future.
4819 constexpr static int32_t numEvents =
4820 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4821 for (int i = 0; i < numEvents; i++) {
4822 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4823
4824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4827 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4828 WithEventTime(expectedEventTime))));
4829 }
4830
4831 // By processing more events with the same timestamp, we should not generate events with a
4832 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4833 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4834 for (int i = 0; i < 3; i++) {
4835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4838 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4839 WithEventTime(cappedEventTime))));
4840 }
4841}
4842
4843TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4844 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004845 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004846
4847 nsecs_t kernelEventTime = ARBITRARY_TIME;
4848 nsecs_t expectedEventTime = ARBITRARY_TIME;
4849 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4850 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4852 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4853 WithEventTime(expectedEventTime))));
4854
4855 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4856 // smoothening is not needed, its timestamp is not affected.
4857 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4858 expectedEventTime = kernelEventTime;
4859
4860 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4861 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4863 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4864 WithEventTime(expectedEventTime))));
4865}
4866
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867// --- TouchInputMapperTest ---
4868
4869class TouchInputMapperTest : public InputMapperTest {
4870protected:
4871 static const int32_t RAW_X_MIN;
4872 static const int32_t RAW_X_MAX;
4873 static const int32_t RAW_Y_MIN;
4874 static const int32_t RAW_Y_MAX;
4875 static const int32_t RAW_TOUCH_MIN;
4876 static const int32_t RAW_TOUCH_MAX;
4877 static const int32_t RAW_TOOL_MIN;
4878 static const int32_t RAW_TOOL_MAX;
4879 static const int32_t RAW_PRESSURE_MIN;
4880 static const int32_t RAW_PRESSURE_MAX;
4881 static const int32_t RAW_ORIENTATION_MIN;
4882 static const int32_t RAW_ORIENTATION_MAX;
4883 static const int32_t RAW_DISTANCE_MIN;
4884 static const int32_t RAW_DISTANCE_MAX;
4885 static const int32_t RAW_TILT_MIN;
4886 static const int32_t RAW_TILT_MAX;
4887 static const int32_t RAW_ID_MIN;
4888 static const int32_t RAW_ID_MAX;
4889 static const int32_t RAW_SLOT_MIN;
4890 static const int32_t RAW_SLOT_MAX;
4891 static const float X_PRECISION;
4892 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004893 static const float X_PRECISION_VIRTUAL;
4894 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004895
4896 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004897 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898
4899 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4900
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004901 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004902 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004903
Michael Wrightd02c5b62014-02-10 15:10:22 -08004904 enum Axes {
4905 POSITION = 1 << 0,
4906 TOUCH = 1 << 1,
4907 TOOL = 1 << 2,
4908 PRESSURE = 1 << 3,
4909 ORIENTATION = 1 << 4,
4910 MINOR = 1 << 5,
4911 ID = 1 << 6,
4912 DISTANCE = 1 << 7,
4913 TILT = 1 << 8,
4914 SLOT = 1 << 9,
4915 TOOL_TYPE = 1 << 10,
4916 };
4917
Michael Wrighta9cf4192022-12-01 23:46:39 +00004918 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004919 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004920 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004921 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004922 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923 int32_t toRawX(float displayX);
4924 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004925 int32_t toRotatedRawX(float displayX);
4926 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004927 float toCookedX(float rawX, float rawY);
4928 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004930 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004932 float toDisplayY(int32_t rawY, int32_t displayHeight);
4933
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934};
4935
4936const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4937const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4938const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4939const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4940const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4941const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4942const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4943const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004944const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4945const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4947const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4948const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4949const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4950const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4951const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4952const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4953const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4954const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4955const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4956const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4957const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004958const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4959 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4960const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4961 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004962const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4963 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964
4965const float TouchInputMapperTest::GEOMETRIC_SCALE =
4966 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4967 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4968
4969const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4970 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4971 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4972};
4973
Michael Wrighta9cf4192022-12-01 23:46:39 +00004974void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004975 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4976 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004977}
4978
4979void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4980 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004981 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982}
4983
Michael Wrighta9cf4192022-12-01 23:46:39 +00004984void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004985 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4986 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4987 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004988}
4989
Michael Wrightd02c5b62014-02-10 15:10:22 -08004990void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004991 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4992 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4993 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4994 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995}
4996
Jason Gerecke489fda82012-09-07 17:19:40 -07004997void TouchInputMapperTest::prepareLocationCalibration() {
4998 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4999}
5000
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001int32_t TouchInputMapperTest::toRawX(float displayX) {
5002 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5003}
5004
5005int32_t TouchInputMapperTest::toRawY(float displayY) {
5006 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5007}
5008
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005009int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5010 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5011}
5012
5013int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5014 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5015}
5016
Jason Gerecke489fda82012-09-07 17:19:40 -07005017float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5018 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5019 return rawX;
5020}
5021
5022float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5023 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5024 return rawY;
5025}
5026
Michael Wrightd02c5b62014-02-10 15:10:22 -08005027float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005028 return toDisplayX(rawX, DISPLAY_WIDTH);
5029}
5030
5031float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5032 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033}
5034
5035float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005036 return toDisplayY(rawY, DISPLAY_HEIGHT);
5037}
5038
5039float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5040 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041}
5042
5043
5044// --- SingleTouchInputMapperTest ---
5045
5046class SingleTouchInputMapperTest : public TouchInputMapperTest {
5047protected:
5048 void prepareButtons();
5049 void prepareAxes(int axes);
5050
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005051 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5052 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5053 void processUp(SingleTouchInputMapper& mappery);
5054 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5055 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5056 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5057 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5058 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5059 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060};
5061
5062void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005063 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064}
5065
5066void SingleTouchInputMapperTest::prepareAxes(int axes) {
5067 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005068 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5069 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070 }
5071 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005072 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5073 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005074 }
5075 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005076 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5077 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078 }
5079 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005080 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5081 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082 }
5083 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005084 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5085 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086 }
5087}
5088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005089void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005093}
5094
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005095void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005098}
5099
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005100void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102}
5103
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005104void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005105 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005106}
5107
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005108void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5109 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111}
5112
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005113void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115}
5116
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005117void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5118 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005121}
5122
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005123void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5124 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005126}
5127
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005128void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130}
5131
Michael Wrightd02c5b62014-02-10 15:10:22 -08005132TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005133 prepareButtons();
5134 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00005135 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005136
Josep del Río2d8c79a2023-01-23 19:33:50 +00005137 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138}
5139
Michael Wrightd02c5b62014-02-10 15:10:22 -08005140TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005141 prepareButtons();
5142 prepareAxes(POSITION);
5143 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00005144 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005146 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005147}
5148
5149TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005151 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005152 prepareButtons();
5153 prepareAxes(POSITION);
5154 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005155 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156
5157 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005158 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005159
5160 // Virtual key is down.
5161 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5162 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5163 processDown(mapper, x, y);
5164 processSync(mapper);
5165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5166
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005167 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005168
5169 // Virtual key is up.
5170 processUp(mapper);
5171 processSync(mapper);
5172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5173
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005174 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005175}
5176
5177TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005179 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180 prepareButtons();
5181 prepareAxes(POSITION);
5182 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005183 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005184
5185 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005186 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005187
5188 // Virtual key is down.
5189 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5190 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5191 processDown(mapper, x, y);
5192 processSync(mapper);
5193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5194
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005195 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005196
5197 // Virtual key is up.
5198 processUp(mapper);
5199 processSync(mapper);
5200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5201
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005202 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005203}
5204
5205TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005206 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005207 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005208 prepareButtons();
5209 prepareAxes(POSITION);
5210 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005211 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005212
Michael Wrightd02c5b62014-02-10 15:10:22 -08005213 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005214 ASSERT_TRUE(
5215 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216 ASSERT_TRUE(flags[0]);
5217 ASSERT_FALSE(flags[1]);
5218}
5219
5220TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005221 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005222 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223 prepareButtons();
5224 prepareAxes(POSITION);
5225 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005226 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005227
arthurhungdcef2dc2020-08-11 14:47:50 +08005228 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229
5230 NotifyKeyArgs args;
5231
5232 // Press virtual key.
5233 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5234 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5235 processDown(mapper, x, y);
5236 processSync(mapper);
5237
5238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5239 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5240 ASSERT_EQ(DEVICE_ID, args.deviceId);
5241 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5242 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5243 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5244 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5245 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5246 ASSERT_EQ(KEY_HOME, args.scanCode);
5247 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5248 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5249
5250 // Release virtual key.
5251 processUp(mapper);
5252 processSync(mapper);
5253
5254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5255 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5256 ASSERT_EQ(DEVICE_ID, args.deviceId);
5257 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5258 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5259 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5260 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5261 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5262 ASSERT_EQ(KEY_HOME, args.scanCode);
5263 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5264 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5265
5266 // Should not have sent any motions.
5267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5268}
5269
5270TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005271 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005272 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005273 prepareButtons();
5274 prepareAxes(POSITION);
5275 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005276 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005277
arthurhungdcef2dc2020-08-11 14:47:50 +08005278 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005279
5280 NotifyKeyArgs keyArgs;
5281
5282 // Press virtual key.
5283 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5284 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5285 processDown(mapper, x, y);
5286 processSync(mapper);
5287
5288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5289 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5290 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5291 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5292 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5293 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5294 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5295 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5296 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5297 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5298 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5299
5300 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5301 // into the display area.
5302 y -= 100;
5303 processMove(mapper, x, y);
5304 processSync(mapper);
5305
5306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5307 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5308 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5309 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5310 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5311 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5312 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5313 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5314 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5315 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5316 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5317 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5318
5319 NotifyMotionArgs motionArgs;
5320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5321 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5322 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5323 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5324 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5325 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5326 ASSERT_EQ(0, motionArgs.flags);
5327 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5328 ASSERT_EQ(0, motionArgs.buttonState);
5329 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005330 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005331 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005332 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5334 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5335 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5336 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5338
5339 // Keep moving out of bounds. Should generate a pointer move.
5340 y -= 50;
5341 processMove(mapper, x, y);
5342 processSync(mapper);
5343
5344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5345 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5346 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5347 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5348 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5350 ASSERT_EQ(0, motionArgs.flags);
5351 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5352 ASSERT_EQ(0, motionArgs.buttonState);
5353 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005354 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005355 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005356 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5358 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5359 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5360 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5361 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5362
5363 // Release out of bounds. Should generate a pointer up.
5364 processUp(mapper);
5365 processSync(mapper);
5366
5367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5368 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5369 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5370 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5371 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5372 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5373 ASSERT_EQ(0, motionArgs.flags);
5374 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5375 ASSERT_EQ(0, motionArgs.buttonState);
5376 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005377 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005378 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005379 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5381 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5382 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5383 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5384 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5385
5386 // Should not have sent any more keys or motions.
5387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5389}
5390
5391TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005392 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005393 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394 prepareButtons();
5395 prepareAxes(POSITION);
5396 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005397 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005398
arthurhungdcef2dc2020-08-11 14:47:50 +08005399 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400
5401 NotifyMotionArgs motionArgs;
5402
5403 // Initially go down out of bounds.
5404 int32_t x = -10;
5405 int32_t y = -10;
5406 processDown(mapper, x, y);
5407 processSync(mapper);
5408
5409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5410
5411 // Move into the display area. Should generate a pointer down.
5412 x = 50;
5413 y = 75;
5414 processMove(mapper, x, y);
5415 processSync(mapper);
5416
5417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5418 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5419 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5420 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5421 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5422 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5423 ASSERT_EQ(0, motionArgs.flags);
5424 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5425 ASSERT_EQ(0, motionArgs.buttonState);
5426 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005427 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005429 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5431 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5432 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5433 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5434 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5435
5436 // Release. Should generate a pointer up.
5437 processUp(mapper);
5438 processSync(mapper);
5439
5440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5441 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5442 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5443 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5444 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5445 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5446 ASSERT_EQ(0, motionArgs.flags);
5447 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5448 ASSERT_EQ(0, motionArgs.buttonState);
5449 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005450 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005451 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005452 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5454 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5455 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5456 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5457 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5458
5459 // Should not have sent any more keys or motions.
5460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5462}
5463
Santos Cordonfa5cf462017-04-05 10:37:00 -07005464TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005465 addConfigurationProperty("touch.deviceType", "touchScreen");
5466 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5467
Michael Wrighta9cf4192022-12-01 23:46:39 +00005468 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005469 prepareButtons();
5470 prepareAxes(POSITION);
5471 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005472 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005473
arthurhungdcef2dc2020-08-11 14:47:50 +08005474 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005475
5476 NotifyMotionArgs motionArgs;
5477
5478 // Down.
5479 int32_t x = 100;
5480 int32_t y = 125;
5481 processDown(mapper, x, y);
5482 processSync(mapper);
5483
5484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5485 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5486 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5487 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5488 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5489 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5490 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5491 ASSERT_EQ(0, motionArgs.flags);
5492 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5493 ASSERT_EQ(0, motionArgs.buttonState);
5494 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005495 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005496 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005497 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5499 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5500 1, 0, 0, 0, 0, 0, 0, 0));
5501 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5502 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5503 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5504
5505 // Move.
5506 x += 50;
5507 y += 75;
5508 processMove(mapper, x, y);
5509 processSync(mapper);
5510
5511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5512 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5513 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5514 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5515 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5516 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5518 ASSERT_EQ(0, motionArgs.flags);
5519 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5520 ASSERT_EQ(0, motionArgs.buttonState);
5521 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005522 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005523 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005524 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5526 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5527 1, 0, 0, 0, 0, 0, 0, 0));
5528 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5529 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5530 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5531
5532 // Up.
5533 processUp(mapper);
5534 processSync(mapper);
5535
5536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5537 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5538 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5539 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5540 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5541 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5542 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5543 ASSERT_EQ(0, motionArgs.flags);
5544 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5545 ASSERT_EQ(0, motionArgs.buttonState);
5546 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005547 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005548 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005549 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5551 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5552 1, 0, 0, 0, 0, 0, 0, 0));
5553 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5554 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5555 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5556
5557 // Should not have sent any more keys or motions.
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5560}
5561
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005563 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005564 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005565 prepareButtons();
5566 prepareAxes(POSITION);
5567 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005568 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005569
arthurhungdcef2dc2020-08-11 14:47:50 +08005570 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005571
5572 NotifyMotionArgs motionArgs;
5573
5574 // Down.
5575 int32_t x = 100;
5576 int32_t y = 125;
5577 processDown(mapper, x, y);
5578 processSync(mapper);
5579
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5581 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5582 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5583 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5584 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5585 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5586 ASSERT_EQ(0, motionArgs.flags);
5587 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5588 ASSERT_EQ(0, motionArgs.buttonState);
5589 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005590 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005591 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005592 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5594 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5595 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5596 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5597 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5598
5599 // Move.
5600 x += 50;
5601 y += 75;
5602 processMove(mapper, x, y);
5603 processSync(mapper);
5604
5605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5606 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5607 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5608 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5609 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5611 ASSERT_EQ(0, motionArgs.flags);
5612 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5613 ASSERT_EQ(0, motionArgs.buttonState);
5614 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005615 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005616 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005617 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5619 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5620 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5621 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5622 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5623
5624 // Up.
5625 processUp(mapper);
5626 processSync(mapper);
5627
5628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5629 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5630 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5631 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5632 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5633 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5634 ASSERT_EQ(0, motionArgs.flags);
5635 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5636 ASSERT_EQ(0, motionArgs.buttonState);
5637 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005638 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005639 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005640 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5642 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5643 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5644 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5645 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5646
5647 // Should not have sent any more keys or motions.
5648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5650}
5651
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005652TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005653 addConfigurationProperty("touch.deviceType", "touchScreen");
5654 prepareButtons();
5655 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005656 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5657 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singh56adebc2023-04-25 13:56:05 +00005658 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005659
5660 NotifyMotionArgs args;
5661
5662 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005663 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005664 processDown(mapper, toRawX(50), toRawY(75));
5665 processSync(mapper);
5666
5667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5668 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5669 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5670
5671 processUp(mapper);
5672 processSync(mapper);
5673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5674}
5675
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005676TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005677 addConfigurationProperty("touch.deviceType", "touchScreen");
5678 prepareButtons();
5679 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005680 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5681 // orientation-aware are affected by display rotation.
5682 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00005683 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005684
5685 NotifyMotionArgs args;
5686
5687 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005688 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005689 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005690 processDown(mapper, toRawX(50), toRawY(75));
5691 processSync(mapper);
5692
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5694 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5695 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5696
5697 processUp(mapper);
5698 processSync(mapper);
5699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5700
5701 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005702 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005703 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005704 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705 processSync(mapper);
5706
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5708 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5709 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5710
5711 processUp(mapper);
5712 processSync(mapper);
5713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5714
5715 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005716 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005717 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005718 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5719 processSync(mapper);
5720
5721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5722 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5723 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5724
5725 processUp(mapper);
5726 processSync(mapper);
5727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5728
5729 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005730 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005731 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005732 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005733 processSync(mapper);
5734
5735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5736 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5737 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5738
5739 processUp(mapper);
5740 processSync(mapper);
5741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5742}
5743
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005744TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5745 addConfigurationProperty("touch.deviceType", "touchScreen");
5746 prepareButtons();
5747 prepareAxes(POSITION);
5748 addConfigurationProperty("touch.orientationAware", "1");
5749 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5750 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005751 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005752 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005753 NotifyMotionArgs args;
5754
5755 // Orientation 0.
5756 processDown(mapper, toRawX(50), toRawY(75));
5757 processSync(mapper);
5758
5759 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5760 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5761 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5762
5763 processUp(mapper);
5764 processSync(mapper);
5765 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5766}
5767
5768TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5769 addConfigurationProperty("touch.deviceType", "touchScreen");
5770 prepareButtons();
5771 prepareAxes(POSITION);
5772 addConfigurationProperty("touch.orientationAware", "1");
5773 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5774 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005775 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005776 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005777 NotifyMotionArgs args;
5778
5779 // Orientation 90.
5780 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5781 processSync(mapper);
5782
5783 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5784 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5785 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5786
5787 processUp(mapper);
5788 processSync(mapper);
5789 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5790}
5791
5792TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5793 addConfigurationProperty("touch.deviceType", "touchScreen");
5794 prepareButtons();
5795 prepareAxes(POSITION);
5796 addConfigurationProperty("touch.orientationAware", "1");
5797 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5798 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005799 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005800 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005801 NotifyMotionArgs args;
5802
5803 // Orientation 180.
5804 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5805 processSync(mapper);
5806
5807 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5808 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5809 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5810
5811 processUp(mapper);
5812 processSync(mapper);
5813 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5814}
5815
5816TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5817 addConfigurationProperty("touch.deviceType", "touchScreen");
5818 prepareButtons();
5819 prepareAxes(POSITION);
5820 addConfigurationProperty("touch.orientationAware", "1");
5821 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5822 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005823 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005824 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005825 NotifyMotionArgs args;
5826
5827 // Orientation 270.
5828 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5829 processSync(mapper);
5830
5831 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5832 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5833 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5834
5835 processUp(mapper);
5836 processSync(mapper);
5837 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5838}
5839
5840TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5841 addConfigurationProperty("touch.deviceType", "touchScreen");
5842 prepareButtons();
5843 prepareAxes(POSITION);
5844 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5845 // orientation-aware are affected by display rotation.
5846 addConfigurationProperty("touch.orientationAware", "0");
5847 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singh56adebc2023-04-25 13:56:05 +00005848 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005849
5850 NotifyMotionArgs args;
5851
5852 // Orientation 90, Rotation 0.
5853 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005854 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005855 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5856 processSync(mapper);
5857
5858 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5859 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5860 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5861
5862 processUp(mapper);
5863 processSync(mapper);
5864 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5865
5866 // Orientation 90, Rotation 90.
5867 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005868 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005869 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005870 processSync(mapper);
5871
5872 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5873 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5874 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5875
5876 processUp(mapper);
5877 processSync(mapper);
5878 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5879
5880 // Orientation 90, Rotation 180.
5881 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005882 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005883 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5884 processSync(mapper);
5885
5886 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5887 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5888 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5889
5890 processUp(mapper);
5891 processSync(mapper);
5892 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5893
5894 // Orientation 90, Rotation 270.
5895 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005896 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005897 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 -07005898 processSync(mapper);
5899
5900 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5901 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5902 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5903
5904 processUp(mapper);
5905 processSync(mapper);
5906 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5907}
5908
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005909TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5910 addConfigurationProperty("touch.deviceType", "touchScreen");
5911 prepareButtons();
5912 prepareAxes(POSITION);
5913 addConfigurationProperty("touch.orientationAware", "1");
5914 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005915 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005916
5917 // Set a physical frame in the display viewport.
5918 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5919 viewport->physicalLeft = 20;
5920 viewport->physicalTop = 600;
5921 viewport->physicalRight = 30;
5922 viewport->physicalBottom = 610;
5923 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005924 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005925
5926 // Start the touch.
5927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5928 processSync(mapper);
5929
5930 // Expect all input starting outside the physical frame to be ignored.
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->assertNotifyMotionWasNotCalled());
5937 }
5938
5939 // Move the touch into the physical frame.
5940 processMove(mapper, toRawX(25), toRawY(605));
5941 processSync(mapper);
5942 NotifyMotionArgs args;
5943 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5944 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5945 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5946 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5947
5948 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5949 for (const auto& p : outsidePoints) {
5950 processMove(mapper, toRawX(p.x), toRawY(p.y));
5951 processSync(mapper);
5952 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5953 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5954 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5955 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5956 }
5957
5958 processUp(mapper);
5959 processSync(mapper);
5960 EXPECT_NO_FATAL_FAILURE(
5961 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5962}
5963
Harry Cutts1db43992023-06-19 17:05:07 +00005964TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
5965 std::shared_ptr<FakePointerController> fakePointerController =
5966 std::make_shared<FakePointerController>();
5967 mFakePolicy->setPointerController(fakePointerController);
5968
5969 addConfigurationProperty("touch.deviceType", "pointer");
5970 prepareAxes(POSITION);
5971 prepareDisplay(ui::ROTATION_0);
5972 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5973
5974 // Set a physical frame in the display viewport.
5975 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5976 viewport->physicalLeft = 20;
5977 viewport->physicalTop = 600;
5978 viewport->physicalRight = 30;
5979 viewport->physicalBottom = 610;
5980 mFakePolicy->updateViewport(*viewport);
5981 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5982
5983 // Start the touch.
5984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5985 processSync(mapper);
5986
5987 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5988 // produced.
5989 const std::array<Point, 6> outsidePoints = {
5990 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5991 for (const auto& p : outsidePoints) {
5992 processMove(mapper, toRawX(p.x), toRawY(p.y));
5993 processSync(mapper);
5994 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5995 }
5996}
5997
Michael Wrightd02c5b62014-02-10 15:10:22 -08005998TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005999 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006000 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006001 prepareButtons();
6002 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singh56adebc2023-04-25 13:56:05 +00006003 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006004
6005 // These calculations are based on the input device calibration documentation.
6006 int32_t rawX = 100;
6007 int32_t rawY = 200;
6008 int32_t rawPressure = 10;
6009 int32_t rawToolMajor = 12;
6010 int32_t rawDistance = 2;
6011 int32_t rawTiltX = 30;
6012 int32_t rawTiltY = 110;
6013
6014 float x = toDisplayX(rawX);
6015 float y = toDisplayY(rawY);
6016 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6017 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6018 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6019 float distance = float(rawDistance);
6020
6021 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6022 float tiltScale = M_PI / 180;
6023 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6024 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6025 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6026 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6027
6028 processDown(mapper, rawX, rawY);
6029 processPressure(mapper, rawPressure);
6030 processToolMajor(mapper, rawToolMajor);
6031 processDistance(mapper, rawDistance);
6032 processTilt(mapper, rawTiltX, rawTiltY);
6033 processSync(mapper);
6034
6035 NotifyMotionArgs args;
6036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6038 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6039 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6040}
6041
Jason Gerecke489fda82012-09-07 17:19:40 -07006042TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006043 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006044 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006045 prepareLocationCalibration();
6046 prepareButtons();
6047 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006048 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006049
6050 int32_t rawX = 100;
6051 int32_t rawY = 200;
6052
6053 float x = toDisplayX(toCookedX(rawX, rawY));
6054 float y = toDisplayY(toCookedY(rawX, rawY));
6055
6056 processDown(mapper, rawX, rawY);
6057 processSync(mapper);
6058
6059 NotifyMotionArgs args;
6060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6062 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6063}
6064
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006066 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006067 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006068 prepareButtons();
6069 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006070 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071
6072 NotifyMotionArgs motionArgs;
6073 NotifyKeyArgs keyArgs;
6074
6075 processDown(mapper, 100, 200);
6076 processSync(mapper);
6077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6078 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6079 ASSERT_EQ(0, motionArgs.buttonState);
6080
6081 // press BTN_LEFT, release BTN_LEFT
6082 processKey(mapper, BTN_LEFT, 1);
6083 processSync(mapper);
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6086 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6087
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6089 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6090 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6091
Michael Wrightd02c5b62014-02-10 15:10:22 -08006092 processKey(mapper, BTN_LEFT, 0);
6093 processSync(mapper);
6094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006095 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006096 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006097
6098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006100 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006101
6102 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6103 processKey(mapper, BTN_RIGHT, 1);
6104 processKey(mapper, BTN_MIDDLE, 1);
6105 processSync(mapper);
6106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6108 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6109 motionArgs.buttonState);
6110
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6112 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6113 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6114
6115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6116 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6117 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6118 motionArgs.buttonState);
6119
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120 processKey(mapper, BTN_RIGHT, 0);
6121 processSync(mapper);
6122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006123 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006124 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006125
6126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006127 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006128 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129
6130 processKey(mapper, BTN_MIDDLE, 0);
6131 processSync(mapper);
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006135
6136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006138 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006139
6140 // press BTN_BACK, release BTN_BACK
6141 processKey(mapper, BTN_BACK, 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_BACK, 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_BACK, 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_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006154
6155 processKey(mapper, BTN_BACK, 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_BACK, keyArgs.keyCode);
6168
6169 // press BTN_SIDE, release BTN_SIDE
6170 processKey(mapper, BTN_SIDE, 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_BACK, 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_BACK, 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_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183
6184 processKey(mapper, BTN_SIDE, 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_BACK, keyArgs.keyCode);
6197
6198 // press BTN_FORWARD, release BTN_FORWARD
6199 processKey(mapper, BTN_FORWARD, 1);
6200 processSync(mapper);
6201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6202 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6203 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006204
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006207 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6208
6209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6210 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6211 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006212
6213 processKey(mapper, BTN_FORWARD, 0);
6214 processSync(mapper);
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006216 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006218
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006221 ASSERT_EQ(0, motionArgs.buttonState);
6222
Michael Wrightd02c5b62014-02-10 15:10:22 -08006223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6224 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6225 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6226
6227 // press BTN_EXTRA, release BTN_EXTRA
6228 processKey(mapper, BTN_EXTRA, 1);
6229 processSync(mapper);
6230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6231 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6232 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006233
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006236 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6237
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6239 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6240 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006241
6242 processKey(mapper, BTN_EXTRA, 0);
6243 processSync(mapper);
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006245 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006246 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006247
6248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006250 ASSERT_EQ(0, motionArgs.buttonState);
6251
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6253 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6254 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6255
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6257
Michael Wrightd02c5b62014-02-10 15:10:22 -08006258 // press BTN_STYLUS, release BTN_STYLUS
6259 processKey(mapper, BTN_STYLUS, 1);
6260 processSync(mapper);
6261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006263 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6264
6265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6266 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6267 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006268
6269 processKey(mapper, BTN_STYLUS, 0);
6270 processSync(mapper);
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006272 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006274
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278
6279 // press BTN_STYLUS2, release BTN_STYLUS2
6280 processKey(mapper, BTN_STYLUS2, 1);
6281 processSync(mapper);
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006284 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6285
6286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6287 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6288 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289
6290 processKey(mapper, BTN_STYLUS2, 0);
6291 processSync(mapper);
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006294 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006295
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006298 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006299
6300 // release touch
6301 processUp(mapper);
6302 processSync(mapper);
6303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6304 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6305 ASSERT_EQ(0, motionArgs.buttonState);
6306}
6307
6308TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006309 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006310 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311 prepareButtons();
6312 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006313 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006314
6315 NotifyMotionArgs motionArgs;
6316
6317 // default tool type is finger
6318 processDown(mapper, 100, 200);
6319 processSync(mapper);
6320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6321 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006322 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006323
6324 // eraser
6325 processKey(mapper, BTN_TOOL_RUBBER, 1);
6326 processSync(mapper);
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006329 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330
6331 // stylus
6332 processKey(mapper, BTN_TOOL_RUBBER, 0);
6333 processKey(mapper, BTN_TOOL_PEN, 1);
6334 processSync(mapper);
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006337 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338
6339 // brush
6340 processKey(mapper, BTN_TOOL_PEN, 0);
6341 processKey(mapper, BTN_TOOL_BRUSH, 1);
6342 processSync(mapper);
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006345 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006346
6347 // pencil
6348 processKey(mapper, BTN_TOOL_BRUSH, 0);
6349 processKey(mapper, BTN_TOOL_PENCIL, 1);
6350 processSync(mapper);
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006353 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006355 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006356 processKey(mapper, BTN_TOOL_PENCIL, 0);
6357 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6358 processSync(mapper);
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6360 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006361 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362
6363 // mouse
6364 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6365 processKey(mapper, BTN_TOOL_MOUSE, 1);
6366 processSync(mapper);
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006369 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370
6371 // lens
6372 processKey(mapper, BTN_TOOL_MOUSE, 0);
6373 processKey(mapper, BTN_TOOL_LENS, 1);
6374 processSync(mapper);
6375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006377 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378
6379 // double-tap
6380 processKey(mapper, BTN_TOOL_LENS, 0);
6381 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6382 processSync(mapper);
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006385 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386
6387 // triple-tap
6388 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6389 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6390 processSync(mapper);
6391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006393 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006394
6395 // quad-tap
6396 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6397 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6398 processSync(mapper);
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006401 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402
6403 // finger
6404 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6405 processKey(mapper, BTN_TOOL_FINGER, 1);
6406 processSync(mapper);
6407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006409 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410
6411 // stylus trumps finger
6412 processKey(mapper, BTN_TOOL_PEN, 1);
6413 processSync(mapper);
6414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006416 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417
6418 // eraser trumps stylus
6419 processKey(mapper, BTN_TOOL_RUBBER, 1);
6420 processSync(mapper);
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006423 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006424
6425 // mouse trumps eraser
6426 processKey(mapper, BTN_TOOL_MOUSE, 1);
6427 processSync(mapper);
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006430 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006431
6432 // back to default tool type
6433 processKey(mapper, BTN_TOOL_MOUSE, 0);
6434 processKey(mapper, BTN_TOOL_RUBBER, 0);
6435 processKey(mapper, BTN_TOOL_PEN, 0);
6436 processKey(mapper, BTN_TOOL_FINGER, 0);
6437 processSync(mapper);
6438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006440 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006441}
6442
6443TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006444 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006445 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006446 prepareButtons();
6447 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006448 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +00006449 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006450
6451 NotifyMotionArgs motionArgs;
6452
6453 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6454 processKey(mapper, BTN_TOOL_FINGER, 1);
6455 processMove(mapper, 100, 200);
6456 processSync(mapper);
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6458 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6460 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6461
6462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6463 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6465 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6466
6467 // move a little
6468 processMove(mapper, 150, 250);
6469 processSync(mapper);
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6473 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6474
6475 // down when BTN_TOUCH is pressed, pressure defaults to 1
6476 processKey(mapper, BTN_TOUCH, 1);
6477 processSync(mapper);
6478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6479 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6481 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6482
6483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6484 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6486 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6487
6488 // up when BTN_TOUCH is released, hover restored
6489 processKey(mapper, BTN_TOUCH, 0);
6490 processSync(mapper);
6491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6492 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6494 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6495
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6497 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6499 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6500
6501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6502 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6504 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6505
6506 // exit hover when pointer goes away
6507 processKey(mapper, BTN_TOOL_FINGER, 0);
6508 processSync(mapper);
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6510 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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
6515TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006516 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006517 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006518 prepareButtons();
6519 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006520 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006521
6522 NotifyMotionArgs motionArgs;
6523
6524 // initially hovering because pressure is 0
6525 processDown(mapper, 100, 200);
6526 processPressure(mapper, 0);
6527 processSync(mapper);
6528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6529 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6531 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6532
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6534 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6536 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6537
6538 // move a little
6539 processMove(mapper, 150, 250);
6540 processSync(mapper);
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6544 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6545
6546 // down when pressure is non-zero
6547 processPressure(mapper, RAW_PRESSURE_MAX);
6548 processSync(mapper);
6549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6550 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6552 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6553
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6555 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6557 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6558
6559 // up when pressure becomes 0, hover restored
6560 processPressure(mapper, 0);
6561 processSync(mapper);
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6563 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6565 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6566
6567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6568 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6570 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6571
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6575 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6576
6577 // exit hover when pointer goes away
6578 processUp(mapper);
6579 processSync(mapper);
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6583 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6584}
6585
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006586TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6587 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006588 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006589 prepareButtons();
6590 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006591 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006592
6593 // Touch down.
6594 processDown(mapper, 100, 200);
6595 processPressure(mapper, 1);
6596 processSync(mapper);
6597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6598 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6599
6600 // Reset the mapper. This should cancel the ongoing gesture.
6601 resetMapper(mapper, ARBITRARY_TIME);
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6603 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6604
6605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6606}
6607
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006608TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6609 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006610 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006611 prepareButtons();
6612 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006613 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006614
6615 // Set the initial state for the touch pointer.
6616 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6617 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6618 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6619 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6620
6621 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006622 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6623 // does not generate any events.
6624 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006625
6626 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6627 // the recreated touch state to generate a down event.
6628 processSync(mapper);
6629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6630 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6631
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6633}
6634
lilinnan687e58f2022-07-19 16:00:50 +08006635TEST_F(SingleTouchInputMapperTest,
6636 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6637 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006638 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006639 prepareButtons();
6640 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006641 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006642 NotifyMotionArgs motionArgs;
6643
6644 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006645 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006646 processSync(mapper);
6647
6648 // We should receive a down event
6649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6650 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6651
6652 // Change display id
6653 clearViewports();
6654 prepareSecondaryDisplay(ViewportType::INTERNAL);
6655
6656 // We should receive a cancel event
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6658 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6659 // Then receive reset called
6660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6661}
6662
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006663TEST_F(SingleTouchInputMapperTest,
6664 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6665 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006666 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006667 prepareButtons();
6668 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006669 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6671 NotifyMotionArgs motionArgs;
6672
6673 // Start a new gesture.
6674 processDown(mapper, 100, 200);
6675 processSync(mapper);
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6677 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6678
6679 // Make the viewport inactive. This will put the device in disabled mode.
6680 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6681 viewport->isActive = false;
6682 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006683 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006684
6685 // We should receive a cancel event for the ongoing gesture.
6686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6687 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6688 // Then we should be notified that the device was reset.
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6690
6691 // No events are generated while the viewport is inactive.
6692 processMove(mapper, 101, 201);
6693 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006694 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006695 processSync(mapper);
6696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6697
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006698 // Start a new gesture while the viewport is still inactive.
6699 processDown(mapper, 300, 400);
6700 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6701 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6702 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6703 processSync(mapper);
6704
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006705 // Make the viewport active again. The device should resume processing events.
6706 viewport->isActive = true;
6707 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006708 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006709
6710 // The device is reset because it changes back to direct mode, without generating any events.
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6713
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006714 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006715 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6717 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006718
6719 // No more events.
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6722}
6723
Prabir Pradhan211ba622022-10-31 21:09:21 +00006724TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6725 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006726 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006727 prepareButtons();
6728 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006729 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6731
6732 // Press a stylus button.
6733 processKey(mapper, BTN_STYLUS, 1);
6734 processSync(mapper);
6735
6736 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6737 processDown(mapper, 100, 200);
6738 processSync(mapper);
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6740 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6741 WithCoords(toDisplayX(100), toDisplayY(200)),
6742 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6744 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6745 WithCoords(toDisplayX(100), toDisplayY(200)),
6746 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6747
6748 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6749 // the button has not actually been released, since there will be no pointers through which the
6750 // button state can be reported. The event is generated at the location of the pointer before
6751 // it went up.
6752 processUp(mapper);
6753 processSync(mapper);
6754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6755 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6756 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6758 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6759 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6760}
6761
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006762TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6763 addConfigurationProperty("touch.deviceType", "touchScreen");
6764 prepareDisplay(ui::ROTATION_0);
6765 prepareButtons();
6766 prepareAxes(POSITION);
6767
6768 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6769
Arpit Singh56adebc2023-04-25 13:56:05 +00006770 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6772
6773 // Press a stylus button.
6774 processKey(mapper, BTN_STYLUS, 1);
6775 processSync(mapper);
6776
6777 // Start a touch gesture and ensure that the stylus button is not reported.
6778 processDown(mapper, 100, 200);
6779 processSync(mapper);
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6781 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6782
6783 // Release and press the stylus button again.
6784 processKey(mapper, BTN_STYLUS, 0);
6785 processSync(mapper);
6786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6787 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6788 processKey(mapper, BTN_STYLUS, 1);
6789 processSync(mapper);
6790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6791 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6792
6793 // Release the touch gesture.
6794 processUp(mapper);
6795 processSync(mapper);
6796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6797 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6798
6799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6800}
6801
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006802TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6803 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6804 prepareDisplay(ui::ROTATION_0);
6805 prepareButtons();
6806 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006807 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6809
6810 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6811}
6812
Seunghwan Choi356026c2023-02-01 14:37:25 +09006813TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6814 std::shared_ptr<FakePointerController> fakePointerController =
6815 std::make_shared<FakePointerController>();
6816 addConfigurationProperty("touch.deviceType", "touchScreen");
6817 prepareDisplay(ui::ROTATION_0);
6818 prepareButtons();
6819 prepareAxes(POSITION);
6820 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6821 mFakePolicy->setPointerController(fakePointerController);
6822 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singh56adebc2023-04-25 13:56:05 +00006823 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006824
6825 processKey(mapper, BTN_TOOL_PEN, 1);
6826 processMove(mapper, 100, 200);
6827 processSync(mapper);
6828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6829 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006830 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006831 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6832 ASSERT_TRUE(fakePointerController->isPointerShown());
6833 ASSERT_NO_FATAL_FAILURE(
6834 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6835}
6836
6837TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6838 std::shared_ptr<FakePointerController> fakePointerController =
6839 std::make_shared<FakePointerController>();
6840 addConfigurationProperty("touch.deviceType", "touchScreen");
6841 prepareDisplay(ui::ROTATION_0);
6842 prepareButtons();
6843 prepareAxes(POSITION);
6844 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6845 mFakePolicy->setPointerController(fakePointerController);
6846 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singh56adebc2023-04-25 13:56:05 +00006847 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006848
6849 processKey(mapper, BTN_TOOL_PEN, 1);
6850 processMove(mapper, 100, 200);
6851 processSync(mapper);
6852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6853 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006854 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006855 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6856 ASSERT_FALSE(fakePointerController->isPointerShown());
6857}
6858
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006859TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6860 // Initialize the device without setting device source to touch navigation.
6861 addConfigurationProperty("touch.deviceType", "touchScreen");
6862 prepareDisplay(ui::ROTATION_0);
6863 prepareButtons();
6864 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006865 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006866
6867 // Ensure that the device is created as a touchscreen, not touch navigation.
6868 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6869
6870 // Add device type association after the device was created.
6871 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6872
6873 // Send update to the mapper.
6874 std::list<NotifyArgs> unused2 =
6875 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006876 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006877
6878 // Check whether device type update was successful.
6879 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6880}
6881
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006882TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6883 // Initialize the device without setting device source to touch navigation.
6884 addConfigurationProperty("touch.deviceType", "touchScreen");
6885 prepareDisplay(ui::ROTATION_0);
6886 prepareButtons();
6887 prepareAxes(POSITION);
6888 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6889
6890 // Set a physical frame in the display viewport.
6891 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6892 viewport->physicalLeft = 0;
6893 viewport->physicalTop = 0;
6894 viewport->physicalRight = DISPLAY_WIDTH / 2;
6895 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6896 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006897 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006898
Arpit Singh56adebc2023-04-25 13:56:05 +00006899 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006900
6901 // Hovering inside the physical frame produces events.
6902 processKey(mapper, BTN_TOOL_PEN, 1);
6903 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6904 processSync(mapper);
6905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6906 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6908 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6909
6910 // Leaving the physical frame ends the hovering gesture.
6911 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6912 processSync(mapper);
6913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6914 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6915
6916 // Moving outside the physical frame does not produce events.
6917 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6918 processSync(mapper);
6919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6920
6921 // Re-entering the physical frame produces events.
6922 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6923 processSync(mapper);
6924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6925 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6927 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6928}
6929
Prabir Pradhan5632d622021-09-06 07:57:20 -07006930// --- TouchDisplayProjectionTest ---
6931
6932class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6933public:
6934 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6935 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6936 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006937 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6938 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6939 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006940 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006941 auto rotatedWidth = naturalDisplayWidth;
6942 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006943 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006944 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006945 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006946 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006947 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006948 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006949 inverseRotationFlags = ui::Transform::ROT_180;
6950 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006951 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006952 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006953 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006954 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006955 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006956 inverseRotationFlags = ui::Transform::ROT_0;
6957 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006958 }
6959
Prabir Pradhana9df3162022-12-05 23:57:27 +00006960 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006961 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6962
6963 std::optional<DisplayViewport> internalViewport =
6964 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6965 DisplayViewport& v = *internalViewport;
6966 v.displayId = DISPLAY_ID;
6967 v.orientation = orientation;
6968
6969 v.logicalLeft = 0;
6970 v.logicalTop = 0;
6971 v.logicalRight = 100;
6972 v.logicalBottom = 100;
6973
6974 v.physicalLeft = rotatedPhysicalDisplay.left;
6975 v.physicalTop = rotatedPhysicalDisplay.top;
6976 v.physicalRight = rotatedPhysicalDisplay.right;
6977 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6978
Prabir Pradhana9df3162022-12-05 23:57:27 +00006979 v.deviceWidth = rotatedWidth;
6980 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006981
6982 v.isActive = true;
6983 v.uniqueId = UNIQUE_ID;
6984 v.type = ViewportType::INTERNAL;
6985 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006986 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006987 }
6988
6989 void assertReceivedMove(const Point& point) {
6990 NotifyMotionArgs motionArgs;
6991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6992 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006993 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6995 1, 0, 0, 0, 0, 0, 0, 0));
6996 }
6997};
6998
6999TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7000 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007001 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007002
7003 prepareButtons();
7004 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00007005 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007006
7007 NotifyMotionArgs motionArgs;
7008
7009 // Configure the DisplayViewport such that the logical display maps to a subsection of
7010 // the display panel called the physical display. Here, the physical display is bounded by the
7011 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7012 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7013 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7014 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7015
Michael Wrighta9cf4192022-12-01 23:46:39 +00007016 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007017 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7018
7019 // Touches outside the physical display should be ignored, and should not generate any
7020 // events. Ensure touches at the following points that lie outside of the physical display
7021 // area do not generate any events.
7022 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7023 processDown(mapper, toRawX(point.x), toRawY(point.y));
7024 processSync(mapper);
7025 processUp(mapper);
7026 processSync(mapper);
7027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7028 << "Unexpected event generated for touch outside physical display at point: "
7029 << point.x << ", " << point.y;
7030 }
7031 }
7032}
7033
7034TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7035 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007036 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007037
7038 prepareButtons();
7039 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00007040 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007041
7042 NotifyMotionArgs motionArgs;
7043
7044 // Configure the DisplayViewport such that the logical display maps to a subsection of
7045 // the display panel called the physical display. Here, the physical display is bounded by the
7046 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7047 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7048
Michael Wrighta9cf4192022-12-01 23:46:39 +00007049 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007050 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7051
7052 // Touches that start outside the physical display should be ignored until it enters the
7053 // physical display bounds, at which point it should generate a down event. Start a touch at
7054 // the point (5, 100), which is outside the physical display bounds.
7055 static const Point kOutsidePoint{5, 100};
7056 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7057 processSync(mapper);
7058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7059
7060 // Move the touch into the physical display area. This should generate a pointer down.
7061 processMove(mapper, toRawX(11), toRawY(21));
7062 processSync(mapper);
7063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7064 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007065 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007066 ASSERT_NO_FATAL_FAILURE(
7067 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7068
7069 // Move the touch inside the physical display area. This should generate a pointer move.
7070 processMove(mapper, toRawX(69), toRawY(159));
7071 processSync(mapper);
7072 assertReceivedMove({69, 159});
7073
7074 // Move outside the physical display area. Since the pointer is already down, this should
7075 // now continue generating events.
7076 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7077 processSync(mapper);
7078 assertReceivedMove(kOutsidePoint);
7079
7080 // Release. This should generate a pointer up.
7081 processUp(mapper);
7082 processSync(mapper);
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7084 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7086 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7087
7088 // Ensure no more events were generated.
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7091 }
7092}
7093
Prabir Pradhana9df3162022-12-05 23:57:27 +00007094// --- TouchscreenPrecisionTests ---
7095
7096// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7097// in various orientations and with different display rotations. We configure the touchscreen to
7098// have a higher resolution than that of the display by an integer scale factor in each axis so that
7099// we can enforce that coordinates match precisely as expected.
7100class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7101 public ::testing::WithParamInterface<ui::Rotation> {
7102public:
7103 void SetUp() override {
7104 SingleTouchInputMapperTest::SetUp();
7105
7106 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7107 // four times the resolution of the display in the Y axis.
7108 prepareButtons();
7109 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007110 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7111 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007112 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007113 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7114 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007115 }
7116
7117 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7118 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7119 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7120 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7121
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007122 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7123 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7124
7125 static const int32_t PRECISION_RAW_X_FLAT = 16;
7126 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7127
7128 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7129 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7130
Prabir Pradhana9df3162022-12-05 23:57:27 +00007131 static const std::array<Point, 4> kRawCorners;
7132};
7133
7134const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7135 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7136 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7137 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7138 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7139}};
7140
7141// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7142// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7143// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7144TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7145 enum class Orientation {
7146 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7147 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7148 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7149 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7150 ftl_last = ORIENTATION_270,
7151 };
7152 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7153 Orientation::ORIENTATION_270;
7154 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7155 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7156 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7157 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7158 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7159 };
7160
7161 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7162
7163 // Configure the touchscreen as being installed in the one of the four different orientations
7164 // relative to the display.
7165 addConfigurationProperty("touch.deviceType", "touchScreen");
7166 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7167 prepareDisplay(ui::ROTATION_0);
7168
Arpit Singh56adebc2023-04-25 13:56:05 +00007169 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007170
7171 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7172 // orientations of either 90 or 270) this means the display's natural resolution will be
7173 // flipped.
7174 const bool displayRotated =
7175 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7176 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7177 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7178 const Rect physicalFrame{0, 0, width, height};
7179 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7180
7181 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7182 const float expectedPrecisionX = displayRotated ? 4 : 2;
7183 const float expectedPrecisionY = displayRotated ? 2 : 4;
7184
7185 // Test all four corners.
7186 for (int i = 0; i < 4; i++) {
7187 const auto& raw = kRawCorners[i];
7188 processDown(mapper, raw.x, raw.y);
7189 processSync(mapper);
7190 const auto& expected = expectedPoints[i];
7191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7192 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7193 WithCoords(expected.x, expected.y),
7194 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7195 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7196 << "with touchscreen orientation "
7197 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7198 << expected.x << ", " << expected.y << ").";
7199 processUp(mapper);
7200 processSync(mapper);
7201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7202 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7203 WithCoords(expected.x, expected.y))));
7204 }
7205}
7206
Prabir Pradhan82687402022-12-06 01:32:53 +00007207TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7208 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7209 kMappedCorners = {
7210 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7211 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7212 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7213 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7214 };
7215
7216 const ui::Rotation displayRotation = GetParam();
7217
7218 addConfigurationProperty("touch.deviceType", "touchScreen");
7219 prepareDisplay(displayRotation);
7220
Arpit Singh56adebc2023-04-25 13:56:05 +00007221 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007222
7223 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7224
7225 // Test all four corners.
7226 for (int i = 0; i < 4; i++) {
7227 const auto& expected = expectedPoints[i];
7228 const auto& raw = kRawCorners[i];
7229 processDown(mapper, raw.x, raw.y);
7230 processSync(mapper);
7231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7232 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7233 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7234 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7235 << "with display rotation " << ui::toCString(displayRotation)
7236 << ", expected point (" << expected.x << ", " << expected.y << ").";
7237 processUp(mapper);
7238 processSync(mapper);
7239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7240 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7241 WithCoords(expected.x, expected.y))));
7242 }
7243}
7244
Prabir Pradhan3e798762022-12-02 21:02:11 +00007245TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7246 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7247 kMappedCorners = {
7248 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7249 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7250 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7251 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7252 };
7253
7254 const ui::Rotation displayRotation = GetParam();
7255
7256 addConfigurationProperty("touch.deviceType", "touchScreen");
7257 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7258
Arpit Singh56adebc2023-04-25 13:56:05 +00007259 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007260
7261 // Ori 270, so width and height swapped
7262 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7263 prepareDisplay(displayRotation);
7264 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7265
7266 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7267
7268 // Test all four corners.
7269 for (int i = 0; i < 4; i++) {
7270 const auto& expected = expectedPoints[i];
7271 const auto& raw = kRawCorners[i];
7272 processDown(mapper, raw.x, raw.y);
7273 processSync(mapper);
7274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7275 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7276 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7277 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7278 << "with display rotation " << ui::toCString(displayRotation)
7279 << ", expected point (" << expected.x << ", " << expected.y << ").";
7280 processUp(mapper);
7281 processSync(mapper);
7282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7283 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7284 WithCoords(expected.x, expected.y))));
7285 }
7286}
7287
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007288TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7289 const ui::Rotation displayRotation = GetParam();
7290
7291 addConfigurationProperty("touch.deviceType", "touchScreen");
7292 prepareDisplay(displayRotation);
7293
7294 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singh56adebc2023-04-25 13:56:05 +00007295 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007296
7297 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7298 // MotionRanges use display pixels as their units
7299 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7300 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7301
7302 // The MotionRanges should be oriented in the rotated display's coordinate space
7303 const bool displayRotated =
7304 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7305
7306 constexpr float MAX_X = 479.5;
7307 constexpr float MAX_Y = 799.75;
7308 EXPECT_EQ(xRange->min, 0.f);
7309 EXPECT_EQ(yRange->min, 0.f);
7310 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7311 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7312
7313 EXPECT_EQ(xRange->flat, 8.f);
7314 EXPECT_EQ(yRange->flat, 8.f);
7315
7316 EXPECT_EQ(xRange->fuzz, 2.f);
7317 EXPECT_EQ(yRange->fuzz, 2.f);
7318
7319 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7320 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7321}
7322
Prabir Pradhana9df3162022-12-05 23:57:27 +00007323// Run the precision tests for all rotations.
7324INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7325 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7326 ui::ROTATION_270),
7327 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7328 return ftl::enum_string(testParamInfo.param);
7329 });
7330
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007331// --- ExternalStylusFusionTest ---
7332
7333class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7334public:
7335 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7336 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007337 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007338 prepareButtons();
7339 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00007340 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007341
7342 mStylusState.when = ARBITRARY_TIME;
7343 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007344 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007345 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007346 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007347 processExternalStylusState(mapper);
7348 return mapper;
7349 }
7350
7351 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7352 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7353 for (const NotifyArgs& args : generatedArgs) {
7354 mFakeListener->notify(args);
7355 }
7356 // Loop the reader to flush the input listener queue.
7357 mReader->loopOnce();
7358 return generatedArgs;
7359 }
7360
7361protected:
7362 StylusState mStylusState{};
7363 static constexpr uint32_t EXPECTED_SOURCE =
7364 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7365
7366 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7367 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007368 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007369
7370 // The first pointer is withheld.
7371 processDown(mapper, 100, 200);
7372 processSync(mapper);
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7374 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7375 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7376
7377 // The external stylus reports pressure. The withheld finger pointer is released as a
7378 // stylus.
7379 mStylusState.pressure = 1.f;
7380 processExternalStylusState(mapper);
7381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7382 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7383 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7384
7385 // Subsequent pointer events are not withheld.
7386 processMove(mapper, 101, 201);
7387 processSync(mapper);
7388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7389 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7390
7391 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7393 }
7394
7395 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7396 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7397
7398 // Releasing the touch pointer ends the gesture.
7399 processUp(mapper);
7400 processSync(mapper);
7401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7402 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007403 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007404
7405 mStylusState.pressure = 0.f;
7406 processExternalStylusState(mapper);
7407 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7409 }
7410
7411 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7412 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007413 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007414
7415 // The first pointer is withheld when an external stylus is connected,
7416 // and a timeout is requested.
7417 processDown(mapper, 100, 200);
7418 processSync(mapper);
7419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7420 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7421 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7422
7423 // If the timeout expires early, it is requested again.
7424 handleTimeout(mapper, ARBITRARY_TIME + 1);
7425 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7426 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7427
7428 // When the timeout expires, the withheld touch is released as a finger pointer.
7429 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7431 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7432
7433 // Subsequent pointer events are not withheld.
7434 processMove(mapper, 101, 201);
7435 processSync(mapper);
7436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7437 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7438 processUp(mapper);
7439 processSync(mapper);
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7441 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7442
7443 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7445 }
7446
7447private:
7448 InputDeviceInfo mExternalStylusDeviceInfo{};
7449};
7450
7451TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7452 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7453 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7454}
7455
7456TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7457 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7458 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7459}
7460
7461TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7462 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7463 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7464}
7465
7466// Test a successful stylus fusion gesture where the pressure is reported by the external
7467// before the touch is reported by the touchscreen.
7468TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7469 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7470 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007471 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007472
7473 // The external stylus reports pressure first. It is ignored for now.
7474 mStylusState.pressure = 1.f;
7475 processExternalStylusState(mapper);
7476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7477 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7478
7479 // When the touch goes down afterwards, it is reported as a stylus pointer.
7480 processDown(mapper, 100, 200);
7481 processSync(mapper);
7482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7483 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7484 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7485
7486 processMove(mapper, 101, 201);
7487 processSync(mapper);
7488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7489 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7490 processUp(mapper);
7491 processSync(mapper);
7492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7493 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7494
7495 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7497}
7498
7499TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7500 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7501
7502 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7503 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7504
7505 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7506 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7507 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7508 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7509}
7510
7511TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7512 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7513 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007514 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007515
7516 mStylusState.pressure = 0.8f;
7517 processExternalStylusState(mapper);
7518 processDown(mapper, 100, 200);
7519 processSync(mapper);
7520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7521 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7522 WithPressure(0.8f))));
7523 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7524
7525 // The external stylus reports a pressure change. We wait for some time for a touch event.
7526 mStylusState.pressure = 0.6f;
7527 processExternalStylusState(mapper);
7528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7529 ASSERT_NO_FATAL_FAILURE(
7530 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7531
7532 // If a touch is reported within the timeout, it reports the updated pressure.
7533 processMove(mapper, 101, 201);
7534 processSync(mapper);
7535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7536 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7537 WithPressure(0.6f))));
7538 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7539
7540 // There is another pressure change.
7541 mStylusState.pressure = 0.5f;
7542 processExternalStylusState(mapper);
7543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7544 ASSERT_NO_FATAL_FAILURE(
7545 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7546
7547 // If a touch is not reported within the timeout, a move event is generated to report
7548 // the new pressure.
7549 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7551 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7552 WithPressure(0.5f))));
7553
7554 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7555 // repeated indefinitely.
7556 mStylusState.pressure = 0.0f;
7557 processExternalStylusState(mapper);
7558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7559 ASSERT_NO_FATAL_FAILURE(
7560 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7561 processMove(mapper, 102, 202);
7562 processSync(mapper);
7563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7564 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7565 WithPressure(0.5f))));
7566 processMove(mapper, 103, 203);
7567 processSync(mapper);
7568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7569 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7570 WithPressure(0.5f))));
7571
7572 processUp(mapper);
7573 processSync(mapper);
7574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7575 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007576 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007577
7578 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7580}
7581
7582TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7583 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7584 auto source = WithSource(EXPECTED_SOURCE);
7585
7586 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007587 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007588 processExternalStylusState(mapper);
7589 processDown(mapper, 100, 200);
7590 processSync(mapper);
7591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7592 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007593 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007594 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7595
7596 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007597 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007598 processExternalStylusState(mapper);
7599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7600 ASSERT_NO_FATAL_FAILURE(
7601 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7602
7603 // If a touch is reported within the timeout, it reports the updated pressure.
7604 processMove(mapper, 101, 201);
7605 processSync(mapper);
7606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7607 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007608 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007609 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7610
7611 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007612 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007613 processExternalStylusState(mapper);
7614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7615 ASSERT_NO_FATAL_FAILURE(
7616 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7617
7618 // If a touch is not reported within the timeout, a move event is generated to report
7619 // the new tool type.
7620 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7622 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007623 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007624
7625 processUp(mapper);
7626 processSync(mapper);
7627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7628 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007629 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007630
7631 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7633}
7634
7635TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7636 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7637 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007638 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007639
7640 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7641
7642 // The external stylus reports a button change. We wait for some time for a touch event.
7643 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7644 processExternalStylusState(mapper);
7645 ASSERT_NO_FATAL_FAILURE(
7646 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7647
7648 // If a touch is reported within the timeout, it reports the updated button state.
7649 processMove(mapper, 101, 201);
7650 processSync(mapper);
7651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7652 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7653 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7655 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7656 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7657 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7658
7659 // The button is now released.
7660 mStylusState.buttons = 0;
7661 processExternalStylusState(mapper);
7662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7663 ASSERT_NO_FATAL_FAILURE(
7664 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7665
7666 // If a touch is not reported within the timeout, a move event is generated to report
7667 // the new button state.
7668 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7670 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7671 WithButtonState(0))));
7672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007673 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7674 WithButtonState(0))));
7675
7676 processUp(mapper);
7677 processSync(mapper);
7678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007679 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7680
7681 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7683}
7684
Michael Wrightd02c5b62014-02-10 15:10:22 -08007685// --- MultiTouchInputMapperTest ---
7686
7687class MultiTouchInputMapperTest : public TouchInputMapperTest {
7688protected:
7689 void prepareAxes(int axes);
7690
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007691 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7692 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7693 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7694 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7695 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7696 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7697 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7698 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7699 void processId(MultiTouchInputMapper& mapper, int32_t id);
7700 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7701 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7702 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007703 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007704 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007705 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7706 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007707};
7708
7709void MultiTouchInputMapperTest::prepareAxes(int axes) {
7710 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007711 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7712 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007713 }
7714 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007715 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7716 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007717 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007718 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7719 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007720 }
7721 }
7722 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007723 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7724 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007725 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007726 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007727 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007728 }
7729 }
7730 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007731 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7732 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007733 }
7734 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007735 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7736 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007737 }
7738 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007739 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7740 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007741 }
7742 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007743 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7744 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007745 }
7746 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007747 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7748 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007749 }
7750 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007751 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007752 }
7753}
7754
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007755void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7756 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007759}
7760
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007761void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7762 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007764}
7765
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007766void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7767 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007769}
7770
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007771void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007773}
7774
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007775void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007777}
7778
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007779void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7780 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007782}
7783
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007784void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007786}
7787
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007788void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007790}
7791
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007792void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007794}
7795
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007796void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007798}
7799
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007800void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007802}
7803
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007804void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7805 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007807}
7808
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007809void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7810 int32_t value) {
7811 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7813}
7814
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007815void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007817}
7818
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007819void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7820 nsecs_t readTime) {
7821 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007822}
7823
Michael Wrightd02c5b62014-02-10 15:10:22 -08007824TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007825 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007826 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007827 prepareAxes(POSITION);
7828 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00007829 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007830
arthurhungdcef2dc2020-08-11 14:47:50 +08007831 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007832
7833 NotifyMotionArgs motionArgs;
7834
7835 // Two fingers down at once.
7836 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7837 processPosition(mapper, x1, y1);
7838 processMTSync(mapper);
7839 processPosition(mapper, x2, y2);
7840 processMTSync(mapper);
7841 processSync(mapper);
7842
7843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7844 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7845 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7846 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7847 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7848 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7849 ASSERT_EQ(0, motionArgs.flags);
7850 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7851 ASSERT_EQ(0, motionArgs.buttonState);
7852 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007853 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007854 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007855 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7857 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7858 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7859 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7860 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7861
7862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7863 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7864 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7865 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7866 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007867 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007868 ASSERT_EQ(0, motionArgs.flags);
7869 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7870 ASSERT_EQ(0, motionArgs.buttonState);
7871 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007872 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007873 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007874 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007875 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007876 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7878 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7880 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7881 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7882 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7883 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7884
7885 // Move.
7886 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7887 processPosition(mapper, x1, y1);
7888 processMTSync(mapper);
7889 processPosition(mapper, x2, y2);
7890 processMTSync(mapper);
7891 processSync(mapper);
7892
7893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7894 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7895 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7896 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7897 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7899 ASSERT_EQ(0, motionArgs.flags);
7900 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7901 ASSERT_EQ(0, motionArgs.buttonState);
7902 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007903 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007904 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007905 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007906 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007907 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7909 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7911 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7912 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7913 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7914 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7915
7916 // First finger up.
7917 x2 += 15; y2 -= 20;
7918 processPosition(mapper, x2, y2);
7919 processMTSync(mapper);
7920 processSync(mapper);
7921
7922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7923 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7924 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7925 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7926 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007927 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007928 ASSERT_EQ(0, motionArgs.flags);
7929 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7930 ASSERT_EQ(0, motionArgs.buttonState);
7931 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007932 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007933 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007934 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007935 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007936 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7938 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7940 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7941 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7942 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7943 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7944
7945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7946 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7947 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7948 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7949 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7951 ASSERT_EQ(0, motionArgs.flags);
7952 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7953 ASSERT_EQ(0, motionArgs.buttonState);
7954 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007955 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007956 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007957 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7959 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7960 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7961 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7962 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7963
7964 // Move.
7965 x2 += 20; y2 -= 25;
7966 processPosition(mapper, x2, y2);
7967 processMTSync(mapper);
7968 processSync(mapper);
7969
7970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7971 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7972 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7973 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7974 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7976 ASSERT_EQ(0, motionArgs.flags);
7977 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7978 ASSERT_EQ(0, motionArgs.buttonState);
7979 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007980 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007981 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007982 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7984 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7985 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7986 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7987 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7988
7989 // New finger down.
7990 int32_t x3 = 700, y3 = 300;
7991 processPosition(mapper, x2, y2);
7992 processMTSync(mapper);
7993 processPosition(mapper, x3, y3);
7994 processMTSync(mapper);
7995 processSync(mapper);
7996
7997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7998 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7999 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8000 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8001 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008002 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008003 ASSERT_EQ(0, motionArgs.flags);
8004 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8005 ASSERT_EQ(0, motionArgs.buttonState);
8006 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008007 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008008 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008009 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008010 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008011 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008012 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8013 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8015 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8016 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8017 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8018 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8019
8020 // Second finger up.
8021 x3 += 30; y3 -= 20;
8022 processPosition(mapper, x3, y3);
8023 processMTSync(mapper);
8024 processSync(mapper);
8025
8026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8027 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8028 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8029 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8030 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008031 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008032 ASSERT_EQ(0, motionArgs.flags);
8033 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8034 ASSERT_EQ(0, motionArgs.buttonState);
8035 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008036 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008037 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008038 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008040 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8042 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8044 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8045 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8046 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8047 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8048
8049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8050 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8051 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8052 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8053 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8055 ASSERT_EQ(0, motionArgs.flags);
8056 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8057 ASSERT_EQ(0, motionArgs.buttonState);
8058 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008059 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008060 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008061 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8063 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8064 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8065 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8066 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8067
8068 // Last finger up.
8069 processMTSync(mapper);
8070 processSync(mapper);
8071
8072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8073 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8074 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8075 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8076 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8077 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8078 ASSERT_EQ(0, motionArgs.flags);
8079 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8080 ASSERT_EQ(0, motionArgs.buttonState);
8081 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008082 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008083 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008084 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8086 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8087 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8088 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8089 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8090
8091 // Should not have sent any more keys or motions.
8092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8094}
8095
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008096TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8097 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008098 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008099
8100 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8101 /*fuzz*/ 0, /*resolution*/ 10);
8102 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8103 /*fuzz*/ 0, /*resolution*/ 11);
8104 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8105 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8106 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8107 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8108 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8109 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8110 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8111 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8112
Arpit Singh56adebc2023-04-25 13:56:05 +00008113 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008114
8115 // X and Y axes
8116 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8117 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8118 // Touch major and minor
8119 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8120 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8121 // Tool major and minor
8122 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8123 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8124}
8125
8126TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8127 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008128 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008129
8130 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8131 /*fuzz*/ 0, /*resolution*/ 10);
8132 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8133 /*fuzz*/ 0, /*resolution*/ 11);
8134
8135 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8136
Arpit Singh56adebc2023-04-25 13:56:05 +00008137 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008138
8139 // Touch major and minor
8140 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8141 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8142 // Tool major and minor
8143 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8144 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8145}
8146
Michael Wrightd02c5b62014-02-10 15:10:22 -08008147TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008148 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008149 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150 prepareAxes(POSITION | ID);
8151 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00008152 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008153
arthurhungdcef2dc2020-08-11 14:47:50 +08008154 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008155
8156 NotifyMotionArgs motionArgs;
8157
8158 // Two fingers down at once.
8159 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8160 processPosition(mapper, x1, y1);
8161 processId(mapper, 1);
8162 processMTSync(mapper);
8163 processPosition(mapper, x2, y2);
8164 processId(mapper, 2);
8165 processMTSync(mapper);
8166 processSync(mapper);
8167
8168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8169 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008170 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008171 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008172 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008173 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8174 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8175
8176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008177 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008178 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008180 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008181 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008182 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8184 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8186 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8187
8188 // Move.
8189 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8190 processPosition(mapper, x1, y1);
8191 processId(mapper, 1);
8192 processMTSync(mapper);
8193 processPosition(mapper, x2, y2);
8194 processId(mapper, 2);
8195 processMTSync(mapper);
8196 processSync(mapper);
8197
8198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8199 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008200 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008201 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008202 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008204 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8206 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8208 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8209
8210 // First finger up.
8211 x2 += 15; y2 -= 20;
8212 processPosition(mapper, x2, y2);
8213 processId(mapper, 2);
8214 processMTSync(mapper);
8215 processSync(mapper);
8216
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008218 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008219 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008220 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008221 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008222 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008223 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8225 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8226 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8227 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8228
8229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008231 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008232 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008233 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8235 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8236
8237 // Move.
8238 x2 += 20; y2 -= 25;
8239 processPosition(mapper, x2, y2);
8240 processId(mapper, 2);
8241 processMTSync(mapper);
8242 processSync(mapper);
8243
8244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008246 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008247 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008248 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8250 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8251
8252 // New finger down.
8253 int32_t x3 = 700, y3 = 300;
8254 processPosition(mapper, x2, y2);
8255 processId(mapper, 2);
8256 processMTSync(mapper);
8257 processPosition(mapper, x3, y3);
8258 processId(mapper, 3);
8259 processMTSync(mapper);
8260 processSync(mapper);
8261
8262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008263 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008264 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008265 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008266 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008267 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008268 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008269 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8270 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8272 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8273
8274 // Second finger up.
8275 x3 += 30; y3 -= 20;
8276 processPosition(mapper, x3, y3);
8277 processId(mapper, 3);
8278 processMTSync(mapper);
8279 processSync(mapper);
8280
8281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008282 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008283 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008284 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008285 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008286 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008287 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8289 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8291 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8292
8293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8294 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008295 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008296 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008297 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8299 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8300
8301 // Last finger up.
8302 processMTSync(mapper);
8303 processSync(mapper);
8304
8305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8306 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008307 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008308 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008309 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8311 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8312
8313 // Should not have sent any more keys or motions.
8314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8316}
8317
8318TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008319 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008320 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008321 prepareAxes(POSITION | ID | SLOT);
8322 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00008323 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008324
arthurhungdcef2dc2020-08-11 14:47:50 +08008325 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008326
8327 NotifyMotionArgs motionArgs;
8328
8329 // Two fingers down at once.
8330 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8331 processPosition(mapper, x1, y1);
8332 processId(mapper, 1);
8333 processSlot(mapper, 1);
8334 processPosition(mapper, x2, y2);
8335 processId(mapper, 2);
8336 processSync(mapper);
8337
8338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8339 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008340 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008341 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008342 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8344 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8345
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008347 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008348 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008349 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008350 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008351 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008352 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8354 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8356 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8357
8358 // Move.
8359 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8360 processSlot(mapper, 0);
8361 processPosition(mapper, x1, y1);
8362 processSlot(mapper, 1);
8363 processPosition(mapper, x2, y2);
8364 processSync(mapper);
8365
8366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008368 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008369 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008370 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008371 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008372 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8374 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8376 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8377
8378 // First finger up.
8379 x2 += 15; y2 -= 20;
8380 processSlot(mapper, 0);
8381 processId(mapper, -1);
8382 processSlot(mapper, 1);
8383 processPosition(mapper, x2, y2);
8384 processSync(mapper);
8385
8386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008387 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008388 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008389 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008390 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008391 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008392 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8394 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8396 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8397
8398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008400 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008401 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008402 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8404 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8405
8406 // Move.
8407 x2 += 20; y2 -= 25;
8408 processPosition(mapper, x2, y2);
8409 processSync(mapper);
8410
8411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008413 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008414 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008415 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8417 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8418
8419 // New finger down.
8420 int32_t x3 = 700, y3 = 300;
8421 processPosition(mapper, x2, y2);
8422 processSlot(mapper, 0);
8423 processId(mapper, 3);
8424 processPosition(mapper, x3, y3);
8425 processSync(mapper);
8426
8427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008428 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008429 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008430 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008431 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008432 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008433 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8435 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8437 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8438
8439 // Second finger up.
8440 x3 += 30; y3 -= 20;
8441 processSlot(mapper, 1);
8442 processId(mapper, -1);
8443 processSlot(mapper, 0);
8444 processPosition(mapper, x3, y3);
8445 processSync(mapper);
8446
8447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008448 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008449 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008450 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008451 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008452 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008453 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8455 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8457 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8458
8459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008461 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008462 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008463 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8465 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8466
8467 // Last finger up.
8468 processId(mapper, -1);
8469 processSync(mapper);
8470
8471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8472 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008473 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008474 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008475 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8477 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8478
8479 // Should not have sent any more keys or motions.
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8482}
8483
8484TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008485 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008486 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008487 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singh56adebc2023-04-25 13:56:05 +00008488 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008489
8490 // These calculations are based on the input device calibration documentation.
8491 int32_t rawX = 100;
8492 int32_t rawY = 200;
8493 int32_t rawTouchMajor = 7;
8494 int32_t rawTouchMinor = 6;
8495 int32_t rawToolMajor = 9;
8496 int32_t rawToolMinor = 8;
8497 int32_t rawPressure = 11;
8498 int32_t rawDistance = 0;
8499 int32_t rawOrientation = 3;
8500 int32_t id = 5;
8501
8502 float x = toDisplayX(rawX);
8503 float y = toDisplayY(rawY);
8504 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8505 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8506 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8507 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8508 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8509 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8510 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8511 float distance = float(rawDistance);
8512
8513 processPosition(mapper, rawX, rawY);
8514 processTouchMajor(mapper, rawTouchMajor);
8515 processTouchMinor(mapper, rawTouchMinor);
8516 processToolMajor(mapper, rawToolMajor);
8517 processToolMinor(mapper, rawToolMinor);
8518 processPressure(mapper, rawPressure);
8519 processOrientation(mapper, rawOrientation);
8520 processDistance(mapper, rawDistance);
8521 processId(mapper, id);
8522 processMTSync(mapper);
8523 processSync(mapper);
8524
8525 NotifyMotionArgs args;
8526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8527 ASSERT_EQ(0, args.pointerProperties[0].id);
8528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8529 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8530 orientation, distance));
8531}
8532
8533TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008534 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008535 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008536 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8537 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singh56adebc2023-04-25 13:56:05 +00008538 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008539
8540 // These calculations are based on the input device calibration documentation.
8541 int32_t rawX = 100;
8542 int32_t rawY = 200;
8543 int32_t rawTouchMajor = 140;
8544 int32_t rawTouchMinor = 120;
8545 int32_t rawToolMajor = 180;
8546 int32_t rawToolMinor = 160;
8547
8548 float x = toDisplayX(rawX);
8549 float y = toDisplayY(rawY);
8550 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8551 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8552 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8553 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8554 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8555
8556 processPosition(mapper, rawX, rawY);
8557 processTouchMajor(mapper, rawTouchMajor);
8558 processTouchMinor(mapper, rawTouchMinor);
8559 processToolMajor(mapper, rawToolMajor);
8560 processToolMinor(mapper, rawToolMinor);
8561 processMTSync(mapper);
8562 processSync(mapper);
8563
8564 NotifyMotionArgs args;
8565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8567 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8568}
8569
8570TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008571 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008572 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008573 prepareAxes(POSITION | TOUCH | TOOL);
8574 addConfigurationProperty("touch.size.calibration", "diameter");
8575 addConfigurationProperty("touch.size.scale", "10");
8576 addConfigurationProperty("touch.size.bias", "160");
8577 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singh56adebc2023-04-25 13:56:05 +00008578 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008579
8580 // These calculations are based on the input device calibration documentation.
8581 // Note: We only provide a single common touch/tool value because the device is assumed
8582 // not to emit separate values for each pointer (isSummed = 1).
8583 int32_t rawX = 100;
8584 int32_t rawY = 200;
8585 int32_t rawX2 = 150;
8586 int32_t rawY2 = 250;
8587 int32_t rawTouchMajor = 5;
8588 int32_t rawToolMajor = 8;
8589
8590 float x = toDisplayX(rawX);
8591 float y = toDisplayY(rawY);
8592 float x2 = toDisplayX(rawX2);
8593 float y2 = toDisplayY(rawY2);
8594 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8595 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8596 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8597
8598 processPosition(mapper, rawX, rawY);
8599 processTouchMajor(mapper, rawTouchMajor);
8600 processToolMajor(mapper, rawToolMajor);
8601 processMTSync(mapper);
8602 processPosition(mapper, rawX2, rawY2);
8603 processTouchMajor(mapper, rawTouchMajor);
8604 processToolMajor(mapper, rawToolMajor);
8605 processMTSync(mapper);
8606 processSync(mapper);
8607
8608 NotifyMotionArgs args;
8609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8610 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8611
8612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008613 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008614 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8616 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8618 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8619}
8620
8621TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008622 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008623 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008624 prepareAxes(POSITION | TOUCH | TOOL);
8625 addConfigurationProperty("touch.size.calibration", "area");
8626 addConfigurationProperty("touch.size.scale", "43");
8627 addConfigurationProperty("touch.size.bias", "3");
Arpit Singh56adebc2023-04-25 13:56:05 +00008628 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008629
8630 // These calculations are based on the input device calibration documentation.
8631 int32_t rawX = 100;
8632 int32_t rawY = 200;
8633 int32_t rawTouchMajor = 5;
8634 int32_t rawToolMajor = 8;
8635
8636 float x = toDisplayX(rawX);
8637 float y = toDisplayY(rawY);
8638 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8639 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8640 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8641
8642 processPosition(mapper, rawX, rawY);
8643 processTouchMajor(mapper, rawTouchMajor);
8644 processToolMajor(mapper, rawToolMajor);
8645 processMTSync(mapper);
8646 processSync(mapper);
8647
8648 NotifyMotionArgs args;
8649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8651 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8652}
8653
8654TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008655 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008656 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008657 prepareAxes(POSITION | PRESSURE);
8658 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8659 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singh56adebc2023-04-25 13:56:05 +00008660 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008661
Michael Wrightaa449c92017-12-13 21:21:43 +00008662 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008663 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008664 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8665 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8666 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8667
Michael Wrightd02c5b62014-02-10 15:10:22 -08008668 // These calculations are based on the input device calibration documentation.
8669 int32_t rawX = 100;
8670 int32_t rawY = 200;
8671 int32_t rawPressure = 60;
8672
8673 float x = toDisplayX(rawX);
8674 float y = toDisplayY(rawY);
8675 float pressure = float(rawPressure) * 0.01f;
8676
8677 processPosition(mapper, rawX, rawY);
8678 processPressure(mapper, rawPressure);
8679 processMTSync(mapper);
8680 processSync(mapper);
8681
8682 NotifyMotionArgs args;
8683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8685 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8686}
8687
8688TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008689 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008690 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008691 prepareAxes(POSITION | ID | SLOT);
Arpit Singh56adebc2023-04-25 13:56:05 +00008692 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008693
8694 NotifyMotionArgs motionArgs;
8695 NotifyKeyArgs keyArgs;
8696
8697 processId(mapper, 1);
8698 processPosition(mapper, 100, 200);
8699 processSync(mapper);
8700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8701 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8702 ASSERT_EQ(0, motionArgs.buttonState);
8703
8704 // press BTN_LEFT, release BTN_LEFT
8705 processKey(mapper, BTN_LEFT, 1);
8706 processSync(mapper);
8707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8708 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8709 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8710
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8712 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8713 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8714
Michael Wrightd02c5b62014-02-10 15:10:22 -08008715 processKey(mapper, BTN_LEFT, 0);
8716 processSync(mapper);
8717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008718 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008719 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008720
8721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008722 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008723 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008724
8725 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8726 processKey(mapper, BTN_RIGHT, 1);
8727 processKey(mapper, BTN_MIDDLE, 1);
8728 processSync(mapper);
8729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8730 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8731 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8732 motionArgs.buttonState);
8733
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8735 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8736 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8737
8738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8739 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8740 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8741 motionArgs.buttonState);
8742
Michael Wrightd02c5b62014-02-10 15:10:22 -08008743 processKey(mapper, BTN_RIGHT, 0);
8744 processSync(mapper);
8745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008746 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008747 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008748
8749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008751 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008752
8753 processKey(mapper, BTN_MIDDLE, 0);
8754 processSync(mapper);
8755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008756 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008757 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008758
8759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008761 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008762
8763 // press BTN_BACK, release BTN_BACK
8764 processKey(mapper, BTN_BACK, 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_BACK, 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_BACK, 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_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008777
8778 processKey(mapper, BTN_BACK, 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_BACK, keyArgs.keyCode);
8791
8792 // press BTN_SIDE, release BTN_SIDE
8793 processKey(mapper, BTN_SIDE, 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_BACK, 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_BACK, 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_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008806
8807 processKey(mapper, BTN_SIDE, 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_BACK, keyArgs.keyCode);
8820
8821 // press BTN_FORWARD, release BTN_FORWARD
8822 processKey(mapper, BTN_FORWARD, 1);
8823 processSync(mapper);
8824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8825 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8826 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008827
Michael Wrightd02c5b62014-02-10 15:10:22 -08008828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008829 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008830 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8831
8832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8833 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8834 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008835
8836 processKey(mapper, BTN_FORWARD, 0);
8837 processSync(mapper);
8838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008839 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008840 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008841
8842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008844 ASSERT_EQ(0, motionArgs.buttonState);
8845
Michael Wrightd02c5b62014-02-10 15:10:22 -08008846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8847 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8848 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8849
8850 // press BTN_EXTRA, release BTN_EXTRA
8851 processKey(mapper, BTN_EXTRA, 1);
8852 processSync(mapper);
8853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8854 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8855 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008856
Michael Wrightd02c5b62014-02-10 15:10:22 -08008857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008859 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8860
8861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8863 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008864
8865 processKey(mapper, BTN_EXTRA, 0);
8866 processSync(mapper);
8867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008868 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008869 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008870
8871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008873 ASSERT_EQ(0, motionArgs.buttonState);
8874
Michael Wrightd02c5b62014-02-10 15:10:22 -08008875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8876 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8877 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8878
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8880
Michael Wrightd02c5b62014-02-10 15:10:22 -08008881 // press BTN_STYLUS, release BTN_STYLUS
8882 processKey(mapper, BTN_STYLUS, 1);
8883 processSync(mapper);
8884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008886 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8887
8888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8889 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8890 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008891
8892 processKey(mapper, BTN_STYLUS, 0);
8893 processSync(mapper);
8894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008895 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008896 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008897
8898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008900 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008901
8902 // press BTN_STYLUS2, release BTN_STYLUS2
8903 processKey(mapper, BTN_STYLUS2, 1);
8904 processSync(mapper);
8905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008907 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8908
8909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8910 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8911 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008912
8913 processKey(mapper, BTN_STYLUS2, 0);
8914 processSync(mapper);
8915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008916 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008917 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008918
8919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008920 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008921 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008922
8923 // release touch
8924 processId(mapper, -1);
8925 processSync(mapper);
8926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8927 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8928 ASSERT_EQ(0, motionArgs.buttonState);
8929}
8930
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008931TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8932 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008933 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008934 prepareAxes(POSITION | ID | SLOT);
Arpit Singh56adebc2023-04-25 13:56:05 +00008935 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008936
8937 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8938 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8939
8940 // Touch down.
8941 processId(mapper, 1);
8942 processPosition(mapper, 100, 200);
8943 processSync(mapper);
8944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8945 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8946
8947 // Press and release button mapped to the primary stylus button.
8948 processKey(mapper, BTN_A, 1);
8949 processSync(mapper);
8950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8951 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8952 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8954 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8955 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8956
8957 processKey(mapper, BTN_A, 0);
8958 processSync(mapper);
8959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8960 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8962 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8963
8964 // Press and release the HID usage mapped to the secondary stylus button.
8965 processHidUsage(mapper, 0xabcd, 1);
8966 processSync(mapper);
8967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8968 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8969 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8971 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8972 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8973
8974 processHidUsage(mapper, 0xabcd, 0);
8975 processSync(mapper);
8976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8977 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8979 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8980
8981 // Release touch.
8982 processId(mapper, -1);
8983 processSync(mapper);
8984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8985 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8986}
8987
Michael Wrightd02c5b62014-02-10 15:10:22 -08008988TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008989 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008990 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008991 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00008992 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008993
8994 NotifyMotionArgs motionArgs;
8995
8996 // default tool type is finger
8997 processId(mapper, 1);
8998 processPosition(mapper, 100, 200);
8999 processSync(mapper);
9000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9001 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009002 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009003
9004 // eraser
9005 processKey(mapper, BTN_TOOL_RUBBER, 1);
9006 processSync(mapper);
9007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009009 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009010
9011 // stylus
9012 processKey(mapper, BTN_TOOL_RUBBER, 0);
9013 processKey(mapper, BTN_TOOL_PEN, 1);
9014 processSync(mapper);
9015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9016 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009017 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009018
9019 // brush
9020 processKey(mapper, BTN_TOOL_PEN, 0);
9021 processKey(mapper, BTN_TOOL_BRUSH, 1);
9022 processSync(mapper);
9023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009025 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009026
9027 // pencil
9028 processKey(mapper, BTN_TOOL_BRUSH, 0);
9029 processKey(mapper, BTN_TOOL_PENCIL, 1);
9030 processSync(mapper);
9031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9032 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009033 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009034
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009035 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009036 processKey(mapper, BTN_TOOL_PENCIL, 0);
9037 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9038 processSync(mapper);
9039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009041 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009042
9043 // mouse
9044 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9045 processKey(mapper, BTN_TOOL_MOUSE, 1);
9046 processSync(mapper);
9047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009049 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009050
9051 // lens
9052 processKey(mapper, BTN_TOOL_MOUSE, 0);
9053 processKey(mapper, BTN_TOOL_LENS, 1);
9054 processSync(mapper);
9055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009057 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009058
9059 // double-tap
9060 processKey(mapper, BTN_TOOL_LENS, 0);
9061 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9062 processSync(mapper);
9063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009065 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009066
9067 // triple-tap
9068 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9069 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9070 processSync(mapper);
9071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009073 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009074
9075 // quad-tap
9076 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9077 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9078 processSync(mapper);
9079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009081 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009082
9083 // finger
9084 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9085 processKey(mapper, BTN_TOOL_FINGER, 1);
9086 processSync(mapper);
9087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009089 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009090
9091 // stylus trumps finger
9092 processKey(mapper, BTN_TOOL_PEN, 1);
9093 processSync(mapper);
9094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9095 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009096 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009097
9098 // eraser trumps stylus
9099 processKey(mapper, BTN_TOOL_RUBBER, 1);
9100 processSync(mapper);
9101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009103 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009104
9105 // mouse trumps eraser
9106 processKey(mapper, BTN_TOOL_MOUSE, 1);
9107 processSync(mapper);
9108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009110 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009111
9112 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9113 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9114 processSync(mapper);
9115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009117 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009118
9119 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9120 processToolType(mapper, MT_TOOL_PEN);
9121 processSync(mapper);
9122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009124 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009125
9126 // back to default tool type
9127 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9128 processKey(mapper, BTN_TOOL_MOUSE, 0);
9129 processKey(mapper, BTN_TOOL_RUBBER, 0);
9130 processKey(mapper, BTN_TOOL_PEN, 0);
9131 processKey(mapper, BTN_TOOL_FINGER, 0);
9132 processSync(mapper);
9133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009135 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009136}
9137
9138TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009139 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009140 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009141 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009142 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +00009143 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009144
9145 NotifyMotionArgs motionArgs;
9146
9147 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9148 processId(mapper, 1);
9149 processPosition(mapper, 100, 200);
9150 processSync(mapper);
9151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9152 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9154 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9155
9156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9157 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9159 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9160
9161 // move a little
9162 processPosition(mapper, 150, 250);
9163 processSync(mapper);
9164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9165 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9167 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9168
9169 // down when BTN_TOUCH is pressed, pressure defaults to 1
9170 processKey(mapper, BTN_TOUCH, 1);
9171 processSync(mapper);
9172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9173 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9175 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9176
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9178 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9180 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9181
9182 // up when BTN_TOUCH is released, hover restored
9183 processKey(mapper, BTN_TOUCH, 0);
9184 processSync(mapper);
9185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9186 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9188 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9189
9190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9191 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9193 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9194
9195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9196 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9198 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9199
9200 // exit hover when pointer goes away
9201 processId(mapper, -1);
9202 processSync(mapper);
9203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9204 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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
9209TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009210 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009211 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009212 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009213 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009214
9215 NotifyMotionArgs motionArgs;
9216
9217 // initially hovering because pressure is 0
9218 processId(mapper, 1);
9219 processPosition(mapper, 100, 200);
9220 processPressure(mapper, 0);
9221 processSync(mapper);
9222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9223 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9225 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9226
9227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9228 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9229 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9230 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9231
9232 // move a little
9233 processPosition(mapper, 150, 250);
9234 processSync(mapper);
9235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9236 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9238 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9239
9240 // down when pressure becomes non-zero
9241 processPressure(mapper, RAW_PRESSURE_MAX);
9242 processSync(mapper);
9243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9244 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9246 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9247
9248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9249 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9251 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9252
9253 // up when pressure becomes 0, hover restored
9254 processPressure(mapper, 0);
9255 processSync(mapper);
9256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9257 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9259 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9260
9261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9262 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9264 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9265
9266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9267 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9269 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9270
9271 // exit hover when pointer goes away
9272 processId(mapper, -1);
9273 processSync(mapper);
9274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9275 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9277 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9278}
9279
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009280/**
9281 * Set the input device port <--> display port associations, and check that the
9282 * events are routed to the display that matches the display port.
9283 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9284 */
9285TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009286 const std::string usb2 = "USB2";
9287 const uint8_t hdmi1 = 0;
9288 const uint8_t hdmi2 = 1;
9289 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009290 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009291
9292 addConfigurationProperty("touch.deviceType", "touchScreen");
9293 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009294 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009295
9296 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9297 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9298
9299 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9300 // for this input device is specified, and the matching viewport is not present,
9301 // the input device should be disabled (at the mapper level).
9302
9303 // Add viewport for display 2 on hdmi2
9304 prepareSecondaryDisplay(type, hdmi2);
9305 // Send a touch event
9306 processPosition(mapper, 100, 100);
9307 processSync(mapper);
9308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9309
9310 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009311 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009312 // Send a touch event again
9313 processPosition(mapper, 100, 100);
9314 processSync(mapper);
9315
9316 NotifyMotionArgs args;
9317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9318 ASSERT_EQ(DISPLAY_ID, args.displayId);
9319}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009320
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009321TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9322 addConfigurationProperty("touch.deviceType", "touchScreen");
9323 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009324 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009325
9326 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9327
Michael Wrighta9cf4192022-12-01 23:46:39 +00009328 prepareDisplay(ui::ROTATION_0);
9329 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009330
9331 // Send a touch event
9332 processPosition(mapper, 100, 100);
9333 processSync(mapper);
9334
9335 NotifyMotionArgs args;
9336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9337 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9338}
9339
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009340TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009341 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009342 std::shared_ptr<FakePointerController> fakePointerController =
9343 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009344 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009345 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009346 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009347
Garfield Tan888a6a42020-01-09 11:39:16 -08009348 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009349 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009350
Michael Wrighta9cf4192022-12-01 23:46:39 +00009351 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009352 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009353 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009354
Josep del Río2d8c79a2023-01-23 19:33:50 +00009355 // Check source is mouse that would obtain the PointerController.
9356 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009357
9358 NotifyMotionArgs motionArgs;
9359 processPosition(mapper, 100, 100);
9360 processSync(mapper);
9361
9362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9363 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9364 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9365}
9366
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009367/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009368 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9369 */
9370TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9371 addConfigurationProperty("touch.deviceType", "touchScreen");
9372 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009373 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009374
Michael Wrighta9cf4192022-12-01 23:46:39 +00009375 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009376 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9377 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9378 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9379 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009380
9381 NotifyMotionArgs args;
9382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9383 ASSERT_EQ(26, args.readTime);
9384
Harry Cutts33476232023-01-30 19:57:29 +00009385 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9386 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9387 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009388
9389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9390 ASSERT_EQ(33, args.readTime);
9391}
9392
9393/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009394 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9395 * events should not be delivered to the listener.
9396 */
9397TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9398 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009399 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009400 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009401 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009402 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009403 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009404 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009405
9406 NotifyMotionArgs motionArgs;
9407 processPosition(mapper, 100, 100);
9408 processSync(mapper);
9409
9410 mFakeListener->assertNotifyMotionWasNotCalled();
9411}
9412
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009413/**
9414 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9415 * the touch mapper can process the events and the events can be delivered to the listener.
9416 */
9417TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9418 addConfigurationProperty("touch.deviceType", "touchScreen");
9419 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009420 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009421 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009422 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009423 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009424 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009425
9426 NotifyMotionArgs motionArgs;
9427 processPosition(mapper, 100, 100);
9428 processSync(mapper);
9429
9430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9431 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9432}
9433
Garfield Tanc734e4f2021-01-15 20:01:39 -08009434TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9435 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009436 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009437 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009438 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009439 std::optional<DisplayViewport> optionalDisplayViewport =
9440 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9441 ASSERT_TRUE(optionalDisplayViewport.has_value());
9442 DisplayViewport displayViewport = *optionalDisplayViewport;
9443
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009444 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009445 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009446 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009447
9448 // Finger down
9449 int32_t x = 100, y = 100;
9450 processPosition(mapper, x, y);
9451 processSync(mapper);
9452
9453 NotifyMotionArgs motionArgs;
9454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9455 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9456
9457 // Deactivate display viewport
9458 displayViewport.isActive = false;
9459 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009460 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009461
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009462 // The ongoing touch should be canceled immediately
9463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9464 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9465
9466 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009467 x += 10, y += 10;
9468 processPosition(mapper, x, y);
9469 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009471
9472 // Reactivate display viewport
9473 displayViewport.isActive = true;
9474 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009475 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009476
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009477 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009478 x += 10, y += 10;
9479 processPosition(mapper, x, y);
9480 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9482 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009483}
9484
Arthur Hung7c645402019-01-25 17:45:42 +08009485TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9486 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009487 prepareAxes(POSITION | ID | SLOT);
9488 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009489 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009490
9491 // Create the second touch screen device, and enable multi fingers.
9492 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009493 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009494 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009495 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009496 std::shared_ptr<InputDevice> device2 =
9497 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009498 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009499
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009500 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009501 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009502 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009503 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009504 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009505 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009506 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009507 /*flat=*/0, /*fuzz=*/0);
9508 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009509 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9510 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009511
9512 // Setup the second touch screen device.
Arpit Singh56adebc2023-04-25 13:56:05 +00009513 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9514 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9515 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009516 std::list<NotifyArgs> unused =
9517 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009518 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009519 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009520
9521 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009522 std::shared_ptr<FakePointerController> fakePointerController =
9523 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009524 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009525
9526 // Setup policy for associated displays and show touches.
9527 const uint8_t hdmi1 = 0;
9528 const uint8_t hdmi2 = 1;
9529 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9530 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9531 mFakePolicy->setShowTouches(true);
9532
9533 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009534 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009535 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009536
9537 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009538 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009539 InputReaderConfiguration::Change::DISPLAY_INFO |
9540 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009541
9542 // Two fingers down at default display.
9543 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9544 processPosition(mapper, x1, y1);
9545 processId(mapper, 1);
9546 processSlot(mapper, 1);
9547 processPosition(mapper, x2, y2);
9548 processId(mapper, 2);
9549 processSync(mapper);
9550
9551 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9552 fakePointerController->getSpots().find(DISPLAY_ID);
9553 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9554 ASSERT_EQ(size_t(2), iter->second.size());
9555
9556 // Two fingers down at second display.
9557 processPosition(mapper2, x1, y1);
9558 processId(mapper2, 1);
9559 processSlot(mapper2, 1);
9560 processPosition(mapper2, x2, y2);
9561 processId(mapper2, 2);
9562 processSync(mapper2);
9563
9564 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9565 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9566 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009567
9568 // Disable the show touches configuration and ensure the spots are cleared.
9569 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009570 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009571 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009572
9573 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009574}
9575
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009576TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009577 prepareAxes(POSITION);
9578 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009579 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00009580 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009581
9582 NotifyMotionArgs motionArgs;
9583 // Unrotated video frame
9584 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9585 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009586 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009587 processPosition(mapper, 100, 200);
9588 processSync(mapper);
9589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9590 ASSERT_EQ(frames, motionArgs.videoFrames);
9591
9592 // Subsequent touch events should not have any videoframes
9593 // This is implemented separately in FakeEventHub,
9594 // but that should match the behaviour of TouchVideoDevice.
9595 processPosition(mapper, 200, 200);
9596 processSync(mapper);
9597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9598 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9599}
9600
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009601TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009602 prepareAxes(POSITION);
9603 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009604 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009605 // Unrotated video frame
9606 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9607 NotifyMotionArgs motionArgs;
9608
9609 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009610 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009611 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9612 clearViewports();
9613 prepareDisplay(orientation);
9614 std::vector<TouchVideoFrame> frames{frame};
9615 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9616 processPosition(mapper, 100, 200);
9617 processSync(mapper);
9618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9619 ASSERT_EQ(frames, motionArgs.videoFrames);
9620 }
9621}
9622
9623TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9624 prepareAxes(POSITION);
9625 addConfigurationProperty("touch.deviceType", "touchScreen");
9626 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9627 // orientation-aware are affected by display rotation.
9628 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00009629 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009630 // Unrotated video frame
9631 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9632 NotifyMotionArgs motionArgs;
9633
9634 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009635 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009636 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9637 clearViewports();
9638 prepareDisplay(orientation);
9639 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009640 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009641 processPosition(mapper, 100, 200);
9642 processSync(mapper);
9643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009644 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9645 // compared to the display. This is so that when the window transform (which contains the
9646 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9647 // window's coordinate space.
9648 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009649 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009650
9651 // Release finger.
9652 processSync(mapper);
9653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009654 }
9655}
9656
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009657TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009658 prepareAxes(POSITION);
9659 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009660 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009661 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9662 // so mix these.
9663 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9664 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9665 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9666 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9667 NotifyMotionArgs motionArgs;
9668
Michael Wrighta9cf4192022-12-01 23:46:39 +00009669 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009670 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009671 processPosition(mapper, 100, 200);
9672 processSync(mapper);
9673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009674 ASSERT_EQ(frames, motionArgs.videoFrames);
9675}
9676
9677TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9678 prepareAxes(POSITION);
9679 addConfigurationProperty("touch.deviceType", "touchScreen");
9680 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9681 // orientation-aware are affected by display rotation.
9682 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00009683 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009684 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9685 // so mix these.
9686 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9687 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9688 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9689 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9690 NotifyMotionArgs motionArgs;
9691
Michael Wrighta9cf4192022-12-01 23:46:39 +00009692 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009693 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9694 processPosition(mapper, 100, 200);
9695 processSync(mapper);
9696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9697 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9698 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9699 // compared to the display. This is so that when the window transform (which contains the
9700 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9701 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009702 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009703 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009704 ASSERT_EQ(frames, motionArgs.videoFrames);
9705}
9706
Arthur Hung9da14732019-09-02 16:16:58 +08009707/**
9708 * If we had defined port associations, but the viewport is not ready, the touch device would be
9709 * expected to be disabled, and it should be enabled after the viewport has found.
9710 */
9711TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009712 constexpr uint8_t hdmi2 = 1;
9713 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009714 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009715
9716 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9717
9718 addConfigurationProperty("touch.deviceType", "touchScreen");
9719 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009720 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009721
9722 ASSERT_EQ(mDevice->isEnabled(), false);
9723
9724 // Add display on hdmi2, the device should be enabled and can receive touch event.
9725 prepareSecondaryDisplay(type, hdmi2);
9726 ASSERT_EQ(mDevice->isEnabled(), true);
9727
9728 // Send a touch event.
9729 processPosition(mapper, 100, 100);
9730 processSync(mapper);
9731
9732 NotifyMotionArgs args;
9733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9734 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9735}
9736
Arthur Hung421eb1c2020-01-16 00:09:42 +08009737TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009738 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009739 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009740 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009741 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009742
9743 NotifyMotionArgs motionArgs;
9744
9745 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9746 // finger down
9747 processId(mapper, 1);
9748 processPosition(mapper, x1, y1);
9749 processSync(mapper);
9750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9751 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009752 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009753
9754 // finger move
9755 processId(mapper, 1);
9756 processPosition(mapper, x2, y2);
9757 processSync(mapper);
9758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009760 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009761
9762 // finger up.
9763 processId(mapper, -1);
9764 processSync(mapper);
9765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9766 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009767 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009768
9769 // new finger down
9770 processId(mapper, 1);
9771 processPosition(mapper, x3, y3);
9772 processSync(mapper);
9773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9774 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009775 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009776}
9777
9778/**
arthurhungcc7f9802020-04-30 17:55:40 +08009779 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9780 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009781 */
arthurhungcc7f9802020-04-30 17:55:40 +08009782TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009783 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009784 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009785 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009786 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009787
9788 NotifyMotionArgs motionArgs;
9789
9790 // default tool type is finger
9791 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009792 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009793 processPosition(mapper, x1, y1);
9794 processSync(mapper);
9795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9796 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009797 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009798
9799 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9800 processToolType(mapper, MT_TOOL_PALM);
9801 processSync(mapper);
9802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9803 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9804
9805 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009806 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009807 processPosition(mapper, x2, y2);
9808 processSync(mapper);
9809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9810
9811 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009812 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009813 processSync(mapper);
9814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9815
9816 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009817 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009818 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009819 processPosition(mapper, x3, y3);
9820 processSync(mapper);
9821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9822 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009823 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009824}
9825
arthurhungbf89a482020-04-17 17:37:55 +08009826/**
arthurhungcc7f9802020-04-30 17:55:40 +08009827 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9828 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009829 */
arthurhungcc7f9802020-04-30 17:55:40 +08009830TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009831 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009832 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009833 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009834 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009835
9836 NotifyMotionArgs motionArgs;
9837
9838 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009839 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9840 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009841 processPosition(mapper, x1, y1);
9842 processSync(mapper);
9843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9844 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009845 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009846
9847 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009848 processSlot(mapper, SECOND_SLOT);
9849 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009850 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009851 processSync(mapper);
9852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009853 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009854 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009855
9856 // If the tool type of the first finger changes to MT_TOOL_PALM,
9857 // we expect to receive ACTION_POINTER_UP with cancel flag.
9858 processSlot(mapper, FIRST_SLOT);
9859 processId(mapper, FIRST_TRACKING_ID);
9860 processToolType(mapper, MT_TOOL_PALM);
9861 processSync(mapper);
9862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009863 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009864 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9865
9866 // The following MOVE events of second finger should be processed.
9867 processSlot(mapper, SECOND_SLOT);
9868 processId(mapper, SECOND_TRACKING_ID);
9869 processPosition(mapper, x2 + 1, y2 + 1);
9870 processSync(mapper);
9871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009873 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009874
9875 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9876 // it. Second finger receive move.
9877 processSlot(mapper, FIRST_SLOT);
9878 processId(mapper, INVALID_TRACKING_ID);
9879 processSync(mapper);
9880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009882 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009883
9884 // Second finger keeps moving.
9885 processSlot(mapper, SECOND_SLOT);
9886 processId(mapper, SECOND_TRACKING_ID);
9887 processPosition(mapper, x2 + 2, y2 + 2);
9888 processSync(mapper);
9889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009891 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009892
9893 // Second finger up.
9894 processId(mapper, INVALID_TRACKING_ID);
9895 processSync(mapper);
9896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9897 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9898 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9899}
9900
9901/**
9902 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9903 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9904 */
9905TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9906 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009907 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009908 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009909 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009910
9911 NotifyMotionArgs motionArgs;
9912
9913 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9914 // First finger down.
9915 processId(mapper, FIRST_TRACKING_ID);
9916 processPosition(mapper, x1, y1);
9917 processSync(mapper);
9918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9919 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009920 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009921
9922 // Second finger down.
9923 processSlot(mapper, SECOND_SLOT);
9924 processId(mapper, SECOND_TRACKING_ID);
9925 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009926 processSync(mapper);
9927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009928 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009929 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009930
arthurhungcc7f9802020-04-30 17:55:40 +08009931 // If the tool type of the first finger changes to MT_TOOL_PALM,
9932 // we expect to receive ACTION_POINTER_UP with cancel flag.
9933 processSlot(mapper, FIRST_SLOT);
9934 processId(mapper, FIRST_TRACKING_ID);
9935 processToolType(mapper, MT_TOOL_PALM);
9936 processSync(mapper);
9937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009938 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009939 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9940
9941 // Second finger keeps moving.
9942 processSlot(mapper, SECOND_SLOT);
9943 processId(mapper, SECOND_TRACKING_ID);
9944 processPosition(mapper, x2 + 1, y2 + 1);
9945 processSync(mapper);
9946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9947 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9948
9949 // second finger becomes palm, receive cancel due to only 1 finger is active.
9950 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009951 processToolType(mapper, MT_TOOL_PALM);
9952 processSync(mapper);
9953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9954 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9955
arthurhungcc7f9802020-04-30 17:55:40 +08009956 // third finger down.
9957 processSlot(mapper, THIRD_SLOT);
9958 processId(mapper, THIRD_TRACKING_ID);
9959 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009960 processPosition(mapper, x3, y3);
9961 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9963 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009964 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009965 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009966
9967 // third finger move
9968 processId(mapper, THIRD_TRACKING_ID);
9969 processPosition(mapper, x3 + 1, y3 + 1);
9970 processSync(mapper);
9971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9973
9974 // first finger up, third finger receive move.
9975 processSlot(mapper, FIRST_SLOT);
9976 processId(mapper, INVALID_TRACKING_ID);
9977 processSync(mapper);
9978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9979 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009980 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009981
9982 // second finger up, third finger receive move.
9983 processSlot(mapper, SECOND_SLOT);
9984 processId(mapper, INVALID_TRACKING_ID);
9985 processSync(mapper);
9986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9987 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009988 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009989
9990 // third finger up.
9991 processSlot(mapper, THIRD_SLOT);
9992 processId(mapper, INVALID_TRACKING_ID);
9993 processSync(mapper);
9994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9995 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9996 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9997}
9998
9999/**
10000 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10001 * and the active finger could still be allowed to receive the events
10002 */
10003TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10004 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010005 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010006 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010007 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010008
10009 NotifyMotionArgs motionArgs;
10010
10011 // default tool type is finger
10012 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10013 processId(mapper, FIRST_TRACKING_ID);
10014 processPosition(mapper, x1, y1);
10015 processSync(mapper);
10016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10017 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010018 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010019
10020 // Second finger down.
10021 processSlot(mapper, SECOND_SLOT);
10022 processId(mapper, SECOND_TRACKING_ID);
10023 processPosition(mapper, x2, y2);
10024 processSync(mapper);
10025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010026 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010027 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010028
10029 // If the tool type of the second finger changes to MT_TOOL_PALM,
10030 // we expect to receive ACTION_POINTER_UP with cancel flag.
10031 processId(mapper, SECOND_TRACKING_ID);
10032 processToolType(mapper, MT_TOOL_PALM);
10033 processSync(mapper);
10034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010035 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010036 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10037
10038 // The following MOVE event should be processed.
10039 processSlot(mapper, FIRST_SLOT);
10040 processId(mapper, FIRST_TRACKING_ID);
10041 processPosition(mapper, x1 + 1, y1 + 1);
10042 processSync(mapper);
10043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010045 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010046
10047 // second finger up.
10048 processSlot(mapper, SECOND_SLOT);
10049 processId(mapper, INVALID_TRACKING_ID);
10050 processSync(mapper);
10051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10053
10054 // first finger keep moving
10055 processSlot(mapper, FIRST_SLOT);
10056 processId(mapper, FIRST_TRACKING_ID);
10057 processPosition(mapper, x1 + 2, y1 + 2);
10058 processSync(mapper);
10059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10061
10062 // first finger up.
10063 processId(mapper, INVALID_TRACKING_ID);
10064 processSync(mapper);
10065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10066 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10067 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010068}
10069
Arthur Hung9ad18942021-06-19 02:04:46 +000010070/**
10071 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10072 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10073 * cause slot be valid again.
10074 */
10075TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10076 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010077 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010078 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010079 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010080
10081 NotifyMotionArgs motionArgs;
10082
10083 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10084 // First finger down.
10085 processId(mapper, FIRST_TRACKING_ID);
10086 processPosition(mapper, x1, y1);
10087 processPressure(mapper, RAW_PRESSURE_MAX);
10088 processSync(mapper);
10089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10090 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010091 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010092
10093 // First finger move.
10094 processId(mapper, FIRST_TRACKING_ID);
10095 processPosition(mapper, x1 + 1, y1 + 1);
10096 processPressure(mapper, RAW_PRESSURE_MAX);
10097 processSync(mapper);
10098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010100 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010101
10102 // Second finger down.
10103 processSlot(mapper, SECOND_SLOT);
10104 processId(mapper, SECOND_TRACKING_ID);
10105 processPosition(mapper, x2, y2);
10106 processPressure(mapper, RAW_PRESSURE_MAX);
10107 processSync(mapper);
10108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010109 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010110 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010111
10112 // second finger up with some unexpected data.
10113 processSlot(mapper, SECOND_SLOT);
10114 processId(mapper, INVALID_TRACKING_ID);
10115 processPosition(mapper, x2, y2);
10116 processSync(mapper);
10117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010118 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010119 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010120
10121 // first finger up with some unexpected data.
10122 processSlot(mapper, FIRST_SLOT);
10123 processId(mapper, INVALID_TRACKING_ID);
10124 processPosition(mapper, x2, y2);
10125 processPressure(mapper, RAW_PRESSURE_MAX);
10126 processSync(mapper);
10127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10128 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010129 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010130}
10131
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010132TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10133 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010134 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010135 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010136 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010137
10138 // First finger down.
10139 processId(mapper, FIRST_TRACKING_ID);
10140 processPosition(mapper, 100, 200);
10141 processPressure(mapper, RAW_PRESSURE_MAX);
10142 processSync(mapper);
10143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10144 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10145
10146 // Second finger down.
10147 processSlot(mapper, SECOND_SLOT);
10148 processId(mapper, SECOND_TRACKING_ID);
10149 processPosition(mapper, 300, 400);
10150 processPressure(mapper, RAW_PRESSURE_MAX);
10151 processSync(mapper);
10152 ASSERT_NO_FATAL_FAILURE(
10153 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10154
10155 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010156 // preserved. Resetting should cancel the ongoing gesture.
10157 resetMapper(mapper, ARBITRARY_TIME);
10158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10159 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010160
10161 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10162 // the existing touch state to generate a down event.
10163 processPosition(mapper, 301, 302);
10164 processSync(mapper);
10165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10166 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10168 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10169
10170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10171}
10172
10173TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10174 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010175 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010176 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010177 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010178
10179 // First finger touches down and releases.
10180 processId(mapper, FIRST_TRACKING_ID);
10181 processPosition(mapper, 100, 200);
10182 processPressure(mapper, RAW_PRESSURE_MAX);
10183 processSync(mapper);
10184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10185 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10186 processId(mapper, INVALID_TRACKING_ID);
10187 processSync(mapper);
10188 ASSERT_NO_FATAL_FAILURE(
10189 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10190
10191 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10192 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010193 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10195
10196 // Send an empty sync frame. Since there are no pointers, no events are generated.
10197 processSync(mapper);
10198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10199}
10200
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010201TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010202 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010203 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010204 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010205 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010207
10208 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10209 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10210 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10211 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10212 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10213
10214 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010215 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010216 processId(mapper, FIRST_TRACKING_ID);
10217 processToolType(mapper, MT_TOOL_PEN);
10218 processPosition(mapper, 100, 200);
10219 processPressure(mapper, RAW_PRESSURE_MAX);
10220 processSync(mapper);
10221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10222 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10223 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010224 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010225
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010226 // Now that we know the device supports styluses, ensure that the device is re-configured with
10227 // the stylus source.
10228 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10229 {
10230 const auto& devices = mReader->getInputDevices();
10231 auto deviceInfo =
10232 std::find_if(devices.begin(), devices.end(),
10233 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10234 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10235 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10236 }
10237
10238 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10240
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010241 processId(mapper, INVALID_TRACKING_ID);
10242 processSync(mapper);
10243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10244 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10245 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010246 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010247}
10248
Seunghwan Choi356026c2023-02-01 14:37:25 +090010249TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10250 addConfigurationProperty("touch.deviceType", "touchScreen");
10251 prepareDisplay(ui::ROTATION_0);
10252 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10253 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10254 // indicate stylus presence dynamically.
10255 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10256 std::shared_ptr<FakePointerController> fakePointerController =
10257 std::make_shared<FakePointerController>();
10258 mFakePolicy->setPointerController(fakePointerController);
10259 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singh56adebc2023-04-25 13:56:05 +000010260 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010261
10262 processId(mapper, FIRST_TRACKING_ID);
10263 processPressure(mapper, RAW_PRESSURE_MIN);
10264 processPosition(mapper, 100, 200);
10265 processToolType(mapper, MT_TOOL_PEN);
10266 processSync(mapper);
10267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10268 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010269 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010270 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10271 ASSERT_TRUE(fakePointerController->isPointerShown());
10272 ASSERT_NO_FATAL_FAILURE(
10273 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10274}
10275
10276TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10277 addConfigurationProperty("touch.deviceType", "touchScreen");
10278 prepareDisplay(ui::ROTATION_0);
10279 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10280 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10281 // indicate stylus presence dynamically.
10282 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10283 std::shared_ptr<FakePointerController> fakePointerController =
10284 std::make_shared<FakePointerController>();
10285 mFakePolicy->setPointerController(fakePointerController);
10286 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singh56adebc2023-04-25 13:56:05 +000010287 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010288
10289 processId(mapper, FIRST_TRACKING_ID);
10290 processPressure(mapper, RAW_PRESSURE_MIN);
10291 processPosition(mapper, 100, 200);
10292 processToolType(mapper, MT_TOOL_PEN);
10293 processSync(mapper);
10294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10295 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010296 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010297 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10298 ASSERT_FALSE(fakePointerController->isPointerShown());
10299}
10300
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010301// --- MultiTouchInputMapperTest_ExternalDevice ---
10302
10303class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10304protected:
Chris Yea52ade12020-08-27 16:49:20 -070010305 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010306};
10307
10308/**
10309 * Expect fallback to internal viewport if device is external and external viewport is not present.
10310 */
10311TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10312 prepareAxes(POSITION);
10313 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010314 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +000010315 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010316
10317 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10318
10319 NotifyMotionArgs motionArgs;
10320
10321 // Expect the event to be sent to the internal viewport,
10322 // because an external viewport is not present.
10323 processPosition(mapper, 100, 100);
10324 processSync(mapper);
10325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10326 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10327
10328 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010329 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010330 processPosition(mapper, 100, 100);
10331 processSync(mapper);
10332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10333 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10334}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010335
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010336TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10337 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10338 std::shared_ptr<FakePointerController> fakePointerController =
10339 std::make_shared<FakePointerController>();
10340 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10341 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010342
10343 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010344 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010345 prepareAxes(POSITION | ID | SLOT);
10346 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10347 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10348 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010349 mFakePolicy->setPointerController(fakePointerController);
Arpit Singh56adebc2023-04-25 13:56:05 +000010350 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010351
10352 // captured touchpad should be a touchpad source
10353 NotifyDeviceResetArgs resetArgs;
10354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10355 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10356
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010357 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010358
10359 const InputDeviceInfo::MotionRange* relRangeX =
10360 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10361 ASSERT_NE(relRangeX, nullptr);
10362 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10363 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10364 const InputDeviceInfo::MotionRange* relRangeY =
10365 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10366 ASSERT_NE(relRangeY, nullptr);
10367 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10368 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10369
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010370 // run captured pointer tests - note that this is unscaled, so input listener events should be
10371 // identical to what the hardware sends (accounting for any
10372 // calibration).
10373 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010374 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010375 processId(mapper, 1);
10376 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10377 processKey(mapper, BTN_TOUCH, 1);
10378 processSync(mapper);
10379
10380 // expect coord[0] to contain initial location of touch 0
10381 NotifyMotionArgs args;
10382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10383 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010384 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010385 ASSERT_EQ(0, args.pointerProperties[0].id);
10386 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10387 ASSERT_NO_FATAL_FAILURE(
10388 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10389
10390 // FINGER 1 DOWN
10391 processSlot(mapper, 1);
10392 processId(mapper, 2);
10393 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10394 processSync(mapper);
10395
10396 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010398 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010399 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010400 ASSERT_EQ(0, args.pointerProperties[0].id);
10401 ASSERT_EQ(1, args.pointerProperties[1].id);
10402 ASSERT_NO_FATAL_FAILURE(
10403 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10404 ASSERT_NO_FATAL_FAILURE(
10405 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10406
10407 // FINGER 1 MOVE
10408 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10409 processSync(mapper);
10410
10411 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10412 // from move
10413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10415 ASSERT_NO_FATAL_FAILURE(
10416 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10417 ASSERT_NO_FATAL_FAILURE(
10418 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10419
10420 // FINGER 0 MOVE
10421 processSlot(mapper, 0);
10422 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10423 processSync(mapper);
10424
10425 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10428 ASSERT_NO_FATAL_FAILURE(
10429 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10430 ASSERT_NO_FATAL_FAILURE(
10431 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10432
10433 // BUTTON DOWN
10434 processKey(mapper, BTN_LEFT, 1);
10435 processSync(mapper);
10436
10437 // touchinputmapper design sends a move before button press
10438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10441 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10442
10443 // BUTTON UP
10444 processKey(mapper, BTN_LEFT, 0);
10445 processSync(mapper);
10446
10447 // touchinputmapper design sends a move after button release
10448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10449 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10452
10453 // FINGER 0 UP
10454 processId(mapper, -1);
10455 processSync(mapper);
10456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10457 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10458
10459 // FINGER 1 MOVE
10460 processSlot(mapper, 1);
10461 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10462 processSync(mapper);
10463
10464 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10466 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010467 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010468 ASSERT_EQ(1, args.pointerProperties[0].id);
10469 ASSERT_NO_FATAL_FAILURE(
10470 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10471
10472 // FINGER 1 UP
10473 processId(mapper, -1);
10474 processKey(mapper, BTN_TOUCH, 0);
10475 processSync(mapper);
10476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10477 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10478
Josep del Río2d8c79a2023-01-23 19:33:50 +000010479 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010480 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010481 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010483 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010484}
10485
10486TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10487 std::shared_ptr<FakePointerController> fakePointerController =
10488 std::make_shared<FakePointerController>();
10489 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10490 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010491
10492 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010493 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010494 prepareAxes(POSITION | ID | SLOT);
10495 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10496 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010497 mFakePolicy->setPointerController(fakePointerController);
Arpit Singh56adebc2023-04-25 13:56:05 +000010498 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010499 // run uncaptured pointer tests - pushes out generic events
10500 // FINGER 0 DOWN
10501 processId(mapper, 3);
10502 processPosition(mapper, 100, 100);
10503 processKey(mapper, BTN_TOUCH, 1);
10504 processSync(mapper);
10505
10506 // start at (100,100), cursor should be at (0,0) * scale
10507 NotifyMotionArgs args;
10508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10509 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10510 ASSERT_NO_FATAL_FAILURE(
10511 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10512
10513 // FINGER 0 MOVE
10514 processPosition(mapper, 200, 200);
10515 processSync(mapper);
10516
10517 // compute scaling to help with touch position checking
10518 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10519 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10520 float scale =
10521 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10522
10523 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10525 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10527 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010528
10529 // BUTTON DOWN
10530 processKey(mapper, BTN_LEFT, 1);
10531 processSync(mapper);
10532
10533 // touchinputmapper design sends a move before button press
10534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10535 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10537 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10538
10539 // BUTTON UP
10540 processKey(mapper, BTN_LEFT, 0);
10541 processSync(mapper);
10542
10543 // touchinputmapper design sends a move after button release
10544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10545 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10547 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010548}
10549
10550TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10551 std::shared_ptr<FakePointerController> fakePointerController =
10552 std::make_shared<FakePointerController>();
10553
Michael Wrighta9cf4192022-12-01 23:46:39 +000010554 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010555 prepareAxes(POSITION | ID | SLOT);
10556 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010557 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010558 mFakePolicy->setPointerCapture(false);
Arpit Singh56adebc2023-04-25 13:56:05 +000010559 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010560
Josep del Río2d8c79a2023-01-23 19:33:50 +000010561 // uncaptured touchpad should be a pointer device
10562 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010563
Josep del Río2d8c79a2023-01-23 19:33:50 +000010564 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010565 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010566 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010567 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10568}
10569
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010570// --- BluetoothMultiTouchInputMapperTest ---
10571
10572class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10573protected:
10574 void SetUp() override {
10575 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10576 }
10577};
10578
10579TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10580 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010581 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010582 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010583 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010584
10585 nsecs_t kernelEventTime = ARBITRARY_TIME;
10586 nsecs_t expectedEventTime = ARBITRARY_TIME;
10587 // Touch down.
10588 processId(mapper, FIRST_TRACKING_ID);
10589 processPosition(mapper, 100, 200);
10590 processPressure(mapper, RAW_PRESSURE_MAX);
10591 processSync(mapper, ARBITRARY_TIME);
10592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10593 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10594
10595 // Process several events that come in quick succession, according to their timestamps.
10596 for (int i = 0; i < 3; i++) {
10597 constexpr static nsecs_t delta = ms2ns(1);
10598 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10599 kernelEventTime += delta;
10600 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10601
10602 processPosition(mapper, 101 + i, 201 + i);
10603 processSync(mapper, kernelEventTime);
10604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10605 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10606 WithEventTime(expectedEventTime))));
10607 }
10608
10609 // Release the touch.
10610 processId(mapper, INVALID_TRACKING_ID);
10611 processPressure(mapper, RAW_PRESSURE_MIN);
10612 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10614 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10615 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10616}
10617
10618// --- MultiTouchPointerModeTest ---
10619
HQ Liue6983c72022-04-19 22:14:56 +000010620class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10621protected:
10622 float mPointerMovementScale;
10623 float mPointerXZoomScale;
10624 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10625 addConfigurationProperty("touch.deviceType", "pointer");
10626 std::shared_ptr<FakePointerController> fakePointerController =
10627 std::make_shared<FakePointerController>();
10628 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10629 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010630 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010631
10632 prepareAxes(POSITION);
10633 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10634 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10635 // needs to be disabled, and the pointer gesture needs to be enabled.
10636 mFakePolicy->setPointerCapture(false);
10637 mFakePolicy->setPointerGestureEnabled(true);
10638 mFakePolicy->setPointerController(fakePointerController);
10639
10640 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10641 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10642 mPointerMovementScale =
10643 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10644 mPointerXZoomScale =
10645 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10646 }
10647
10648 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10649 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10650 /*flat*/ 0,
10651 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10652 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10653 /*flat*/ 0,
10654 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10655 }
10656};
10657
10658/**
10659 * Two fingers down on a pointer mode touch pad. The width
10660 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10661 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10662 * be greater than the both value to be freeform gesture, so that after two
10663 * fingers start to move downwards, the gesture should be swipe.
10664 */
10665TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10666 // The min freeform gesture width is 25units/mm x 30mm = 750
10667 // which is greater than fraction of the diagnal length of the touchpad (349).
10668 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010669 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010670 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010671 NotifyMotionArgs motionArgs;
10672
10673 // Two fingers down at once.
10674 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10675 // Pointer's initial position is used the [0,0] coordinate.
10676 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10677
10678 processId(mapper, FIRST_TRACKING_ID);
10679 processPosition(mapper, x1, y1);
10680 processMTSync(mapper);
10681 processId(mapper, SECOND_TRACKING_ID);
10682 processPosition(mapper, x2, y2);
10683 processMTSync(mapper);
10684 processSync(mapper);
10685
10686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010687 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010688 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010689 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010690 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010691 ASSERT_NO_FATAL_FAILURE(
10692 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10693
10694 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10695 // that there should be 1 pointer.
10696 int32_t movingDistance = 200;
10697 y1 += movingDistance;
10698 y2 += movingDistance;
10699
10700 processId(mapper, FIRST_TRACKING_ID);
10701 processPosition(mapper, x1, y1);
10702 processMTSync(mapper);
10703 processId(mapper, SECOND_TRACKING_ID);
10704 processPosition(mapper, x2, y2);
10705 processMTSync(mapper);
10706 processSync(mapper);
10707
10708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010709 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010711 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010712 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10714 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10715 0, 0, 0, 0));
10716}
10717
10718/**
10719 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10720 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10721 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10722 * value to be freeform gesture, so that after two fingers start to move downwards,
10723 * the gesture should be swipe.
10724 */
10725TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10726 // The min freeform gesture width is 5units/mm x 30mm = 150
10727 // which is greater than fraction of the diagnal length of the touchpad (349).
10728 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010729 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singh56adebc2023-04-25 13:56:05 +000010730 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010731 NotifyMotionArgs motionArgs;
10732
10733 // Two fingers down at once.
10734 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10735 // Pointer's initial position is used the [0,0] coordinate.
10736 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10737
10738 processId(mapper, FIRST_TRACKING_ID);
10739 processPosition(mapper, x1, y1);
10740 processMTSync(mapper);
10741 processId(mapper, SECOND_TRACKING_ID);
10742 processPosition(mapper, x2, y2);
10743 processMTSync(mapper);
10744 processSync(mapper);
10745
10746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010747 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010748 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010749 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010750 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010751 ASSERT_NO_FATAL_FAILURE(
10752 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10753
10754 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10755 // and there should be 1 pointer.
10756 int32_t movingDistance = 200;
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));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010769 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010770 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010771 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010772 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010773 // New coordinate is the scaled relative coordinate from the initial coordinate.
10774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10775 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10776 0, 0, 0, 0));
10777}
10778
10779/**
10780 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10781 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10782 * freeform gestures after two fingers start to move downwards.
10783 */
10784TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010785 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010786 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010787
10788 NotifyMotionArgs motionArgs;
10789
10790 // Two fingers down at once. Wider than the max swipe width.
10791 // The gesture is expected to be PRESS, then transformed to FREEFORM
10792 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10793
10794 processId(mapper, FIRST_TRACKING_ID);
10795 processPosition(mapper, x1, y1);
10796 processMTSync(mapper);
10797 processId(mapper, SECOND_TRACKING_ID);
10798 processPosition(mapper, x2, y2);
10799 processMTSync(mapper);
10800 processSync(mapper);
10801
10802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010803 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010804 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010805 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010806 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010807 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10808 ASSERT_NO_FATAL_FAILURE(
10809 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10810
10811 int32_t movingDistance = 200;
10812
10813 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10814 // then two down events for two pointers.
10815 y1 += movingDistance;
10816 y2 += movingDistance;
10817
10818 processId(mapper, FIRST_TRACKING_ID);
10819 processPosition(mapper, x1, y1);
10820 processMTSync(mapper);
10821 processId(mapper, SECOND_TRACKING_ID);
10822 processPosition(mapper, x2, y2);
10823 processMTSync(mapper);
10824 processSync(mapper);
10825
10826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10827 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010828 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010829 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010831 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010832 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010833 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010835 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010836 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010837 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010838 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010839 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010840 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010841 // Two pointers' scaled relative coordinates from their initial centroid.
10842 // Initial y coordinates are 0 as y1 and y2 have the same value.
10843 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10844 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10845 // When pointers move, the new coordinates equal to the initial coordinates plus
10846 // scaled moving distance.
10847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10848 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10849 0, 0, 0, 0));
10850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10851 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10852 0, 0, 0, 0));
10853
10854 // Move two fingers down again, expect one MOVE motion event.
10855 y1 += movingDistance;
10856 y2 += movingDistance;
10857
10858 processId(mapper, FIRST_TRACKING_ID);
10859 processPosition(mapper, x1, y1);
10860 processMTSync(mapper);
10861 processId(mapper, SECOND_TRACKING_ID);
10862 processPosition(mapper, x2, y2);
10863 processMTSync(mapper);
10864 processSync(mapper);
10865
10866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010867 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010869 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010870 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10872 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10873 0, 0, 0, 0, 0));
10874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10875 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10876 0, 0, 0, 0, 0));
10877}
10878
Harry Cutts39b7ca22022-10-05 15:55:48 +000010879TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010880 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010881 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010882 NotifyMotionArgs motionArgs;
10883
10884 // Place two fingers down.
10885 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10886
10887 processId(mapper, FIRST_TRACKING_ID);
10888 processPosition(mapper, x1, y1);
10889 processMTSync(mapper);
10890 processId(mapper, SECOND_TRACKING_ID);
10891 processPosition(mapper, x2, y2);
10892 processMTSync(mapper);
10893 processSync(mapper);
10894
10895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010896 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010897 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10898 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10899 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10900 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10901
10902 // Move the two fingers down and to the left.
10903 int32_t movingDistance = 200;
10904 x1 -= movingDistance;
10905 y1 += movingDistance;
10906 x2 -= movingDistance;
10907 y2 += movingDistance;
10908
10909 processId(mapper, FIRST_TRACKING_ID);
10910 processPosition(mapper, x1, y1);
10911 processMTSync(mapper);
10912 processId(mapper, SECOND_TRACKING_ID);
10913 processPosition(mapper, x2, y2);
10914 processMTSync(mapper);
10915 processSync(mapper);
10916
10917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010918 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10920 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10921 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10922 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10923}
10924
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010925TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010926 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010927 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +000010928 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10930
10931 // Start a stylus gesture.
10932 processKey(mapper, BTN_TOOL_PEN, 1);
10933 processId(mapper, FIRST_TRACKING_ID);
10934 processPosition(mapper, 100, 200);
10935 processSync(mapper);
10936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10937 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10938 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010939 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010940 // TODO(b/257078296): Pointer mode generates extra event.
10941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10942 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10943 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010944 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10946
10947 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10948 // gesture should be disabled.
10949 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10950 viewport->isActive = false;
10951 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010952 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10954 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10955 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010956 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010957 // TODO(b/257078296): Pointer mode generates extra event.
10958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10959 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10960 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010961 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10963}
10964
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010965// --- JoystickInputMapperTest ---
10966
10967class JoystickInputMapperTest : public InputMapperTest {
10968protected:
10969 static const int32_t RAW_X_MIN;
10970 static const int32_t RAW_X_MAX;
10971 static const int32_t RAW_Y_MIN;
10972 static const int32_t RAW_Y_MAX;
10973
10974 void SetUp() override {
10975 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10976 }
10977 void prepareAxes() {
10978 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10979 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10980 }
10981
10982 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10984 }
10985
10986 void processSync(JoystickInputMapper& mapper) {
10987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10988 }
10989
Michael Wrighta9cf4192022-12-01 23:46:39 +000010990 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010991 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10992 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10993 NO_PORT, ViewportType::VIRTUAL);
10994 }
10995};
10996
10997const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10998const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10999const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11000const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11001
11002TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11003 prepareAxes();
Arpit Singh2be4a362023-04-26 14:16:50 +000011004 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011005
11006 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11007
Michael Wrighta9cf4192022-12-01 23:46:39 +000011008 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011009
11010 // Send an axis event
11011 processAxis(mapper, ABS_X, 100);
11012 processSync(mapper);
11013
11014 NotifyMotionArgs args;
11015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11016 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11017
11018 // Send another axis event
11019 processAxis(mapper, ABS_Y, 100);
11020 processSync(mapper);
11021
11022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11023 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11024}
11025
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011026// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011027
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011028class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011029protected:
11030 static const char* DEVICE_NAME;
11031 static const char* DEVICE_LOCATION;
11032 static const int32_t DEVICE_ID;
11033 static const int32_t DEVICE_GENERATION;
11034 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011035 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011036 static const int32_t EVENTHUB_ID;
11037
11038 std::shared_ptr<FakeEventHub> mFakeEventHub;
11039 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011040 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011041 std::unique_ptr<InstrumentedInputReader> mReader;
11042 std::shared_ptr<InputDevice> mDevice;
11043
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011044 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011045 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011046 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011047 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011048 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011049 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011050 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11051 }
11052
11053 void SetUp() override { SetUp(DEVICE_CLASSES); }
11054
11055 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011056 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011057 mFakePolicy.clear();
11058 }
11059
Chris Yee2b1e5c2021-03-10 22:45:12 -080011060 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11061 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011062 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011063 InputDeviceIdentifier identifier;
11064 identifier.name = name;
11065 identifier.location = location;
11066 std::shared_ptr<InputDevice> device =
11067 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11068 identifier);
11069 mReader->pushNextDevice(device);
11070 mFakeEventHub->addDevice(eventHubId, name, classes);
11071 mReader->loopOnce();
11072 return device;
11073 }
11074
11075 template <class T, typename... Args>
11076 T& addControllerAndConfigure(Args... args) {
11077 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11078
11079 return controller;
11080 }
11081};
11082
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011083const char* PeripheralControllerTest::DEVICE_NAME = "device";
11084const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11085const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11086const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11087const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011088const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11089 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011090const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011091
11092// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011093class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011094protected:
11095 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011096 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011097 }
11098};
11099
11100TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011101 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011102
Harry Cuttsa5b71292022-11-28 12:56:17 +000011103 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11104 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11105 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011106}
11107
11108TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011109 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011110
Harry Cuttsa5b71292022-11-28 12:56:17 +000011111 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11112 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11113 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011114}
11115
11116// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011117class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011118protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011119 void SetUp() override {
11120 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11121 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011122};
11123
Chris Ye85758332021-05-16 23:05:17 -070011124TEST_F(LightControllerTest, MonoLight) {
11125 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011126 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011127 .maxBrightness = 255,
11128 .flags = InputLightClass::BRIGHTNESS,
11129 .path = ""};
11130 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011131
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011132 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011133 InputDeviceInfo info;
11134 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011135 std::vector<InputDeviceLightInfo> lights = info.getLights();
11136 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011137 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11138 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11139
11140 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11141 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11142}
11143
11144TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11145 RawLightInfo infoMono = {.id = 1,
11146 .name = "mono_keyboard_backlight",
11147 .maxBrightness = 255,
11148 .flags = InputLightClass::BRIGHTNESS |
11149 InputLightClass::KEYBOARD_BACKLIGHT,
11150 .path = ""};
11151 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11152
11153 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11154 InputDeviceInfo info;
11155 controller.populateDeviceInfo(&info);
11156 std::vector<InputDeviceLightInfo> lights = info.getLights();
11157 ASSERT_EQ(1U, lights.size());
11158 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11159 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011160
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011161 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11162 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011163}
11164
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011165TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11166 RawLightInfo infoMono = {.id = 1,
11167 .name = "mono_light",
11168 .maxBrightness = 255,
11169 .flags = InputLightClass::BRIGHTNESS,
11170 .path = ""};
11171 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11172 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11173 "0,100,200");
11174
11175 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11176 std::list<NotifyArgs> unused =
11177 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11178 /*changes=*/{});
11179
11180 InputDeviceInfo info;
11181 controller.populateDeviceInfo(&info);
11182 std::vector<InputDeviceLightInfo> lights = info.getLights();
11183 ASSERT_EQ(1U, lights.size());
11184 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11185}
11186
11187TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11188 RawLightInfo infoMono = {.id = 1,
11189 .name = "mono_keyboard_backlight",
11190 .maxBrightness = 255,
11191 .flags = InputLightClass::BRIGHTNESS |
11192 InputLightClass::KEYBOARD_BACKLIGHT,
11193 .path = ""};
11194 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11195
11196 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11197 std::list<NotifyArgs> unused =
11198 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11199 /*changes=*/{});
11200
11201 InputDeviceInfo info;
11202 controller.populateDeviceInfo(&info);
11203 std::vector<InputDeviceLightInfo> lights = info.getLights();
11204 ASSERT_EQ(1U, lights.size());
11205 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11206}
11207
11208TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11209 RawLightInfo infoMono = {.id = 1,
11210 .name = "mono_keyboard_backlight",
11211 .maxBrightness = 255,
11212 .flags = InputLightClass::BRIGHTNESS |
11213 InputLightClass::KEYBOARD_BACKLIGHT,
11214 .path = ""};
11215 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11216 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11217 "0,100,200");
11218
11219 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11220 std::list<NotifyArgs> unused =
11221 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11222 /*changes=*/{});
11223
11224 InputDeviceInfo info;
11225 controller.populateDeviceInfo(&info);
11226 std::vector<InputDeviceLightInfo> lights = info.getLights();
11227 ASSERT_EQ(1U, lights.size());
11228 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
11229 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
11230 ASSERT_EQ(BrightnessLevel(0), *it);
11231 std::advance(it, 1);
11232 ASSERT_EQ(BrightnessLevel(100), *it);
11233 std::advance(it, 1);
11234 ASSERT_EQ(BrightnessLevel(200), *it);
11235}
11236
11237TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
11238 RawLightInfo infoMono = {.id = 1,
11239 .name = "mono_keyboard_backlight",
11240 .maxBrightness = 255,
11241 .flags = InputLightClass::BRIGHTNESS |
11242 InputLightClass::KEYBOARD_BACKLIGHT,
11243 .path = ""};
11244 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11245 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11246 "0,100,200,300,400,500");
11247
11248 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11249 std::list<NotifyArgs> unused =
11250 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11251 /*changes=*/{});
11252
11253 InputDeviceInfo info;
11254 controller.populateDeviceInfo(&info);
11255 std::vector<InputDeviceLightInfo> lights = info.getLights();
11256 ASSERT_EQ(1U, lights.size());
11257 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11258}
11259
Chris Yee2b1e5c2021-03-10 22:45:12 -080011260TEST_F(LightControllerTest, RGBLight) {
11261 RawLightInfo infoRed = {.id = 1,
11262 .name = "red",
11263 .maxBrightness = 255,
11264 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11265 .path = ""};
11266 RawLightInfo infoGreen = {.id = 2,
11267 .name = "green",
11268 .maxBrightness = 255,
11269 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11270 .path = ""};
11271 RawLightInfo infoBlue = {.id = 3,
11272 .name = "blue",
11273 .maxBrightness = 255,
11274 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11275 .path = ""};
11276 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11277 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11278 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11279
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011280 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011281 InputDeviceInfo info;
11282 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011283 std::vector<InputDeviceLightInfo> lights = info.getLights();
11284 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011285 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11286 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11287 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11288
11289 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11290 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11291}
11292
11293TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11294 RawLightInfo infoRed = {.id = 1,
11295 .name = "red_keyboard_backlight",
11296 .maxBrightness = 255,
11297 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11298 InputLightClass::KEYBOARD_BACKLIGHT,
11299 .path = ""};
11300 RawLightInfo infoGreen = {.id = 2,
11301 .name = "green_keyboard_backlight",
11302 .maxBrightness = 255,
11303 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11304 InputLightClass::KEYBOARD_BACKLIGHT,
11305 .path = ""};
11306 RawLightInfo infoBlue = {.id = 3,
11307 .name = "blue_keyboard_backlight",
11308 .maxBrightness = 255,
11309 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11310 InputLightClass::KEYBOARD_BACKLIGHT,
11311 .path = ""};
11312 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11313 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11314 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11315
11316 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11317 InputDeviceInfo info;
11318 controller.populateDeviceInfo(&info);
11319 std::vector<InputDeviceLightInfo> lights = info.getLights();
11320 ASSERT_EQ(1U, lights.size());
11321 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11322 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11323 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11324
11325 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11326 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11327}
11328
11329TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11330 RawLightInfo infoRed = {.id = 1,
11331 .name = "red",
11332 .maxBrightness = 255,
11333 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11334 .path = ""};
11335 RawLightInfo infoGreen = {.id = 2,
11336 .name = "green",
11337 .maxBrightness = 255,
11338 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11339 .path = ""};
11340 RawLightInfo infoBlue = {.id = 3,
11341 .name = "blue",
11342 .maxBrightness = 255,
11343 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11344 .path = ""};
11345 RawLightInfo infoGlobal = {.id = 3,
11346 .name = "global_keyboard_backlight",
11347 .maxBrightness = 255,
11348 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11349 InputLightClass::KEYBOARD_BACKLIGHT,
11350 .path = ""};
11351 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11352 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11353 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11354 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11355
11356 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11357 InputDeviceInfo info;
11358 controller.populateDeviceInfo(&info);
11359 std::vector<InputDeviceLightInfo> lights = info.getLights();
11360 ASSERT_EQ(1U, lights.size());
11361 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11362 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11363 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011364
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011365 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11366 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011367}
11368
11369TEST_F(LightControllerTest, MultiColorRGBLight) {
11370 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011371 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011372 .maxBrightness = 255,
11373 .flags = InputLightClass::BRIGHTNESS |
11374 InputLightClass::MULTI_INTENSITY |
11375 InputLightClass::MULTI_INDEX,
11376 .path = ""};
11377
11378 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11379
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011380 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011381 InputDeviceInfo info;
11382 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011383 std::vector<InputDeviceLightInfo> lights = info.getLights();
11384 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011385 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11386 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11387 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11388
11389 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11390 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11391}
11392
11393TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11394 RawLightInfo infoColor = {.id = 1,
11395 .name = "multi_color_keyboard_backlight",
11396 .maxBrightness = 255,
11397 .flags = InputLightClass::BRIGHTNESS |
11398 InputLightClass::MULTI_INTENSITY |
11399 InputLightClass::MULTI_INDEX |
11400 InputLightClass::KEYBOARD_BACKLIGHT,
11401 .path = ""};
11402
11403 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11404
11405 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11406 InputDeviceInfo info;
11407 controller.populateDeviceInfo(&info);
11408 std::vector<InputDeviceLightInfo> lights = info.getLights();
11409 ASSERT_EQ(1U, lights.size());
11410 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11411 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11412 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011413
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011414 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11415 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011416}
11417
11418TEST_F(LightControllerTest, PlayerIdLight) {
11419 RawLightInfo info1 = {.id = 1,
11420 .name = "player1",
11421 .maxBrightness = 255,
11422 .flags = InputLightClass::BRIGHTNESS,
11423 .path = ""};
11424 RawLightInfo info2 = {.id = 2,
11425 .name = "player2",
11426 .maxBrightness = 255,
11427 .flags = InputLightClass::BRIGHTNESS,
11428 .path = ""};
11429 RawLightInfo info3 = {.id = 3,
11430 .name = "player3",
11431 .maxBrightness = 255,
11432 .flags = InputLightClass::BRIGHTNESS,
11433 .path = ""};
11434 RawLightInfo info4 = {.id = 4,
11435 .name = "player4",
11436 .maxBrightness = 255,
11437 .flags = InputLightClass::BRIGHTNESS,
11438 .path = ""};
11439 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11440 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11441 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11442 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11443
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011444 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011445 InputDeviceInfo info;
11446 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011447 std::vector<InputDeviceLightInfo> lights = info.getLights();
11448 ASSERT_EQ(1U, lights.size());
11449 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011450 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11451 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011452
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011453 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11454 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11455 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011456}
11457
Michael Wrightd02c5b62014-02-10 15:10:22 -080011458} // namespace android