blob: dd93e93a864a6e00abbef8f872b27eb012d20b33 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
Harry Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020
Prabir Pradhan2770d242019-09-02 18:07:11 -070021#include <CursorInputMapper.h>
22#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080025#include <InputReaderBase.h>
26#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000027#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <KeyboardInputMapper.h>
29#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070031#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070032#include <SingleTouchInputMapper.h>
33#include <SwitchInputMapper.h>
34#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000035#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070036#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080037#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000038#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039#include <android-base/thread_annotations.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000040#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050042#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000043#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080044
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070045#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000046#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000047#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000048#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000049#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000050#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000051#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000052#include "input/DisplayViewport.h"
53#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010054
Michael Wrightd02c5b62014-02-10 15:10:22 -080055namespace android {
56
Dominik Laskowski2f01d772022-03-23 16:01:29 -070057using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000058using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070059using std::chrono_literals::operator""ms;
60
Michael Wrightd02c5b62014-02-10 15:10:22 -080061// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000063static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t DISPLAY_WIDTH = 480;
67static constexpr int32_t DISPLAY_HEIGHT = 800;
68static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
69static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
70static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070071static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070072static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080073
arthurhungcc7f9802020-04-30 17:55:40 +080074static constexpr int32_t FIRST_SLOT = 0;
75static constexpr int32_t SECOND_SLOT = 1;
76static constexpr int32_t THIRD_SLOT = 2;
77static constexpr int32_t INVALID_TRACKING_ID = -1;
78static constexpr int32_t FIRST_TRACKING_ID = 0;
79static constexpr int32_t SECOND_TRACKING_ID = 1;
80static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080081static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
82static constexpr int32_t LIGHT_COLOR = 0x7F448866;
83static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080084
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080085static constexpr int32_t ACTION_POINTER_0_DOWN =
86 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
87static constexpr int32_t ACTION_POINTER_0_UP =
88 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
89static constexpr int32_t ACTION_POINTER_1_DOWN =
90 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_1_UP =
92 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000094// Minimum timestamp separation between subsequent input events from a Bluetooth device.
95static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
96// Maximum smoothing time delta so that we don't generate events too far into the future.
97constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
98
Michael Wrightd02c5b62014-02-10 15:10:22 -080099template<typename T>
100static inline T min(T a, T b) {
101 return a < b ? a : b;
102}
103
104static inline float avg(float x, float y) {
105 return (x + y) / 2;
106}
107
Chris Ye3fdbfef2021-01-06 18:45:18 -0800108// Mapping for light color name and the light color
109const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
110 {"green", LightColor::GREEN},
111 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112
Michael Wrighta9cf4192022-12-01 23:46:39 +0000113static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700114 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000115 case ui::ROTATION_90:
116 return ui::ROTATION_270;
117 case ui::ROTATION_270:
118 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700119 default:
120 return orientation;
121 }
122}
123
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800124static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
125 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000126 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800127
128 const InputDeviceInfo::MotionRange* motionRange =
129 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
130 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
131}
132
133static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
134 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000135 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800136
137 const InputDeviceInfo::MotionRange* motionRange =
138 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
139 ASSERT_EQ(nullptr, motionRange);
140}
141
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700142[[maybe_unused]] static void dumpReader(InputReader& reader) {
143 std::string dump;
144 reader.dump(dump);
145 std::istringstream iss(dump);
146 for (std::string line; std::getline(iss, line);) {
147 ALOGE("%s", line.c_str());
148 std::this_thread::sleep_for(std::chrono::milliseconds(1));
149 }
150}
151
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152// --- FakeInputMapper ---
153
154class FakeInputMapper : public InputMapper {
155 uint32_t mSources;
156 int32_t mKeyboardType;
157 int32_t mMetaState;
158 KeyedVector<int32_t, int32_t> mKeyCodeStates;
159 KeyedVector<int32_t, int32_t> mScanCodeStates;
160 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100161 // fake mapping which would normally come from keyCharacterMap
162 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800163 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700165 std::mutex mLock;
166 std::condition_variable mStateChangedCondition;
167 bool mConfigureWasCalled GUARDED_BY(mLock);
168 bool mResetWasCalled GUARDED_BY(mLock);
169 bool mProcessWasCalled GUARDED_BY(mLock);
170 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171
Arthur Hungc23540e2018-11-29 20:42:11 +0800172 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000174 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
175 uint32_t sources)
176 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800177 mSources(sources),
178 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800180 mConfigureWasCalled(false),
181 mResetWasCalled(false),
182 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
Chris Yea52ade12020-08-27 16:49:20 -0700184 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185
186 void setKeyboardType(int32_t keyboardType) {
187 mKeyboardType = keyboardType;
188 }
189
190 void setMetaState(int32_t metaState) {
191 mMetaState = metaState;
192 }
193
194 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700195 std::unique_lock<std::mutex> lock(mLock);
196 base::ScopedLockAssertion assumeLocked(mLock);
197 const bool configureCalled =
198 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
199 return mConfigureWasCalled;
200 });
201 if (!configureCalled) {
202 FAIL() << "Expected configure() to have been called.";
203 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204 mConfigureWasCalled = false;
205 }
206
207 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208 std::unique_lock<std::mutex> lock(mLock);
209 base::ScopedLockAssertion assumeLocked(mLock);
210 const bool resetCalled =
211 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
212 return mResetWasCalled;
213 });
214 if (!resetCalled) {
215 FAIL() << "Expected reset() to have been called.";
216 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 mResetWasCalled = false;
218 }
219
Yi Kong9b14ac62018-07-17 13:48:38 -0700220 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 std::unique_lock<std::mutex> lock(mLock);
222 base::ScopedLockAssertion assumeLocked(mLock);
223 const bool processCalled =
224 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
225 return mProcessWasCalled;
226 });
227 if (!processCalled) {
228 FAIL() << "Expected process() to have been called.";
229 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 if (outLastEvent) {
231 *outLastEvent = mLastEvent;
232 }
233 mProcessWasCalled = false;
234 }
235
236 void setKeyCodeState(int32_t keyCode, int32_t state) {
237 mKeyCodeStates.replaceValueFor(keyCode, state);
238 }
239
240 void setScanCodeState(int32_t scanCode, int32_t state) {
241 mScanCodeStates.replaceValueFor(scanCode, state);
242 }
243
244 void setSwitchState(int32_t switchCode, int32_t state) {
245 mSwitchStates.replaceValueFor(switchCode, state);
246 }
247
248 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800249 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250 }
251
Philip Junker4af3b3d2021-12-14 10:36:55 +0100252 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
253 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
254 }
255
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100257 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258
Harry Cuttsd02ea102023-03-17 18:21:30 +0000259 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 InputMapper::populateDeviceInfo(deviceInfo);
261
262 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000263 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264 }
265 }
266
Arpit Singhed6c3de2023-04-05 19:24:37 +0000267 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000268 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700269 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800271
272 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800273 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000274 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000275 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800276 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700277
278 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700279 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280 }
281
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700282 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700283 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700285 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700286 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 }
288
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700289 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700290 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291 mLastEvent = *rawEvent;
292 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700293 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700294 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295 }
296
Chris Yea52ade12020-08-27 16:49:20 -0700297 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
299 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
300 }
301
Philip Junker4af3b3d2021-12-14 10:36:55 +0100302 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
303 auto it = mKeyCodeMapping.find(locationKeyCode);
304 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
305 }
306
Chris Yea52ade12020-08-27 16:49:20 -0700307 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
309 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
310 }
311
Chris Yea52ade12020-08-27 16:49:20 -0700312 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 ssize_t index = mSwitchStates.indexOfKey(switchCode);
314 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
315 }
316
Chris Yea52ade12020-08-27 16:49:20 -0700317 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700318 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700319 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700320 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
322 if (keyCodes[i] == mSupportedKeyCodes[j]) {
323 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800324 }
325 }
326 }
Chris Yea52ade12020-08-27 16:49:20 -0700327 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328 return result;
329 }
330
331 virtual int32_t getMetaState() {
332 return mMetaState;
333 }
334
335 virtual void fadePointer() {
336 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800337
338 virtual std::optional<int32_t> getAssociatedDisplay() {
339 if (mViewport) {
340 return std::make_optional(mViewport->displayId);
341 }
342 return std::nullopt;
343 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344};
345
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700346// --- InputReaderPolicyTest ---
347class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700348protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700349 sp<FakeInputReaderPolicy> mFakePolicy;
350
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700351 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700352 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700353};
354
355/**
356 * Check that empty set of viewports is an acceptable configuration.
357 * Also try to get internal viewport two different ways - by type and by uniqueId.
358 *
359 * There will be confusion if two viewports with empty uniqueId and identical type are present.
360 * Such configuration is not currently allowed.
361 */
362TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700363 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700364
365 // We didn't add any viewports yet, so there shouldn't be any.
366 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100367 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700368 ASSERT_FALSE(internalViewport);
369
370 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000371 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000372 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700373
374 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700375 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700376 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100377 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700378
379 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100380 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700381 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700382 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700383
384 mFakePolicy->clearViewports();
385 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700386 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700387 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100388 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700389 ASSERT_FALSE(internalViewport);
390}
391
392TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
393 const std::string internalUniqueId = "local:0";
394 const std::string externalUniqueId = "local:1";
395 const std::string virtualUniqueId1 = "virtual:2";
396 const std::string virtualUniqueId2 = "virtual:3";
397 constexpr int32_t virtualDisplayId1 = 2;
398 constexpr int32_t virtualDisplayId2 = 3;
399
400 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000401 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000402 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000403 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700404 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000405 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000406 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000407 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700408 // Add an virtual viewport
409 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000410 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000411 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700412 // Add another virtual viewport
413 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000414 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000415 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700416
417 // Check matching by type for internal
418 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100419 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700420 ASSERT_TRUE(internalViewport);
421 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
422
423 // Check matching by type for external
424 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100425 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700426 ASSERT_TRUE(externalViewport);
427 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
428
429 // Check matching by uniqueId for virtual viewport #1
430 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700431 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700432 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100433 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700434 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
435 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
436
437 // Check matching by uniqueId for virtual viewport #2
438 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700439 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700440 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100441 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700442 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
443 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
444}
445
446
447/**
448 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
449 * that lookup works by checking display id.
450 * Check that 2 viewports of each kind is possible, for all existing viewport types.
451 */
452TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
453 const std::string uniqueId1 = "uniqueId1";
454 const std::string uniqueId2 = "uniqueId2";
455 constexpr int32_t displayId1 = 2;
456 constexpr int32_t displayId2 = 3;
457
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100458 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
459 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700460 for (const ViewportType& type : types) {
461 mFakePolicy->clearViewports();
462 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000463 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000464 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700465 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000466 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000467 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700468
469 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700470 std::optional<DisplayViewport> viewport1 =
471 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700472 ASSERT_TRUE(viewport1);
473 ASSERT_EQ(displayId1, viewport1->displayId);
474 ASSERT_EQ(type, viewport1->type);
475
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700476 std::optional<DisplayViewport> viewport2 =
477 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700478 ASSERT_TRUE(viewport2);
479 ASSERT_EQ(displayId2, viewport2->displayId);
480 ASSERT_EQ(type, viewport2->type);
481
482 // When there are multiple viewports of the same kind, and uniqueId is not specified
483 // in the call to getDisplayViewport, then that situation is not supported.
484 // The viewports can be stored in any order, so we cannot rely on the order, since that
485 // is just implementation detail.
486 // However, we can check that it still returns *a* viewport, we just cannot assert
487 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700488 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700489 ASSERT_TRUE(someViewport);
490 }
491}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700493/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000494 * When we have multiple internal displays make sure we always return the default display when
495 * querying by type.
496 */
497TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
498 const std::string uniqueId1 = "uniqueId1";
499 const std::string uniqueId2 = "uniqueId2";
500 constexpr int32_t nonDefaultDisplayId = 2;
501 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
502 "Test display ID should not be ADISPLAY_ID_DEFAULT");
503
504 // Add the default display first and ensure it gets returned.
505 mFakePolicy->clearViewports();
506 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000507 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000508 ViewportType::INTERNAL);
509 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000510 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000511 ViewportType::INTERNAL);
512
513 std::optional<DisplayViewport> viewport =
514 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
515 ASSERT_TRUE(viewport);
516 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
517 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
518
519 // Add the default display second to make sure order doesn't matter.
520 mFakePolicy->clearViewports();
521 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000522 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000523 ViewportType::INTERNAL);
524 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000525 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000526 ViewportType::INTERNAL);
527
528 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
529 ASSERT_TRUE(viewport);
530 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
531 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
532}
533
534/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700535 * Check getDisplayViewportByPort
536 */
537TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100538 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700539 const std::string uniqueId1 = "uniqueId1";
540 const std::string uniqueId2 = "uniqueId2";
541 constexpr int32_t displayId1 = 1;
542 constexpr int32_t displayId2 = 2;
543 const uint8_t hdmi1 = 0;
544 const uint8_t hdmi2 = 1;
545 const uint8_t hdmi3 = 2;
546
547 mFakePolicy->clearViewports();
548 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000549 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000550 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700551 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000552 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000553 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700554
555 // Check that correct display viewport was returned by comparing the display ports.
556 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
557 ASSERT_TRUE(hdmi1Viewport);
558 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
559 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
560
561 // Check that we can still get the same viewport using the uniqueId
562 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
563 ASSERT_TRUE(hdmi1Viewport);
564 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
565 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
566 ASSERT_EQ(type, hdmi1Viewport->type);
567
568 // Check that we cannot find a port with "HDMI2", because we never added one
569 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
570 ASSERT_FALSE(hdmi2Viewport);
571}
572
Michael Wrightd02c5b62014-02-10 15:10:22 -0800573// --- InputReaderTest ---
574
575class InputReaderTest : public testing::Test {
576protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700577 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800578 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700579 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000580 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800581
Chris Yea52ade12020-08-27 16:49:20 -0700582 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700583 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700584 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700585 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586
Prabir Pradhan28efc192019-11-05 01:10:04 +0000587 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700588 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589 }
590
Chris Yea52ade12020-08-27 16:49:20 -0700591 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700592 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594 }
595
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700596 void addDevice(int32_t eventHubId, const std::string& name,
597 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800598 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599
600 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800601 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 }
603 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000604 mReader->loopOnce();
605 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700606 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700608 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 }
610
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800611 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700612 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000613 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700614 }
615
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800616 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700617 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000618 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700619 }
620
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700622 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700623 ftl::Flags<InputDeviceClass> classes,
624 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800625 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800626 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000627 FakeInputMapper& mapper =
628 device->addMapper<FakeInputMapper>(eventHubId,
629 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800630 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800631 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 return mapper;
633 }
634};
635
Chris Ye98d3f532020-10-01 21:48:59 -0700636TEST_F(InputReaderTest, PolicyGetInputDevices) {
637 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700638 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700639 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640
641 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700642 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800644 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100645 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800646 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
647 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000648 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649}
650
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000651TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
652 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
653 mFakeEventHub->setSysfsRootPath(1, "xyz");
654
655 // Should also have received a notification describing the new input device.
656 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
657 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
658 ASSERT_EQ(0U, inputDevice.getLights().size());
659
660 RawLightInfo infoMonolight = {.id = 123,
661 .name = "mono_keyboard_backlight",
662 .maxBrightness = 255,
663 .flags = InputLightClass::BRIGHTNESS,
664 .path = ""};
665 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
666 mReader->sysfsNodeChanged("xyz");
667 mReader->loopOnce();
668
669 // Should also have received a notification describing the new recreated input device.
670 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
671 inputDevice = mFakePolicy->getInputDevices()[0];
672 ASSERT_EQ(1U, inputDevice.getLights().size());
673}
674
Chris Yee7310032020-09-22 15:36:28 -0700675TEST_F(InputReaderTest, GetMergedInputDevices) {
676 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
677 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
678 // Add two subdevices to device
679 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
680 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000681 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
682 AINPUT_SOURCE_KEYBOARD);
683 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
684 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700685
686 // Push same device instance for next device to be added, so they'll have same identifier.
687 mReader->pushNextDevice(device);
688 mReader->pushNextDevice(device);
689 ASSERT_NO_FATAL_FAILURE(
690 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
691 ASSERT_NO_FATAL_FAILURE(
692 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
693
694 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000695 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700696}
697
Chris Yee14523a2020-12-19 13:46:00 -0800698TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
700 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
701 // Add two subdevices to device
702 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
703 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000704 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
705 AINPUT_SOURCE_KEYBOARD);
706 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
707 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800708
709 // Push same device instance for next device to be added, so they'll have same identifier.
710 mReader->pushNextDevice(device);
711 mReader->pushNextDevice(device);
712 // Sensor device is initially disabled
713 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
714 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
715 nullptr));
716 // Device is disabled because the only sub device is a sensor device and disabled initially.
717 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
718 ASSERT_FALSE(device->isEnabled());
719 ASSERT_NO_FATAL_FAILURE(
720 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
721 // The merged device is enabled if any sub device is enabled
722 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
723 ASSERT_TRUE(device->isEnabled());
724}
725
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700726TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800727 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700728 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800729 constexpr int32_t eventHubId = 1;
730 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700731 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000732 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
733 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800734 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800735 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700736
Yi Kong9b14ac62018-07-17 13:48:38 -0700737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700738
739 NotifyDeviceResetArgs resetArgs;
740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700741 ASSERT_EQ(deviceId, resetArgs.deviceId);
742
743 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800744 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000745 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700746
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700748 ASSERT_EQ(deviceId, resetArgs.deviceId);
749 ASSERT_EQ(device->isEnabled(), false);
750
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800751 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000752 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700755 ASSERT_EQ(device->isEnabled(), false);
756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800757 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000758 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700760 ASSERT_EQ(deviceId, resetArgs.deviceId);
761 ASSERT_EQ(device->isEnabled(), true);
762}
763
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800765 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700766 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800767 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800768 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800769 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800770 AINPUT_SOURCE_KEYBOARD, nullptr);
771 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800772
773 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
774 AINPUT_SOURCE_ANY, AKEYCODE_A))
775 << "Should return unknown when the device id is >= 0 but unknown.";
776
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800777 ASSERT_EQ(AKEY_STATE_UNKNOWN,
778 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
779 << "Should return unknown when the device id is valid but the sources are not "
780 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800781
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800782 ASSERT_EQ(AKEY_STATE_DOWN,
783 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
784 AKEYCODE_A))
785 << "Should return value provided by mapper when device id is valid and the device "
786 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787
788 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
789 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
790 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
791
792 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
793 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
794 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
795}
796
Philip Junker4af3b3d2021-12-14 10:36:55 +0100797TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
798 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
799 constexpr int32_t eventHubId = 1;
800 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
801 InputDeviceClass::KEYBOARD,
802 AINPUT_SOURCE_KEYBOARD, nullptr);
803 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
804
805 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
806 << "Should return unknown when the device with the specified id is not found.";
807
808 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
809 << "Should return correct mapping when device id is valid and mapping exists.";
810
811 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
812 << "Should return the location key code when device id is valid and there's no "
813 "mapping.";
814}
815
816TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
817 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
818 constexpr int32_t eventHubId = 1;
819 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
820 InputDeviceClass::JOYSTICK,
821 AINPUT_SOURCE_GAMEPAD, nullptr);
822 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
823
824 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
825 << "Should return unknown when the device id is valid but there is no keyboard mapper";
826}
827
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800829 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700830 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800831 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800832 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800833 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800834 AINPUT_SOURCE_KEYBOARD, nullptr);
835 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836
837 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
838 AINPUT_SOURCE_ANY, KEY_A))
839 << "Should return unknown when the device id is >= 0 but unknown.";
840
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800841 ASSERT_EQ(AKEY_STATE_UNKNOWN,
842 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
843 << "Should return unknown when the device id is valid but the sources are not "
844 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800846 ASSERT_EQ(AKEY_STATE_DOWN,
847 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
848 KEY_A))
849 << "Should return value provided by mapper when device id is valid and the device "
850 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851
852 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
853 AINPUT_SOURCE_TRACKBALL, KEY_A))
854 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
855
856 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
857 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
858 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
859}
860
861TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800862 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700863 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800865 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800866 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800867 AINPUT_SOURCE_KEYBOARD, nullptr);
868 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869
870 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
871 AINPUT_SOURCE_ANY, SW_LID))
872 << "Should return unknown when the device id is >= 0 but unknown.";
873
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800874 ASSERT_EQ(AKEY_STATE_UNKNOWN,
875 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
876 << "Should return unknown when the device id is valid but the sources are not "
877 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800879 ASSERT_EQ(AKEY_STATE_DOWN,
880 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
881 SW_LID))
882 << "Should return value provided by mapper when device id is valid and the device "
883 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800884
885 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
886 AINPUT_SOURCE_TRACKBALL, SW_LID))
887 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
888
889 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
890 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
891 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
892}
893
894TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800895 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700896 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800897 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800898 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800899 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800900 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100901
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800902 mapper.addSupportedKeyCode(AKEYCODE_A);
903 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700905 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800906 uint8_t flags[4] = { 0, 0, 0, 1 };
907
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700908 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 << "Should return false when device id is >= 0 but unknown.";
910 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
911
912 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700913 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800914 << "Should return false when device id is valid but the sources are not supported by "
915 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800916 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
917
918 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700919 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800920 keyCodes, flags))
921 << "Should return value provided by mapper when device id is valid and the device "
922 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
924
925 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700926 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
927 << "Should return false when the device id is < 0 but the sources are not supported by "
928 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
930
931 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700932 ASSERT_TRUE(
933 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
934 << "Should return value provided by mapper when device id is < 0 and one of the "
935 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800936 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
937}
938
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000939TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800940 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700941 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942
943 NotifyConfigurationChangedArgs args;
944
945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
946 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
947}
948
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000949TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800950 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700951 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000952 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800953 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000954 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800955 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800956 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800957 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000959 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000960 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
962
963 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800964 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000965 ASSERT_EQ(when, event.when);
966 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800967 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 ASSERT_EQ(EV_KEY, event.type);
969 ASSERT_EQ(KEY_A, event.code);
970 ASSERT_EQ(1, event.value);
971}
972
Garfield Tan1c7bc862020-01-28 13:24:04 -0800973TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800974 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700975 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800976 constexpr int32_t eventHubId = 1;
977 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800978 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000979 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
980 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800981 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800982 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800983
984 NotifyDeviceResetArgs resetArgs;
985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800986 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800987
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800988 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000989 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800991 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800992 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800994 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000995 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800997 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800998 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001000 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001001 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001003 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001004 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001005}
1006
Garfield Tan1c7bc862020-01-28 13:24:04 -08001007TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1008 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001009 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001010 constexpr int32_t eventHubId = 1;
1011 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1012 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001013 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1014 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001015 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001016 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1017
1018 NotifyDeviceResetArgs resetArgs;
1019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1020 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1021}
1022
Arthur Hungc23540e2018-11-29 20:42:11 +08001023TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001024 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001025 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001026 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001027 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001028 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1029 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001030 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1031 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001032 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001033
1034 const uint8_t hdmi1 = 1;
1035
1036 // Associated touch screen with second display.
1037 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1038
1039 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001040 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001041 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001042 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001043 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001044 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001045 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001046 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001047 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001048
1049 // Add the device, and make sure all of the callbacks are triggered.
1050 // The device is added after the input port associations are processed since
1051 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001052 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001055 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001056
Arthur Hung2c9a3342019-07-23 14:18:59 +08001057 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001058 ASSERT_EQ(deviceId, device->getId());
1059 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1060 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001061
1062 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001063 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001064 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001065 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001066}
1067
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001068TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1069 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001070 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001071 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1072 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1073 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001074 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1075 AINPUT_SOURCE_KEYBOARD);
1076 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1077 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001078 mReader->pushNextDevice(device);
1079 mReader->pushNextDevice(device);
1080 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1081 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1082
1083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1084
1085 NotifyDeviceResetArgs resetArgs;
1086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1087 ASSERT_EQ(deviceId, resetArgs.deviceId);
1088 ASSERT_TRUE(device->isEnabled());
1089 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1090 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1091
1092 disableDevice(deviceId);
1093 mReader->loopOnce();
1094
1095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1096 ASSERT_EQ(deviceId, resetArgs.deviceId);
1097 ASSERT_FALSE(device->isEnabled());
1098 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1099 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1100
1101 enableDevice(deviceId);
1102 mReader->loopOnce();
1103
1104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1105 ASSERT_EQ(deviceId, resetArgs.deviceId);
1106 ASSERT_TRUE(device->isEnabled());
1107 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1108 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1109}
1110
1111TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1112 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001113 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001114 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1115 // Add two subdevices to device
1116 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1117 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001118 device->addMapper<FakeInputMapper>(eventHubIds[0],
1119 mFakePolicy->getReaderConfiguration(),
1120 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001121 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001122 device->addMapper<FakeInputMapper>(eventHubIds[1],
1123 mFakePolicy->getReaderConfiguration(),
1124 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001125 mReader->pushNextDevice(device);
1126 mReader->pushNextDevice(device);
1127 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1128 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1129
1130 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1131 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1132
1133 ASSERT_EQ(AKEY_STATE_DOWN,
1134 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1135 ASSERT_EQ(AKEY_STATE_DOWN,
1136 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1137 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1138 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1139}
1140
Prabir Pradhan7e186182020-11-10 13:56:45 -08001141TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1142 NotifyPointerCaptureChangedArgs args;
1143
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001144 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001145 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001146 mReader->loopOnce();
1147 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001148 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1149 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001150
1151 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001152 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001153 mReader->loopOnce();
1154 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001155 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001156
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001157 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001158 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001159 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001160 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001161 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001162}
1163
Chris Ye87143712020-11-10 05:05:58 +00001164class FakeVibratorInputMapper : public FakeInputMapper {
1165public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001166 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1167 const InputReaderConfiguration& readerConfig, uint32_t sources)
1168 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001169
1170 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1171};
1172
1173TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1174 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001175 ftl::Flags<InputDeviceClass> deviceClass =
1176 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001177 constexpr int32_t eventHubId = 1;
1178 const char* DEVICE_LOCATION = "BLUETOOTH";
1179 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1180 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001181 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1182 mFakePolicy->getReaderConfiguration(),
1183 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001184 mReader->pushNextDevice(device);
1185
1186 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1187 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1188
1189 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1190 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1191}
1192
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001193// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001194
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001195class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001196public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001197 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001198
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001199 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001200
Andy Chenf9f1a022022-08-29 20:07:10 -04001201 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1202
Chris Yee2b1e5c2021-03-10 22:45:12 -08001203 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1204
1205 void dump(std::string& dump) override {}
1206
1207 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1208 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001209 }
1210
Chris Yee2b1e5c2021-03-10 22:45:12 -08001211 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1212 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001213 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001214
1215 bool setLightColor(int32_t lightId, int32_t color) override {
1216 getDeviceContext().setLightBrightness(lightId, color >> 24);
1217 return true;
1218 }
1219
1220 std::optional<int32_t> getLightColor(int32_t lightId) override {
1221 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1222 if (!result.has_value()) {
1223 return std::nullopt;
1224 }
1225 return result.value() << 24;
1226 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001227
1228 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1229
1230 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1231
1232private:
1233 InputDeviceContext& mDeviceContext;
1234 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1235 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001236 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001237};
1238
Chris Yee2b1e5c2021-03-10 22:45:12 -08001239TEST_F(InputReaderTest, BatteryGetCapacity) {
1240 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001241 ftl::Flags<InputDeviceClass> deviceClass =
1242 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001243 constexpr int32_t eventHubId = 1;
1244 const char* DEVICE_LOCATION = "BLUETOOTH";
1245 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001246 FakePeripheralController& controller =
1247 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001248 mReader->pushNextDevice(device);
1249
1250 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1251
Harry Cuttsa5b71292022-11-28 12:56:17 +00001252 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1253 FakeEventHub::BATTERY_CAPACITY);
1254 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001255}
1256
1257TEST_F(InputReaderTest, BatteryGetStatus) {
1258 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001259 ftl::Flags<InputDeviceClass> deviceClass =
1260 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001261 constexpr int32_t eventHubId = 1;
1262 const char* DEVICE_LOCATION = "BLUETOOTH";
1263 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001264 FakePeripheralController& controller =
1265 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001266 mReader->pushNextDevice(device);
1267
1268 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1269
Harry Cuttsa5b71292022-11-28 12:56:17 +00001270 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1271 FakeEventHub::BATTERY_STATUS);
1272 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001273}
1274
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001275TEST_F(InputReaderTest, BatteryGetDevicePath) {
1276 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1277 ftl::Flags<InputDeviceClass> deviceClass =
1278 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1279 constexpr int32_t eventHubId = 1;
1280 const char* DEVICE_LOCATION = "BLUETOOTH";
1281 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1282 device->addController<FakePeripheralController>(eventHubId);
1283 mReader->pushNextDevice(device);
1284
1285 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1286
Harry Cuttsa5b71292022-11-28 12:56:17 +00001287 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001288}
1289
Chris Ye3fdbfef2021-01-06 18:45:18 -08001290TEST_F(InputReaderTest, LightGetColor) {
1291 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001292 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001293 constexpr int32_t eventHubId = 1;
1294 const char* DEVICE_LOCATION = "BLUETOOTH";
1295 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001296 FakePeripheralController& controller =
1297 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001298 mReader->pushNextDevice(device);
1299 RawLightInfo info = {.id = 1,
1300 .name = "Mono",
1301 .maxBrightness = 255,
1302 .flags = InputLightClass::BRIGHTNESS,
1303 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001304 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1305 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001306
1307 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001308
Harry Cutts33476232023-01-30 19:57:29 +00001309 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1310 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1311 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1312 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001313}
1314
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001315// --- InputReaderIntegrationTest ---
1316
1317// These tests create and interact with the InputReader only through its interface.
1318// The InputReader is started during SetUp(), which starts its processing in its own
1319// thread. The tests use linux uinput to emulate input devices.
1320// NOTE: Interacting with the physical device while these tests are running may cause
1321// the tests to fail.
1322class InputReaderIntegrationTest : public testing::Test {
1323protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001324 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001325 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001326 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001327
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001328 std::shared_ptr<FakePointerController> mFakePointerController;
1329
Chris Yea52ade12020-08-27 16:49:20 -07001330 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001331#if !defined(__ANDROID__)
1332 GTEST_SKIP();
1333#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001334 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001335 mFakePointerController = std::make_shared<FakePointerController>();
1336 mFakePolicy->setPointerController(mFakePointerController);
Harry Cutts33476232023-01-30 19:57:29 +00001337 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1338 /*eventDidNotHappenTimeout=*/30ms);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001339
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001340 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1341 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001342 ASSERT_EQ(mReader->start(), OK);
1343
1344 // Since this test is run on a real device, all the input devices connected
1345 // to the test device will show up in mReader. We wait for those input devices to
1346 // show up before beginning the tests.
1347 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +00001348 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1350 }
1351
Chris Yea52ade12020-08-27 16:49:20 -07001352 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001353#if !defined(__ANDROID__)
1354 return;
1355#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001356 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001357 mReader.reset();
1358 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001359 mFakePolicy.clear();
1360 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001361
1362 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1363 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1364 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1365 [&name](const InputDeviceInfo& info) {
1366 return info.getIdentifier().name == name;
1367 });
1368 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1369 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001370};
1371
1372TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1373 // An invalid input device that is only used for this test.
1374 class InvalidUinputDevice : public UinputDevice {
1375 public:
Harry Cutts33476232023-01-30 19:57:29 +00001376 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001377
1378 private:
1379 void configureDevice(int fd, uinput_user_dev* device) override {}
1380 };
1381
1382 const size_t numDevices = mFakePolicy->getInputDevices().size();
1383
1384 // UinputDevice does not set any event or key bits, so InputReader should not
1385 // consider it as a valid device.
1386 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1387 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1388 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1389 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1390
1391 invalidDevice.reset();
1392 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1394 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1395}
1396
1397TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1398 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1399
1400 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1401 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1402 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1403 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1404
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001405 const auto device = findDeviceByName(keyboard->getName());
1406 ASSERT_TRUE(device.has_value());
1407 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1408 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1409 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001410
1411 keyboard.reset();
1412 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1414 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1415}
1416
1417TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1418 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1419 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1420
1421 NotifyConfigurationChangedArgs configChangedArgs;
1422 ASSERT_NO_FATAL_FAILURE(
1423 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001424 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001425 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1426
1427 NotifyKeyArgs keyArgs;
1428 keyboard->pressAndReleaseHomeKey();
1429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1430 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001431 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001432 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001433 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001434 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001435 prevTimestamp = keyArgs.eventTime;
1436
1437 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1438 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001439 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001440 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001441 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001442}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001443
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001444TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1445 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1446 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1447
1448 const auto device = findDeviceByName(stylus->getName());
1449 ASSERT_TRUE(device.has_value());
1450
Prabir Pradhana3621852022-10-14 18:57:23 +00001451 // An external stylus with buttons should also be recognized as a keyboard.
1452 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001453 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1454 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1455
1456 const auto DOWN =
1457 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1458 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1459
1460 stylus->pressAndReleaseKey(BTN_STYLUS);
1461 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1462 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1463 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1464 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1465
1466 stylus->pressAndReleaseKey(BTN_STYLUS2);
1467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1468 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1469 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1470 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1471
1472 stylus->pressAndReleaseKey(BTN_STYLUS3);
1473 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1474 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1475 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1476 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1477}
1478
Prabir Pradhan05f6c602023-08-18 20:02:01 +00001479TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1480 std::unique_ptr<UinputKeyboard> keyboard =
1481 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1482 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1483 KEY_R, KEY_T, KEY_Y,
1484 BTN_STYLUS, BTN_STYLUS2,
1485 BTN_STYLUS3});
1486 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1487
1488 const auto device = findDeviceByName(keyboard->getName());
1489 ASSERT_TRUE(device.has_value());
1490
1491 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1492 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1493 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1494 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1495}
1496
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001497/**
1498 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1499 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1500 * are passed to the listener.
1501 */
1502static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1503TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1504 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1505 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1506 NotifyKeyArgs keyArgs;
1507
1508 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1509 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1510 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1511 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1512
1513 controller->pressAndReleaseKey(BTN_GEAR_UP);
1514 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1515 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1516 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1517}
1518
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001519// --- TouchIntegrationTest ---
1520
Arthur Hungaab25622020-01-16 11:22:11 +08001521class TouchIntegrationTest : public InputReaderIntegrationTest {
1522protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001523 const std::string UNIQUE_ID = "local:0";
1524
Chris Yea52ade12020-08-27 16:49:20 -07001525 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001526#if !defined(__ANDROID__)
1527 GTEST_SKIP();
1528#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001529 InputReaderIntegrationTest::SetUp();
1530 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001531 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1532 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001533
1534 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1535 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1536 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001537 const auto info = findDeviceByName(mDevice->getName());
1538 ASSERT_TRUE(info);
1539 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001540 }
1541
1542 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001543 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001544 std::optional<uint8_t> physicalPort,
1545 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001546 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001547 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001548 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001549 }
1550
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001551 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1552 NotifyMotionArgs args;
1553 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1554 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001555 ASSERT_EQ(points.size(), args.getPointerCount());
1556 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001557 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1558 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1559 }
1560 }
1561
Arthur Hungaab25622020-01-16 11:22:11 +08001562 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001563 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001564};
1565
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001566TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1567 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1568 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1569 // presses).
1570 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1571 mDeviceInfo.getSources());
1572}
1573
Arthur Hungaab25622020-01-16 11:22:11 +08001574TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1575 NotifyMotionArgs args;
1576 const Point centerPoint = mDevice->getCenterPoint();
1577
1578 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001579 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001580 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001581 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001582 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1583 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1584
1585 // ACTION_MOVE
1586 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001587 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001588 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1589 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1590
1591 // ACTION_UP
1592 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001593 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001594 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1595 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1596}
1597
1598TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1599 NotifyMotionArgs args;
1600 const Point centerPoint = mDevice->getCenterPoint();
1601
1602 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001603 mDevice->sendSlot(FIRST_SLOT);
1604 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001605 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001606 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001607 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1608 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1609
1610 // ACTION_POINTER_DOWN (Second slot)
1611 const Point secondPoint = centerPoint + Point(100, 100);
1612 mDevice->sendSlot(SECOND_SLOT);
1613 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001614 mDevice->sendDown(secondPoint);
1615 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001616 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001617 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001618
1619 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001620 mDevice->sendMove(secondPoint + Point(1, 1));
1621 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001622 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1624
1625 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001626 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001627 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001628 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001629 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001630
1631 // ACTION_UP
1632 mDevice->sendSlot(FIRST_SLOT);
1633 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001634 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001635 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1636 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1637}
1638
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001639/**
1640 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1641 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1642 * data?
1643 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1644 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1645 * for Pointer 0 only is generated after.
1646 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1647 * events, we will not miss any information.
1648 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1649 * event generated afterwards that contains the newest movement of pointer 0.
1650 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1651 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1652 * losing information about non-palm pointers.
1653 */
1654TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1655 NotifyMotionArgs args;
1656 const Point centerPoint = mDevice->getCenterPoint();
1657
1658 // ACTION_DOWN
1659 mDevice->sendSlot(FIRST_SLOT);
1660 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1661 mDevice->sendDown(centerPoint);
1662 mDevice->sendSync();
1663 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1664
1665 // ACTION_POINTER_DOWN (Second slot)
1666 const Point secondPoint = centerPoint + Point(100, 100);
1667 mDevice->sendSlot(SECOND_SLOT);
1668 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1669 mDevice->sendDown(secondPoint);
1670 mDevice->sendSync();
1671 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1672
1673 // ACTION_MOVE (First slot)
1674 mDevice->sendSlot(FIRST_SLOT);
1675 mDevice->sendMove(centerPoint + Point(5, 5));
1676 // ACTION_POINTER_UP (Second slot)
1677 mDevice->sendSlot(SECOND_SLOT);
1678 mDevice->sendPointerUp();
1679 // Send a single sync for the above 2 pointer updates
1680 mDevice->sendSync();
1681
1682 // First, we should get POINTER_UP for the second pointer
1683 assertReceivedMotion(ACTION_POINTER_1_UP,
1684 {/*first pointer */ centerPoint + Point(5, 5),
1685 /*second pointer*/ secondPoint});
1686
1687 // Next, the MOVE event for the first pointer
1688 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1689}
1690
1691/**
1692 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1693 * move, and then it will go up, all in the same frame.
1694 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1695 * gets sent to the listener.
1696 */
1697TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1698 NotifyMotionArgs args;
1699 const Point centerPoint = mDevice->getCenterPoint();
1700
1701 // ACTION_DOWN
1702 mDevice->sendSlot(FIRST_SLOT);
1703 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1704 mDevice->sendDown(centerPoint);
1705 mDevice->sendSync();
1706 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1707
1708 // ACTION_POINTER_DOWN (Second slot)
1709 const Point secondPoint = centerPoint + Point(100, 100);
1710 mDevice->sendSlot(SECOND_SLOT);
1711 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1712 mDevice->sendDown(secondPoint);
1713 mDevice->sendSync();
1714 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1715
1716 // ACTION_MOVE (First slot)
1717 mDevice->sendSlot(FIRST_SLOT);
1718 mDevice->sendMove(centerPoint + Point(5, 5));
1719 // ACTION_POINTER_UP (Second slot)
1720 mDevice->sendSlot(SECOND_SLOT);
1721 mDevice->sendMove(secondPoint + Point(6, 6));
1722 mDevice->sendPointerUp();
1723 // Send a single sync for the above 2 pointer updates
1724 mDevice->sendSync();
1725
1726 // First, we should get POINTER_UP for the second pointer
1727 // The movement of the second pointer during the liftoff frame is ignored.
1728 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1729 assertReceivedMotion(ACTION_POINTER_1_UP,
1730 {/*first pointer */ centerPoint + Point(5, 5),
1731 /*second pointer*/ secondPoint});
1732
1733 // Next, the MOVE event for the first pointer
1734 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1735}
1736
Arthur Hungaab25622020-01-16 11:22:11 +08001737TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1738 NotifyMotionArgs args;
1739 const Point centerPoint = mDevice->getCenterPoint();
1740
1741 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001742 mDevice->sendSlot(FIRST_SLOT);
1743 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001744 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001745 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001746 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1747 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1748
arthurhungcc7f9802020-04-30 17:55:40 +08001749 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001750 const Point secondPoint = centerPoint + Point(100, 100);
1751 mDevice->sendSlot(SECOND_SLOT);
1752 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1753 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001754 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001755 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001756 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001757
arthurhungcc7f9802020-04-30 17:55:40 +08001758 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001759 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001760 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001761 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1763
arthurhungcc7f9802020-04-30 17:55:40 +08001764 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1765 // a palm event.
1766 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001767 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001768 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001769 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001770 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001771 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001772
arthurhungcc7f9802020-04-30 17:55:40 +08001773 // Send up to second slot, expect first slot send moving.
1774 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001775 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001776 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1777 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001778
arthurhungcc7f9802020-04-30 17:55:40 +08001779 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001780 mDevice->sendSlot(FIRST_SLOT);
1781 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001782 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001783
arthurhungcc7f9802020-04-30 17:55:40 +08001784 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1785 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001786}
1787
Prabir Pradhanda20b172022-09-26 17:01:18 +00001788TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1789 const Point centerPoint = mDevice->getCenterPoint();
1790
1791 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1792 mDevice->sendSlot(FIRST_SLOT);
1793 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1794 mDevice->sendToolType(MT_TOOL_PEN);
1795 mDevice->sendDown(centerPoint);
1796 mDevice->sendSync();
1797 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1798 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001799 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001800
1801 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1802
1803 // Release the stylus touch.
1804 mDevice->sendUp();
1805 mDevice->sendSync();
1806 ASSERT_NO_FATAL_FAILURE(
1807 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1808
1809 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1810
1811 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1812 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1813 mDevice->sendToolType(MT_TOOL_FINGER);
1814 mDevice->sendDown(centerPoint);
1815 mDevice->sendSync();
1816 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1817 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001818 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001819
1820 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1821
1822 mDevice->sendUp();
1823 mDevice->sendSync();
1824 ASSERT_NO_FATAL_FAILURE(
1825 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1826
1827 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1828 // The policy should be notified of the stylus presence.
1829 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1830 mDevice->sendToolType(MT_TOOL_PEN);
1831 mDevice->sendMove(centerPoint);
1832 mDevice->sendSync();
1833 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1834 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001835 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001836
1837 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1838}
1839
Prabir Pradhan124ea442022-10-28 20:27:44 +00001840// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001841
Prabir Pradhan124ea442022-10-28 20:27:44 +00001842// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1843// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1844// stylus.
1845template <typename UinputStylusDevice>
1846class StylusButtonIntegrationTest : public TouchIntegrationTest {
1847protected:
1848 void SetUp() override {
1849#if !defined(__ANDROID__)
1850 GTEST_SKIP();
1851#endif
1852 TouchIntegrationTest::SetUp();
1853 mTouchscreen = mDevice.get();
1854 mTouchscreenInfo = mDeviceInfo;
1855
1856 setUpStylusDevice();
1857 }
1858
1859 UinputStylusDevice* mStylus{nullptr};
1860 InputDeviceInfo mStylusInfo{};
1861
1862 UinputTouchScreen* mTouchscreen{nullptr};
1863 InputDeviceInfo mTouchscreenInfo{};
1864
1865private:
1866 // When we are attempting to test stylus button events that are sent from the touchscreen,
1867 // use the same Uinput device for the touchscreen and the stylus.
1868 template <typename T = UinputStylusDevice>
1869 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1870 mStylus = mDevice.get();
1871 mStylusInfo = mDeviceInfo;
1872 }
1873
1874 // When we are attempting to stylus buttons from an external stylus being merged with touches
1875 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1876 template <typename T = UinputStylusDevice>
1877 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1878 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1879 mStylus = mStylusDeviceLifecycleTracker.get();
1880 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1881 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1882 const auto info = findDeviceByName(mStylus->getName());
1883 ASSERT_TRUE(info);
1884 mStylusInfo = *info;
1885 }
1886
1887 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1888
1889 // Hide the base class's device to expose it with a different name for readability.
1890 using TouchIntegrationTest::mDevice;
1891 using TouchIntegrationTest::mDeviceInfo;
1892};
1893
1894using StylusButtonIntegrationTestTypes =
1895 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1896TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1897
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001898TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001899 const auto stylusId = TestFixture::mStylusInfo.getId();
1900
1901 TestFixture::mStylus->pressKey(BTN_STYLUS);
1902 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1903 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1904 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1905
1906 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1907 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001908 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001909 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001910}
1911
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001912TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001913 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1914 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1915 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001916
1917 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001918 TestFixture::mStylus->pressKey(BTN_STYLUS);
1919 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001920 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001921 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001922
1923 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001924 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1925 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1926 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1927 TestFixture::mTouchscreen->sendDown(centerPoint);
1928 TestFixture::mTouchscreen->sendSync();
1929 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001930 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001931 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001932 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1933 WithDeviceId(touchscreenId))));
1934 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001935 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001936 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001937 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1938 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001939
Prabir Pradhan124ea442022-10-28 20:27:44 +00001940 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1941 TestFixture::mTouchscreen->sendSync();
1942 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001943 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001944 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001945 WithDeviceId(touchscreenId))));
1946 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001947 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001948 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001949 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001950
1951 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001952 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1953 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001954 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001955 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001956}
1957
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001958TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001959 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1960 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1961 const auto stylusId = TestFixture::mStylusInfo.getId();
1962 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001963 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001964
1965 // Press the stylus button.
1966 TestFixture::mStylus->pressKey(BTN_STYLUS);
1967 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1968 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1969 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1970
1971 // Start hovering with the stylus.
1972 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1973 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1974 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1975 TestFixture::mTouchscreen->sendMove(centerPoint);
1976 TestFixture::mTouchscreen->sendSync();
1977 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1978 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1979 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1980 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1981 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1982 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1983 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1984 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1985 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1986
1987 // Touch down with the stylus.
1988 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1989 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1990 TestFixture::mTouchscreen->sendDown(centerPoint);
1991 TestFixture::mTouchscreen->sendSync();
1992 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1993 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1994 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1995
1996 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1997 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1998 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1999
2000 // Stop touching with the stylus, and start hovering.
2001 TestFixture::mTouchscreen->sendUp();
2002 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2003 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2004 TestFixture::mTouchscreen->sendMove(centerPoint);
2005 TestFixture::mTouchscreen->sendSync();
2006 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2007 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2008 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2009 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2010 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2011 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2012 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2013 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2014 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2015
2016 // Stop hovering.
2017 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2018 TestFixture::mTouchscreen->sendSync();
2019 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2020 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2021 WithButtonState(0))));
2022 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2023 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2024 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2025 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2026
2027 // Release the stylus button.
2028 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2029 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2030 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2031 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2032}
2033
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002034TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002035 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2036 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2037 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002038
2039 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002040 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2041 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2042 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2043 TestFixture::mTouchscreen->sendDown(centerPoint);
2044 TestFixture::mTouchscreen->sendSync();
2045 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002046 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002047 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002048 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002049
2050 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002051 TestFixture::mStylus->pressKey(BTN_STYLUS);
2052 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002053 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002054 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2055 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002056 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002057 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002058 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2059 WithDeviceId(touchscreenId))));
2060 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002061 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002062 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002063 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2064 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002065
Prabir Pradhan124ea442022-10-28 20:27:44 +00002066 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2067 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002068 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002069 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2070 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002071 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002072 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002073 WithDeviceId(touchscreenId))));
2074 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002075 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002076 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002077 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002078
2079 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002080 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2081 TestFixture::mTouchscreen->sendSync();
2082 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002083 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002084 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002085 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002086}
2087
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002088TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002089 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2090 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002091 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002092
2093 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2094 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2095 const auto stylusId = TestFixture::mStylusInfo.getId();
2096
2097 // Start a stylus gesture. By the time this event is processed, the configuration change that
2098 // was requested is guaranteed to be completed.
2099 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2100 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2101 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2102 TestFixture::mTouchscreen->sendDown(centerPoint);
2103 TestFixture::mTouchscreen->sendSync();
2104 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2105 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002106 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002107 WithDeviceId(touchscreenId))));
2108
2109 // Press and release a stylus button. Each change only generates a MOVE motion event.
2110 // Key events are unaffected.
2111 TestFixture::mStylus->pressKey(BTN_STYLUS);
2112 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2113 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2114 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2115 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2116 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002117 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002118 WithDeviceId(touchscreenId))));
2119
2120 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2121 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2122 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2123 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2124 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2125 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002126 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002127 WithDeviceId(touchscreenId))));
2128
2129 // Finish the stylus gesture.
2130 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2131 TestFixture::mTouchscreen->sendSync();
2132 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2133 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002134 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002135 WithDeviceId(touchscreenId))));
2136}
2137
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002138// --- ExternalStylusIntegrationTest ---
2139
2140// Verify the behavior of an external stylus. An external stylus can report pressure or button
2141// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2142// ongoing stylus gesture that is being emitted by the touchscreen.
2143using ExternalStylusIntegrationTest = TouchIntegrationTest;
2144
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002145TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002146 const Point centerPoint = mDevice->getCenterPoint();
2147
2148 // Create an external stylus capable of reporting pressure data that
2149 // should be fused with a touch pointer.
2150 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2151 createUinputDevice<UinputExternalStylusWithPressure>();
2152 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2153 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2154 const auto stylusInfo = findDeviceByName(stylus->getName());
2155 ASSERT_TRUE(stylusInfo);
2156
2157 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2158
2159 const auto touchscreenId = mDeviceInfo.getId();
2160
2161 // Set a pressure value on the stylus. It doesn't generate any events.
2162 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2163 stylus->setPressure(100);
2164 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2165
2166 // Start a finger gesture, and ensure it shows up as stylus gesture
2167 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002168 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002169 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002170 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002171 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002172 mDevice->sendSync();
2173 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2174 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002175 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002176 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002177
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002178 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2179 // event with the updated pressure.
2180 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002181 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2182 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002183 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002184 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002185
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002186 // The external stylus did not generate any events.
2187 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2188 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2189}
2190
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002191TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002192 const Point centerPoint = mDevice->getCenterPoint();
2193
2194 // Create an external stylus capable of reporting pressure data that
2195 // should be fused with a touch pointer.
2196 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2197 createUinputDevice<UinputExternalStylusWithPressure>();
2198 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2199 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2200 const auto stylusInfo = findDeviceByName(stylus->getName());
2201 ASSERT_TRUE(stylusInfo);
2202
2203 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2204
2205 const auto touchscreenId = mDeviceInfo.getId();
2206
2207 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2208 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002209 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2210 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002211 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002212 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002213
2214 // Start a finger gesture. The touch device will withhold generating any touches for
2215 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2216 mDevice->sendSlot(FIRST_SLOT);
2217 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2218 mDevice->sendToolType(MT_TOOL_FINGER);
2219 mDevice->sendDown(centerPoint);
2220 auto waitUntil = std::chrono::system_clock::now() +
2221 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002222 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002223 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002224
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002225 // Since the external stylus did not report a pressure value within the timeout,
2226 // it shows up as a finger pointer.
2227 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2228 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002229 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002230 WithPressure(1.f))));
2231
2232 // Change the pressure on the external stylus. Since the pressure was not present at the start
2233 // of the gesture, it is ignored for now.
2234 stylus->setPressure(200);
2235 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2236
2237 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002238 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2239 mDevice->sendSync();
2240 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2241 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002242 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002243
2244 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2245 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2246 mDevice->sendToolType(MT_TOOL_FINGER);
2247 mDevice->sendDown(centerPoint);
2248 mDevice->sendSync();
2249 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2250 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002251 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002252 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2253
2254 // The external stylus did not generate any events.
2255 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2256 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002257}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002258
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002259TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002260 const Point centerPoint = mDevice->getCenterPoint();
2261
2262 // Create an external stylus device that does not support pressure. It should not affect any
2263 // touch pointers.
2264 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2265 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2266 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2267 const auto stylusInfo = findDeviceByName(stylus->getName());
2268 ASSERT_TRUE(stylusInfo);
2269
2270 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2271
2272 const auto touchscreenId = mDeviceInfo.getId();
2273
2274 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2275 // pressure data from the external stylus.
2276 mDevice->sendSlot(FIRST_SLOT);
2277 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2278 mDevice->sendToolType(MT_TOOL_FINGER);
2279 mDevice->sendDown(centerPoint);
2280 auto waitUntil = std::chrono::system_clock::now() +
2281 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2282 mDevice->sendSync();
2283 ASSERT_NO_FATAL_FAILURE(
2284 mTestListener
2285 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2286 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002287 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002288 WithButtonState(0),
2289 WithDeviceId(touchscreenId),
2290 WithPressure(1.f)),
2291 waitUntil));
2292
2293 // The external stylus did not generate any events.
2294 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2295 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2296}
2297
Michael Wrightd02c5b62014-02-10 15:10:22 -08002298// --- InputDeviceTest ---
2299class InputDeviceTest : public testing::Test {
2300protected:
2301 static const char* DEVICE_NAME;
2302 static const char* DEVICE_LOCATION;
2303 static const int32_t DEVICE_ID;
2304 static const int32_t DEVICE_GENERATION;
2305 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002306 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002307 static const int32_t EVENTHUB_ID;
2308 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2309
2310 std::shared_ptr<FakeEventHub> mFakeEventHub;
2311 sp<FakeInputReaderPolicy> mFakePolicy;
2312 std::unique_ptr<TestInputListener> mFakeListener;
2313 std::unique_ptr<InstrumentedInputReader> mReader;
2314 std::shared_ptr<InputDevice> mDevice;
2315
2316 void SetUp() override {
2317 mFakeEventHub = std::make_unique<FakeEventHub>();
2318 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2319 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002320 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002321 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002322 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002323 identifier.name = DEVICE_NAME;
2324 identifier.location = DEVICE_LOCATION;
2325 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2326 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2327 identifier);
2328 mReader->pushNextDevice(mDevice);
2329 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002330 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002331 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002332
2333 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002334 mFakeListener.reset();
2335 mFakePolicy.clear();
2336 }
2337};
2338
2339const char* InputDeviceTest::DEVICE_NAME = "device";
2340const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2341const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2342const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002343const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002344const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2345 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002346const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002347const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2348
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002349TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002350 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002351 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2352 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002353}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002354
Michael Wrightd02c5b62014-02-10 15:10:22 -08002355TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2356 ASSERT_EQ(mDevice->isEnabled(), false);
2357}
2358
2359TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2360 // Configuration.
2361 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002362 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002363
2364 // Reset.
2365 unused += mDevice->reset(ARBITRARY_TIME);
2366
2367 NotifyDeviceResetArgs resetArgs;
2368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2369 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2370 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2371
2372 // Metadata.
2373 ASSERT_TRUE(mDevice->isIgnored());
2374 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2375
2376 InputDeviceInfo info = mDevice->getDeviceInfo();
2377 ASSERT_EQ(DEVICE_ID, info.getId());
2378 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2379 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2380 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2381
2382 // State queries.
2383 ASSERT_EQ(0, mDevice->getMetaState());
2384
2385 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2386 << "Ignored device should return unknown key code state.";
2387 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2388 << "Ignored device should return unknown scan code state.";
2389 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2390 << "Ignored device should return unknown switch state.";
2391
2392 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2393 uint8_t flags[2] = { 0, 1 };
2394 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2395 << "Ignored device should never mark any key codes.";
2396 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2397 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2398}
2399
2400TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2401 // Configuration.
2402 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2403
2404 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002405 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2406 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002407 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2408 mapper1.setMetaState(AMETA_ALT_ON);
2409 mapper1.addSupportedKeyCode(AKEYCODE_A);
2410 mapper1.addSupportedKeyCode(AKEYCODE_B);
2411 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2412 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2413 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2414 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2415 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2416
2417 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002418 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2419 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002420 mapper2.setMetaState(AMETA_SHIFT_ON);
2421
2422 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002423 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424
Harry Cuttsf13161a2023-03-08 14:15:49 +00002425 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2426 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002427 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002428 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002429
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002430 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2431 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432
2433 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002434 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002435 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2436 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002437
2438 NotifyDeviceResetArgs resetArgs;
2439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2440 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2441 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2442
2443 // Metadata.
2444 ASSERT_FALSE(mDevice->isIgnored());
2445 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2446
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002447 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002448 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002449 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002450 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2451 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2452
2453 // State queries.
2454 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2455 << "Should query mappers and combine meta states.";
2456
2457 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2458 << "Should return unknown key code state when source not supported.";
2459 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2460 << "Should return unknown scan code state when source not supported.";
2461 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2462 << "Should return unknown switch state when source not supported.";
2463
2464 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2465 << "Should query mapper when source is supported.";
2466 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2467 << "Should query mapper when source is supported.";
2468 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2469 << "Should query mapper when source is supported.";
2470
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002471 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002473 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474 << "Should do nothing when source is unsupported.";
2475 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2476 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2477 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2478 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2479
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002480 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002481 << "Should query mapper when source is supported.";
2482 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2483 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2484 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2485 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2486
2487 // Event handling.
2488 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002489 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002490 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002491
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002492 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2493 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002494}
2495
Arthur Hung2c9a3342019-07-23 14:18:59 +08002496// A single input device is associated with a specific display. Check that:
2497// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh3e56f7e2023-07-07 13:12:37 +00002498// 2. Device is disabled when setEnabled API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002499TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002500 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2501 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002502
2503 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002504 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002505 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2506 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002507
2508 // Device should be enabled by default.
2509 ASSERT_TRUE(mDevice->isEnabled());
2510
2511 // Prepare associated info.
2512 constexpr uint8_t hdmi = 1;
2513 const std::string UNIQUE_ID = "local:1";
2514
2515 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002516 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002517 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002518 // Device should be disabled because it is associated with a specific display via
2519 // input port <-> display port association, but the corresponding display is not found
2520 ASSERT_FALSE(mDevice->isEnabled());
2521
2522 // Prepare displays.
2523 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002524 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002525 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002526 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002527 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002528 ASSERT_TRUE(mDevice->isEnabled());
2529
2530 // Device should be disabled after set disable.
2531 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002532 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002533 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002534 ASSERT_FALSE(mDevice->isEnabled());
2535
2536 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002537 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002538 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002539 ASSERT_FALSE(mDevice->isEnabled());
2540}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002541
Christine Franks1ba71cc2021-04-07 14:37:42 -07002542TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2543 // Device should be enabled by default.
2544 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002545 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2546 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002547 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002548 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2549 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002550 ASSERT_TRUE(mDevice->isEnabled());
2551
2552 // Device should be disabled because it is associated with a specific display, but the
2553 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002554 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002555 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002556 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002557 ASSERT_FALSE(mDevice->isEnabled());
2558
2559 // Device should be enabled when a display is found.
2560 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002561 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002562 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002563 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002564 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002565 ASSERT_TRUE(mDevice->isEnabled());
2566
2567 // Device should be disabled after set disable.
2568 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002569 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002570 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002571 ASSERT_FALSE(mDevice->isEnabled());
2572
2573 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002574 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002575 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002576 ASSERT_FALSE(mDevice->isEnabled());
2577}
2578
Christine Franks2a2293c2022-01-18 11:51:16 -08002579TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2580 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002581 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2582 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002583 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002584 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2585 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002586
Christine Franks2a2293c2022-01-18 11:51:16 -08002587 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2588 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002589 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002590 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002591 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002592 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002593 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2594}
2595
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002596/**
2597 * This test reproduces a crash caused by a dangling reference that remains after device is added
2598 * and removed. The reference is accessed in InputDevice::dump(..);
2599 */
2600TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2601 constexpr int32_t TEST_EVENTHUB_ID = 10;
2602 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2603
Harry Cutts33476232023-01-30 19:57:29 +00002604 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh7f1765e2023-07-07 13:12:37 +00002605 device.addEventHubDevice(TEST_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002606 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2607 std::string dumpStr, eventHubDevStr;
2608 device.dump(dumpStr, eventHubDevStr);
2609}
2610
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002611TEST_F(InputDeviceTest, GetBluetoothAddress) {
2612 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2613 ASSERT_TRUE(address);
2614 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2615}
2616
Michael Wrightd02c5b62014-02-10 15:10:22 -08002617// --- SwitchInputMapperTest ---
2618
2619class SwitchInputMapperTest : public InputMapperTest {
2620protected:
2621};
2622
2623TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002624 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002625
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002626 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002627}
2628
2629TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002630 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002631
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002632 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002633 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002634
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002635 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002636 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637}
2638
2639TEST_F(SwitchInputMapperTest, Process) {
Arpit Singh54a45f62023-04-26 16:12:10 +00002640 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002641 std::list<NotifyArgs> out;
2642 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2643 ASSERT_TRUE(out.empty());
2644 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2645 ASSERT_TRUE(out.empty());
2646 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2647 ASSERT_TRUE(out.empty());
2648 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002650 ASSERT_EQ(1u, out.size());
2651 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002652 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002653 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2654 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002655 args.switchMask);
2656 ASSERT_EQ(uint32_t(0), args.policyFlags);
2657}
2658
Chris Ye87143712020-11-10 05:05:58 +00002659// --- VibratorInputMapperTest ---
2660class VibratorInputMapperTest : public InputMapperTest {
2661protected:
2662 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2663};
2664
2665TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singhc68f85b2023-04-26 16:23:13 +00002666 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002667
2668 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2669}
2670
2671TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singhc68f85b2023-04-26 16:23:13 +00002672 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002673
2674 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2675}
2676
2677TEST_F(VibratorInputMapperTest, Vibrate) {
2678 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002679 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singhc68f85b2023-04-26 16:23:13 +00002680 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002681
2682 VibrationElement pattern(2);
2683 VibrationSequence sequence(2);
2684 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002685 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2686 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002687 sequence.addElement(pattern);
2688 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002689 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2690 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002691 sequence.addElement(pattern);
2692
2693 std::vector<int64_t> timings = {0, 1};
2694 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2695
2696 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002697 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002698 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002699 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002700 // Verify vibrator state listener was notified.
2701 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002702 ASSERT_EQ(1u, out.size());
2703 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2704 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2705 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002706 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002707 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002708 ASSERT_FALSE(mapper.isVibrating());
2709 // Verify vibrator state listener was notified.
2710 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002711 ASSERT_EQ(1u, out.size());
2712 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2713 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2714 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002715}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002716
Chris Yef59a2f42020-10-16 12:55:26 -07002717// --- SensorInputMapperTest ---
2718
2719class SensorInputMapperTest : public InputMapperTest {
2720protected:
2721 static const int32_t ACCEL_RAW_MIN;
2722 static const int32_t ACCEL_RAW_MAX;
2723 static const int32_t ACCEL_RAW_FUZZ;
2724 static const int32_t ACCEL_RAW_FLAT;
2725 static const int32_t ACCEL_RAW_RESOLUTION;
2726
2727 static const int32_t GYRO_RAW_MIN;
2728 static const int32_t GYRO_RAW_MAX;
2729 static const int32_t GYRO_RAW_FUZZ;
2730 static const int32_t GYRO_RAW_FLAT;
2731 static const int32_t GYRO_RAW_RESOLUTION;
2732
2733 static const float GRAVITY_MS2_UNIT;
2734 static const float DEGREE_RADIAN_UNIT;
2735
2736 void prepareAccelAxes();
2737 void prepareGyroAxes();
2738 void setAccelProperties();
2739 void setGyroProperties();
2740 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2741};
2742
2743const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2744const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2745const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2746const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2747const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2748
2749const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2750const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2751const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2752const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2753const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2754
2755const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2756const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2757
2758void SensorInputMapperTest::prepareAccelAxes() {
2759 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2760 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2761 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2762 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2763 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2764 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2765}
2766
2767void SensorInputMapperTest::prepareGyroAxes() {
2768 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2769 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2770 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2771 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2772 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2773 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2774}
2775
2776void SensorInputMapperTest::setAccelProperties() {
2777 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2778 /* sensorDataIndex */ 0);
2779 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2780 /* sensorDataIndex */ 1);
2781 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2782 /* sensorDataIndex */ 2);
2783 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2784 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2785 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2786 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2787 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2788}
2789
2790void SensorInputMapperTest::setGyroProperties() {
2791 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2792 /* sensorDataIndex */ 0);
2793 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2794 /* sensorDataIndex */ 1);
2795 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2796 /* sensorDataIndex */ 2);
2797 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2798 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2799 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2800 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2801 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2802}
2803
2804TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singh61dd33e2023-04-26 15:07:55 +00002805 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002806
2807 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2808}
2809
2810TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2811 setAccelProperties();
2812 prepareAccelAxes();
Arpit Singh61dd33e2023-04-26 15:07:55 +00002813 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002814
2815 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2816 std::chrono::microseconds(10000),
2817 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002818 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2820 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002824
2825 NotifySensorArgs args;
2826 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2827 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2828 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2829
2830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2831 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2832 ASSERT_EQ(args.deviceId, DEVICE_ID);
2833 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2834 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2835 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2836 ASSERT_EQ(args.values, values);
2837 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2838}
2839
2840TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2841 setGyroProperties();
2842 prepareGyroAxes();
Arpit Singh61dd33e2023-04-26 15:07:55 +00002843 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07002844
2845 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2846 std::chrono::microseconds(10000),
2847 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002848 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2850 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2852 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2853 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002854
2855 NotifySensorArgs args;
2856 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2857 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2858 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2859
2860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2861 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2862 ASSERT_EQ(args.deviceId, DEVICE_ID);
2863 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2864 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2865 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2866 ASSERT_EQ(args.values, values);
2867 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2868}
2869
Michael Wrightd02c5b62014-02-10 15:10:22 -08002870// --- KeyboardInputMapperTest ---
2871
2872class KeyboardInputMapperTest : public InputMapperTest {
2873protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002874 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002875 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002876 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002877
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002878 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002879 int32_t originalKeyCode, int32_t rotatedKeyCode,
2880 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881};
2882
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002883/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2884 * orientation.
2885 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002886void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002887 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2888 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002889}
2890
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002891void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002892 int32_t originalScanCode, int32_t originalKeyCode,
2893 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002894 NotifyKeyArgs args;
2895
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002896 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2898 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2899 ASSERT_EQ(originalScanCode, args.scanCode);
2900 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002901 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002902
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002903 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2905 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2906 ASSERT_EQ(originalScanCode, args.scanCode);
2907 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002908 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002909}
2910
Michael Wrightd02c5b62014-02-10 15:10:22 -08002911TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002912 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00002913 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002914 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002916 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002917}
2918
2919TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2920 const int32_t USAGE_A = 0x070004;
2921 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002922 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2923 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002924 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2925 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2926 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002927
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002928 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00002929 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002930 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002931 // Initial metastate is AMETA_NONE.
2932 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933
2934 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936 NotifyKeyArgs args;
2937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2938 ASSERT_EQ(DEVICE_ID, args.deviceId);
2939 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2940 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2941 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2942 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2943 ASSERT_EQ(KEY_HOME, args.scanCode);
2944 ASSERT_EQ(AMETA_NONE, args.metaState);
2945 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2946 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2947 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2948
2949 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002950 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2952 ASSERT_EQ(DEVICE_ID, args.deviceId);
2953 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2954 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2955 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2956 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2957 ASSERT_EQ(KEY_HOME, args.scanCode);
2958 ASSERT_EQ(AMETA_NONE, args.metaState);
2959 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2960 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2961 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2962
2963 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2965 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2967 ASSERT_EQ(DEVICE_ID, args.deviceId);
2968 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2969 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2970 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2971 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2972 ASSERT_EQ(0, args.scanCode);
2973 ASSERT_EQ(AMETA_NONE, args.metaState);
2974 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2975 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2976 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2977
2978 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002979 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2980 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2982 ASSERT_EQ(DEVICE_ID, args.deviceId);
2983 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2984 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2985 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2986 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2987 ASSERT_EQ(0, args.scanCode);
2988 ASSERT_EQ(AMETA_NONE, args.metaState);
2989 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2990 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2991 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2992
2993 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2997 ASSERT_EQ(DEVICE_ID, args.deviceId);
2998 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2999 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3000 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3001 ASSERT_EQ(0, args.keyCode);
3002 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3003 ASSERT_EQ(AMETA_NONE, args.metaState);
3004 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3005 ASSERT_EQ(0U, args.policyFlags);
3006 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3007
3008 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3010 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3012 ASSERT_EQ(DEVICE_ID, args.deviceId);
3013 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3014 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3015 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3016 ASSERT_EQ(0, args.keyCode);
3017 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3018 ASSERT_EQ(AMETA_NONE, args.metaState);
3019 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3020 ASSERT_EQ(0U, args.policyFlags);
3021 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3022}
3023
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003024TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3025 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3026 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3027 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3028
3029 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003030 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003031 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3032
3033 // Key down by scan code.
3034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3035 NotifyKeyArgs args;
3036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3037 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3038
3039 // Key up by scan code.
3040 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3042 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3043}
3044
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003045/**
3046 * Ensure that the readTime is set to the time when the EV_KEY is received.
3047 */
3048TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3049 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3050
3051 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003052 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003053 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3054 NotifyKeyArgs args;
3055
3056 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003057 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3059 ASSERT_EQ(12, args.readTime);
3060
3061 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003062 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3064 ASSERT_EQ(15, args.readTime);
3065}
3066
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003068 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3069 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003070 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3071 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3072 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003074 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003075 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003076 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077
Arthur Hung95f68612022-04-07 14:08:22 +08003078 // Initial metastate is AMETA_NONE.
3079 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080
3081 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083 NotifyKeyArgs args;
3084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3085 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003086 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003087 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088
3089 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003090 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3092 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003093 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094
3095 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003096 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3098 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003099 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100
3101 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003102 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3104 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003105 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003106 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107}
3108
3109TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003110 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3111 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3112 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3113 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003115 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003116 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003117 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118
Michael Wrighta9cf4192022-12-01 23:46:39 +00003119 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3121 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3122 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3123 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3124 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3125 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3126 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3127 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3128}
3129
3130TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003131 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3132 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3133 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3134 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003137 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003138 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003139 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003140
Michael Wrighta9cf4192022-12-01 23:46:39 +00003141 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003142 ASSERT_NO_FATAL_FAILURE(
3143 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3144 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3145 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3146 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3147 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3148 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3149 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003150
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003151 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003152 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003153 ASSERT_NO_FATAL_FAILURE(
3154 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3155 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3156 AKEYCODE_DPAD_UP, DISPLAY_ID));
3157 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3158 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3159 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3160 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003161
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003162 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003163 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003164 ASSERT_NO_FATAL_FAILURE(
3165 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3166 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3167 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3168 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3169 AKEYCODE_DPAD_UP, DISPLAY_ID));
3170 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3171 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003173 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003174 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003175 ASSERT_NO_FATAL_FAILURE(
3176 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3177 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3178 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3179 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3180 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3181 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3182 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183
3184 // Special case: if orientation changes while key is down, we still emit the same keycode
3185 // in the key up as we did in the key down.
3186 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003187 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003188 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3191 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3192 ASSERT_EQ(KEY_UP, args.scanCode);
3193 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3194
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003195 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003196 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3199 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3200 ASSERT_EQ(KEY_UP, args.scanCode);
3201 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3202}
3203
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003204TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3205 // If the keyboard is not orientation aware,
3206 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003207 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003208
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003209 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003210 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003211 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003212 NotifyKeyArgs args;
3213
3214 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3219 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3220
Michael Wrighta9cf4192022-12-01 23:46:39 +00003221 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003222 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3226 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3227}
3228
3229TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3230 // If the keyboard is orientation aware,
3231 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003232 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003233
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003234 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003235 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003236 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003237 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003238 NotifyKeyArgs args;
3239
3240 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3241 // ^--- already checked by the previous test
3242
Michael Wrighta9cf4192022-12-01 23:46:39 +00003243 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003244 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003245 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3249 ASSERT_EQ(DISPLAY_ID, args.displayId);
3250
3251 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003252 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003253 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003254 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
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(newDisplayId, args.displayId);
3260}
3261
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003263 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003264 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003267 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003268 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003269
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003270 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003271 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272}
3273
Philip Junker4af3b3d2021-12-14 10:36:55 +01003274TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3275 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003276 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003277 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3278
3279 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3280 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3281 << "If a mapping is available, the result is equal to the mapping";
3282
3283 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3284 << "If no mapping is available, the result is the key location";
3285}
3286
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003288 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003289 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003290 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003292 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003293 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003294
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003295 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003296 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003297}
3298
3299TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003300 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003301 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003302 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003303
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003304 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003305
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003307 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308 ASSERT_TRUE(flags[0]);
3309 ASSERT_FALSE(flags[1]);
3310}
3311
3312TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003313 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3314 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3315 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3316 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3317 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3318 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003320 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003321 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003322 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003323 // Initial metastate is AMETA_NONE.
3324 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325
3326 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003327 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3328 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3329 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330
3331 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003334 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3335 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3336 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003337 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338
3339 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003342 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3343 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3344 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003345 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346
3347 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003350 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3351 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3352 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003353 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354
3355 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003356 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003358 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3359 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3360 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003361 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362
3363 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003364 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3365 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003366 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3367 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3368 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003369 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003370
3371 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003374 ASSERT_FALSE(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_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003378}
3379
Chris Yea52ade12020-08-27 16:49:20 -07003380TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3381 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3382 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3383 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3384 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3385
3386 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003387 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003388 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3389
Chris Yea52ade12020-08-27 16:49:20 -07003390 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003391 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003392 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3393 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3394 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3395 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3396
3397 NotifyKeyArgs args;
3398 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3401 ASSERT_EQ(AMETA_NONE, args.metaState);
3402 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3403 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3404 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3405
3406 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003407 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3409 ASSERT_EQ(AMETA_NONE, args.metaState);
3410 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3411 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3412 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3413}
3414
Arthur Hung2c9a3342019-07-23 14:18:59 +08003415TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3416 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003417 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3418 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3419 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3420 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003421
3422 // keyboard 2.
3423 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003424 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003425 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003426 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003427 std::shared_ptr<InputDevice> device2 =
3428 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003429 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003430
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003431 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3432 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3433 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3434 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003435
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003436 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003437 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003438 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003439
Arpit Singh033e3ec2023-04-26 14:43:16 +00003440 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003441 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003442 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3443 mFakePolicy
3444 ->getReaderConfiguration(),
3445 AINPUT_SOURCE_KEYBOARD,
3446 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003447 std::list<NotifyArgs> unused =
3448 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003449 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003450 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003451
3452 // Prepared displays and associated info.
3453 constexpr uint8_t hdmi1 = 0;
3454 constexpr uint8_t hdmi2 = 1;
3455 const std::string SECONDARY_UNIQUE_ID = "local:1";
3456
3457 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3458 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3459
3460 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003461 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003462 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003463 ASSERT_FALSE(device2->isEnabled());
3464
3465 // Prepare second display.
3466 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003467 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003468 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003469 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003470 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003471 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003472 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003473 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003474
3475 // Device should be enabled after the associated display is found.
3476 ASSERT_TRUE(mDevice->isEnabled());
3477 ASSERT_TRUE(device2->isEnabled());
3478
3479 // Test pad key events
3480 ASSERT_NO_FATAL_FAILURE(
3481 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3482 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3483 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3484 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3485 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3486 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3487 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3488
3489 ASSERT_NO_FATAL_FAILURE(
3490 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3491 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3492 AKEYCODE_DPAD_RIGHT, newDisplayId));
3493 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3494 AKEYCODE_DPAD_DOWN, newDisplayId));
3495 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3496 AKEYCODE_DPAD_LEFT, newDisplayId));
3497}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498
arthurhungc903df12020-08-11 15:08:42 +08003499TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3500 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3501 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3502 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3503 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3504 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3505 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3506
3507 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003508 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003509 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003510 // Initial metastate is AMETA_NONE.
3511 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003512
3513 // Initialization should have turned all of the lights off.
3514 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3515 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3516 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3517
3518 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003521 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3522 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3523
3524 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003527 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3528 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3529
3530 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003533 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3534 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3535
3536 mFakeEventHub->removeDevice(EVENTHUB_ID);
3537 mReader->loopOnce();
3538
3539 // keyboard 2 should default toggle keys.
3540 const std::string USB2 = "USB2";
3541 const std::string DEVICE_NAME2 = "KEYBOARD2";
3542 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3543 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3544 std::shared_ptr<InputDevice> device2 =
3545 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003546 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003547 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3548 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3549 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3550 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3551 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3552 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3553
Arpit Singh033e3ec2023-04-26 14:43:16 +00003554 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003555 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003556 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3557 mFakePolicy
3558 ->getReaderConfiguration(),
3559 AINPUT_SOURCE_KEYBOARD,
3560 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003561 std::list<NotifyArgs> unused =
3562 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003563 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003564 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003565
3566 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3567 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3568 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003569 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3570 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003571}
3572
Arthur Hungcb40a002021-08-03 14:31:01 +00003573TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3574 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3575 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3576 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3577
3578 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh033e3ec2023-04-26 14:43:16 +00003579 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003580 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3581 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003582 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003583 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003584 // Initial metastate is AMETA_NONE.
3585 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003586
3587 mReader->toggleCapsLockState(DEVICE_ID);
3588 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3589}
3590
Arthur Hungfb3cc112022-04-13 07:39:50 +00003591TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3592 // keyboard 1.
3593 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3594 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3595 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3596 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3597 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3598 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3599
3600 KeyboardInputMapper& mapper1 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003601 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003602 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3603
3604 // keyboard 2.
3605 const std::string USB2 = "USB2";
3606 const std::string DEVICE_NAME2 = "KEYBOARD2";
3607 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3608 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3609 std::shared_ptr<InputDevice> device2 =
3610 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3611 ftl::Flags<InputDeviceClass>(0));
3612 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3613 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3614 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3615 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3616 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3617 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3618
Arpit Singh033e3ec2023-04-26 14:43:16 +00003619 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003620 KeyboardInputMapper& mapper2 =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003621 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3622 mFakePolicy
3623 ->getReaderConfiguration(),
3624 AINPUT_SOURCE_KEYBOARD,
3625 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003626 std::list<NotifyArgs> unused =
3627 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003628 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003629 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003630
Arthur Hung95f68612022-04-07 14:08:22 +08003631 // Initial metastate is AMETA_NONE.
3632 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3633 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3634
3635 // Toggle num lock on and off.
3636 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3637 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003638 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3639 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3640 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3641
3642 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3643 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3644 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3645 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3646 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3647
3648 // Toggle caps lock on and off.
3649 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3650 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3651 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3652 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3653 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3654
3655 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3656 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3657 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3658 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3659 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3660
3661 // Toggle scroll lock on and off.
3662 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3663 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3664 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3665 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3666 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3667
3668 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3669 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3670 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3671 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3672 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3673}
3674
Arthur Hung2141d542022-08-23 07:45:21 +00003675TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3676 const int32_t USAGE_A = 0x070004;
3677 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3678 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3679
3680 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003681 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003682 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3683 // Key down by scan code.
3684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3685 NotifyKeyArgs args;
3686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3687 ASSERT_EQ(DEVICE_ID, args.deviceId);
3688 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3689 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3690 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3691 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3692 ASSERT_EQ(KEY_HOME, args.scanCode);
3693 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3694
3695 // Disable device, it should synthesize cancellation events for down events.
3696 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003697 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003698
3699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3700 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3701 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3702 ASSERT_EQ(KEY_HOME, args.scanCode);
3703 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3704}
3705
Zixuan Qufecb6062022-11-12 04:44:31 +00003706TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh033e3ec2023-04-26 14:43:16 +00003707 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3708 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003709 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003710 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3711 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003712
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003713 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003714 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3715
3716 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003717 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003718
3719 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3720 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3721 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3722 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3723 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003724 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3725
3726 // Call change layout association with the same values: Generation shouldn't change
3727 generation = mReader->getContext()->getGeneration();
3728 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3729 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3730 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3731 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003732}
3733
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003734TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3735 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3736 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3737
3738 // Configuration
Arpit Singh033e3ec2023-04-26 14:43:16 +00003739 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003740 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3741 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003742 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003743
3744 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3745 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3746}
3747
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003748// --- KeyboardInputMapperTest_ExternalDevice ---
3749
3750class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3751protected:
Chris Yea52ade12020-08-27 16:49:20 -07003752 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003753};
3754
3755TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003756 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3757 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003758
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003759 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3760 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3761 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3762 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003763
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003764 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003765 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003766 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003767
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003769 NotifyKeyArgs args;
3770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3771 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3772
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003773 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3775 ASSERT_EQ(uint32_t(0), args.policyFlags);
3776
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003779 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003780
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003781 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3783 ASSERT_EQ(uint32_t(0), args.policyFlags);
3784
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3787 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3788
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003789 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3791 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3792}
3793
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003794TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003795 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003796
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003797 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3798 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3799 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003800
Powei Fengd041c5d2019-05-03 17:11:33 -07003801 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003802 KeyboardInputMapper& mapper =
Arpit Singh033e3ec2023-04-26 14:43:16 +00003803 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003804 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003805
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003807 NotifyKeyArgs args;
3808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3809 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3810
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003811 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3813 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3814
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3817 ASSERT_EQ(uint32_t(0), args.policyFlags);
3818
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003819 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3821 ASSERT_EQ(uint32_t(0), args.policyFlags);
3822
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3825 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3826
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003827 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3829 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3830}
3831
Michael Wrightd02c5b62014-02-10 15:10:22 -08003832// --- CursorInputMapperTest ---
3833
3834class CursorInputMapperTest : public InputMapperTest {
3835protected:
3836 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3837
Michael Wright17db18e2020-06-26 20:51:44 +01003838 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003839
Chris Yea52ade12020-08-27 16:49:20 -07003840 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003841 InputMapperTest::SetUp();
3842
Michael Wright17db18e2020-06-26 20:51:44 +01003843 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003844 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003845 }
3846
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003847 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3848 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003849
Michael Wrighta9cf4192022-12-01 23:46:39 +00003850 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003851 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3852 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3853 }
3854
3855 void prepareSecondaryDisplay() {
3856 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003857 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003858 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003859 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003860
3861 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3862 float pressure) {
3863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3864 0.0f, 0.0f, 0.0f, EPSILON));
3865 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866};
3867
3868const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3869
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003870void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3871 int32_t originalY, int32_t rotatedX,
3872 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003873 NotifyMotionArgs args;
3874
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3876 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3877 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3879 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003880 ASSERT_NO_FATAL_FAILURE(
3881 assertCursorPointerCoords(args.pointerCoords[0],
3882 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3883 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003884}
3885
3886TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003888 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003889
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003890 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003891}
3892
3893TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003894 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003895 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003896
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003897 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003898}
3899
3900TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003902 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003903
3904 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003905 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906
3907 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003908 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3909 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003910 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3911 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3912
3913 // When the bounds are set, then there should be a valid motion range.
3914 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3915
3916 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003917 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918
3919 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3920 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3921 1, 800 - 1, 0.0f, 0.0f));
3922 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3923 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3924 2, 480 - 1, 0.0f, 0.0f));
3925 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3926 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3927 0.0f, 1.0f, 0.0f, 0.0f));
3928}
3929
3930TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003932 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933
3934 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003935 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936
3937 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3938 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3939 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3940 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3941 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3942 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3943 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3944 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3945 0.0f, 1.0f, 0.0f, 0.0f));
3946}
3947
3948TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003949 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00003950 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003951
arthurhungdcef2dc2020-08-11 14:47:50 +08003952 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953
3954 NotifyMotionArgs args;
3955
3956 // Button press.
3957 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3961 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3962 ASSERT_EQ(DEVICE_ID, args.deviceId);
3963 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3964 ASSERT_EQ(uint32_t(0), args.policyFlags);
3965 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3966 ASSERT_EQ(0, args.flags);
3967 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3968 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3969 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07003970 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003971 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003972 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003973 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003974 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3975 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3976 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3977
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3979 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3980 ASSERT_EQ(DEVICE_ID, args.deviceId);
3981 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3982 ASSERT_EQ(uint32_t(0), args.policyFlags);
3983 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3984 ASSERT_EQ(0, args.flags);
3985 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3986 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3987 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07003988 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003989 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003990 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003991 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003992 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3993 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3994 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3995
Michael Wrightd02c5b62014-02-10 15:10:22 -08003996 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003997 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3998 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4000 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4001 ASSERT_EQ(DEVICE_ID, args.deviceId);
4002 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4003 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004004 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4005 ASSERT_EQ(0, args.flags);
4006 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4007 ASSERT_EQ(0, args.buttonState);
4008 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004009 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004010 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004011 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004012 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004013 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4014 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4015 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4016
4017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4018 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4019 ASSERT_EQ(DEVICE_ID, args.deviceId);
4020 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4021 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4023 ASSERT_EQ(0, args.flags);
4024 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4025 ASSERT_EQ(0, args.buttonState);
4026 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004027 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004028 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004029 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004030 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4032 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4033 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4034}
4035
4036TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004038 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039
4040 NotifyMotionArgs args;
4041
4042 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004047 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4048 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4049 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004050
4051 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4055 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004056 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4057 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004058}
4059
4060TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004062 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004063
4064 NotifyMotionArgs args;
4065
4066 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4070 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004071 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4074 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004075 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004076
Michael Wrightd02c5b62014-02-10 15:10:22 -08004077 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004081 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004082 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004083
4084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004086 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087}
4088
4089TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004091 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092
4093 NotifyMotionArgs args;
4094
4095 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4101 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004102 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4103 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4104 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004108 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4109 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4110 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004111
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004118 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4119 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4120 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121
4122 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004126 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004127 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004128
4129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004131 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132}
4133
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004134TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004135 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004136 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004137 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4138 // need to be rotated.
4139 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004140 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141
Michael Wrighta9cf4192022-12-01 23:46:39 +00004142 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4147 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4148 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4150 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4151}
4152
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004153TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004154 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004156 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4157 // orientation-aware are affected by display rotation.
Arpit Singhd8510bd2023-04-27 12:48:15 +00004158 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004160 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004161 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4163 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4164 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4165 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4166 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4167 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4168 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4169 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4170
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004171 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004172 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004173 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4174 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4175 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4176 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4177 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4178 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4179 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4180 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004182 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004183 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4187 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4188 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4189 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4190 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4191 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4192
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004193 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004194 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004195 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4196 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4197 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4198 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4199 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4200 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4201 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4202 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203}
4204
4205TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004207 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208
4209 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4210 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211
4212 NotifyMotionArgs motionArgs;
4213 NotifyKeyArgs keyArgs;
4214
4215 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4219 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4220 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004221 ASSERT_NO_FATAL_FAILURE(
4222 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4226 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004227 ASSERT_NO_FATAL_FAILURE(
4228 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004229
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004230 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004233 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004235 ASSERT_NO_FATAL_FAILURE(
4236 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237
4238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004239 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004241 ASSERT_NO_FATAL_FAILURE(
4242 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004243
4244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004246 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004247 ASSERT_NO_FATAL_FAILURE(
4248 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249
4250 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004251 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4252 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4255 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4256 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004257 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004258 ASSERT_NO_FATAL_FAILURE(
4259 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4262 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4263 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004264 ASSERT_NO_FATAL_FAILURE(
4265 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004266
4267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4268 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4269 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004270 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004271 ASSERT_NO_FATAL_FAILURE(
4272 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004273
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004277 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004279 ASSERT_NO_FATAL_FAILURE(
4280 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004281
4282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004284 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004285 ASSERT_NO_FATAL_FAILURE(
4286 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004291 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4292 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004293 ASSERT_NO_FATAL_FAILURE(
4294 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004297
4298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004301 ASSERT_NO_FATAL_FAILURE(
4302 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004303
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4305 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004307 ASSERT_NO_FATAL_FAILURE(
4308 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309
4310 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4312 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4314 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4315 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004316
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004318 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004320 ASSERT_NO_FATAL_FAILURE(
4321 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004322
4323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4324 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4325 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004326 ASSERT_NO_FATAL_FAILURE(
4327 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004332 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004334 ASSERT_NO_FATAL_FAILURE(
4335 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004336
4337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004339 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004340
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004341 ASSERT_NO_FATAL_FAILURE(
4342 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4344 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4345 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4346
4347 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004348 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4351 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4352 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004353
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004357 ASSERT_NO_FATAL_FAILURE(
4358 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004359
4360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4361 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4362 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004363 ASSERT_NO_FATAL_FAILURE(
4364 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004369 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004371 ASSERT_NO_FATAL_FAILURE(
4372 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004373
4374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4375 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4376 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004377 ASSERT_NO_FATAL_FAILURE(
4378 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004379
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4381 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4382 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4383
4384 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4388 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4389 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004390
Michael Wrightd02c5b62014-02-10 15:10:22 -08004391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004392 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004394 ASSERT_NO_FATAL_FAILURE(
4395 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004396
4397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4398 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4399 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004400 ASSERT_NO_FATAL_FAILURE(
4401 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004403 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004408 ASSERT_NO_FATAL_FAILURE(
4409 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410
4411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4412 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4413 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004414 ASSERT_NO_FATAL_FAILURE(
4415 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004416
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4418 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4419 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4420
4421 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4425 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4426 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004427
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004429 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004431 ASSERT_NO_FATAL_FAILURE(
4432 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433
4434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4435 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4436 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004437 ASSERT_NO_FATAL_FAILURE(
4438 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004443 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004445 ASSERT_NO_FATAL_FAILURE(
4446 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004447
4448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4449 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4450 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004451 ASSERT_NO_FATAL_FAILURE(
4452 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004453
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4455 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4456 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4457}
4458
4459TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004461 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004462
4463 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4464 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465
4466 NotifyMotionArgs args;
4467
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4470 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004472 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4473 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4475 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 +00004476 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004477}
4478
4479TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004480 addConfigurationProperty("cursor.mode", "pointer");
4481 mFakePolicy->setPointerCapture(true);
Arpit Singhd8510bd2023-04-27 12:48:15 +00004482 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004483
4484 NotifyDeviceResetArgs resetArgs;
4485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4486 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4487 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4488
4489 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4490 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004491
4492 NotifyMotionArgs args;
4493
4494 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4499 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4500 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4502 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 +00004503 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004504
4505 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4509 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4510 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4512 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4514 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4515 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4517 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4518
4519 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004520 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4521 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4523 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4526 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4528 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4529 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4531 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4532
4533 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4538 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4541 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 +00004542 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004543
4544 // Disable pointer capture and check that the device generation got bumped
4545 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004546 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004547 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004548 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004549 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004550
4551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004552 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4553
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4558 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4561 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 +00004562 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004563}
4564
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004565/**
4566 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4567 * pointer acceleration or speed processing should not be applied.
4568 */
4569TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4570 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004571 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4572 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004573 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhd8510bd2023-04-27 12:48:15 +00004574 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004575
4576 NotifyDeviceResetArgs resetArgs;
4577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4578 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4579 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4580
4581 NotifyMotionArgs args;
4582
4583 // Move and verify scale is applied.
4584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4588 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4589 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4590 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4591 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4592 ASSERT_GT(relX, 10);
4593 ASSERT_GT(relY, 20);
4594
4595 // Enable Pointer Capture
4596 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004597 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004598 NotifyPointerCaptureChangedArgs captureArgs;
4599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4600 ASSERT_TRUE(captureArgs.request.enable);
4601
4602 // Move and verify scale is not applied.
4603 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4604 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4607 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4609 ASSERT_EQ(10, args.pointerCoords[0].getX());
4610 ASSERT_EQ(20, args.pointerCoords[0].getY());
4611}
4612
Prabir Pradhan208360b2022-06-24 18:37:04 +00004613TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4614 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004615 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004616
4617 NotifyDeviceResetArgs resetArgs;
4618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4619 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4620 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4621
4622 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004623 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004624
4625 NotifyMotionArgs args;
4626
4627 // Verify that the coordinates are rotated.
4628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4632 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4633 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4634 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4635 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4636
4637 // Enable Pointer Capture.
4638 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004639 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004640 NotifyPointerCaptureChangedArgs captureArgs;
4641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4642 ASSERT_TRUE(captureArgs.request.enable);
4643
4644 // Move and verify rotation is not applied.
4645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4646 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4649 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4651 ASSERT_EQ(10, args.pointerCoords[0].getX());
4652 ASSERT_EQ(20, args.pointerCoords[0].getY());
4653}
4654
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004655TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004656 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004657
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004658 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004659 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004660
4661 // Set up the secondary display as the display on which the pointer should be shown.
4662 // The InputDevice is not associated with any display.
4663 prepareSecondaryDisplay();
4664 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004665 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004666
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004667 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004668 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004669
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004670 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004671 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4672 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004675 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4676 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4677 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004678 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004679}
4680
4681TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004682 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004683
4684 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004685 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004686
4687 // Set up the secondary display as the display on which the pointer should be shown,
4688 // and associate the InputDevice with the secondary display.
4689 prepareSecondaryDisplay();
4690 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4691 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004692 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004693
4694 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4695 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004696
4697 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4698 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004701 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4702 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4703 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004704 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004705}
4706
4707TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhd8510bd2023-04-27 12:48:15 +00004708 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004709
4710 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004711 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004712 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4713
4714 // Associate the InputDevice with the secondary display.
4715 prepareSecondaryDisplay();
4716 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004717 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004718
4719 // The mapper should not generate any events because it is associated with a display that is
4720 // different from the pointer display.
4721 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4722 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004725}
4726
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004727// --- BluetoothCursorInputMapperTest ---
4728
4729class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4730protected:
4731 void SetUp() override {
4732 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4733
4734 mFakePointerController = std::make_shared<FakePointerController>();
4735 mFakePolicy->setPointerController(mFakePointerController);
4736 }
4737};
4738
4739TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4740 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004741 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004742
4743 nsecs_t kernelEventTime = ARBITRARY_TIME;
4744 nsecs_t expectedEventTime = ARBITRARY_TIME;
4745 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4746 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4748 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4749 WithEventTime(expectedEventTime))));
4750
4751 // Process several events that come in quick succession, according to their timestamps.
4752 for (int i = 0; i < 3; i++) {
4753 constexpr static nsecs_t delta = ms2ns(1);
4754 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4755 kernelEventTime += delta;
4756 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4757
4758 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4759 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4761 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4762 WithEventTime(expectedEventTime))));
4763 }
4764}
4765
4766TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4767 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004768 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004769
4770 nsecs_t expectedEventTime = ARBITRARY_TIME;
4771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4774 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4775 WithEventTime(expectedEventTime))));
4776
4777 // Process several events with the same timestamp from the kernel.
4778 // Ensure that we do not generate events too far into the future.
4779 constexpr static int32_t numEvents =
4780 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4781 for (int i = 0; i < numEvents; i++) {
4782 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4783
4784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4787 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4788 WithEventTime(expectedEventTime))));
4789 }
4790
4791 // By processing more events with the same timestamp, we should not generate events with a
4792 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4793 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4794 for (int i = 0; i < 3; i++) {
4795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4798 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4799 WithEventTime(cappedEventTime))));
4800 }
4801}
4802
4803TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4804 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhd8510bd2023-04-27 12:48:15 +00004805 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004806
4807 nsecs_t kernelEventTime = ARBITRARY_TIME;
4808 nsecs_t expectedEventTime = ARBITRARY_TIME;
4809 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4810 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4812 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4813 WithEventTime(expectedEventTime))));
4814
4815 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4816 // smoothening is not needed, its timestamp is not affected.
4817 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4818 expectedEventTime = kernelEventTime;
4819
4820 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4821 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4823 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4824 WithEventTime(expectedEventTime))));
4825}
4826
Michael Wrightd02c5b62014-02-10 15:10:22 -08004827// --- TouchInputMapperTest ---
4828
4829class TouchInputMapperTest : public InputMapperTest {
4830protected:
4831 static const int32_t RAW_X_MIN;
4832 static const int32_t RAW_X_MAX;
4833 static const int32_t RAW_Y_MIN;
4834 static const int32_t RAW_Y_MAX;
4835 static const int32_t RAW_TOUCH_MIN;
4836 static const int32_t RAW_TOUCH_MAX;
4837 static const int32_t RAW_TOOL_MIN;
4838 static const int32_t RAW_TOOL_MAX;
4839 static const int32_t RAW_PRESSURE_MIN;
4840 static const int32_t RAW_PRESSURE_MAX;
4841 static const int32_t RAW_ORIENTATION_MIN;
4842 static const int32_t RAW_ORIENTATION_MAX;
4843 static const int32_t RAW_DISTANCE_MIN;
4844 static const int32_t RAW_DISTANCE_MAX;
4845 static const int32_t RAW_TILT_MIN;
4846 static const int32_t RAW_TILT_MAX;
4847 static const int32_t RAW_ID_MIN;
4848 static const int32_t RAW_ID_MAX;
4849 static const int32_t RAW_SLOT_MIN;
4850 static const int32_t RAW_SLOT_MAX;
4851 static const float X_PRECISION;
4852 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004853 static const float X_PRECISION_VIRTUAL;
4854 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855
4856 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004857 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004858
4859 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4860
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004861 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004862 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004863
Michael Wrightd02c5b62014-02-10 15:10:22 -08004864 enum Axes {
4865 POSITION = 1 << 0,
4866 TOUCH = 1 << 1,
4867 TOOL = 1 << 2,
4868 PRESSURE = 1 << 3,
4869 ORIENTATION = 1 << 4,
4870 MINOR = 1 << 5,
4871 ID = 1 << 6,
4872 DISTANCE = 1 << 7,
4873 TILT = 1 << 8,
4874 SLOT = 1 << 9,
4875 TOOL_TYPE = 1 << 10,
4876 };
4877
Michael Wrighta9cf4192022-12-01 23:46:39 +00004878 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004879 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004880 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004881 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004882 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883 int32_t toRawX(float displayX);
4884 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004885 int32_t toRotatedRawX(float displayX);
4886 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004887 float toCookedX(float rawX, float rawY);
4888 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004890 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004891 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004892 float toDisplayY(int32_t rawY, int32_t displayHeight);
4893
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894};
4895
4896const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4897const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4898const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4899const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4900const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4901const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4902const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4903const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004904const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4905const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4907const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4908const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4909const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4910const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4911const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4912const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4913const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4914const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4915const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4916const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4917const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004918const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4919 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4920const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4921 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004922const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4923 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924
4925const float TouchInputMapperTest::GEOMETRIC_SCALE =
4926 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4927 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4928
4929const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4930 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4931 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4932};
4933
Michael Wrighta9cf4192022-12-01 23:46:39 +00004934void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004935 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4936 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004937}
4938
4939void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4940 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004941 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004942}
4943
Michael Wrighta9cf4192022-12-01 23:46:39 +00004944void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004945 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4946 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4947 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004948}
4949
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004951 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4952 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4953 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4954 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004955}
4956
Jason Gerecke489fda82012-09-07 17:19:40 -07004957void TouchInputMapperTest::prepareLocationCalibration() {
4958 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4959}
4960
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961int32_t TouchInputMapperTest::toRawX(float displayX) {
4962 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4963}
4964
4965int32_t TouchInputMapperTest::toRawY(float displayY) {
4966 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4967}
4968
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004969int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4970 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4971}
4972
4973int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4974 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4975}
4976
Jason Gerecke489fda82012-09-07 17:19:40 -07004977float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4978 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4979 return rawX;
4980}
4981
4982float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4983 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4984 return rawY;
4985}
4986
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004988 return toDisplayX(rawX, DISPLAY_WIDTH);
4989}
4990
4991float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4992 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993}
4994
4995float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004996 return toDisplayY(rawY, DISPLAY_HEIGHT);
4997}
4998
4999float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5000 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001}
5002
5003
5004// --- SingleTouchInputMapperTest ---
5005
5006class SingleTouchInputMapperTest : public TouchInputMapperTest {
5007protected:
5008 void prepareButtons();
5009 void prepareAxes(int axes);
5010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005011 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5012 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5013 void processUp(SingleTouchInputMapper& mappery);
5014 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5015 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5016 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5017 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5018 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5019 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020};
5021
5022void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005023 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005024}
5025
5026void SingleTouchInputMapperTest::prepareAxes(int axes) {
5027 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005028 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5029 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030 }
5031 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005032 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5033 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034 }
5035 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005036 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5037 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038 }
5039 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005040 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5041 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042 }
5043 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005044 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5045 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046 }
5047}
5048
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005049void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005053}
5054
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005055void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5057 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058}
5059
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005060void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062}
5063
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005064void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066}
5067
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005068void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5069 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005071}
5072
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005073void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075}
5076
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005077void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5078 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005081}
5082
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005083void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5084 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086}
5087
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005088void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005089 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005090}
5091
Michael Wrightd02c5b62014-02-10 15:10:22 -08005092TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005093 prepareButtons();
5094 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00005095 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005096
Josep del Río2d8c79a2023-01-23 19:33:50 +00005097 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005098}
5099
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005101 prepareButtons();
5102 prepareAxes(POSITION);
5103 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00005104 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005106 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107}
5108
5109TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005111 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005112 prepareButtons();
5113 prepareAxes(POSITION);
5114 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005115 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116
5117 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005118 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119
5120 // Virtual key is down.
5121 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5122 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5123 processDown(mapper, x, y);
5124 processSync(mapper);
5125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5126
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005127 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005128
5129 // Virtual key is up.
5130 processUp(mapper);
5131 processSync(mapper);
5132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5133
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005134 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005135}
5136
5137TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005139 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005140 prepareButtons();
5141 prepareAxes(POSITION);
5142 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005143 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005144
5145 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005146 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005147
5148 // Virtual key is down.
5149 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5150 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5151 processDown(mapper, x, y);
5152 processSync(mapper);
5153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5154
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005155 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156
5157 // Virtual key is up.
5158 processUp(mapper);
5159 processSync(mapper);
5160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5161
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005162 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005163}
5164
5165TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005167 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005168 prepareButtons();
5169 prepareAxes(POSITION);
5170 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005171 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005174 ASSERT_TRUE(
5175 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176 ASSERT_TRUE(flags[0]);
5177 ASSERT_FALSE(flags[1]);
5178}
5179
5180TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005181 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005182 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183 prepareButtons();
5184 prepareAxes(POSITION);
5185 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005186 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005187
arthurhungdcef2dc2020-08-11 14:47:50 +08005188 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189
5190 NotifyKeyArgs args;
5191
5192 // Press virtual key.
5193 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5194 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5195 processDown(mapper, x, y);
5196 processSync(mapper);
5197
5198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5199 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5200 ASSERT_EQ(DEVICE_ID, args.deviceId);
5201 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5202 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5203 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5204 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5205 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5206 ASSERT_EQ(KEY_HOME, args.scanCode);
5207 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5208 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5209
5210 // Release virtual key.
5211 processUp(mapper);
5212 processSync(mapper);
5213
5214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5215 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5216 ASSERT_EQ(DEVICE_ID, args.deviceId);
5217 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5218 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5219 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5220 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5221 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5222 ASSERT_EQ(KEY_HOME, args.scanCode);
5223 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5224 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5225
5226 // Should not have sent any motions.
5227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5228}
5229
5230TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005231 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005232 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005233 prepareButtons();
5234 prepareAxes(POSITION);
5235 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005236 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005237
arthurhungdcef2dc2020-08-11 14:47:50 +08005238 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005239
5240 NotifyKeyArgs keyArgs;
5241
5242 // Press virtual key.
5243 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5244 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5245 processDown(mapper, x, y);
5246 processSync(mapper);
5247
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5249 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5250 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5251 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5252 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5253 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5254 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5255 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5256 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5257 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5258 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5259
5260 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5261 // into the display area.
5262 y -= 100;
5263 processMove(mapper, x, y);
5264 processSync(mapper);
5265
5266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5267 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5268 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5269 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5270 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5271 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5272 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5273 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5274 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5275 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5276 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5277 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5278
5279 NotifyMotionArgs motionArgs;
5280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5281 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5282 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5283 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5284 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5285 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5286 ASSERT_EQ(0, motionArgs.flags);
5287 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5288 ASSERT_EQ(0, motionArgs.buttonState);
5289 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005290 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005291 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005292 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5294 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5295 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5296 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5297 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5298
5299 // Keep moving out of bounds. Should generate a pointer move.
5300 y -= 50;
5301 processMove(mapper, x, y);
5302 processSync(mapper);
5303
5304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5305 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5306 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5307 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5308 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5310 ASSERT_EQ(0, motionArgs.flags);
5311 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5312 ASSERT_EQ(0, motionArgs.buttonState);
5313 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005314 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005315 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005316 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5318 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5319 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5320 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5321 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5322
5323 // Release out of bounds. Should generate a pointer up.
5324 processUp(mapper);
5325 processSync(mapper);
5326
5327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5328 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5329 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5330 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5331 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5332 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5333 ASSERT_EQ(0, motionArgs.flags);
5334 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5335 ASSERT_EQ(0, motionArgs.buttonState);
5336 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005337 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005338 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005339 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5341 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5342 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5343 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5344 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5345
5346 // Should not have sent any more keys or motions.
5347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5349}
5350
5351TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005352 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005353 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354 prepareButtons();
5355 prepareAxes(POSITION);
5356 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005357 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358
arthurhungdcef2dc2020-08-11 14:47:50 +08005359 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360
5361 NotifyMotionArgs motionArgs;
5362
5363 // Initially go down out of bounds.
5364 int32_t x = -10;
5365 int32_t y = -10;
5366 processDown(mapper, x, y);
5367 processSync(mapper);
5368
5369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5370
5371 // Move into the display area. Should generate a pointer down.
5372 x = 50;
5373 y = 75;
5374 processMove(mapper, x, y);
5375 processSync(mapper);
5376
5377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5378 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5379 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5380 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5381 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5382 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5383 ASSERT_EQ(0, motionArgs.flags);
5384 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5385 ASSERT_EQ(0, motionArgs.buttonState);
5386 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005387 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005389 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5391 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5392 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5393 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5394 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5395
5396 // Release. Should generate a pointer up.
5397 processUp(mapper);
5398 processSync(mapper);
5399
5400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5401 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5402 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5403 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5404 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5405 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5406 ASSERT_EQ(0, motionArgs.flags);
5407 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5408 ASSERT_EQ(0, motionArgs.buttonState);
5409 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005410 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005412 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5414 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5415 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5416 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5417 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5418
5419 // Should not have sent any more keys or motions.
5420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5422}
5423
Santos Cordonfa5cf462017-04-05 10:37:00 -07005424TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005425 addConfigurationProperty("touch.deviceType", "touchScreen");
5426 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5427
Michael Wrighta9cf4192022-12-01 23:46:39 +00005428 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005429 prepareButtons();
5430 prepareAxes(POSITION);
5431 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005432 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005433
arthurhungdcef2dc2020-08-11 14:47:50 +08005434 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005435
5436 NotifyMotionArgs motionArgs;
5437
5438 // Down.
5439 int32_t x = 100;
5440 int32_t y = 125;
5441 processDown(mapper, x, y);
5442 processSync(mapper);
5443
5444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5445 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5446 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5447 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5448 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5449 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5450 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5451 ASSERT_EQ(0, motionArgs.flags);
5452 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5453 ASSERT_EQ(0, motionArgs.buttonState);
5454 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005455 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005456 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005457 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5459 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5460 1, 0, 0, 0, 0, 0, 0, 0));
5461 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5462 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5463 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5464
5465 // Move.
5466 x += 50;
5467 y += 75;
5468 processMove(mapper, x, y);
5469 processSync(mapper);
5470
5471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5472 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5473 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5474 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5475 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5476 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5478 ASSERT_EQ(0, motionArgs.flags);
5479 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5480 ASSERT_EQ(0, motionArgs.buttonState);
5481 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005482 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005483 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005484 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5486 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5487 1, 0, 0, 0, 0, 0, 0, 0));
5488 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5489 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5490 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5491
5492 // Up.
5493 processUp(mapper);
5494 processSync(mapper);
5495
5496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5497 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5498 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5499 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5500 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5501 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5502 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5503 ASSERT_EQ(0, motionArgs.flags);
5504 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5505 ASSERT_EQ(0, motionArgs.buttonState);
5506 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005507 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005508 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005509 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5511 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5512 1, 0, 0, 0, 0, 0, 0, 0));
5513 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5514 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5515 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5516
5517 // Should not have sent any more keys or motions.
5518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5520}
5521
Michael Wrightd02c5b62014-02-10 15:10:22 -08005522TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005523 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005524 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005525 prepareButtons();
5526 prepareAxes(POSITION);
5527 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00005528 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005529
arthurhungdcef2dc2020-08-11 14:47:50 +08005530 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005531
5532 NotifyMotionArgs motionArgs;
5533
5534 // Down.
5535 int32_t x = 100;
5536 int32_t y = 125;
5537 processDown(mapper, x, y);
5538 processSync(mapper);
5539
5540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5541 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5542 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5543 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5544 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5545 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5546 ASSERT_EQ(0, motionArgs.flags);
5547 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5548 ASSERT_EQ(0, motionArgs.buttonState);
5549 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005550 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005551 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005552 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5554 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5555 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5556 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5557 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5558
5559 // Move.
5560 x += 50;
5561 y += 75;
5562 processMove(mapper, x, y);
5563 processSync(mapper);
5564
5565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5566 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5567 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5568 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5569 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5570 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5571 ASSERT_EQ(0, motionArgs.flags);
5572 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5573 ASSERT_EQ(0, motionArgs.buttonState);
5574 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005575 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005576 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005577 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5579 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5580 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5581 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5582 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5583
5584 // Up.
5585 processUp(mapper);
5586 processSync(mapper);
5587
5588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5589 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5590 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5591 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5592 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5593 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5594 ASSERT_EQ(0, motionArgs.flags);
5595 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5596 ASSERT_EQ(0, motionArgs.buttonState);
5597 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005598 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005600 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5602 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5603 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5604 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5605 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5606
5607 // Should not have sent any more keys or motions.
5608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5610}
5611
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005612TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005613 addConfigurationProperty("touch.deviceType", "touchScreen");
5614 prepareButtons();
5615 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005616 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5617 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singh56adebc2023-04-25 13:56:05 +00005618 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005619
5620 NotifyMotionArgs args;
5621
5622 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005623 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005624 processDown(mapper, toRawX(50), toRawY(75));
5625 processSync(mapper);
5626
5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5628 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5629 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5630
5631 processUp(mapper);
5632 processSync(mapper);
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5634}
5635
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005636TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005637 addConfigurationProperty("touch.deviceType", "touchScreen");
5638 prepareButtons();
5639 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005640 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5641 // orientation-aware are affected by display rotation.
5642 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00005643 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005644
5645 NotifyMotionArgs args;
5646
5647 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005648 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005649 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005650 processDown(mapper, toRawX(50), toRawY(75));
5651 processSync(mapper);
5652
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5654 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5655 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5656
5657 processUp(mapper);
5658 processSync(mapper);
5659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5660
5661 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005662 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005663 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005664 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005665 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 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005676 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005677 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005678 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5679 processSync(mapper);
5680
5681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5682 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5683 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5684
5685 processUp(mapper);
5686 processSync(mapper);
5687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5688
5689 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005690 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005691 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005692 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005693 processSync(mapper);
5694
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5696 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5697 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5698
5699 processUp(mapper);
5700 processSync(mapper);
5701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5702}
5703
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005704TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5705 addConfigurationProperty("touch.deviceType", "touchScreen");
5706 prepareButtons();
5707 prepareAxes(POSITION);
5708 addConfigurationProperty("touch.orientationAware", "1");
5709 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5710 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005711 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005712 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005713 NotifyMotionArgs args;
5714
5715 // Orientation 0.
5716 processDown(mapper, toRawX(50), toRawY(75));
5717 processSync(mapper);
5718
5719 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5720 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5721 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5722
5723 processUp(mapper);
5724 processSync(mapper);
5725 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5726}
5727
5728TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5729 addConfigurationProperty("touch.deviceType", "touchScreen");
5730 prepareButtons();
5731 prepareAxes(POSITION);
5732 addConfigurationProperty("touch.orientationAware", "1");
5733 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5734 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005735 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005736 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005737 NotifyMotionArgs args;
5738
5739 // Orientation 90.
5740 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5741 processSync(mapper);
5742
5743 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5744 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5745 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5746
5747 processUp(mapper);
5748 processSync(mapper);
5749 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5750}
5751
5752TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5753 addConfigurationProperty("touch.deviceType", "touchScreen");
5754 prepareButtons();
5755 prepareAxes(POSITION);
5756 addConfigurationProperty("touch.orientationAware", "1");
5757 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5758 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005759 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005760 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005761 NotifyMotionArgs args;
5762
5763 // Orientation 180.
5764 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5765 processSync(mapper);
5766
5767 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5768 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5769 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5770
5771 processUp(mapper);
5772 processSync(mapper);
5773 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5774}
5775
5776TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5777 addConfigurationProperty("touch.deviceType", "touchScreen");
5778 prepareButtons();
5779 prepareAxes(POSITION);
5780 addConfigurationProperty("touch.orientationAware", "1");
5781 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5782 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005783 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005784 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005785 NotifyMotionArgs args;
5786
5787 // Orientation 270.
5788 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5789 processSync(mapper);
5790
5791 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5792 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5793 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5794
5795 processUp(mapper);
5796 processSync(mapper);
5797 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5798}
5799
5800TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5801 addConfigurationProperty("touch.deviceType", "touchScreen");
5802 prepareButtons();
5803 prepareAxes(POSITION);
5804 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5805 // orientation-aware are affected by display rotation.
5806 addConfigurationProperty("touch.orientationAware", "0");
5807 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singh56adebc2023-04-25 13:56:05 +00005808 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005809
5810 NotifyMotionArgs args;
5811
5812 // Orientation 90, Rotation 0.
5813 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005814 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005815 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5816 processSync(mapper);
5817
5818 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5819 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5820 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5821
5822 processUp(mapper);
5823 processSync(mapper);
5824 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5825
5826 // Orientation 90, Rotation 90.
5827 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005828 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005829 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005830 processSync(mapper);
5831
5832 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5833 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5834 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5835
5836 processUp(mapper);
5837 processSync(mapper);
5838 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5839
5840 // Orientation 90, Rotation 180.
5841 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005842 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005843 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5844 processSync(mapper);
5845
5846 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5847 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5848 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5849
5850 processUp(mapper);
5851 processSync(mapper);
5852 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5853
5854 // Orientation 90, Rotation 270.
5855 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005856 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005857 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 -07005858 processSync(mapper);
5859
5860 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5861 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5862 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5863
5864 processUp(mapper);
5865 processSync(mapper);
5866 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5867}
5868
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005869TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5870 addConfigurationProperty("touch.deviceType", "touchScreen");
5871 prepareButtons();
5872 prepareAxes(POSITION);
5873 addConfigurationProperty("touch.orientationAware", "1");
5874 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00005875 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005876
5877 // Set a physical frame in the display viewport.
5878 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5879 viewport->physicalLeft = 20;
5880 viewport->physicalTop = 600;
5881 viewport->physicalRight = 30;
5882 viewport->physicalBottom = 610;
5883 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005884 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005885
5886 // Start the touch.
5887 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5888 processSync(mapper);
5889
5890 // Expect all input starting outside the physical frame to be ignored.
5891 const std::array<Point, 6> outsidePoints = {
5892 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5893 for (const auto& p : outsidePoints) {
5894 processMove(mapper, toRawX(p.x), toRawY(p.y));
5895 processSync(mapper);
5896 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5897 }
5898
5899 // Move the touch into the physical frame.
5900 processMove(mapper, toRawX(25), toRawY(605));
5901 processSync(mapper);
5902 NotifyMotionArgs args;
5903 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5904 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5905 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5906 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5907
5908 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5909 for (const auto& p : outsidePoints) {
5910 processMove(mapper, toRawX(p.x), toRawY(p.y));
5911 processSync(mapper);
5912 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5913 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5914 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5915 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5916 }
5917
5918 processUp(mapper);
5919 processSync(mapper);
5920 EXPECT_NO_FATAL_FAILURE(
5921 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5922}
5923
Harry Cutts1db43992023-06-19 17:05:07 +00005924TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
5925 std::shared_ptr<FakePointerController> fakePointerController =
5926 std::make_shared<FakePointerController>();
5927 mFakePolicy->setPointerController(fakePointerController);
5928
5929 addConfigurationProperty("touch.deviceType", "pointer");
5930 prepareAxes(POSITION);
5931 prepareDisplay(ui::ROTATION_0);
5932 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5933
5934 // Set a physical frame in the display viewport.
5935 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5936 viewport->physicalLeft = 20;
5937 viewport->physicalTop = 600;
5938 viewport->physicalRight = 30;
5939 viewport->physicalBottom = 610;
5940 mFakePolicy->updateViewport(*viewport);
5941 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5942
5943 // Start the touch.
5944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5945 processSync(mapper);
5946
5947 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5948 // produced.
5949 const std::array<Point, 6> outsidePoints = {
5950 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5951 for (const auto& p : outsidePoints) {
5952 processMove(mapper, toRawX(p.x), toRawY(p.y));
5953 processSync(mapper);
5954 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5955 }
5956}
5957
Michael Wrightd02c5b62014-02-10 15:10:22 -08005958TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005959 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005960 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005961 prepareButtons();
5962 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singh56adebc2023-04-25 13:56:05 +00005963 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005964
5965 // These calculations are based on the input device calibration documentation.
5966 int32_t rawX = 100;
5967 int32_t rawY = 200;
5968 int32_t rawPressure = 10;
5969 int32_t rawToolMajor = 12;
5970 int32_t rawDistance = 2;
5971 int32_t rawTiltX = 30;
5972 int32_t rawTiltY = 110;
5973
5974 float x = toDisplayX(rawX);
5975 float y = toDisplayY(rawY);
5976 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5977 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5978 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5979 float distance = float(rawDistance);
5980
5981 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5982 float tiltScale = M_PI / 180;
5983 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5984 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5985 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5986 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5987
5988 processDown(mapper, rawX, rawY);
5989 processPressure(mapper, rawPressure);
5990 processToolMajor(mapper, rawToolMajor);
5991 processDistance(mapper, rawDistance);
5992 processTilt(mapper, rawTiltX, rawTiltY);
5993 processSync(mapper);
5994
5995 NotifyMotionArgs args;
5996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5998 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5999 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6000}
6001
Jason Gerecke489fda82012-09-07 17:19:40 -07006002TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006003 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006004 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006005 prepareLocationCalibration();
6006 prepareButtons();
6007 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006008 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006009
6010 int32_t rawX = 100;
6011 int32_t rawY = 200;
6012
6013 float x = toDisplayX(toCookedX(rawX, rawY));
6014 float y = toDisplayY(toCookedY(rawX, rawY));
6015
6016 processDown(mapper, rawX, rawY);
6017 processSync(mapper);
6018
6019 NotifyMotionArgs args;
6020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6022 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6023}
6024
Michael Wrightd02c5b62014-02-10 15:10:22 -08006025TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006026 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006027 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006028 prepareButtons();
6029 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006030 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031
6032 NotifyMotionArgs motionArgs;
6033 NotifyKeyArgs keyArgs;
6034
6035 processDown(mapper, 100, 200);
6036 processSync(mapper);
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6038 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6039 ASSERT_EQ(0, motionArgs.buttonState);
6040
6041 // press BTN_LEFT, release BTN_LEFT
6042 processKey(mapper, BTN_LEFT, 1);
6043 processSync(mapper);
6044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6046 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6047
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6050 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6051
Michael Wrightd02c5b62014-02-10 15:10:22 -08006052 processKey(mapper, BTN_LEFT, 0);
6053 processSync(mapper);
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006057
6058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006060 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006061
6062 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6063 processKey(mapper, BTN_RIGHT, 1);
6064 processKey(mapper, BTN_MIDDLE, 1);
6065 processSync(mapper);
6066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6068 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6069 motionArgs.buttonState);
6070
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6072 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6073 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6074
6075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6077 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6078 motionArgs.buttonState);
6079
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080 processKey(mapper, BTN_RIGHT, 0);
6081 processSync(mapper);
6082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006083 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006084 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006085
6086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006087 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006088 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006089
6090 processKey(mapper, BTN_MIDDLE, 0);
6091 processSync(mapper);
6092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006093 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006094 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006095
6096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006098 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006099
6100 // press BTN_BACK, release BTN_BACK
6101 processKey(mapper, BTN_BACK, 1);
6102 processSync(mapper);
6103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6104 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6105 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006106
Michael Wrightd02c5b62014-02-10 15:10:22 -08006107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006109 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6110
6111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6112 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6113 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114
6115 processKey(mapper, BTN_BACK, 0);
6116 processSync(mapper);
6117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006118 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006119 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006120
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006123 ASSERT_EQ(0, motionArgs.buttonState);
6124
Michael Wrightd02c5b62014-02-10 15:10:22 -08006125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6126 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6127 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6128
6129 // press BTN_SIDE, release BTN_SIDE
6130 processKey(mapper, BTN_SIDE, 1);
6131 processSync(mapper);
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6133 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6134 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006135
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136 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(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6139
6140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6141 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6142 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006143
6144 processKey(mapper, BTN_SIDE, 0);
6145 processSync(mapper);
6146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006147 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006148 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006149
6150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006152 ASSERT_EQ(0, motionArgs.buttonState);
6153
Michael Wrightd02c5b62014-02-10 15:10:22 -08006154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6155 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6156 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6157
6158 // press BTN_FORWARD, release BTN_FORWARD
6159 processKey(mapper, BTN_FORWARD, 1);
6160 processSync(mapper);
6161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6162 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6163 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006164
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006167 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6168
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6170 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6171 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006172
6173 processKey(mapper, BTN_FORWARD, 0);
6174 processSync(mapper);
6175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006176 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006178
6179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006181 ASSERT_EQ(0, motionArgs.buttonState);
6182
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6184 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6185 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6186
6187 // press BTN_EXTRA, release BTN_EXTRA
6188 processKey(mapper, BTN_EXTRA, 1);
6189 processSync(mapper);
6190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6191 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6192 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006193
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006196 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6197
6198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6199 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6200 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006201
6202 processKey(mapper, BTN_EXTRA, 0);
6203 processSync(mapper);
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006205 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006207
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006210 ASSERT_EQ(0, motionArgs.buttonState);
6211
Michael Wrightd02c5b62014-02-10 15:10:22 -08006212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6213 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6214 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6215
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6217
Michael Wrightd02c5b62014-02-10 15:10:22 -08006218 // press BTN_STYLUS, release BTN_STYLUS
6219 processKey(mapper, BTN_STYLUS, 1);
6220 processSync(mapper);
6221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006223 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6224
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6226 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6227 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006228
6229 processKey(mapper, BTN_STYLUS, 0);
6230 processSync(mapper);
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006232 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006233 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006234
6235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006237 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006238
6239 // press BTN_STYLUS2, release BTN_STYLUS2
6240 processKey(mapper, BTN_STYLUS2, 1);
6241 processSync(mapper);
6242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006244 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6245
6246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6248 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249
6250 processKey(mapper, BTN_STYLUS2, 0);
6251 processSync(mapper);
6252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006253 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006254 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006255
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006258 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006259
6260 // release touch
6261 processUp(mapper);
6262 processSync(mapper);
6263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6264 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6265 ASSERT_EQ(0, motionArgs.buttonState);
6266}
6267
6268TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006269 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006270 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006271 prepareButtons();
6272 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006273 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006274
6275 NotifyMotionArgs motionArgs;
6276
6277 // default tool type is finger
6278 processDown(mapper, 100, 200);
6279 processSync(mapper);
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6281 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006282 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283
6284 // eraser
6285 processKey(mapper, BTN_TOOL_RUBBER, 1);
6286 processSync(mapper);
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006289 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290
6291 // stylus
6292 processKey(mapper, BTN_TOOL_RUBBER, 0);
6293 processKey(mapper, BTN_TOOL_PEN, 1);
6294 processSync(mapper);
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006297 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298
6299 // brush
6300 processKey(mapper, BTN_TOOL_PEN, 0);
6301 processKey(mapper, BTN_TOOL_BRUSH, 1);
6302 processSync(mapper);
6303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006305 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006306
6307 // pencil
6308 processKey(mapper, BTN_TOOL_BRUSH, 0);
6309 processKey(mapper, BTN_TOOL_PENCIL, 1);
6310 processSync(mapper);
6311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006313 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006314
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006315 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316 processKey(mapper, BTN_TOOL_PENCIL, 0);
6317 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6318 processSync(mapper);
6319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006321 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006322
6323 // mouse
6324 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6325 processKey(mapper, BTN_TOOL_MOUSE, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330
6331 // lens
6332 processKey(mapper, BTN_TOOL_MOUSE, 0);
6333 processKey(mapper, BTN_TOOL_LENS, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338
6339 // double-tap
6340 processKey(mapper, BTN_TOOL_LENS, 0);
6341 processKey(mapper, BTN_TOOL_DOUBLETAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006346
6347 // triple-tap
6348 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6349 processKey(mapper, BTN_TOOL_TRIPLETAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354
6355 // quad-tap
6356 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6357 processKey(mapper, BTN_TOOL_QUADTAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362
6363 // finger
6364 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6365 processKey(mapper, BTN_TOOL_FINGER, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370
6371 // stylus trumps finger
6372 processKey(mapper, BTN_TOOL_PEN, 1);
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006376 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377
6378 // eraser trumps stylus
6379 processKey(mapper, BTN_TOOL_RUBBER, 1);
6380 processSync(mapper);
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006383 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006384
6385 // mouse trumps eraser
6386 processKey(mapper, BTN_TOOL_MOUSE, 1);
6387 processSync(mapper);
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006390 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006391
6392 // back to default tool type
6393 processKey(mapper, BTN_TOOL_MOUSE, 0);
6394 processKey(mapper, BTN_TOOL_RUBBER, 0);
6395 processKey(mapper, BTN_TOOL_PEN, 0);
6396 processKey(mapper, BTN_TOOL_FINGER, 0);
6397 processSync(mapper);
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006400 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401}
6402
6403TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006404 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006405 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406 prepareButtons();
6407 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006408 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +00006409 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410
6411 NotifyMotionArgs motionArgs;
6412
6413 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6414 processKey(mapper, BTN_TOOL_FINGER, 1);
6415 processMove(mapper, 100, 200);
6416 processSync(mapper);
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6418 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6420 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6421
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6425 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6426
6427 // move a little
6428 processMove(mapper, 150, 250);
6429 processSync(mapper);
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6431 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6433 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6434
6435 // down when BTN_TOUCH is pressed, pressure defaults to 1
6436 processKey(mapper, BTN_TOUCH, 1);
6437 processSync(mapper);
6438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6439 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6441 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6442
6443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6444 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6446 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6447
6448 // up when BTN_TOUCH is released, hover restored
6449 processKey(mapper, BTN_TOUCH, 0);
6450 processSync(mapper);
6451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6452 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6454 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6455
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6457 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6459 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6460
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6462 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6464 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6465
6466 // exit hover when pointer goes away
6467 processKey(mapper, BTN_TOOL_FINGER, 0);
6468 processSync(mapper);
6469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6470 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6472 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6473}
6474
6475TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006476 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006477 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478 prepareButtons();
6479 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006480 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006481
6482 NotifyMotionArgs motionArgs;
6483
6484 // initially hovering because pressure is 0
6485 processDown(mapper, 100, 200);
6486 processPressure(mapper, 0);
6487 processSync(mapper);
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6491 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6492
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6496 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6497
6498 // move a little
6499 processMove(mapper, 150, 250);
6500 processSync(mapper);
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 // down when pressure is non-zero
6507 processPressure(mapper, RAW_PRESSURE_MAX);
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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6515 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6517 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6518
6519 // up when pressure becomes 0, hover restored
6520 processPressure(mapper, 0);
6521 processSync(mapper);
6522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6523 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6525 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6526
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6530 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6531
6532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6533 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6535 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6536
6537 // exit hover when pointer goes away
6538 processUp(mapper);
6539 processSync(mapper);
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6541 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6543 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6544}
6545
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006546TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6547 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006548 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006549 prepareButtons();
6550 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006551 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006552
6553 // Touch down.
6554 processDown(mapper, 100, 200);
6555 processPressure(mapper, 1);
6556 processSync(mapper);
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6558 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6559
6560 // Reset the mapper. This should cancel the ongoing gesture.
6561 resetMapper(mapper, ARBITRARY_TIME);
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6563 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6564
6565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6566}
6567
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006568TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6569 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006570 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006571 prepareButtons();
6572 prepareAxes(POSITION | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00006573 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006574
6575 // Set the initial state for the touch pointer.
6576 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6577 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6578 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6579 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6580
6581 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006582 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6583 // does not generate any events.
6584 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006585
6586 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6587 // the recreated touch state to generate a down event.
6588 processSync(mapper);
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6590 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6591
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6593}
6594
lilinnan687e58f2022-07-19 16:00:50 +08006595TEST_F(SingleTouchInputMapperTest,
6596 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6597 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006598 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006599 prepareButtons();
6600 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006601 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006602 NotifyMotionArgs motionArgs;
6603
6604 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006605 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006606 processSync(mapper);
6607
6608 // We should receive a down event
6609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6610 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6611
6612 // Change display id
6613 clearViewports();
6614 prepareSecondaryDisplay(ViewportType::INTERNAL);
6615
6616 // We should receive a cancel event
6617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6618 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6619 // Then receive reset called
6620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6621}
6622
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006623TEST_F(SingleTouchInputMapperTest,
6624 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6625 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006626 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006627 prepareButtons();
6628 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006629 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6631 NotifyMotionArgs motionArgs;
6632
6633 // Start a new gesture.
6634 processDown(mapper, 100, 200);
6635 processSync(mapper);
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6637 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6638
6639 // Make the viewport inactive. This will put the device in disabled mode.
6640 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6641 viewport->isActive = false;
6642 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006643 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006644
6645 // We should receive a cancel event for the ongoing gesture.
6646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6647 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6648 // Then we should be notified that the device was reset.
6649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6650
6651 // No events are generated while the viewport is inactive.
6652 processMove(mapper, 101, 201);
6653 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006654 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006655 processSync(mapper);
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6657
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006658 // Start a new gesture while the viewport is still inactive.
6659 processDown(mapper, 300, 400);
6660 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6661 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6662 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6663 processSync(mapper);
6664
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006665 // Make the viewport active again. The device should resume processing events.
6666 viewport->isActive = true;
6667 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006668 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006669
6670 // The device is reset because it changes back to direct mode, without generating any events.
6671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6673
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006674 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006675 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6677 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006678
6679 // No more events.
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6682}
6683
Prabir Pradhan211ba622022-10-31 21:09:21 +00006684TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6685 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006686 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006687 prepareButtons();
6688 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006689 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6691
6692 // Press a stylus button.
6693 processKey(mapper, BTN_STYLUS, 1);
6694 processSync(mapper);
6695
6696 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6697 processDown(mapper, 100, 200);
6698 processSync(mapper);
6699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6700 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6701 WithCoords(toDisplayX(100), toDisplayY(200)),
6702 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6704 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6705 WithCoords(toDisplayX(100), toDisplayY(200)),
6706 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6707
6708 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6709 // the button has not actually been released, since there will be no pointers through which the
6710 // button state can be reported. The event is generated at the location of the pointer before
6711 // it went up.
6712 processUp(mapper);
6713 processSync(mapper);
6714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6715 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6716 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6718 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6719 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6720}
6721
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006722TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6723 addConfigurationProperty("touch.deviceType", "touchScreen");
6724 prepareDisplay(ui::ROTATION_0);
6725 prepareButtons();
6726 prepareAxes(POSITION);
6727
6728 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6729
Arpit Singh56adebc2023-04-25 13:56:05 +00006730 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6732
6733 // Press a stylus button.
6734 processKey(mapper, BTN_STYLUS, 1);
6735 processSync(mapper);
6736
6737 // Start a touch gesture and ensure that the stylus button is not reported.
6738 processDown(mapper, 100, 200);
6739 processSync(mapper);
6740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6741 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6742
6743 // Release and press the stylus button again.
6744 processKey(mapper, BTN_STYLUS, 0);
6745 processSync(mapper);
6746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6747 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6748 processKey(mapper, BTN_STYLUS, 1);
6749 processSync(mapper);
6750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6751 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6752
6753 // Release the touch gesture.
6754 processUp(mapper);
6755 processSync(mapper);
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6757 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6758
6759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6760}
6761
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006762TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6763 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6764 prepareDisplay(ui::ROTATION_0);
6765 prepareButtons();
6766 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006767 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6769
6770 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6771}
6772
Seunghwan Choi356026c2023-02-01 14:37:25 +09006773TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6774 std::shared_ptr<FakePointerController> fakePointerController =
6775 std::make_shared<FakePointerController>();
6776 addConfigurationProperty("touch.deviceType", "touchScreen");
6777 prepareDisplay(ui::ROTATION_0);
6778 prepareButtons();
6779 prepareAxes(POSITION);
6780 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6781 mFakePolicy->setPointerController(fakePointerController);
6782 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singh56adebc2023-04-25 13:56:05 +00006783 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006784
6785 processKey(mapper, BTN_TOOL_PEN, 1);
6786 processMove(mapper, 100, 200);
6787 processSync(mapper);
6788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6789 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006790 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006791 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6792 ASSERT_TRUE(fakePointerController->isPointerShown());
6793 ASSERT_NO_FATAL_FAILURE(
6794 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6795}
6796
6797TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6798 std::shared_ptr<FakePointerController> fakePointerController =
6799 std::make_shared<FakePointerController>();
6800 addConfigurationProperty("touch.deviceType", "touchScreen");
6801 prepareDisplay(ui::ROTATION_0);
6802 prepareButtons();
6803 prepareAxes(POSITION);
6804 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6805 mFakePolicy->setPointerController(fakePointerController);
6806 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singh56adebc2023-04-25 13:56:05 +00006807 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006808
6809 processKey(mapper, BTN_TOOL_PEN, 1);
6810 processMove(mapper, 100, 200);
6811 processSync(mapper);
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6813 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006814 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006815 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6816 ASSERT_FALSE(fakePointerController->isPointerShown());
6817}
6818
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006819TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6820 // Initialize the device without setting device source to touch navigation.
6821 addConfigurationProperty("touch.deviceType", "touchScreen");
6822 prepareDisplay(ui::ROTATION_0);
6823 prepareButtons();
6824 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006825 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006826
6827 // Ensure that the device is created as a touchscreen, not touch navigation.
6828 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6829
6830 // Add device type association after the device was created.
6831 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6832
6833 // Send update to the mapper.
6834 std::list<NotifyArgs> unused2 =
6835 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006836 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006837
6838 // Check whether device type update was successful.
6839 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6840}
6841
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006842TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6843 // Initialize the device without setting device source to touch navigation.
6844 addConfigurationProperty("touch.deviceType", "touchScreen");
6845 prepareDisplay(ui::ROTATION_0);
6846 prepareButtons();
6847 prepareAxes(POSITION);
6848 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6849
6850 // Set a physical frame in the display viewport.
6851 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6852 viewport->physicalLeft = 0;
6853 viewport->physicalTop = 0;
6854 viewport->physicalRight = DISPLAY_WIDTH / 2;
6855 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6856 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006857 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006858
Arpit Singh56adebc2023-04-25 13:56:05 +00006859 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006860
6861 // Hovering inside the physical frame produces events.
6862 processKey(mapper, BTN_TOOL_PEN, 1);
6863 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6864 processSync(mapper);
6865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6866 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6868 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6869
6870 // Leaving the physical frame ends the hovering gesture.
6871 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6872 processSync(mapper);
6873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6874 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6875
6876 // Moving outside the physical frame does not produce events.
6877 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6878 processSync(mapper);
6879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6880
6881 // Re-entering the physical frame produces events.
6882 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6883 processSync(mapper);
6884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6885 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6887 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6888}
6889
Prabir Pradhan5632d622021-09-06 07:57:20 -07006890// --- TouchDisplayProjectionTest ---
6891
6892class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6893public:
6894 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6895 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6896 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006897 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6898 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6899 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006900 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006901 auto rotatedWidth = naturalDisplayWidth;
6902 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006903 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006904 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006905 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006906 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006907 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006908 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006909 inverseRotationFlags = ui::Transform::ROT_180;
6910 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006911 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006912 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006913 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006914 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006915 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006916 inverseRotationFlags = ui::Transform::ROT_0;
6917 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006918 }
6919
Prabir Pradhana9df3162022-12-05 23:57:27 +00006920 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006921 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6922
6923 std::optional<DisplayViewport> internalViewport =
6924 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6925 DisplayViewport& v = *internalViewport;
6926 v.displayId = DISPLAY_ID;
6927 v.orientation = orientation;
6928
6929 v.logicalLeft = 0;
6930 v.logicalTop = 0;
6931 v.logicalRight = 100;
6932 v.logicalBottom = 100;
6933
6934 v.physicalLeft = rotatedPhysicalDisplay.left;
6935 v.physicalTop = rotatedPhysicalDisplay.top;
6936 v.physicalRight = rotatedPhysicalDisplay.right;
6937 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6938
Prabir Pradhana9df3162022-12-05 23:57:27 +00006939 v.deviceWidth = rotatedWidth;
6940 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006941
6942 v.isActive = true;
6943 v.uniqueId = UNIQUE_ID;
6944 v.type = ViewportType::INTERNAL;
6945 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006946 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006947 }
6948
6949 void assertReceivedMove(const Point& point) {
6950 NotifyMotionArgs motionArgs;
6951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006953 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6955 1, 0, 0, 0, 0, 0, 0, 0));
6956 }
6957};
6958
6959TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6960 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006961 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006962
6963 prepareButtons();
6964 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00006965 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006966
6967 NotifyMotionArgs motionArgs;
6968
6969 // Configure the DisplayViewport such that the logical display maps to a subsection of
6970 // the display panel called the physical display. Here, the physical display is bounded by the
6971 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6972 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6973 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6974 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6975
Michael Wrighta9cf4192022-12-01 23:46:39 +00006976 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006977 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6978
6979 // Touches outside the physical display should be ignored, and should not generate any
6980 // events. Ensure touches at the following points that lie outside of the physical display
6981 // area do not generate any events.
6982 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6983 processDown(mapper, toRawX(point.x), toRawY(point.y));
6984 processSync(mapper);
6985 processUp(mapper);
6986 processSync(mapper);
6987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6988 << "Unexpected event generated for touch outside physical display at point: "
6989 << point.x << ", " << point.y;
6990 }
6991 }
6992}
6993
6994TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6995 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006996 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006997
6998 prepareButtons();
6999 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00007000 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007001
7002 NotifyMotionArgs motionArgs;
7003
7004 // Configure the DisplayViewport such that the logical display maps to a subsection of
7005 // the display panel called the physical display. Here, the physical display is bounded by the
7006 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7007 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7008
Michael Wrighta9cf4192022-12-01 23:46:39 +00007009 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007010 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7011
7012 // Touches that start outside the physical display should be ignored until it enters the
7013 // physical display bounds, at which point it should generate a down event. Start a touch at
7014 // the point (5, 100), which is outside the physical display bounds.
7015 static const Point kOutsidePoint{5, 100};
7016 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7017 processSync(mapper);
7018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7019
7020 // Move the touch into the physical display area. This should generate a pointer down.
7021 processMove(mapper, toRawX(11), toRawY(21));
7022 processSync(mapper);
7023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7024 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007025 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007026 ASSERT_NO_FATAL_FAILURE(
7027 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7028
7029 // Move the touch inside the physical display area. This should generate a pointer move.
7030 processMove(mapper, toRawX(69), toRawY(159));
7031 processSync(mapper);
7032 assertReceivedMove({69, 159});
7033
7034 // Move outside the physical display area. Since the pointer is already down, this should
7035 // now continue generating events.
7036 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7037 processSync(mapper);
7038 assertReceivedMove(kOutsidePoint);
7039
7040 // Release. This should generate a pointer up.
7041 processUp(mapper);
7042 processSync(mapper);
7043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7044 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7046 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7047
7048 // Ensure no more events were generated.
7049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7051 }
7052}
7053
Prabir Pradhana9df3162022-12-05 23:57:27 +00007054// --- TouchscreenPrecisionTests ---
7055
7056// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7057// in various orientations and with different display rotations. We configure the touchscreen to
7058// have a higher resolution than that of the display by an integer scale factor in each axis so that
7059// we can enforce that coordinates match precisely as expected.
7060class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7061 public ::testing::WithParamInterface<ui::Rotation> {
7062public:
7063 void SetUp() override {
7064 SingleTouchInputMapperTest::SetUp();
7065
7066 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7067 // four times the resolution of the display in the Y axis.
7068 prepareButtons();
7069 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007070 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7071 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007072 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007073 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7074 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007075 }
7076
7077 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7078 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7079 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7080 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7081
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007082 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7083 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7084
7085 static const int32_t PRECISION_RAW_X_FLAT = 16;
7086 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7087
7088 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7089 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7090
Prabir Pradhana9df3162022-12-05 23:57:27 +00007091 static const std::array<Point, 4> kRawCorners;
7092};
7093
7094const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7095 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7096 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7097 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7098 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7099}};
7100
7101// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7102// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7103// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7104TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7105 enum class Orientation {
7106 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7107 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7108 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7109 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7110 ftl_last = ORIENTATION_270,
7111 };
7112 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7113 Orientation::ORIENTATION_270;
7114 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7115 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7116 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7117 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7118 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7119 };
7120
7121 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7122
7123 // Configure the touchscreen as being installed in the one of the four different orientations
7124 // relative to the display.
7125 addConfigurationProperty("touch.deviceType", "touchScreen");
7126 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7127 prepareDisplay(ui::ROTATION_0);
7128
Arpit Singh56adebc2023-04-25 13:56:05 +00007129 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007130
7131 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7132 // orientations of either 90 or 270) this means the display's natural resolution will be
7133 // flipped.
7134 const bool displayRotated =
7135 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7136 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7137 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7138 const Rect physicalFrame{0, 0, width, height};
7139 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7140
7141 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7142 const float expectedPrecisionX = displayRotated ? 4 : 2;
7143 const float expectedPrecisionY = displayRotated ? 2 : 4;
7144
7145 // Test all four corners.
7146 for (int i = 0; i < 4; i++) {
7147 const auto& raw = kRawCorners[i];
7148 processDown(mapper, raw.x, raw.y);
7149 processSync(mapper);
7150 const auto& expected = expectedPoints[i];
7151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7152 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7153 WithCoords(expected.x, expected.y),
7154 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7155 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7156 << "with touchscreen orientation "
7157 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7158 << expected.x << ", " << expected.y << ").";
7159 processUp(mapper);
7160 processSync(mapper);
7161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7162 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7163 WithCoords(expected.x, expected.y))));
7164 }
7165}
7166
Prabir Pradhan82687402022-12-06 01:32:53 +00007167TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7168 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7169 kMappedCorners = {
7170 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7171 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7172 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7173 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7174 };
7175
7176 const ui::Rotation displayRotation = GetParam();
7177
7178 addConfigurationProperty("touch.deviceType", "touchScreen");
7179 prepareDisplay(displayRotation);
7180
Arpit Singh56adebc2023-04-25 13:56:05 +00007181 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007182
7183 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7184
7185 // Test all four corners.
7186 for (int i = 0; i < 4; i++) {
7187 const auto& expected = expectedPoints[i];
7188 const auto& raw = kRawCorners[i];
7189 processDown(mapper, raw.x, raw.y);
7190 processSync(mapper);
7191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7192 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7193 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7194 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7195 << "with display rotation " << ui::toCString(displayRotation)
7196 << ", expected point (" << expected.x << ", " << expected.y << ").";
7197 processUp(mapper);
7198 processSync(mapper);
7199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7200 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7201 WithCoords(expected.x, expected.y))));
7202 }
7203}
7204
Prabir Pradhan3e798762022-12-02 21:02:11 +00007205TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7206 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7207 kMappedCorners = {
7208 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7209 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7210 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7211 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7212 };
7213
7214 const ui::Rotation displayRotation = GetParam();
7215
7216 addConfigurationProperty("touch.deviceType", "touchScreen");
7217 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7218
Arpit Singh56adebc2023-04-25 13:56:05 +00007219 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007220
7221 // Ori 270, so width and height swapped
7222 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7223 prepareDisplay(displayRotation);
7224 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7225
7226 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7227
7228 // Test all four corners.
7229 for (int i = 0; i < 4; i++) {
7230 const auto& expected = expectedPoints[i];
7231 const auto& raw = kRawCorners[i];
7232 processDown(mapper, raw.x, raw.y);
7233 processSync(mapper);
7234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7235 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7236 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7237 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7238 << "with display rotation " << ui::toCString(displayRotation)
7239 << ", expected point (" << expected.x << ", " << expected.y << ").";
7240 processUp(mapper);
7241 processSync(mapper);
7242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7243 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7244 WithCoords(expected.x, expected.y))));
7245 }
7246}
7247
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007248TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7249 const ui::Rotation displayRotation = GetParam();
7250
7251 addConfigurationProperty("touch.deviceType", "touchScreen");
7252 prepareDisplay(displayRotation);
7253
7254 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singh56adebc2023-04-25 13:56:05 +00007255 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007256
7257 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7258 // MotionRanges use display pixels as their units
7259 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7260 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7261
7262 // The MotionRanges should be oriented in the rotated display's coordinate space
7263 const bool displayRotated =
7264 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7265
7266 constexpr float MAX_X = 479.5;
7267 constexpr float MAX_Y = 799.75;
7268 EXPECT_EQ(xRange->min, 0.f);
7269 EXPECT_EQ(yRange->min, 0.f);
7270 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7271 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7272
7273 EXPECT_EQ(xRange->flat, 8.f);
7274 EXPECT_EQ(yRange->flat, 8.f);
7275
7276 EXPECT_EQ(xRange->fuzz, 2.f);
7277 EXPECT_EQ(yRange->fuzz, 2.f);
7278
7279 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7280 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7281}
7282
Prabir Pradhana9df3162022-12-05 23:57:27 +00007283// Run the precision tests for all rotations.
7284INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7285 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7286 ui::ROTATION_270),
7287 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7288 return ftl::enum_string(testParamInfo.param);
7289 });
7290
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007291// --- ExternalStylusFusionTest ---
7292
7293class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7294public:
7295 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7296 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007297 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007298 prepareButtons();
7299 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00007300 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007301
7302 mStylusState.when = ARBITRARY_TIME;
7303 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007304 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007305 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007306 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007307 processExternalStylusState(mapper);
7308 return mapper;
7309 }
7310
7311 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7312 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7313 for (const NotifyArgs& args : generatedArgs) {
7314 mFakeListener->notify(args);
7315 }
7316 // Loop the reader to flush the input listener queue.
7317 mReader->loopOnce();
7318 return generatedArgs;
7319 }
7320
7321protected:
7322 StylusState mStylusState{};
7323 static constexpr uint32_t EXPECTED_SOURCE =
7324 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7325
7326 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7327 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007328 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007329
7330 // The first pointer is withheld.
7331 processDown(mapper, 100, 200);
7332 processSync(mapper);
7333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7334 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7335 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7336
7337 // The external stylus reports pressure. The withheld finger pointer is released as a
7338 // stylus.
7339 mStylusState.pressure = 1.f;
7340 processExternalStylusState(mapper);
7341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7342 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7343 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7344
7345 // Subsequent pointer events are not withheld.
7346 processMove(mapper, 101, 201);
7347 processSync(mapper);
7348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7349 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7350
7351 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7353 }
7354
7355 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7356 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7357
7358 // Releasing the touch pointer ends the gesture.
7359 processUp(mapper);
7360 processSync(mapper);
7361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7362 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007363 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007364
7365 mStylusState.pressure = 0.f;
7366 processExternalStylusState(mapper);
7367 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7369 }
7370
7371 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7372 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007373 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007374
7375 // The first pointer is withheld when an external stylus is connected,
7376 // and a timeout is requested.
7377 processDown(mapper, 100, 200);
7378 processSync(mapper);
7379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7380 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7381 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7382
7383 // If the timeout expires early, it is requested again.
7384 handleTimeout(mapper, ARBITRARY_TIME + 1);
7385 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7386 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7387
7388 // When the timeout expires, the withheld touch is released as a finger pointer.
7389 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7391 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7392
7393 // Subsequent pointer events are not withheld.
7394 processMove(mapper, 101, 201);
7395 processSync(mapper);
7396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7397 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7398 processUp(mapper);
7399 processSync(mapper);
7400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7401 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7402
7403 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7405 }
7406
7407private:
7408 InputDeviceInfo mExternalStylusDeviceInfo{};
7409};
7410
7411TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7412 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7413 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7414}
7415
7416TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7417 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7418 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7419}
7420
7421TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7422 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7423 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7424}
7425
7426// Test a successful stylus fusion gesture where the pressure is reported by the external
7427// before the touch is reported by the touchscreen.
7428TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7429 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7430 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007431 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007432
7433 // The external stylus reports pressure first. It is ignored for now.
7434 mStylusState.pressure = 1.f;
7435 processExternalStylusState(mapper);
7436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7437 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7438
7439 // When the touch goes down afterwards, it is reported as a stylus pointer.
7440 processDown(mapper, 100, 200);
7441 processSync(mapper);
7442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7443 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7444 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7445
7446 processMove(mapper, 101, 201);
7447 processSync(mapper);
7448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7449 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7450 processUp(mapper);
7451 processSync(mapper);
7452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7453 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7454
7455 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7457}
7458
7459TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7460 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7461
7462 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7463 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7464
7465 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7466 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7467 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7468 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7469}
7470
7471TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7472 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7473 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007474 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007475
7476 mStylusState.pressure = 0.8f;
7477 processExternalStylusState(mapper);
7478 processDown(mapper, 100, 200);
7479 processSync(mapper);
7480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7481 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7482 WithPressure(0.8f))));
7483 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7484
7485 // The external stylus reports a pressure change. We wait for some time for a touch event.
7486 mStylusState.pressure = 0.6f;
7487 processExternalStylusState(mapper);
7488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7489 ASSERT_NO_FATAL_FAILURE(
7490 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7491
7492 // If a touch is reported within the timeout, it reports the updated pressure.
7493 processMove(mapper, 101, 201);
7494 processSync(mapper);
7495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7496 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7497 WithPressure(0.6f))));
7498 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7499
7500 // There is another pressure change.
7501 mStylusState.pressure = 0.5f;
7502 processExternalStylusState(mapper);
7503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7504 ASSERT_NO_FATAL_FAILURE(
7505 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7506
7507 // If a touch is not reported within the timeout, a move event is generated to report
7508 // the new pressure.
7509 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7511 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7512 WithPressure(0.5f))));
7513
7514 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7515 // repeated indefinitely.
7516 mStylusState.pressure = 0.0f;
7517 processExternalStylusState(mapper);
7518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7519 ASSERT_NO_FATAL_FAILURE(
7520 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7521 processMove(mapper, 102, 202);
7522 processSync(mapper);
7523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7524 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7525 WithPressure(0.5f))));
7526 processMove(mapper, 103, 203);
7527 processSync(mapper);
7528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7529 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7530 WithPressure(0.5f))));
7531
7532 processUp(mapper);
7533 processSync(mapper);
7534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7535 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007536 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007537
7538 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7540}
7541
7542TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7543 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7544 auto source = WithSource(EXPECTED_SOURCE);
7545
7546 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007547 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007548 processExternalStylusState(mapper);
7549 processDown(mapper, 100, 200);
7550 processSync(mapper);
7551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7552 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007553 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007554 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7555
7556 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007557 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007558 processExternalStylusState(mapper);
7559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7560 ASSERT_NO_FATAL_FAILURE(
7561 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7562
7563 // If a touch is reported within the timeout, it reports the updated pressure.
7564 processMove(mapper, 101, 201);
7565 processSync(mapper);
7566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7567 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007568 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007569 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7570
7571 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007572 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007573 processExternalStylusState(mapper);
7574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7575 ASSERT_NO_FATAL_FAILURE(
7576 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7577
7578 // If a touch is not reported within the timeout, a move event is generated to report
7579 // the new tool type.
7580 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7582 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007583 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007584
7585 processUp(mapper);
7586 processSync(mapper);
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7588 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007589 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007590
7591 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7593}
7594
7595TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7596 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7597 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007598 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007599
7600 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7601
7602 // The external stylus reports a button change. We wait for some time for a touch event.
7603 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7604 processExternalStylusState(mapper);
7605 ASSERT_NO_FATAL_FAILURE(
7606 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7607
7608 // If a touch is reported within the timeout, it reports the updated button state.
7609 processMove(mapper, 101, 201);
7610 processSync(mapper);
7611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7612 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7613 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7615 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7616 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7617 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7618
7619 // The button is now released.
7620 mStylusState.buttons = 0;
7621 processExternalStylusState(mapper);
7622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7623 ASSERT_NO_FATAL_FAILURE(
7624 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7625
7626 // If a touch is not reported within the timeout, a move event is generated to report
7627 // the new button state.
7628 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7630 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7631 WithButtonState(0))));
7632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007633 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7634 WithButtonState(0))));
7635
7636 processUp(mapper);
7637 processSync(mapper);
7638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007639 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7640
7641 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7643}
7644
Michael Wrightd02c5b62014-02-10 15:10:22 -08007645// --- MultiTouchInputMapperTest ---
7646
7647class MultiTouchInputMapperTest : public TouchInputMapperTest {
7648protected:
7649 void prepareAxes(int axes);
7650
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007651 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7652 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7653 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7654 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7655 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7656 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7657 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7658 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7659 void processId(MultiTouchInputMapper& mapper, int32_t id);
7660 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7661 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7662 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007663 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007664 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007665 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7666 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007667};
7668
7669void MultiTouchInputMapperTest::prepareAxes(int axes) {
7670 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007671 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7672 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007673 }
7674 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007675 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7676 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007677 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007678 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7679 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007680 }
7681 }
7682 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007683 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7684 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007685 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007686 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007687 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007688 }
7689 }
7690 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007691 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7692 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007693 }
7694 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007695 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7696 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007697 }
7698 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007699 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7700 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007701 }
7702 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007703 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7704 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007705 }
7706 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007707 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7708 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007709 }
7710 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007711 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007712 }
7713}
7714
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007715void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7716 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007717 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007719}
7720
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007721void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7722 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007724}
7725
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007726void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7727 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007729}
7730
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007731void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007733}
7734
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007735void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007737}
7738
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007739void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7740 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007742}
7743
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007744void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007746}
7747
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007748void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007749 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007750}
7751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007752void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007754}
7755
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007756void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007758}
7759
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007760void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007762}
7763
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007764void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7765 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007767}
7768
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007769void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7770 int32_t value) {
7771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7773}
7774
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007775void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007777}
7778
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007779void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7780 nsecs_t readTime) {
7781 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007782}
7783
Michael Wrightd02c5b62014-02-10 15:10:22 -08007784TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007785 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007786 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007787 prepareAxes(POSITION);
7788 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00007789 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007790
arthurhungdcef2dc2020-08-11 14:47:50 +08007791 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007792
7793 NotifyMotionArgs motionArgs;
7794
7795 // Two fingers down at once.
7796 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7797 processPosition(mapper, x1, y1);
7798 processMTSync(mapper);
7799 processPosition(mapper, x2, y2);
7800 processMTSync(mapper);
7801 processSync(mapper);
7802
7803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7804 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7805 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7806 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7807 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7808 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7809 ASSERT_EQ(0, motionArgs.flags);
7810 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7811 ASSERT_EQ(0, motionArgs.buttonState);
7812 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007813 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007814 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007815 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7817 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7818 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7819 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7820 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7821
7822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7823 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7824 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7825 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7826 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007827 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007828 ASSERT_EQ(0, motionArgs.flags);
7829 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7830 ASSERT_EQ(0, motionArgs.buttonState);
7831 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007832 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007833 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007834 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007835 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007836 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7838 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7840 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7841 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7842 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7843 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7844
7845 // Move.
7846 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7847 processPosition(mapper, x1, y1);
7848 processMTSync(mapper);
7849 processPosition(mapper, x2, y2);
7850 processMTSync(mapper);
7851 processSync(mapper);
7852
7853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7854 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7855 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7856 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7857 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7859 ASSERT_EQ(0, motionArgs.flags);
7860 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7861 ASSERT_EQ(0, motionArgs.buttonState);
7862 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007863 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007864 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007865 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007866 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007867 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7869 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7871 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7872 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7873 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7874 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7875
7876 // First finger up.
7877 x2 += 15; y2 -= 20;
7878 processPosition(mapper, x2, y2);
7879 processMTSync(mapper);
7880 processSync(mapper);
7881
7882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7883 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7884 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7885 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7886 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007887 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007888 ASSERT_EQ(0, motionArgs.flags);
7889 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7890 ASSERT_EQ(0, motionArgs.buttonState);
7891 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007892 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007893 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007894 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007895 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007896 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007897 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7898 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7899 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7900 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7901 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7902 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7903 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7904
7905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7906 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7907 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7908 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7909 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7911 ASSERT_EQ(0, motionArgs.flags);
7912 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7913 ASSERT_EQ(0, motionArgs.buttonState);
7914 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007915 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007916 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007917 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007918 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7919 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7920 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7921 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7922 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7923
7924 // Move.
7925 x2 += 20; y2 -= 25;
7926 processPosition(mapper, x2, y2);
7927 processMTSync(mapper);
7928 processSync(mapper);
7929
7930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7931 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7932 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7933 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7934 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7936 ASSERT_EQ(0, motionArgs.flags);
7937 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7938 ASSERT_EQ(0, motionArgs.buttonState);
7939 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007940 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007941 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007942 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7944 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7945 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7946 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7947 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7948
7949 // New finger down.
7950 int32_t x3 = 700, y3 = 300;
7951 processPosition(mapper, x2, y2);
7952 processMTSync(mapper);
7953 processPosition(mapper, x3, y3);
7954 processMTSync(mapper);
7955 processSync(mapper);
7956
7957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7958 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7959 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7960 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7961 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007962 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007963 ASSERT_EQ(0, motionArgs.flags);
7964 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7965 ASSERT_EQ(0, motionArgs.buttonState);
7966 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007967 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007968 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007969 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007970 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007971 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7973 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7975 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7976 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7977 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7978 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7979
7980 // Second finger up.
7981 x3 += 30; y3 -= 20;
7982 processPosition(mapper, x3, y3);
7983 processMTSync(mapper);
7984 processSync(mapper);
7985
7986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7987 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7988 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7989 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7990 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007991 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007992 ASSERT_EQ(0, motionArgs.flags);
7993 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7994 ASSERT_EQ(0, motionArgs.buttonState);
7995 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007996 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007997 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007998 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007999 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008000 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8002 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8004 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8005 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8006 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8007 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8008
8009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8010 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8011 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8012 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8013 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8015 ASSERT_EQ(0, motionArgs.flags);
8016 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8017 ASSERT_EQ(0, motionArgs.buttonState);
8018 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008019 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008020 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008021 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8023 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8024 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8025 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8026 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8027
8028 // Last finger up.
8029 processMTSync(mapper);
8030 processSync(mapper);
8031
8032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8033 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8034 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8035 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8036 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8037 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8038 ASSERT_EQ(0, motionArgs.flags);
8039 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8040 ASSERT_EQ(0, motionArgs.buttonState);
8041 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008042 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008043 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008044 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8046 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8047 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8048 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8049 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8050
8051 // Should not have sent any more keys or motions.
8052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8054}
8055
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008056TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8057 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008058 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008059
8060 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8061 /*fuzz*/ 0, /*resolution*/ 10);
8062 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8063 /*fuzz*/ 0, /*resolution*/ 11);
8064 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8065 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8066 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8067 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8068 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8069 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8070 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8071 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8072
Arpit Singh56adebc2023-04-25 13:56:05 +00008073 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008074
8075 // X and Y axes
8076 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8077 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8078 // Touch major and minor
8079 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8080 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8081 // Tool major and minor
8082 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8083 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8084}
8085
8086TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8087 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008088 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008089
8090 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8091 /*fuzz*/ 0, /*resolution*/ 10);
8092 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8093 /*fuzz*/ 0, /*resolution*/ 11);
8094
8095 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8096
Arpit Singh56adebc2023-04-25 13:56:05 +00008097 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008098
8099 // Touch major and minor
8100 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8101 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8102 // Tool major and minor
8103 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8104 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8105}
8106
Michael Wrightd02c5b62014-02-10 15:10:22 -08008107TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008108 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008109 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008110 prepareAxes(POSITION | ID);
8111 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00008112 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008113
arthurhungdcef2dc2020-08-11 14:47:50 +08008114 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008115
8116 NotifyMotionArgs motionArgs;
8117
8118 // Two fingers down at once.
8119 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8120 processPosition(mapper, x1, y1);
8121 processId(mapper, 1);
8122 processMTSync(mapper);
8123 processPosition(mapper, x2, y2);
8124 processId(mapper, 2);
8125 processMTSync(mapper);
8126 processSync(mapper);
8127
8128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8129 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008130 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008131 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008132 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8134 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8135
8136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008137 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008138 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008139 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008140 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008141 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008142 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008143 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8144 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8146 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8147
8148 // Move.
8149 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8150 processPosition(mapper, x1, y1);
8151 processId(mapper, 1);
8152 processMTSync(mapper);
8153 processPosition(mapper, x2, y2);
8154 processId(mapper, 2);
8155 processMTSync(mapper);
8156 processSync(mapper);
8157
8158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008160 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008162 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008164 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008165 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8166 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8168 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8169
8170 // First finger up.
8171 x2 += 15; y2 -= 20;
8172 processPosition(mapper, x2, y2);
8173 processId(mapper, 2);
8174 processMTSync(mapper);
8175 processSync(mapper);
8176
8177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008178 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008179 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008181 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008182 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008183 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8185 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8187 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8188
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008191 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008193 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008194 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8195 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8196
8197 // Move.
8198 x2 += 20; y2 -= 25;
8199 processPosition(mapper, x2, y2);
8200 processId(mapper, 2);
8201 processMTSync(mapper);
8202 processSync(mapper);
8203
8204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008206 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008208 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8210 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8211
8212 // New finger down.
8213 int32_t x3 = 700, y3 = 300;
8214 processPosition(mapper, x2, y2);
8215 processId(mapper, 2);
8216 processMTSync(mapper);
8217 processPosition(mapper, x3, y3);
8218 processId(mapper, 3);
8219 processMTSync(mapper);
8220 processSync(mapper);
8221
8222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008223 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008224 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008225 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008226 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008227 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008228 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008229 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8230 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8232 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8233
8234 // Second finger up.
8235 x3 += 30; y3 -= 20;
8236 processPosition(mapper, x3, y3);
8237 processId(mapper, 3);
8238 processMTSync(mapper);
8239 processSync(mapper);
8240
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008242 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008243 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008244 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008245 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008246 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008247 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8249 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8251 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8252
8253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008255 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008256 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008257 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8259 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8260
8261 // Last finger up.
8262 processMTSync(mapper);
8263 processSync(mapper);
8264
8265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8266 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008267 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008268 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008269 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8271 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8272
8273 // Should not have sent any more keys or motions.
8274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8276}
8277
8278TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008279 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008280 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008281 prepareAxes(POSITION | ID | SLOT);
8282 prepareVirtualKeys();
Arpit Singh56adebc2023-04-25 13:56:05 +00008283 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008284
arthurhungdcef2dc2020-08-11 14:47:50 +08008285 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008286
8287 NotifyMotionArgs motionArgs;
8288
8289 // Two fingers down at once.
8290 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8291 processPosition(mapper, x1, y1);
8292 processId(mapper, 1);
8293 processSlot(mapper, 1);
8294 processPosition(mapper, x2, y2);
8295 processId(mapper, 2);
8296 processSync(mapper);
8297
8298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8299 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008300 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008301 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008302 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8304 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8305
8306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008307 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008308 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008309 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008310 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008311 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008312 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8314 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8316 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8317
8318 // Move.
8319 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8320 processSlot(mapper, 0);
8321 processPosition(mapper, x1, y1);
8322 processSlot(mapper, 1);
8323 processPosition(mapper, x2, y2);
8324 processSync(mapper);
8325
8326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008328 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008329 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008330 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008331 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008332 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8334 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8336 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8337
8338 // First finger up.
8339 x2 += 15; y2 -= 20;
8340 processSlot(mapper, 0);
8341 processId(mapper, -1);
8342 processSlot(mapper, 1);
8343 processPosition(mapper, x2, y2);
8344 processSync(mapper);
8345
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008347 ASSERT_EQ(ACTION_POINTER_0_UP, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008360 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008361 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008362 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8364 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8365
8366 // Move.
8367 x2 += 20; y2 -= 25;
8368 processPosition(mapper, x2, y2);
8369 processSync(mapper);
8370
8371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008373 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008374 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008375 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8377 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8378
8379 // New finger down.
8380 int32_t x3 = 700, y3 = 300;
8381 processPosition(mapper, x2, y2);
8382 processSlot(mapper, 0);
8383 processId(mapper, 3);
8384 processPosition(mapper, x3, y3);
8385 processSync(mapper);
8386
8387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008388 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008389 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008390 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008391 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008392 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008393 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8395 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8397 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8398
8399 // Second finger up.
8400 x3 += 30; y3 -= 20;
8401 processSlot(mapper, 1);
8402 processId(mapper, -1);
8403 processSlot(mapper, 0);
8404 processPosition(mapper, x3, y3);
8405 processSync(mapper);
8406
8407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008408 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008409 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008410 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008411 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008412 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008413 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8415 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8417 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8418
8419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008421 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008422 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008423 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8425 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8426
8427 // Last finger up.
8428 processId(mapper, -1);
8429 processSync(mapper);
8430
8431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8432 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008433 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008434 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008435 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8437 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8438
8439 // Should not have sent any more keys or motions.
8440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8442}
8443
8444TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008445 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008446 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008447 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singh56adebc2023-04-25 13:56:05 +00008448 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008449
8450 // These calculations are based on the input device calibration documentation.
8451 int32_t rawX = 100;
8452 int32_t rawY = 200;
8453 int32_t rawTouchMajor = 7;
8454 int32_t rawTouchMinor = 6;
8455 int32_t rawToolMajor = 9;
8456 int32_t rawToolMinor = 8;
8457 int32_t rawPressure = 11;
8458 int32_t rawDistance = 0;
8459 int32_t rawOrientation = 3;
8460 int32_t id = 5;
8461
8462 float x = toDisplayX(rawX);
8463 float y = toDisplayY(rawY);
8464 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8465 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8466 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8467 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8468 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8469 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8470 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8471 float distance = float(rawDistance);
8472
8473 processPosition(mapper, rawX, rawY);
8474 processTouchMajor(mapper, rawTouchMajor);
8475 processTouchMinor(mapper, rawTouchMinor);
8476 processToolMajor(mapper, rawToolMajor);
8477 processToolMinor(mapper, rawToolMinor);
8478 processPressure(mapper, rawPressure);
8479 processOrientation(mapper, rawOrientation);
8480 processDistance(mapper, rawDistance);
8481 processId(mapper, id);
8482 processMTSync(mapper);
8483 processSync(mapper);
8484
8485 NotifyMotionArgs args;
8486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8487 ASSERT_EQ(0, args.pointerProperties[0].id);
8488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8489 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8490 orientation, distance));
8491}
8492
8493TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008494 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008495 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008496 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8497 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singh56adebc2023-04-25 13:56:05 +00008498 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008499
8500 // These calculations are based on the input device calibration documentation.
8501 int32_t rawX = 100;
8502 int32_t rawY = 200;
8503 int32_t rawTouchMajor = 140;
8504 int32_t rawTouchMinor = 120;
8505 int32_t rawToolMajor = 180;
8506 int32_t rawToolMinor = 160;
8507
8508 float x = toDisplayX(rawX);
8509 float y = toDisplayY(rawY);
8510 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8511 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8512 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8513 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8514 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8515
8516 processPosition(mapper, rawX, rawY);
8517 processTouchMajor(mapper, rawTouchMajor);
8518 processTouchMinor(mapper, rawTouchMinor);
8519 processToolMajor(mapper, rawToolMajor);
8520 processToolMinor(mapper, rawToolMinor);
8521 processMTSync(mapper);
8522 processSync(mapper);
8523
8524 NotifyMotionArgs args;
8525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8527 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8528}
8529
8530TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008531 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008532 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008533 prepareAxes(POSITION | TOUCH | TOOL);
8534 addConfigurationProperty("touch.size.calibration", "diameter");
8535 addConfigurationProperty("touch.size.scale", "10");
8536 addConfigurationProperty("touch.size.bias", "160");
8537 addConfigurationProperty("touch.size.isSummed", "1");
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 // Note: We only provide a single common touch/tool value because the device is assumed
8542 // not to emit separate values for each pointer (isSummed = 1).
8543 int32_t rawX = 100;
8544 int32_t rawY = 200;
8545 int32_t rawX2 = 150;
8546 int32_t rawY2 = 250;
8547 int32_t rawTouchMajor = 5;
8548 int32_t rawToolMajor = 8;
8549
8550 float x = toDisplayX(rawX);
8551 float y = toDisplayY(rawY);
8552 float x2 = toDisplayX(rawX2);
8553 float y2 = toDisplayY(rawY2);
8554 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8555 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8556 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8557
8558 processPosition(mapper, rawX, rawY);
8559 processTouchMajor(mapper, rawTouchMajor);
8560 processToolMajor(mapper, rawToolMajor);
8561 processMTSync(mapper);
8562 processPosition(mapper, rawX2, rawY2);
8563 processTouchMajor(mapper, rawTouchMajor);
8564 processToolMajor(mapper, rawToolMajor);
8565 processMTSync(mapper);
8566 processSync(mapper);
8567
8568 NotifyMotionArgs args;
8569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8570 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8571
8572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008573 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008574 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8576 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8578 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8579}
8580
8581TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008582 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008583 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008584 prepareAxes(POSITION | TOUCH | TOOL);
8585 addConfigurationProperty("touch.size.calibration", "area");
8586 addConfigurationProperty("touch.size.scale", "43");
8587 addConfigurationProperty("touch.size.bias", "3");
Arpit Singh56adebc2023-04-25 13:56:05 +00008588 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008589
8590 // These calculations are based on the input device calibration documentation.
8591 int32_t rawX = 100;
8592 int32_t rawY = 200;
8593 int32_t rawTouchMajor = 5;
8594 int32_t rawToolMajor = 8;
8595
8596 float x = toDisplayX(rawX);
8597 float y = toDisplayY(rawY);
8598 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8599 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8600 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8601
8602 processPosition(mapper, rawX, rawY);
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_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8611 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8612}
8613
8614TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008615 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008616 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008617 prepareAxes(POSITION | PRESSURE);
8618 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8619 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singh56adebc2023-04-25 13:56:05 +00008620 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008621
Michael Wrightaa449c92017-12-13 21:21:43 +00008622 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008623 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008624 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8625 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8626 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8627
Michael Wrightd02c5b62014-02-10 15:10:22 -08008628 // These calculations are based on the input device calibration documentation.
8629 int32_t rawX = 100;
8630 int32_t rawY = 200;
8631 int32_t rawPressure = 60;
8632
8633 float x = toDisplayX(rawX);
8634 float y = toDisplayY(rawY);
8635 float pressure = float(rawPressure) * 0.01f;
8636
8637 processPosition(mapper, rawX, rawY);
8638 processPressure(mapper, rawPressure);
8639 processMTSync(mapper);
8640 processSync(mapper);
8641
8642 NotifyMotionArgs args;
8643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8645 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8646}
8647
8648TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008649 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008650 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008651 prepareAxes(POSITION | ID | SLOT);
Arpit Singh56adebc2023-04-25 13:56:05 +00008652 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008653
8654 NotifyMotionArgs motionArgs;
8655 NotifyKeyArgs keyArgs;
8656
8657 processId(mapper, 1);
8658 processPosition(mapper, 100, 200);
8659 processSync(mapper);
8660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8661 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8662 ASSERT_EQ(0, motionArgs.buttonState);
8663
8664 // press BTN_LEFT, release BTN_LEFT
8665 processKey(mapper, BTN_LEFT, 1);
8666 processSync(mapper);
8667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8669 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8670
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8672 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8673 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8674
Michael Wrightd02c5b62014-02-10 15:10:22 -08008675 processKey(mapper, BTN_LEFT, 0);
8676 processSync(mapper);
8677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008678 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008679 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008680
8681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008682 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008683 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008684
8685 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8686 processKey(mapper, BTN_RIGHT, 1);
8687 processKey(mapper, BTN_MIDDLE, 1);
8688 processSync(mapper);
8689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8691 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8692 motionArgs.buttonState);
8693
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8695 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8696 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8697
8698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8699 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8700 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8701 motionArgs.buttonState);
8702
Michael Wrightd02c5b62014-02-10 15:10:22 -08008703 processKey(mapper, BTN_RIGHT, 0);
8704 processSync(mapper);
8705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008706 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008707 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008708
8709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008711 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008712
8713 processKey(mapper, BTN_MIDDLE, 0);
8714 processSync(mapper);
8715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008716 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008717 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008718
8719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008720 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008721 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008722
8723 // press BTN_BACK, release BTN_BACK
8724 processKey(mapper, BTN_BACK, 1);
8725 processSync(mapper);
8726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8727 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8728 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008729
Michael Wrightd02c5b62014-02-10 15:10:22 -08008730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008731 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008732 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8733
8734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8735 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8736 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008737
8738 processKey(mapper, BTN_BACK, 0);
8739 processSync(mapper);
8740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008741 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008742 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008743
8744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008746 ASSERT_EQ(0, motionArgs.buttonState);
8747
Michael Wrightd02c5b62014-02-10 15:10:22 -08008748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8749 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8750 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8751
8752 // press BTN_SIDE, release BTN_SIDE
8753 processKey(mapper, BTN_SIDE, 1);
8754 processSync(mapper);
8755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8756 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8757 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008758
Michael Wrightd02c5b62014-02-10 15:10:22 -08008759 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(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8762
8763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8764 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8765 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008766
8767 processKey(mapper, BTN_SIDE, 0);
8768 processSync(mapper);
8769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008770 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008771 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008772
8773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008774 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008775 ASSERT_EQ(0, motionArgs.buttonState);
8776
Michael Wrightd02c5b62014-02-10 15:10:22 -08008777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8778 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8779 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8780
8781 // press BTN_FORWARD, release BTN_FORWARD
8782 processKey(mapper, BTN_FORWARD, 1);
8783 processSync(mapper);
8784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8785 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8786 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008787
Michael Wrightd02c5b62014-02-10 15:10:22 -08008788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008790 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8791
8792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8793 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8794 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008795
8796 processKey(mapper, BTN_FORWARD, 0);
8797 processSync(mapper);
8798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008799 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008800 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008801
8802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008804 ASSERT_EQ(0, motionArgs.buttonState);
8805
Michael Wrightd02c5b62014-02-10 15:10:22 -08008806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8807 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8808 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8809
8810 // press BTN_EXTRA, release BTN_EXTRA
8811 processKey(mapper, BTN_EXTRA, 1);
8812 processSync(mapper);
8813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8814 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8815 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008816
Michael Wrightd02c5b62014-02-10 15:10:22 -08008817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008819 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8820
8821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8822 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8823 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008824
8825 processKey(mapper, BTN_EXTRA, 0);
8826 processSync(mapper);
8827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008828 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008829 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008830
8831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008833 ASSERT_EQ(0, motionArgs.buttonState);
8834
Michael Wrightd02c5b62014-02-10 15:10:22 -08008835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8836 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8837 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8838
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8840
Michael Wrightd02c5b62014-02-10 15:10:22 -08008841 // press BTN_STYLUS, release BTN_STYLUS
8842 processKey(mapper, BTN_STYLUS, 1);
8843 processSync(mapper);
8844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8845 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008846 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8847
8848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8849 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8850 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008851
8852 processKey(mapper, BTN_STYLUS, 0);
8853 processSync(mapper);
8854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008855 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008856 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008857
8858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008860 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008861
8862 // press BTN_STYLUS2, release BTN_STYLUS2
8863 processKey(mapper, BTN_STYLUS2, 1);
8864 processSync(mapper);
8865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008867 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8868
8869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8870 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8871 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008872
8873 processKey(mapper, BTN_STYLUS2, 0);
8874 processSync(mapper);
8875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008876 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008877 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008878
8879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008881 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008882
8883 // release touch
8884 processId(mapper, -1);
8885 processSync(mapper);
8886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8887 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8888 ASSERT_EQ(0, motionArgs.buttonState);
8889}
8890
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008891TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8892 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008893 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008894 prepareAxes(POSITION | ID | SLOT);
Arpit Singh56adebc2023-04-25 13:56:05 +00008895 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008896
8897 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8898 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8899
8900 // Touch down.
8901 processId(mapper, 1);
8902 processPosition(mapper, 100, 200);
8903 processSync(mapper);
8904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8905 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8906
8907 // Press and release button mapped to the primary stylus button.
8908 processKey(mapper, BTN_A, 1);
8909 processSync(mapper);
8910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8911 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8912 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8914 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8915 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8916
8917 processKey(mapper, BTN_A, 0);
8918 processSync(mapper);
8919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8920 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8922 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8923
8924 // Press and release the HID usage mapped to the secondary stylus button.
8925 processHidUsage(mapper, 0xabcd, 1);
8926 processSync(mapper);
8927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8928 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8929 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8931 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8932 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8933
8934 processHidUsage(mapper, 0xabcd, 0);
8935 processSync(mapper);
8936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8937 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8939 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8940
8941 // Release touch.
8942 processId(mapper, -1);
8943 processSync(mapper);
8944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8945 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8946}
8947
Michael Wrightd02c5b62014-02-10 15:10:22 -08008948TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008949 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008950 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008951 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00008952 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008953
8954 NotifyMotionArgs motionArgs;
8955
8956 // default tool type is finger
8957 processId(mapper, 1);
8958 processPosition(mapper, 100, 200);
8959 processSync(mapper);
8960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8961 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008962 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008963
8964 // eraser
8965 processKey(mapper, BTN_TOOL_RUBBER, 1);
8966 processSync(mapper);
8967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008969 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008970
8971 // stylus
8972 processKey(mapper, BTN_TOOL_RUBBER, 0);
8973 processKey(mapper, BTN_TOOL_PEN, 1);
8974 processSync(mapper);
8975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8976 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008977 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008978
8979 // brush
8980 processKey(mapper, BTN_TOOL_PEN, 0);
8981 processKey(mapper, BTN_TOOL_BRUSH, 1);
8982 processSync(mapper);
8983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8984 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008985 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008986
8987 // pencil
8988 processKey(mapper, BTN_TOOL_BRUSH, 0);
8989 processKey(mapper, BTN_TOOL_PENCIL, 1);
8990 processSync(mapper);
8991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8992 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008993 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008994
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008995 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008996 processKey(mapper, BTN_TOOL_PENCIL, 0);
8997 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8998 processSync(mapper);
8999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9000 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009001 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009002
9003 // mouse
9004 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9005 processKey(mapper, BTN_TOOL_MOUSE, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009010
9011 // lens
9012 processKey(mapper, BTN_TOOL_MOUSE, 0);
9013 processKey(mapper, BTN_TOOL_LENS, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009018
9019 // double-tap
9020 processKey(mapper, BTN_TOOL_LENS, 0);
9021 processKey(mapper, BTN_TOOL_DOUBLETAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009026
9027 // triple-tap
9028 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9029 processKey(mapper, BTN_TOOL_TRIPLETAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009034
9035 // quad-tap
9036 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9037 processKey(mapper, BTN_TOOL_QUADTAP, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009042
9043 // finger
9044 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9045 processKey(mapper, BTN_TOOL_FINGER, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009050
9051 // stylus trumps finger
9052 processKey(mapper, BTN_TOOL_PEN, 1);
9053 processSync(mapper);
9054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9055 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009056 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009057
9058 // eraser trumps stylus
9059 processKey(mapper, BTN_TOOL_RUBBER, 1);
9060 processSync(mapper);
9061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009063 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009064
9065 // mouse trumps eraser
9066 processKey(mapper, BTN_TOOL_MOUSE, 1);
9067 processSync(mapper);
9068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009070 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009071
9072 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9073 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9074 processSync(mapper);
9075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009077 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009078
9079 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9080 processToolType(mapper, MT_TOOL_PEN);
9081 processSync(mapper);
9082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009084 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009085
9086 // back to default tool type
9087 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9088 processKey(mapper, BTN_TOOL_MOUSE, 0);
9089 processKey(mapper, BTN_TOOL_RUBBER, 0);
9090 processKey(mapper, BTN_TOOL_PEN, 0);
9091 processKey(mapper, BTN_TOOL_FINGER, 0);
9092 processSync(mapper);
9093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009095 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009096}
9097
9098TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009099 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009100 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009101 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009102 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +00009103 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009104
9105 NotifyMotionArgs motionArgs;
9106
9107 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9108 processId(mapper, 1);
9109 processPosition(mapper, 100, 200);
9110 processSync(mapper);
9111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9112 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9114 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9115
9116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9117 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9119 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9120
9121 // move a little
9122 processPosition(mapper, 150, 250);
9123 processSync(mapper);
9124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9125 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9127 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9128
9129 // down when BTN_TOUCH is pressed, pressure defaults to 1
9130 processKey(mapper, BTN_TOUCH, 1);
9131 processSync(mapper);
9132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9133 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9135 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9136
9137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9138 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9140 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9141
9142 // up when BTN_TOUCH is released, hover restored
9143 processKey(mapper, BTN_TOUCH, 0);
9144 processSync(mapper);
9145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9146 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9148 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9149
9150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9151 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9153 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9154
9155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9156 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9158 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9159
9160 // exit hover when pointer goes away
9161 processId(mapper, -1);
9162 processSync(mapper);
9163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9164 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9165 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9166 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9167}
9168
9169TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009170 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009171 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009172 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009173 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009174
9175 NotifyMotionArgs motionArgs;
9176
9177 // initially hovering because pressure is 0
9178 processId(mapper, 1);
9179 processPosition(mapper, 100, 200);
9180 processPressure(mapper, 0);
9181 processSync(mapper);
9182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9183 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9185 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9186
9187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9188 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9190 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9191
9192 // move a little
9193 processPosition(mapper, 150, 250);
9194 processSync(mapper);
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 // down when pressure becomes non-zero
9201 processPressure(mapper, RAW_PRESSURE_MAX);
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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9209 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9211 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9212
9213 // up when pressure becomes 0, hover restored
9214 processPressure(mapper, 0);
9215 processSync(mapper);
9216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9217 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9219 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9220
9221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9222 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9224 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9225
9226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9227 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9229 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9230
9231 // exit hover when pointer goes away
9232 processId(mapper, -1);
9233 processSync(mapper);
9234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9235 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9237 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9238}
9239
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009240/**
9241 * Set the input device port <--> display port associations, and check that the
9242 * events are routed to the display that matches the display port.
9243 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9244 */
9245TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009246 const std::string usb2 = "USB2";
9247 const uint8_t hdmi1 = 0;
9248 const uint8_t hdmi2 = 1;
9249 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009250 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009251
9252 addConfigurationProperty("touch.deviceType", "touchScreen");
9253 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009254 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009255
9256 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9257 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9258
9259 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9260 // for this input device is specified, and the matching viewport is not present,
9261 // the input device should be disabled (at the mapper level).
9262
9263 // Add viewport for display 2 on hdmi2
9264 prepareSecondaryDisplay(type, hdmi2);
9265 // Send a touch event
9266 processPosition(mapper, 100, 100);
9267 processSync(mapper);
9268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9269
9270 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009271 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009272 // Send a touch event again
9273 processPosition(mapper, 100, 100);
9274 processSync(mapper);
9275
9276 NotifyMotionArgs args;
9277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9278 ASSERT_EQ(DISPLAY_ID, args.displayId);
9279}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009280
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009281TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9282 addConfigurationProperty("touch.deviceType", "touchScreen");
9283 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009284 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009285
9286 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9287
Michael Wrighta9cf4192022-12-01 23:46:39 +00009288 prepareDisplay(ui::ROTATION_0);
9289 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009290
9291 // Send a touch event
9292 processPosition(mapper, 100, 100);
9293 processSync(mapper);
9294
9295 NotifyMotionArgs args;
9296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9297 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9298}
9299
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009300TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009301 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009302 std::shared_ptr<FakePointerController> fakePointerController =
9303 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009304 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009305 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009306 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009307
Garfield Tan888a6a42020-01-09 11:39:16 -08009308 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009309 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009310
Michael Wrighta9cf4192022-12-01 23:46:39 +00009311 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009312 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009313 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009314
Josep del Río2d8c79a2023-01-23 19:33:50 +00009315 // Check source is mouse that would obtain the PointerController.
9316 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009317
9318 NotifyMotionArgs motionArgs;
9319 processPosition(mapper, 100, 100);
9320 processSync(mapper);
9321
9322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9323 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9324 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9325}
9326
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009327/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009328 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9329 */
9330TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9331 addConfigurationProperty("touch.deviceType", "touchScreen");
9332 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009333 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009334
Michael Wrighta9cf4192022-12-01 23:46:39 +00009335 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009336 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9337 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9338 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9339 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009340
9341 NotifyMotionArgs args;
9342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9343 ASSERT_EQ(26, args.readTime);
9344
Harry Cutts33476232023-01-30 19:57:29 +00009345 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9346 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9347 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009348
9349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9350 ASSERT_EQ(33, args.readTime);
9351}
9352
9353/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009354 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9355 * events should not be delivered to the listener.
9356 */
9357TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9358 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009359 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009360 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009361 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009362 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009363 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009364 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009365
9366 NotifyMotionArgs motionArgs;
9367 processPosition(mapper, 100, 100);
9368 processSync(mapper);
9369
9370 mFakeListener->assertNotifyMotionWasNotCalled();
9371}
9372
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009373/**
9374 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9375 * the touch mapper can process the events and the events can be delivered to the listener.
9376 */
9377TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9378 addConfigurationProperty("touch.deviceType", "touchScreen");
9379 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009380 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009381 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009382 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009383 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009384 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009385
9386 NotifyMotionArgs motionArgs;
9387 processPosition(mapper, 100, 100);
9388 processSync(mapper);
9389
9390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9391 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9392}
9393
Garfield Tanc734e4f2021-01-15 20:01:39 -08009394TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9395 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009396 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009397 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009398 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009399 std::optional<DisplayViewport> optionalDisplayViewport =
9400 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9401 ASSERT_TRUE(optionalDisplayViewport.has_value());
9402 DisplayViewport displayViewport = *optionalDisplayViewport;
9403
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009404 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009405 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009406 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08009407
9408 // Finger down
9409 int32_t x = 100, y = 100;
9410 processPosition(mapper, x, y);
9411 processSync(mapper);
9412
9413 NotifyMotionArgs motionArgs;
9414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9415 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9416
9417 // Deactivate display viewport
9418 displayViewport.isActive = false;
9419 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009420 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009421
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009422 // The ongoing touch should be canceled immediately
9423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9424 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9425
9426 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009427 x += 10, y += 10;
9428 processPosition(mapper, x, y);
9429 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009431
9432 // Reactivate display viewport
9433 displayViewport.isActive = true;
9434 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009435 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009436
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009437 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009438 x += 10, y += 10;
9439 processPosition(mapper, x, y);
9440 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9442 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009443}
9444
Arthur Hung7c645402019-01-25 17:45:42 +08009445TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9446 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009447 prepareAxes(POSITION | ID | SLOT);
9448 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009449 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009450
9451 // Create the second touch screen device, and enable multi fingers.
9452 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009453 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009454 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009455 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009456 std::shared_ptr<InputDevice> device2 =
9457 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009458 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009459
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009460 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009461 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009462 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009463 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009464 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009465 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009466 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009467 /*flat=*/0, /*fuzz=*/0);
9468 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009469 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9470 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009471
9472 // Setup the second touch screen device.
Arpit Singh56adebc2023-04-25 13:56:05 +00009473 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
9474 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
9475 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009476 std::list<NotifyArgs> unused =
9477 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009478 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009479 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009480
9481 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009482 std::shared_ptr<FakePointerController> fakePointerController =
9483 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009484 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009485
9486 // Setup policy for associated displays and show touches.
9487 const uint8_t hdmi1 = 0;
9488 const uint8_t hdmi2 = 1;
9489 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9490 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9491 mFakePolicy->setShowTouches(true);
9492
9493 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009494 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009495 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009496
9497 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009498 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009499 InputReaderConfiguration::Change::DISPLAY_INFO |
9500 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009501
9502 // Two fingers down at default display.
9503 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9504 processPosition(mapper, x1, y1);
9505 processId(mapper, 1);
9506 processSlot(mapper, 1);
9507 processPosition(mapper, x2, y2);
9508 processId(mapper, 2);
9509 processSync(mapper);
9510
9511 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9512 fakePointerController->getSpots().find(DISPLAY_ID);
9513 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9514 ASSERT_EQ(size_t(2), iter->second.size());
9515
9516 // Two fingers down at second display.
9517 processPosition(mapper2, x1, y1);
9518 processId(mapper2, 1);
9519 processSlot(mapper2, 1);
9520 processPosition(mapper2, x2, y2);
9521 processId(mapper2, 2);
9522 processSync(mapper2);
9523
9524 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9525 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9526 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009527
9528 // Disable the show touches configuration and ensure the spots are cleared.
9529 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009530 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009531 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009532
9533 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009534}
9535
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009536TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009537 prepareAxes(POSITION);
9538 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009539 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +00009540 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009541
9542 NotifyMotionArgs motionArgs;
9543 // Unrotated video frame
9544 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9545 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009546 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009547 processPosition(mapper, 100, 200);
9548 processSync(mapper);
9549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9550 ASSERT_EQ(frames, motionArgs.videoFrames);
9551
9552 // Subsequent touch events should not have any videoframes
9553 // This is implemented separately in FakeEventHub,
9554 // but that should match the behaviour of TouchVideoDevice.
9555 processPosition(mapper, 200, 200);
9556 processSync(mapper);
9557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9558 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9559}
9560
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009561TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009562 prepareAxes(POSITION);
9563 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009564 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009565 // Unrotated video frame
9566 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9567 NotifyMotionArgs motionArgs;
9568
9569 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009570 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009571 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9572 clearViewports();
9573 prepareDisplay(orientation);
9574 std::vector<TouchVideoFrame> frames{frame};
9575 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9576 processPosition(mapper, 100, 200);
9577 processSync(mapper);
9578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9579 ASSERT_EQ(frames, motionArgs.videoFrames);
9580 }
9581}
9582
9583TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9584 prepareAxes(POSITION);
9585 addConfigurationProperty("touch.deviceType", "touchScreen");
9586 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9587 // orientation-aware are affected by display rotation.
9588 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00009589 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009590 // Unrotated video frame
9591 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9592 NotifyMotionArgs motionArgs;
9593
9594 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009595 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009596 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9597 clearViewports();
9598 prepareDisplay(orientation);
9599 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009600 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009601 processPosition(mapper, 100, 200);
9602 processSync(mapper);
9603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009604 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9605 // compared to the display. This is so that when the window transform (which contains the
9606 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9607 // window's coordinate space.
9608 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009609 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009610
9611 // Release finger.
9612 processSync(mapper);
9613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009614 }
9615}
9616
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009617TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009618 prepareAxes(POSITION);
9619 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singh56adebc2023-04-25 13:56:05 +00009620 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009621 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9622 // so mix these.
9623 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9624 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9625 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9626 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9627 NotifyMotionArgs motionArgs;
9628
Michael Wrighta9cf4192022-12-01 23:46:39 +00009629 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009630 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009631 processPosition(mapper, 100, 200);
9632 processSync(mapper);
9633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009634 ASSERT_EQ(frames, motionArgs.videoFrames);
9635}
9636
9637TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9638 prepareAxes(POSITION);
9639 addConfigurationProperty("touch.deviceType", "touchScreen");
9640 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9641 // orientation-aware are affected by display rotation.
9642 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singh56adebc2023-04-25 13:56:05 +00009643 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009644 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9645 // so mix these.
9646 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9647 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9648 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9649 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9650 NotifyMotionArgs motionArgs;
9651
Michael Wrighta9cf4192022-12-01 23:46:39 +00009652 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009653 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9654 processPosition(mapper, 100, 200);
9655 processSync(mapper);
9656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9657 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9658 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9659 // compared to the display. This is so that when the window transform (which contains the
9660 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9661 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009662 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009663 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009664 ASSERT_EQ(frames, motionArgs.videoFrames);
9665}
9666
Arthur Hung9da14732019-09-02 16:16:58 +08009667/**
9668 * If we had defined port associations, but the viewport is not ready, the touch device would be
9669 * expected to be disabled, and it should be enabled after the viewport has found.
9670 */
9671TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009672 constexpr uint8_t hdmi2 = 1;
9673 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009674 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009675
9676 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9677
9678 addConfigurationProperty("touch.deviceType", "touchScreen");
9679 prepareAxes(POSITION);
Arpit Singh56adebc2023-04-25 13:56:05 +00009680 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009681
9682 ASSERT_EQ(mDevice->isEnabled(), false);
9683
9684 // Add display on hdmi2, the device should be enabled and can receive touch event.
9685 prepareSecondaryDisplay(type, hdmi2);
9686 ASSERT_EQ(mDevice->isEnabled(), true);
9687
9688 // Send a touch event.
9689 processPosition(mapper, 100, 100);
9690 processSync(mapper);
9691
9692 NotifyMotionArgs args;
9693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9694 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9695}
9696
Arthur Hung421eb1c2020-01-16 00:09:42 +08009697TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009698 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009699 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009700 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009701 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009702
9703 NotifyMotionArgs motionArgs;
9704
9705 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9706 // finger down
9707 processId(mapper, 1);
9708 processPosition(mapper, x1, y1);
9709 processSync(mapper);
9710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9711 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009712 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009713
9714 // finger move
9715 processId(mapper, 1);
9716 processPosition(mapper, x2, y2);
9717 processSync(mapper);
9718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9719 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009720 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009721
9722 // finger up.
9723 processId(mapper, -1);
9724 processSync(mapper);
9725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9726 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009727 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009728
9729 // new finger down
9730 processId(mapper, 1);
9731 processPosition(mapper, x3, y3);
9732 processSync(mapper);
9733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9734 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009735 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009736}
9737
9738/**
arthurhungcc7f9802020-04-30 17:55:40 +08009739 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9740 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009741 */
arthurhungcc7f9802020-04-30 17:55:40 +08009742TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009743 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009744 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009745 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009746 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009747
9748 NotifyMotionArgs motionArgs;
9749
9750 // default tool type is finger
9751 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009752 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009753 processPosition(mapper, x1, y1);
9754 processSync(mapper);
9755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9756 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009757 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009758
9759 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9760 processToolType(mapper, MT_TOOL_PALM);
9761 processSync(mapper);
9762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9763 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9764
9765 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009766 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009767 processPosition(mapper, x2, y2);
9768 processSync(mapper);
9769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9770
9771 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009772 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009773 processSync(mapper);
9774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9775
9776 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009777 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009778 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009779 processPosition(mapper, x3, y3);
9780 processSync(mapper);
9781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9782 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009783 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009784}
9785
arthurhungbf89a482020-04-17 17:37:55 +08009786/**
arthurhungcc7f9802020-04-30 17:55:40 +08009787 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9788 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009789 */
arthurhungcc7f9802020-04-30 17:55:40 +08009790TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009791 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009792 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009793 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009794 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009795
9796 NotifyMotionArgs motionArgs;
9797
9798 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009799 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9800 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009801 processPosition(mapper, x1, y1);
9802 processSync(mapper);
9803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9804 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009805 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009806
9807 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009808 processSlot(mapper, SECOND_SLOT);
9809 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009810 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009811 processSync(mapper);
9812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009813 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009814 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009815
9816 // If the tool type of the first finger changes to MT_TOOL_PALM,
9817 // we expect to receive ACTION_POINTER_UP with cancel flag.
9818 processSlot(mapper, FIRST_SLOT);
9819 processId(mapper, FIRST_TRACKING_ID);
9820 processToolType(mapper, MT_TOOL_PALM);
9821 processSync(mapper);
9822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009823 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009824 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9825
9826 // The following MOVE events of second finger should be processed.
9827 processSlot(mapper, SECOND_SLOT);
9828 processId(mapper, SECOND_TRACKING_ID);
9829 processPosition(mapper, x2 + 1, y2 + 1);
9830 processSync(mapper);
9831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009833 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009834
9835 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9836 // it. Second finger receive move.
9837 processSlot(mapper, FIRST_SLOT);
9838 processId(mapper, INVALID_TRACKING_ID);
9839 processSync(mapper);
9840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009842 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009843
9844 // Second finger keeps moving.
9845 processSlot(mapper, SECOND_SLOT);
9846 processId(mapper, SECOND_TRACKING_ID);
9847 processPosition(mapper, x2 + 2, y2 + 2);
9848 processSync(mapper);
9849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009851 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009852
9853 // Second finger up.
9854 processId(mapper, INVALID_TRACKING_ID);
9855 processSync(mapper);
9856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9857 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9858 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9859}
9860
9861/**
9862 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9863 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9864 */
9865TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9866 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009867 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009868 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009869 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009870
9871 NotifyMotionArgs motionArgs;
9872
9873 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9874 // First finger down.
9875 processId(mapper, FIRST_TRACKING_ID);
9876 processPosition(mapper, x1, y1);
9877 processSync(mapper);
9878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9879 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009880 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009881
9882 // Second finger down.
9883 processSlot(mapper, SECOND_SLOT);
9884 processId(mapper, SECOND_TRACKING_ID);
9885 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009886 processSync(mapper);
9887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009888 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009889 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009890
arthurhungcc7f9802020-04-30 17:55:40 +08009891 // If the tool type of the first finger changes to MT_TOOL_PALM,
9892 // we expect to receive ACTION_POINTER_UP with cancel flag.
9893 processSlot(mapper, FIRST_SLOT);
9894 processId(mapper, FIRST_TRACKING_ID);
9895 processToolType(mapper, MT_TOOL_PALM);
9896 processSync(mapper);
9897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009898 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009899 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9900
9901 // Second finger keeps moving.
9902 processSlot(mapper, SECOND_SLOT);
9903 processId(mapper, SECOND_TRACKING_ID);
9904 processPosition(mapper, x2 + 1, y2 + 1);
9905 processSync(mapper);
9906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9908
9909 // second finger becomes palm, receive cancel due to only 1 finger is active.
9910 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009911 processToolType(mapper, MT_TOOL_PALM);
9912 processSync(mapper);
9913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9914 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9915
arthurhungcc7f9802020-04-30 17:55:40 +08009916 // third finger down.
9917 processSlot(mapper, THIRD_SLOT);
9918 processId(mapper, THIRD_TRACKING_ID);
9919 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009920 processPosition(mapper, x3, y3);
9921 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9923 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009924 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009925 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009926
9927 // third finger move
9928 processId(mapper, THIRD_TRACKING_ID);
9929 processPosition(mapper, x3 + 1, y3 + 1);
9930 processSync(mapper);
9931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9933
9934 // first finger up, third finger receive move.
9935 processSlot(mapper, FIRST_SLOT);
9936 processId(mapper, INVALID_TRACKING_ID);
9937 processSync(mapper);
9938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009940 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009941
9942 // second finger up, third finger receive move.
9943 processSlot(mapper, SECOND_SLOT);
9944 processId(mapper, INVALID_TRACKING_ID);
9945 processSync(mapper);
9946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9947 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009948 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009949
9950 // third finger up.
9951 processSlot(mapper, THIRD_SLOT);
9952 processId(mapper, INVALID_TRACKING_ID);
9953 processSync(mapper);
9954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9955 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9956 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9957}
9958
9959/**
9960 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9961 * and the active finger could still be allowed to receive the events
9962 */
9963TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9964 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009965 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009966 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +00009967 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009968
9969 NotifyMotionArgs motionArgs;
9970
9971 // default tool type is finger
9972 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9973 processId(mapper, FIRST_TRACKING_ID);
9974 processPosition(mapper, x1, y1);
9975 processSync(mapper);
9976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9977 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009978 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009979
9980 // Second finger down.
9981 processSlot(mapper, SECOND_SLOT);
9982 processId(mapper, SECOND_TRACKING_ID);
9983 processPosition(mapper, x2, y2);
9984 processSync(mapper);
9985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009986 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009987 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009988
9989 // If the tool type of the second finger changes to MT_TOOL_PALM,
9990 // we expect to receive ACTION_POINTER_UP with cancel flag.
9991 processId(mapper, SECOND_TRACKING_ID);
9992 processToolType(mapper, MT_TOOL_PALM);
9993 processSync(mapper);
9994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009995 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009996 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9997
9998 // The following MOVE event should be processed.
9999 processSlot(mapper, FIRST_SLOT);
10000 processId(mapper, FIRST_TRACKING_ID);
10001 processPosition(mapper, x1 + 1, y1 + 1);
10002 processSync(mapper);
10003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10004 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010005 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010006
10007 // second finger up.
10008 processSlot(mapper, SECOND_SLOT);
10009 processId(mapper, INVALID_TRACKING_ID);
10010 processSync(mapper);
10011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10012 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10013
10014 // first finger keep moving
10015 processSlot(mapper, FIRST_SLOT);
10016 processId(mapper, FIRST_TRACKING_ID);
10017 processPosition(mapper, x1 + 2, y1 + 2);
10018 processSync(mapper);
10019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10021
10022 // first finger up.
10023 processId(mapper, INVALID_TRACKING_ID);
10024 processSync(mapper);
10025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10026 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10027 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010028}
10029
Arthur Hung9ad18942021-06-19 02:04:46 +000010030/**
10031 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10032 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10033 * cause slot be valid again.
10034 */
10035TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10036 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010037 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010038 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010039 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010040
10041 NotifyMotionArgs motionArgs;
10042
10043 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10044 // First finger down.
10045 processId(mapper, FIRST_TRACKING_ID);
10046 processPosition(mapper, x1, y1);
10047 processPressure(mapper, RAW_PRESSURE_MAX);
10048 processSync(mapper);
10049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10050 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010051 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010052
10053 // First finger move.
10054 processId(mapper, FIRST_TRACKING_ID);
10055 processPosition(mapper, x1 + 1, y1 + 1);
10056 processPressure(mapper, RAW_PRESSURE_MAX);
10057 processSync(mapper);
10058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010060 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010061
10062 // Second finger down.
10063 processSlot(mapper, SECOND_SLOT);
10064 processId(mapper, SECOND_TRACKING_ID);
10065 processPosition(mapper, x2, y2);
10066 processPressure(mapper, RAW_PRESSURE_MAX);
10067 processSync(mapper);
10068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010069 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010070 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010071
10072 // second finger up with some unexpected data.
10073 processSlot(mapper, SECOND_SLOT);
10074 processId(mapper, INVALID_TRACKING_ID);
10075 processPosition(mapper, x2, y2);
10076 processSync(mapper);
10077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010078 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010079 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010080
10081 // first finger up with some unexpected data.
10082 processSlot(mapper, FIRST_SLOT);
10083 processId(mapper, INVALID_TRACKING_ID);
10084 processPosition(mapper, x2, y2);
10085 processPressure(mapper, RAW_PRESSURE_MAX);
10086 processSync(mapper);
10087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10088 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010089 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010090}
10091
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010092TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10093 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010094 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010095 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010096 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010097
10098 // First finger down.
10099 processId(mapper, FIRST_TRACKING_ID);
10100 processPosition(mapper, 100, 200);
10101 processPressure(mapper, RAW_PRESSURE_MAX);
10102 processSync(mapper);
10103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10104 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10105
10106 // Second finger down.
10107 processSlot(mapper, SECOND_SLOT);
10108 processId(mapper, SECOND_TRACKING_ID);
10109 processPosition(mapper, 300, 400);
10110 processPressure(mapper, RAW_PRESSURE_MAX);
10111 processSync(mapper);
10112 ASSERT_NO_FATAL_FAILURE(
10113 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10114
10115 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010116 // preserved. Resetting should cancel the ongoing gesture.
10117 resetMapper(mapper, ARBITRARY_TIME);
10118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10119 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010120
10121 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10122 // the existing touch state to generate a down event.
10123 processPosition(mapper, 301, 302);
10124 processSync(mapper);
10125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10126 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10128 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10129
10130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10131}
10132
10133TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10134 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010135 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010136 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010137 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010138
10139 // First finger touches down and releases.
10140 processId(mapper, FIRST_TRACKING_ID);
10141 processPosition(mapper, 100, 200);
10142 processPressure(mapper, RAW_PRESSURE_MAX);
10143 processSync(mapper);
10144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10145 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10146 processId(mapper, INVALID_TRACKING_ID);
10147 processSync(mapper);
10148 ASSERT_NO_FATAL_FAILURE(
10149 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10150
10151 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10152 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010153 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10155
10156 // Send an empty sync frame. Since there are no pointers, no events are generated.
10157 processSync(mapper);
10158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10159}
10160
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010161TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010162 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010163 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010164 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010165 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010167
10168 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10169 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10170 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10171 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10172 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10173
10174 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010175 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010176 processId(mapper, FIRST_TRACKING_ID);
10177 processToolType(mapper, MT_TOOL_PEN);
10178 processPosition(mapper, 100, 200);
10179 processPressure(mapper, RAW_PRESSURE_MAX);
10180 processSync(mapper);
10181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10182 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10183 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010184 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010185
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010186 // Now that we know the device supports styluses, ensure that the device is re-configured with
10187 // the stylus source.
10188 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10189 {
10190 const auto& devices = mReader->getInputDevices();
10191 auto deviceInfo =
10192 std::find_if(devices.begin(), devices.end(),
10193 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10194 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10195 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10196 }
10197
10198 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10200
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010201 processId(mapper, INVALID_TRACKING_ID);
10202 processSync(mapper);
10203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10204 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10205 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010206 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010207}
10208
Seunghwan Choi356026c2023-02-01 14:37:25 +090010209TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10210 addConfigurationProperty("touch.deviceType", "touchScreen");
10211 prepareDisplay(ui::ROTATION_0);
10212 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10213 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10214 // indicate stylus presence dynamically.
10215 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10216 std::shared_ptr<FakePointerController> fakePointerController =
10217 std::make_shared<FakePointerController>();
10218 mFakePolicy->setPointerController(fakePointerController);
10219 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singh56adebc2023-04-25 13:56:05 +000010220 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010221
10222 processId(mapper, FIRST_TRACKING_ID);
10223 processPressure(mapper, RAW_PRESSURE_MIN);
10224 processPosition(mapper, 100, 200);
10225 processToolType(mapper, MT_TOOL_PEN);
10226 processSync(mapper);
10227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10228 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010229 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010230 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10231 ASSERT_TRUE(fakePointerController->isPointerShown());
10232 ASSERT_NO_FATAL_FAILURE(
10233 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10234}
10235
10236TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10237 addConfigurationProperty("touch.deviceType", "touchScreen");
10238 prepareDisplay(ui::ROTATION_0);
10239 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10240 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10241 // indicate stylus presence dynamically.
10242 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10243 std::shared_ptr<FakePointerController> fakePointerController =
10244 std::make_shared<FakePointerController>();
10245 mFakePolicy->setPointerController(fakePointerController);
10246 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singh56adebc2023-04-25 13:56:05 +000010247 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090010248
10249 processId(mapper, FIRST_TRACKING_ID);
10250 processPressure(mapper, RAW_PRESSURE_MIN);
10251 processPosition(mapper, 100, 200);
10252 processToolType(mapper, MT_TOOL_PEN);
10253 processSync(mapper);
10254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10255 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010256 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010257 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10258 ASSERT_FALSE(fakePointerController->isPointerShown());
10259}
10260
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010261// --- MultiTouchInputMapperTest_ExternalDevice ---
10262
10263class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10264protected:
Chris Yea52ade12020-08-27 16:49:20 -070010265 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010266};
10267
10268/**
10269 * Expect fallback to internal viewport if device is external and external viewport is not present.
10270 */
10271TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10272 prepareAxes(POSITION);
10273 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010274 prepareDisplay(ui::ROTATION_0);
Arpit Singh56adebc2023-04-25 13:56:05 +000010275 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010276
10277 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10278
10279 NotifyMotionArgs motionArgs;
10280
10281 // Expect the event to be sent to the internal viewport,
10282 // because an external viewport is not present.
10283 processPosition(mapper, 100, 100);
10284 processSync(mapper);
10285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10286 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10287
10288 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010289 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010290 processPosition(mapper, 100, 100);
10291 processSync(mapper);
10292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10293 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10294}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010295
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010296TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10297 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10298 std::shared_ptr<FakePointerController> fakePointerController =
10299 std::make_shared<FakePointerController>();
10300 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10301 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010302
10303 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010304 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010305 prepareAxes(POSITION | ID | SLOT);
10306 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10307 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10308 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010309 mFakePolicy->setPointerController(fakePointerController);
Arpit Singh56adebc2023-04-25 13:56:05 +000010310 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010311
10312 // captured touchpad should be a touchpad source
10313 NotifyDeviceResetArgs resetArgs;
10314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10315 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10316
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010317 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010318
10319 const InputDeviceInfo::MotionRange* relRangeX =
10320 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10321 ASSERT_NE(relRangeX, nullptr);
10322 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10323 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10324 const InputDeviceInfo::MotionRange* relRangeY =
10325 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10326 ASSERT_NE(relRangeY, nullptr);
10327 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10328 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10329
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010330 // run captured pointer tests - note that this is unscaled, so input listener events should be
10331 // identical to what the hardware sends (accounting for any
10332 // calibration).
10333 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010334 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010335 processId(mapper, 1);
10336 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10337 processKey(mapper, BTN_TOUCH, 1);
10338 processSync(mapper);
10339
10340 // expect coord[0] to contain initial location of touch 0
10341 NotifyMotionArgs args;
10342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10343 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010344 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010345 ASSERT_EQ(0, args.pointerProperties[0].id);
10346 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10347 ASSERT_NO_FATAL_FAILURE(
10348 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10349
10350 // FINGER 1 DOWN
10351 processSlot(mapper, 1);
10352 processId(mapper, 2);
10353 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10354 processSync(mapper);
10355
10356 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010358 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010359 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010360 ASSERT_EQ(0, args.pointerProperties[0].id);
10361 ASSERT_EQ(1, args.pointerProperties[1].id);
10362 ASSERT_NO_FATAL_FAILURE(
10363 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10364 ASSERT_NO_FATAL_FAILURE(
10365 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10366
10367 // FINGER 1 MOVE
10368 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10369 processSync(mapper);
10370
10371 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10372 // from move
10373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10375 ASSERT_NO_FATAL_FAILURE(
10376 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10377 ASSERT_NO_FATAL_FAILURE(
10378 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10379
10380 // FINGER 0 MOVE
10381 processSlot(mapper, 0);
10382 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10383 processSync(mapper);
10384
10385 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10388 ASSERT_NO_FATAL_FAILURE(
10389 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10390 ASSERT_NO_FATAL_FAILURE(
10391 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10392
10393 // BUTTON DOWN
10394 processKey(mapper, BTN_LEFT, 1);
10395 processSync(mapper);
10396
10397 // touchinputmapper design sends a move before button press
10398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10401 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10402
10403 // BUTTON UP
10404 processKey(mapper, BTN_LEFT, 0);
10405 processSync(mapper);
10406
10407 // touchinputmapper design sends a move after button release
10408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10409 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10412
10413 // FINGER 0 UP
10414 processId(mapper, -1);
10415 processSync(mapper);
10416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10417 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10418
10419 // FINGER 1 MOVE
10420 processSlot(mapper, 1);
10421 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10422 processSync(mapper);
10423
10424 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010427 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010428 ASSERT_EQ(1, args.pointerProperties[0].id);
10429 ASSERT_NO_FATAL_FAILURE(
10430 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10431
10432 // FINGER 1 UP
10433 processId(mapper, -1);
10434 processKey(mapper, BTN_TOUCH, 0);
10435 processSync(mapper);
10436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10437 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10438
Josep del Río2d8c79a2023-01-23 19:33:50 +000010439 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010440 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010441 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010443 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010444}
10445
10446TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10447 std::shared_ptr<FakePointerController> fakePointerController =
10448 std::make_shared<FakePointerController>();
10449 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10450 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010451
10452 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010453 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010454 prepareAxes(POSITION | ID | SLOT);
10455 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10456 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010457 mFakePolicy->setPointerController(fakePointerController);
Arpit Singh56adebc2023-04-25 13:56:05 +000010458 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010459 // run uncaptured pointer tests - pushes out generic events
10460 // FINGER 0 DOWN
10461 processId(mapper, 3);
10462 processPosition(mapper, 100, 100);
10463 processKey(mapper, BTN_TOUCH, 1);
10464 processSync(mapper);
10465
10466 // start at (100,100), cursor should be at (0,0) * scale
10467 NotifyMotionArgs args;
10468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10469 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10470 ASSERT_NO_FATAL_FAILURE(
10471 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10472
10473 // FINGER 0 MOVE
10474 processPosition(mapper, 200, 200);
10475 processSync(mapper);
10476
10477 // compute scaling to help with touch position checking
10478 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10479 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10480 float scale =
10481 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10482
10483 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10485 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10487 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010488
10489 // BUTTON DOWN
10490 processKey(mapper, BTN_LEFT, 1);
10491 processSync(mapper);
10492
10493 // touchinputmapper design sends a move before button press
10494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10495 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10498
10499 // BUTTON UP
10500 processKey(mapper, BTN_LEFT, 0);
10501 processSync(mapper);
10502
10503 // touchinputmapper design sends a move after button release
10504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10505 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10507 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010508}
10509
10510TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10511 std::shared_ptr<FakePointerController> fakePointerController =
10512 std::make_shared<FakePointerController>();
10513
Michael Wrighta9cf4192022-12-01 23:46:39 +000010514 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010515 prepareAxes(POSITION | ID | SLOT);
10516 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010517 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010518 mFakePolicy->setPointerCapture(false);
Arpit Singh56adebc2023-04-25 13:56:05 +000010519 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010520
Josep del Río2d8c79a2023-01-23 19:33:50 +000010521 // uncaptured touchpad should be a pointer device
10522 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010523
Josep del Río2d8c79a2023-01-23 19:33:50 +000010524 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010525 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010526 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010527 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10528}
10529
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010530// --- BluetoothMultiTouchInputMapperTest ---
10531
10532class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10533protected:
10534 void SetUp() override {
10535 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10536 }
10537};
10538
10539TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10540 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010541 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010542 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singh56adebc2023-04-25 13:56:05 +000010543 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010544
10545 nsecs_t kernelEventTime = ARBITRARY_TIME;
10546 nsecs_t expectedEventTime = ARBITRARY_TIME;
10547 // Touch down.
10548 processId(mapper, FIRST_TRACKING_ID);
10549 processPosition(mapper, 100, 200);
10550 processPressure(mapper, RAW_PRESSURE_MAX);
10551 processSync(mapper, ARBITRARY_TIME);
10552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10553 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10554
10555 // Process several events that come in quick succession, according to their timestamps.
10556 for (int i = 0; i < 3; i++) {
10557 constexpr static nsecs_t delta = ms2ns(1);
10558 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10559 kernelEventTime += delta;
10560 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10561
10562 processPosition(mapper, 101 + i, 201 + i);
10563 processSync(mapper, kernelEventTime);
10564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10565 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10566 WithEventTime(expectedEventTime))));
10567 }
10568
10569 // Release the touch.
10570 processId(mapper, INVALID_TRACKING_ID);
10571 processPressure(mapper, RAW_PRESSURE_MIN);
10572 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10574 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10575 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10576}
10577
10578// --- MultiTouchPointerModeTest ---
10579
HQ Liue6983c72022-04-19 22:14:56 +000010580class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10581protected:
10582 float mPointerMovementScale;
10583 float mPointerXZoomScale;
10584 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10585 addConfigurationProperty("touch.deviceType", "pointer");
10586 std::shared_ptr<FakePointerController> fakePointerController =
10587 std::make_shared<FakePointerController>();
10588 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10589 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010590 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010591
10592 prepareAxes(POSITION);
10593 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10594 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10595 // needs to be disabled, and the pointer gesture needs to be enabled.
10596 mFakePolicy->setPointerCapture(false);
10597 mFakePolicy->setPointerGestureEnabled(true);
10598 mFakePolicy->setPointerController(fakePointerController);
10599
10600 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10601 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10602 mPointerMovementScale =
10603 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10604 mPointerXZoomScale =
10605 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10606 }
10607
10608 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10609 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10610 /*flat*/ 0,
10611 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10612 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10613 /*flat*/ 0,
10614 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10615 }
10616};
10617
10618/**
10619 * Two fingers down on a pointer mode touch pad. The width
10620 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10621 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10622 * be greater than the both value to be freeform gesture, so that after two
10623 * fingers start to move downwards, the gesture should be swipe.
10624 */
10625TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10626 // The min freeform gesture width is 25units/mm x 30mm = 750
10627 // which is greater than fraction of the diagnal length of the touchpad (349).
10628 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010629 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010630 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010631 NotifyMotionArgs motionArgs;
10632
10633 // Two fingers down at once.
10634 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10635 // Pointer's initial position is used the [0,0] coordinate.
10636 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10637
10638 processId(mapper, FIRST_TRACKING_ID);
10639 processPosition(mapper, x1, y1);
10640 processMTSync(mapper);
10641 processId(mapper, SECOND_TRACKING_ID);
10642 processPosition(mapper, x2, y2);
10643 processMTSync(mapper);
10644 processSync(mapper);
10645
10646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010647 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010648 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010649 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010650 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010651 ASSERT_NO_FATAL_FAILURE(
10652 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10653
10654 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10655 // that there should be 1 pointer.
10656 int32_t movingDistance = 200;
10657 y1 += movingDistance;
10658 y2 += movingDistance;
10659
10660 processId(mapper, FIRST_TRACKING_ID);
10661 processPosition(mapper, x1, y1);
10662 processMTSync(mapper);
10663 processId(mapper, SECOND_TRACKING_ID);
10664 processPosition(mapper, x2, y2);
10665 processMTSync(mapper);
10666 processSync(mapper);
10667
10668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010669 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010671 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010672 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10674 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10675 0, 0, 0, 0));
10676}
10677
10678/**
10679 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10680 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10681 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10682 * value to be freeform gesture, so that after two fingers start to move downwards,
10683 * the gesture should be swipe.
10684 */
10685TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10686 // The min freeform gesture width is 5units/mm x 30mm = 150
10687 // which is greater than fraction of the diagnal length of the touchpad (349).
10688 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010689 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singh56adebc2023-04-25 13:56:05 +000010690 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010691 NotifyMotionArgs motionArgs;
10692
10693 // Two fingers down at once.
10694 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10695 // Pointer's initial position is used the [0,0] coordinate.
10696 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10697
10698 processId(mapper, FIRST_TRACKING_ID);
10699 processPosition(mapper, x1, y1);
10700 processMTSync(mapper);
10701 processId(mapper, SECOND_TRACKING_ID);
10702 processPosition(mapper, x2, y2);
10703 processMTSync(mapper);
10704 processSync(mapper);
10705
10706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010707 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010708 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010709 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010710 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010711 ASSERT_NO_FATAL_FAILURE(
10712 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10713
10714 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10715 // and there should be 1 pointer.
10716 int32_t movingDistance = 200;
10717 y1 += movingDistance;
10718 y2 += movingDistance;
10719
10720 processId(mapper, FIRST_TRACKING_ID);
10721 processPosition(mapper, x1, y1);
10722 processMTSync(mapper);
10723 processId(mapper, SECOND_TRACKING_ID);
10724 processPosition(mapper, x2, y2);
10725 processMTSync(mapper);
10726 processSync(mapper);
10727
10728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010729 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010730 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010731 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010732 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010733 // New coordinate is the scaled relative coordinate from the initial coordinate.
10734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10735 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10736 0, 0, 0, 0));
10737}
10738
10739/**
10740 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10741 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10742 * freeform gestures after two fingers start to move downwards.
10743 */
10744TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010745 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010746 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010747
10748 NotifyMotionArgs motionArgs;
10749
10750 // Two fingers down at once. Wider than the max swipe width.
10751 // The gesture is expected to be PRESS, then transformed to FREEFORM
10752 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10753
10754 processId(mapper, FIRST_TRACKING_ID);
10755 processPosition(mapper, x1, y1);
10756 processMTSync(mapper);
10757 processId(mapper, SECOND_TRACKING_ID);
10758 processPosition(mapper, x2, y2);
10759 processMTSync(mapper);
10760 processSync(mapper);
10761
10762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010763 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010764 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010765 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010766 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010767 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10768 ASSERT_NO_FATAL_FAILURE(
10769 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10770
10771 int32_t movingDistance = 200;
10772
10773 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10774 // then two down events for two pointers.
10775 y1 += movingDistance;
10776 y2 += movingDistance;
10777
10778 processId(mapper, FIRST_TRACKING_ID);
10779 processPosition(mapper, x1, y1);
10780 processMTSync(mapper);
10781 processId(mapper, SECOND_TRACKING_ID);
10782 processPosition(mapper, x2, y2);
10783 processMTSync(mapper);
10784 processSync(mapper);
10785
10786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10787 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010788 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010789 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010791 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010792 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010793 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010795 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010796 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010797 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010798 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010799 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010800 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010801 // Two pointers' scaled relative coordinates from their initial centroid.
10802 // Initial y coordinates are 0 as y1 and y2 have the same value.
10803 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10804 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10805 // When pointers move, the new coordinates equal to the initial coordinates plus
10806 // scaled moving distance.
10807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10808 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10809 0, 0, 0, 0));
10810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10811 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10812 0, 0, 0, 0));
10813
10814 // Move two fingers down again, expect one MOVE motion event.
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));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010827 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010829 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010830 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10832 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10833 0, 0, 0, 0, 0));
10834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10835 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10836 0, 0, 0, 0, 0));
10837}
10838
Harry Cutts39b7ca22022-10-05 15:55:48 +000010839TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010840 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singh56adebc2023-04-25 13:56:05 +000010841 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010842 NotifyMotionArgs motionArgs;
10843
10844 // Place two fingers down.
10845 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10846
10847 processId(mapper, FIRST_TRACKING_ID);
10848 processPosition(mapper, x1, y1);
10849 processMTSync(mapper);
10850 processId(mapper, SECOND_TRACKING_ID);
10851 processPosition(mapper, x2, y2);
10852 processMTSync(mapper);
10853 processSync(mapper);
10854
10855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010856 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010857 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10858 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10859 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10860 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10861
10862 // Move the two fingers down and to the left.
10863 int32_t movingDistance = 200;
10864 x1 -= movingDistance;
10865 y1 += movingDistance;
10866 x2 -= movingDistance;
10867 y2 += movingDistance;
10868
10869 processId(mapper, FIRST_TRACKING_ID);
10870 processPosition(mapper, x1, y1);
10871 processMTSync(mapper);
10872 processId(mapper, SECOND_TRACKING_ID);
10873 processPosition(mapper, x2, y2);
10874 processMTSync(mapper);
10875 processSync(mapper);
10876
10877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010878 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010879 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10880 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10881 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10882 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10883}
10884
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010885TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010886 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010887 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singh56adebc2023-04-25 13:56:05 +000010888 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10890
10891 // Start a stylus gesture.
10892 processKey(mapper, BTN_TOOL_PEN, 1);
10893 processId(mapper, FIRST_TRACKING_ID);
10894 processPosition(mapper, 100, 200);
10895 processSync(mapper);
10896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10897 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10898 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010899 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010900 // TODO(b/257078296): Pointer mode generates extra event.
10901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10902 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10903 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010904 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10906
10907 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10908 // gesture should be disabled.
10909 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10910 viewport->isActive = false;
10911 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010912 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10914 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10915 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010916 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010917 // TODO(b/257078296): Pointer mode generates extra event.
10918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10919 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10920 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010921 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10923}
10924
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010925// --- JoystickInputMapperTest ---
10926
10927class JoystickInputMapperTest : public InputMapperTest {
10928protected:
10929 static const int32_t RAW_X_MIN;
10930 static const int32_t RAW_X_MAX;
10931 static const int32_t RAW_Y_MIN;
10932 static const int32_t RAW_Y_MAX;
10933
10934 void SetUp() override {
10935 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10936 }
10937 void prepareAxes() {
10938 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10939 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10940 }
10941
10942 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10944 }
10945
10946 void processSync(JoystickInputMapper& mapper) {
10947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10948 }
10949
Michael Wrighta9cf4192022-12-01 23:46:39 +000010950 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010951 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10952 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10953 NO_PORT, ViewportType::VIRTUAL);
10954 }
10955};
10956
10957const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10958const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10959const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10960const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10961
10962TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10963 prepareAxes();
Arpit Singh2be4a362023-04-26 14:16:50 +000010964 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010965
10966 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10967
Michael Wrighta9cf4192022-12-01 23:46:39 +000010968 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010969
10970 // Send an axis event
10971 processAxis(mapper, ABS_X, 100);
10972 processSync(mapper);
10973
10974 NotifyMotionArgs args;
10975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10976 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10977
10978 // Send another axis event
10979 processAxis(mapper, ABS_Y, 100);
10980 processSync(mapper);
10981
10982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10983 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10984}
10985
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010986// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010987
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010988class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010989protected:
10990 static const char* DEVICE_NAME;
10991 static const char* DEVICE_LOCATION;
10992 static const int32_t DEVICE_ID;
10993 static const int32_t DEVICE_GENERATION;
10994 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010995 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010996 static const int32_t EVENTHUB_ID;
10997
10998 std::shared_ptr<FakeEventHub> mFakeEventHub;
10999 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011000 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011001 std::unique_ptr<InstrumentedInputReader> mReader;
11002 std::shared_ptr<InputDevice> mDevice;
11003
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011004 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011005 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011006 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011007 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011008 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011009 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011010 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11011 }
11012
11013 void SetUp() override { SetUp(DEVICE_CLASSES); }
11014
11015 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011016 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011017 mFakePolicy.clear();
11018 }
11019
Chris Yee2b1e5c2021-03-10 22:45:12 -080011020 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11021 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011022 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011023 InputDeviceIdentifier identifier;
11024 identifier.name = name;
11025 identifier.location = location;
11026 std::shared_ptr<InputDevice> device =
11027 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11028 identifier);
11029 mReader->pushNextDevice(device);
11030 mFakeEventHub->addDevice(eventHubId, name, classes);
11031 mReader->loopOnce();
11032 return device;
11033 }
11034
11035 template <class T, typename... Args>
11036 T& addControllerAndConfigure(Args... args) {
11037 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11038
11039 return controller;
11040 }
11041};
11042
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011043const char* PeripheralControllerTest::DEVICE_NAME = "device";
11044const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11045const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11046const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11047const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011048const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11049 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011050const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011051
11052// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011053class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011054protected:
11055 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011056 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011057 }
11058};
11059
11060TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011061 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011062
Harry Cuttsa5b71292022-11-28 12:56:17 +000011063 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11064 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11065 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011066}
11067
11068TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011069 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011070
Harry Cuttsa5b71292022-11-28 12:56:17 +000011071 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11072 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11073 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011074}
11075
11076// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011077class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011078protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011079 void SetUp() override {
11080 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11081 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011082};
11083
Chris Ye85758332021-05-16 23:05:17 -070011084TEST_F(LightControllerTest, MonoLight) {
11085 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011086 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011087 .maxBrightness = 255,
11088 .flags = InputLightClass::BRIGHTNESS,
11089 .path = ""};
11090 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011091
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011092 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011093 InputDeviceInfo info;
11094 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011095 std::vector<InputDeviceLightInfo> lights = info.getLights();
11096 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011097 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11098 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11099
11100 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11101 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11102}
11103
11104TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11105 RawLightInfo infoMono = {.id = 1,
11106 .name = "mono_keyboard_backlight",
11107 .maxBrightness = 255,
11108 .flags = InputLightClass::BRIGHTNESS |
11109 InputLightClass::KEYBOARD_BACKLIGHT,
11110 .path = ""};
11111 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11112
11113 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11114 InputDeviceInfo info;
11115 controller.populateDeviceInfo(&info);
11116 std::vector<InputDeviceLightInfo> lights = info.getLights();
11117 ASSERT_EQ(1U, lights.size());
11118 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11119 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011120
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011121 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11122 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011123}
11124
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011125TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11126 RawLightInfo infoMono = {.id = 1,
11127 .name = "mono_light",
11128 .maxBrightness = 255,
11129 .flags = InputLightClass::BRIGHTNESS,
11130 .path = ""};
11131 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11132 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11133 "0,100,200");
11134
11135 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11136 std::list<NotifyArgs> unused =
11137 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11138 /*changes=*/{});
11139
11140 InputDeviceInfo info;
11141 controller.populateDeviceInfo(&info);
11142 std::vector<InputDeviceLightInfo> lights = info.getLights();
11143 ASSERT_EQ(1U, lights.size());
11144 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11145}
11146
11147TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11148 RawLightInfo infoMono = {.id = 1,
11149 .name = "mono_keyboard_backlight",
11150 .maxBrightness = 255,
11151 .flags = InputLightClass::BRIGHTNESS |
11152 InputLightClass::KEYBOARD_BACKLIGHT,
11153 .path = ""};
11154 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11155
11156 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11157 std::list<NotifyArgs> unused =
11158 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11159 /*changes=*/{});
11160
11161 InputDeviceInfo info;
11162 controller.populateDeviceInfo(&info);
11163 std::vector<InputDeviceLightInfo> lights = info.getLights();
11164 ASSERT_EQ(1U, lights.size());
11165 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11166}
11167
11168TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11169 RawLightInfo infoMono = {.id = 1,
11170 .name = "mono_keyboard_backlight",
11171 .maxBrightness = 255,
11172 .flags = InputLightClass::BRIGHTNESS |
11173 InputLightClass::KEYBOARD_BACKLIGHT,
11174 .path = ""};
11175 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11176 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11177 "0,100,200");
11178
11179 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11180 std::list<NotifyArgs> unused =
11181 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11182 /*changes=*/{});
11183
11184 InputDeviceInfo info;
11185 controller.populateDeviceInfo(&info);
11186 std::vector<InputDeviceLightInfo> lights = info.getLights();
11187 ASSERT_EQ(1U, lights.size());
11188 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
11189 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
11190 ASSERT_EQ(BrightnessLevel(0), *it);
11191 std::advance(it, 1);
11192 ASSERT_EQ(BrightnessLevel(100), *it);
11193 std::advance(it, 1);
11194 ASSERT_EQ(BrightnessLevel(200), *it);
11195}
11196
11197TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
11198 RawLightInfo infoMono = {.id = 1,
11199 .name = "mono_keyboard_backlight",
11200 .maxBrightness = 255,
11201 .flags = InputLightClass::BRIGHTNESS |
11202 InputLightClass::KEYBOARD_BACKLIGHT,
11203 .path = ""};
11204 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11205 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11206 "0,100,200,300,400,500");
11207
11208 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11209 std::list<NotifyArgs> unused =
11210 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11211 /*changes=*/{});
11212
11213 InputDeviceInfo info;
11214 controller.populateDeviceInfo(&info);
11215 std::vector<InputDeviceLightInfo> lights = info.getLights();
11216 ASSERT_EQ(1U, lights.size());
11217 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11218}
11219
Chris Yee2b1e5c2021-03-10 22:45:12 -080011220TEST_F(LightControllerTest, RGBLight) {
11221 RawLightInfo infoRed = {.id = 1,
11222 .name = "red",
11223 .maxBrightness = 255,
11224 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11225 .path = ""};
11226 RawLightInfo infoGreen = {.id = 2,
11227 .name = "green",
11228 .maxBrightness = 255,
11229 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11230 .path = ""};
11231 RawLightInfo infoBlue = {.id = 3,
11232 .name = "blue",
11233 .maxBrightness = 255,
11234 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11235 .path = ""};
11236 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11237 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11238 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11239
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011240 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011241 InputDeviceInfo info;
11242 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011243 std::vector<InputDeviceLightInfo> lights = info.getLights();
11244 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011245 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11246 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11247 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11248
11249 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11250 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11251}
11252
11253TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11254 RawLightInfo infoRed = {.id = 1,
11255 .name = "red_keyboard_backlight",
11256 .maxBrightness = 255,
11257 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11258 InputLightClass::KEYBOARD_BACKLIGHT,
11259 .path = ""};
11260 RawLightInfo infoGreen = {.id = 2,
11261 .name = "green_keyboard_backlight",
11262 .maxBrightness = 255,
11263 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11264 InputLightClass::KEYBOARD_BACKLIGHT,
11265 .path = ""};
11266 RawLightInfo infoBlue = {.id = 3,
11267 .name = "blue_keyboard_backlight",
11268 .maxBrightness = 255,
11269 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11270 InputLightClass::KEYBOARD_BACKLIGHT,
11271 .path = ""};
11272 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11273 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11274 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11275
11276 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11277 InputDeviceInfo info;
11278 controller.populateDeviceInfo(&info);
11279 std::vector<InputDeviceLightInfo> lights = info.getLights();
11280 ASSERT_EQ(1U, lights.size());
11281 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11282 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11283 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11284
11285 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11286 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11287}
11288
11289TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11290 RawLightInfo infoRed = {.id = 1,
11291 .name = "red",
11292 .maxBrightness = 255,
11293 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11294 .path = ""};
11295 RawLightInfo infoGreen = {.id = 2,
11296 .name = "green",
11297 .maxBrightness = 255,
11298 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11299 .path = ""};
11300 RawLightInfo infoBlue = {.id = 3,
11301 .name = "blue",
11302 .maxBrightness = 255,
11303 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11304 .path = ""};
11305 RawLightInfo infoGlobal = {.id = 3,
11306 .name = "global_keyboard_backlight",
11307 .maxBrightness = 255,
11308 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11309 InputLightClass::KEYBOARD_BACKLIGHT,
11310 .path = ""};
11311 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11312 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11313 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11314 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
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::INPUT, lights[0].type);
11322 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11323 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011324
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011325 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11326 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011327}
11328
11329TEST_F(LightControllerTest, MultiColorRGBLight) {
11330 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011331 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011332 .maxBrightness = 255,
11333 .flags = InputLightClass::BRIGHTNESS |
11334 InputLightClass::MULTI_INTENSITY |
11335 InputLightClass::MULTI_INDEX,
11336 .path = ""};
11337
11338 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11339
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011340 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011341 InputDeviceInfo info;
11342 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011343 std::vector<InputDeviceLightInfo> lights = info.getLights();
11344 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011345 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11346 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11347 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11348
11349 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11350 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11351}
11352
11353TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11354 RawLightInfo infoColor = {.id = 1,
11355 .name = "multi_color_keyboard_backlight",
11356 .maxBrightness = 255,
11357 .flags = InputLightClass::BRIGHTNESS |
11358 InputLightClass::MULTI_INTENSITY |
11359 InputLightClass::MULTI_INDEX |
11360 InputLightClass::KEYBOARD_BACKLIGHT,
11361 .path = ""};
11362
11363 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11364
11365 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11366 InputDeviceInfo info;
11367 controller.populateDeviceInfo(&info);
11368 std::vector<InputDeviceLightInfo> lights = info.getLights();
11369 ASSERT_EQ(1U, lights.size());
11370 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11371 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11372 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011373
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011374 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11375 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011376}
11377
11378TEST_F(LightControllerTest, PlayerIdLight) {
11379 RawLightInfo info1 = {.id = 1,
11380 .name = "player1",
11381 .maxBrightness = 255,
11382 .flags = InputLightClass::BRIGHTNESS,
11383 .path = ""};
11384 RawLightInfo info2 = {.id = 2,
11385 .name = "player2",
11386 .maxBrightness = 255,
11387 .flags = InputLightClass::BRIGHTNESS,
11388 .path = ""};
11389 RawLightInfo info3 = {.id = 3,
11390 .name = "player3",
11391 .maxBrightness = 255,
11392 .flags = InputLightClass::BRIGHTNESS,
11393 .path = ""};
11394 RawLightInfo info4 = {.id = 4,
11395 .name = "player4",
11396 .maxBrightness = 255,
11397 .flags = InputLightClass::BRIGHTNESS,
11398 .path = ""};
11399 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11400 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11401 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11402 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11403
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011404 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011405 InputDeviceInfo info;
11406 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011407 std::vector<InputDeviceLightInfo> lights = info.getLights();
11408 ASSERT_EQ(1U, lights.size());
11409 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011410 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11411 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011412
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011413 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11414 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11415 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011416}
11417
Michael Wrightd02c5b62014-02-10 15:10:22 -080011418} // namespace android