blob: 853c5b0115949303ad5ed6366ffe8c875cbd7987 [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:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800174 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
175 : InputMapper(deviceContext),
176 mSources(sources),
177 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800179 mConfigureWasCalled(false),
180 mResetWasCalled(false),
181 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800182
Chris Yea52ade12020-08-27 16:49:20 -0700183 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184
185 void setKeyboardType(int32_t keyboardType) {
186 mKeyboardType = keyboardType;
187 }
188
189 void setMetaState(int32_t metaState) {
190 mMetaState = metaState;
191 }
192
193 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700194 std::unique_lock<std::mutex> lock(mLock);
195 base::ScopedLockAssertion assumeLocked(mLock);
196 const bool configureCalled =
197 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
198 return mConfigureWasCalled;
199 });
200 if (!configureCalled) {
201 FAIL() << "Expected configure() to have been called.";
202 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800203 mConfigureWasCalled = false;
204 }
205
206 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700207 std::unique_lock<std::mutex> lock(mLock);
208 base::ScopedLockAssertion assumeLocked(mLock);
209 const bool resetCalled =
210 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
211 return mResetWasCalled;
212 });
213 if (!resetCalled) {
214 FAIL() << "Expected reset() to have been called.";
215 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800216 mResetWasCalled = false;
217 }
218
Yi Kong9b14ac62018-07-17 13:48:38 -0700219 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700220 std::unique_lock<std::mutex> lock(mLock);
221 base::ScopedLockAssertion assumeLocked(mLock);
222 const bool processCalled =
223 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
224 return mProcessWasCalled;
225 });
226 if (!processCalled) {
227 FAIL() << "Expected process() to have been called.";
228 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800229 if (outLastEvent) {
230 *outLastEvent = mLastEvent;
231 }
232 mProcessWasCalled = false;
233 }
234
235 void setKeyCodeState(int32_t keyCode, int32_t state) {
236 mKeyCodeStates.replaceValueFor(keyCode, state);
237 }
238
239 void setScanCodeState(int32_t scanCode, int32_t state) {
240 mScanCodeStates.replaceValueFor(scanCode, state);
241 }
242
243 void setSwitchState(int32_t switchCode, int32_t state) {
244 mSwitchStates.replaceValueFor(switchCode, state);
245 }
246
247 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800248 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249 }
250
Philip Junker4af3b3d2021-12-14 10:36:55 +0100251 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
252 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
253 }
254
Michael Wrightd02c5b62014-02-10 15:10:22 -0800255private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100256 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800257
Harry Cuttsd02ea102023-03-17 18:21:30 +0000258 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 InputMapper::populateDeviceInfo(deviceInfo);
260
261 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000262 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800263 }
264 }
265
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700266 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
267 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700268 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800269 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800270
271 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800272 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800273 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
274 mViewport = config->getDisplayViewportByPort(*displayPort);
275 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700276
277 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700278 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279 }
280
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700281 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700282 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700284 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700285 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800286 }
287
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700288 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700289 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800290 mLastEvent = *rawEvent;
291 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700292 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700293 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800294 }
295
Chris Yea52ade12020-08-27 16:49:20 -0700296 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800297 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
298 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
299 }
300
Philip Junker4af3b3d2021-12-14 10:36:55 +0100301 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
302 auto it = mKeyCodeMapping.find(locationKeyCode);
303 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
304 }
305
Chris Yea52ade12020-08-27 16:49:20 -0700306 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
308 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
309 }
310
Chris Yea52ade12020-08-27 16:49:20 -0700311 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800312 ssize_t index = mSwitchStates.indexOfKey(switchCode);
313 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
314 }
315
Chris Yea52ade12020-08-27 16:49:20 -0700316 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700317 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700318 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700319 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
321 if (keyCodes[i] == mSupportedKeyCodes[j]) {
322 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323 }
324 }
325 }
Chris Yea52ade12020-08-27 16:49:20 -0700326 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 return result;
328 }
329
330 virtual int32_t getMetaState() {
331 return mMetaState;
332 }
333
334 virtual void fadePointer() {
335 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800336
337 virtual std::optional<int32_t> getAssociatedDisplay() {
338 if (mViewport) {
339 return std::make_optional(mViewport->displayId);
340 }
341 return std::nullopt;
342 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800343};
344
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700345// --- InputReaderPolicyTest ---
346class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700347protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700348 sp<FakeInputReaderPolicy> mFakePolicy;
349
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700350 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700351 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700352};
353
354/**
355 * Check that empty set of viewports is an acceptable configuration.
356 * Also try to get internal viewport two different ways - by type and by uniqueId.
357 *
358 * There will be confusion if two viewports with empty uniqueId and identical type are present.
359 * Such configuration is not currently allowed.
360 */
361TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700362 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700363
364 // We didn't add any viewports yet, so there shouldn't be any.
365 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100366 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700367 ASSERT_FALSE(internalViewport);
368
369 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000370 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000371 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700372
373 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700374 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700375 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100376 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700377
378 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100379 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700380 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700381 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700382
383 mFakePolicy->clearViewports();
384 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700385 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700386 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100387 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700388 ASSERT_FALSE(internalViewport);
389}
390
391TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
392 const std::string internalUniqueId = "local:0";
393 const std::string externalUniqueId = "local:1";
394 const std::string virtualUniqueId1 = "virtual:2";
395 const std::string virtualUniqueId2 = "virtual:3";
396 constexpr int32_t virtualDisplayId1 = 2;
397 constexpr int32_t virtualDisplayId2 = 3;
398
399 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000400 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000401 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000402 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700403 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000404 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000405 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000406 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700407 // Add an virtual viewport
408 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000409 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000410 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700411 // Add another virtual viewport
412 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000413 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000414 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700415
416 // Check matching by type for internal
417 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100418 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700419 ASSERT_TRUE(internalViewport);
420 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
421
422 // Check matching by type for external
423 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100424 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700425 ASSERT_TRUE(externalViewport);
426 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
427
428 // Check matching by uniqueId for virtual viewport #1
429 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700430 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700431 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100432 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700433 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
434 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
435
436 // Check matching by uniqueId for virtual viewport #2
437 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700438 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700439 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100440 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700441 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
442 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
443}
444
445
446/**
447 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
448 * that lookup works by checking display id.
449 * Check that 2 viewports of each kind is possible, for all existing viewport types.
450 */
451TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
452 const std::string uniqueId1 = "uniqueId1";
453 const std::string uniqueId2 = "uniqueId2";
454 constexpr int32_t displayId1 = 2;
455 constexpr int32_t displayId2 = 3;
456
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100457 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
458 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700459 for (const ViewportType& type : types) {
460 mFakePolicy->clearViewports();
461 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000462 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000463 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700464 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000465 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000466 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700467
468 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700469 std::optional<DisplayViewport> viewport1 =
470 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700471 ASSERT_TRUE(viewport1);
472 ASSERT_EQ(displayId1, viewport1->displayId);
473 ASSERT_EQ(type, viewport1->type);
474
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700475 std::optional<DisplayViewport> viewport2 =
476 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700477 ASSERT_TRUE(viewport2);
478 ASSERT_EQ(displayId2, viewport2->displayId);
479 ASSERT_EQ(type, viewport2->type);
480
481 // When there are multiple viewports of the same kind, and uniqueId is not specified
482 // in the call to getDisplayViewport, then that situation is not supported.
483 // The viewports can be stored in any order, so we cannot rely on the order, since that
484 // is just implementation detail.
485 // However, we can check that it still returns *a* viewport, we just cannot assert
486 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700487 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700488 ASSERT_TRUE(someViewport);
489 }
490}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800491
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700492/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000493 * When we have multiple internal displays make sure we always return the default display when
494 * querying by type.
495 */
496TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
497 const std::string uniqueId1 = "uniqueId1";
498 const std::string uniqueId2 = "uniqueId2";
499 constexpr int32_t nonDefaultDisplayId = 2;
500 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
501 "Test display ID should not be ADISPLAY_ID_DEFAULT");
502
503 // Add the default display first and ensure it gets returned.
504 mFakePolicy->clearViewports();
505 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000506 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000507 ViewportType::INTERNAL);
508 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000509 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000510 ViewportType::INTERNAL);
511
512 std::optional<DisplayViewport> viewport =
513 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
514 ASSERT_TRUE(viewport);
515 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
516 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
517
518 // Add the default display second to make sure order doesn't matter.
519 mFakePolicy->clearViewports();
520 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000521 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000522 ViewportType::INTERNAL);
523 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000524 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000525 ViewportType::INTERNAL);
526
527 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
528 ASSERT_TRUE(viewport);
529 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
530 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
531}
532
533/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700534 * Check getDisplayViewportByPort
535 */
536TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100537 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700538 const std::string uniqueId1 = "uniqueId1";
539 const std::string uniqueId2 = "uniqueId2";
540 constexpr int32_t displayId1 = 1;
541 constexpr int32_t displayId2 = 2;
542 const uint8_t hdmi1 = 0;
543 const uint8_t hdmi2 = 1;
544 const uint8_t hdmi3 = 2;
545
546 mFakePolicy->clearViewports();
547 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000548 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000549 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700550 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000551 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000552 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700553
554 // Check that correct display viewport was returned by comparing the display ports.
555 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
556 ASSERT_TRUE(hdmi1Viewport);
557 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
558 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
559
560 // Check that we can still get the same viewport using the uniqueId
561 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
562 ASSERT_TRUE(hdmi1Viewport);
563 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
564 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
565 ASSERT_EQ(type, hdmi1Viewport->type);
566
567 // Check that we cannot find a port with "HDMI2", because we never added one
568 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
569 ASSERT_FALSE(hdmi2Viewport);
570}
571
Michael Wrightd02c5b62014-02-10 15:10:22 -0800572// --- InputReaderTest ---
573
574class InputReaderTest : public testing::Test {
575protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700576 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800577 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700578 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000579 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800580
Chris Yea52ade12020-08-27 16:49:20 -0700581 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700582 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700583 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700584 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800585
Prabir Pradhan28efc192019-11-05 01:10:04 +0000586 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700587 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 }
589
Chris Yea52ade12020-08-27 16:49:20 -0700590 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700591 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 }
594
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700595 void addDevice(int32_t eventHubId, const std::string& name,
596 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800597 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598
599 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800600 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601 }
602 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000603 mReader->loopOnce();
604 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700605 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
606 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800607 }
608
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800609 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700610 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000611 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700612 }
613
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800614 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700615 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000616 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700617 }
618
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800619 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700620 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700621 ftl::Flags<InputDeviceClass> classes,
622 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800623 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800624 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
625 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800626 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800627 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800628 return mapper;
629 }
630};
631
Chris Ye98d3f532020-10-01 21:48:59 -0700632TEST_F(InputReaderTest, PolicyGetInputDevices) {
633 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700634 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700635 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800636
637 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700638 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800639 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800640 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100641 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800642 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
643 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000644 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645}
646
Chris Yee7310032020-09-22 15:36:28 -0700647TEST_F(InputReaderTest, GetMergedInputDevices) {
648 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
649 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
650 // Add two subdevices to device
651 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
652 // Must add at least one mapper or the device will be ignored!
653 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
654 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
655
656 // Push same device instance for next device to be added, so they'll have same identifier.
657 mReader->pushNextDevice(device);
658 mReader->pushNextDevice(device);
659 ASSERT_NO_FATAL_FAILURE(
660 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
661 ASSERT_NO_FATAL_FAILURE(
662 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
663
664 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000665 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700666}
667
Chris Yee14523a2020-12-19 13:46:00 -0800668TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
669 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
670 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
671 // Add two subdevices to device
672 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
673 // Must add at least one mapper or the device will be ignored!
674 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
675 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
676
677 // Push same device instance for next device to be added, so they'll have same identifier.
678 mReader->pushNextDevice(device);
679 mReader->pushNextDevice(device);
680 // Sensor device is initially disabled
681 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
682 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
683 nullptr));
684 // Device is disabled because the only sub device is a sensor device and disabled initially.
685 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
686 ASSERT_FALSE(device->isEnabled());
687 ASSERT_NO_FATAL_FAILURE(
688 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
689 // The merged device is enabled if any sub device is enabled
690 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
691 ASSERT_TRUE(device->isEnabled());
692}
693
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700694TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800695 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700696 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800697 constexpr int32_t eventHubId = 1;
698 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700699 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800700 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800701 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800702 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700703
Yi Kong9b14ac62018-07-17 13:48:38 -0700704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700705
706 NotifyDeviceResetArgs resetArgs;
707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700708 ASSERT_EQ(deviceId, resetArgs.deviceId);
709
710 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800711 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000712 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700713
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700715 ASSERT_EQ(deviceId, resetArgs.deviceId);
716 ASSERT_EQ(device->isEnabled(), false);
717
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800718 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000719 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700722 ASSERT_EQ(device->isEnabled(), false);
723
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800724 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000725 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700727 ASSERT_EQ(deviceId, resetArgs.deviceId);
728 ASSERT_EQ(device->isEnabled(), true);
729}
730
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800732 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700733 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800734 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800735 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800736 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800737 AINPUT_SOURCE_KEYBOARD, nullptr);
738 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800739
740 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
741 AINPUT_SOURCE_ANY, AKEYCODE_A))
742 << "Should return unknown when the device id is >= 0 but unknown.";
743
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800744 ASSERT_EQ(AKEY_STATE_UNKNOWN,
745 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
746 << "Should return unknown when the device id is valid but the sources are not "
747 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800748
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800749 ASSERT_EQ(AKEY_STATE_DOWN,
750 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
751 AKEYCODE_A))
752 << "Should return value provided by mapper when device id is valid and the device "
753 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754
755 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
756 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
757 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
758
759 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
760 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
761 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
762}
763
Philip Junker4af3b3d2021-12-14 10:36:55 +0100764TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
765 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
766 constexpr int32_t eventHubId = 1;
767 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
768 InputDeviceClass::KEYBOARD,
769 AINPUT_SOURCE_KEYBOARD, nullptr);
770 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
771
772 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
773 << "Should return unknown when the device with the specified id is not found.";
774
775 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
776 << "Should return correct mapping when device id is valid and mapping exists.";
777
778 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
779 << "Should return the location key code when device id is valid and there's no "
780 "mapping.";
781}
782
783TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
784 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
785 constexpr int32_t eventHubId = 1;
786 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
787 InputDeviceClass::JOYSTICK,
788 AINPUT_SOURCE_GAMEPAD, nullptr);
789 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
790
791 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
792 << "Should return unknown when the device id is valid but there is no keyboard mapper";
793}
794
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800796 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700797 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800798 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800799 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800800 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800801 AINPUT_SOURCE_KEYBOARD, nullptr);
802 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803
804 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
805 AINPUT_SOURCE_ANY, KEY_A))
806 << "Should return unknown when the device id is >= 0 but unknown.";
807
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800808 ASSERT_EQ(AKEY_STATE_UNKNOWN,
809 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
810 << "Should return unknown when the device id is valid but the sources are not "
811 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800812
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800813 ASSERT_EQ(AKEY_STATE_DOWN,
814 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
815 KEY_A))
816 << "Should return value provided by mapper when device id is valid and the device "
817 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818
819 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
820 AINPUT_SOURCE_TRACKBALL, KEY_A))
821 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
822
823 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
824 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
825 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
826}
827
828TEST_F(InputReaderTest, GetSwitchState_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.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836
837 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
838 AINPUT_SOURCE_ANY, SW_LID))
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->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
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->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
848 SW_LID))
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->getSwitchState(-1,
853 AINPUT_SOURCE_TRACKBALL, SW_LID))
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->getSwitchState(-1,
857 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
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, MarkSupportedKeyCodes_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);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100868
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800869 mapper.addSupportedKeyCode(AKEYCODE_A);
870 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800871
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700872 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873 uint8_t flags[4] = { 0, 0, 0, 1 };
874
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700875 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876 << "Should return false when device id is >= 0 but unknown.";
877 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
878
879 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700880 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800881 << "Should return false when device id is valid but the sources are not supported by "
882 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800883 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
884
885 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700886 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800887 keyCodes, flags))
888 << "Should return value provided by mapper when device id is valid and the device "
889 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800890 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
891
892 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700893 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
894 << "Should return false when the device id is < 0 but the sources are not supported by "
895 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
897
898 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700899 ASSERT_TRUE(
900 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
901 << "Should return value provided by mapper when device id is < 0 and one of the "
902 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800903 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
904}
905
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000906TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800907 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700908 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909
910 NotifyConfigurationChangedArgs args;
911
912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
913 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
914}
915
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000916TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800917 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700918 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000919 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800920 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000921 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800922 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800923 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800924 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800925
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000926 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000927 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800928 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
929
930 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800931 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000932 ASSERT_EQ(when, event.when);
933 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800934 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935 ASSERT_EQ(EV_KEY, event.type);
936 ASSERT_EQ(KEY_A, event.code);
937 ASSERT_EQ(1, event.value);
938}
939
Garfield Tan1c7bc862020-01-28 13:24:04 -0800940TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800941 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700942 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800943 constexpr int32_t eventHubId = 1;
944 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800945 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800946 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800947 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800948 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800949
950 NotifyDeviceResetArgs resetArgs;
951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800952 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800953
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800954 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000955 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800957 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800958 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800959
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800960 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000961 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800963 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800964 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800965
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800966 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000967 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800969 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800970 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800971}
972
Garfield Tan1c7bc862020-01-28 13:24:04 -0800973TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
974 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700975 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800976 constexpr int32_t eventHubId = 1;
977 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
978 // Must add at least one mapper or the device will be ignored!
979 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800980 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -0800981 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
982
983 NotifyDeviceResetArgs resetArgs;
984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
985 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
986}
987
Arthur Hungc23540e2018-11-29 20:42:11 +0800988TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800989 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700990 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800991 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +0800992 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800993 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
994 FakeInputMapper& mapper =
995 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800996 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +0800997
998 const uint8_t hdmi1 = 1;
999
1000 // Associated touch screen with second display.
1001 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1002
1003 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001004 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001005 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001006 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001007 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001008 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001009 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001010 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001011 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001012
1013 // Add the device, and make sure all of the callbacks are triggered.
1014 // The device is added after the input port associations are processed since
1015 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001016 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001019 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001020
Arthur Hung2c9a3342019-07-23 14:18:59 +08001021 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001022 ASSERT_EQ(deviceId, device->getId());
1023 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1024 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001025
1026 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001027 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001028 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001029 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001030}
1031
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001032TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1033 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001034 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001035 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1036 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1037 // Must add at least one mapper or the device will be ignored!
1038 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1039 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1040 mReader->pushNextDevice(device);
1041 mReader->pushNextDevice(device);
1042 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1043 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1044
1045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1046
1047 NotifyDeviceResetArgs resetArgs;
1048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1049 ASSERT_EQ(deviceId, resetArgs.deviceId);
1050 ASSERT_TRUE(device->isEnabled());
1051 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1052 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1053
1054 disableDevice(deviceId);
1055 mReader->loopOnce();
1056
1057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1058 ASSERT_EQ(deviceId, resetArgs.deviceId);
1059 ASSERT_FALSE(device->isEnabled());
1060 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1061 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1062
1063 enableDevice(deviceId);
1064 mReader->loopOnce();
1065
1066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1067 ASSERT_EQ(deviceId, resetArgs.deviceId);
1068 ASSERT_TRUE(device->isEnabled());
1069 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1070 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1071}
1072
1073TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1074 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001075 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001076 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1077 // Add two subdevices to device
1078 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1079 FakeInputMapper& mapperDevice1 =
1080 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1081 FakeInputMapper& mapperDevice2 =
1082 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1083 mReader->pushNextDevice(device);
1084 mReader->pushNextDevice(device);
1085 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1086 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1087
1088 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1089 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1090
1091 ASSERT_EQ(AKEY_STATE_DOWN,
1092 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1093 ASSERT_EQ(AKEY_STATE_DOWN,
1094 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1095 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1096 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1097}
1098
Prabir Pradhan7e186182020-11-10 13:56:45 -08001099TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1100 NotifyPointerCaptureChangedArgs args;
1101
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001102 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001103 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1104 mReader->loopOnce();
1105 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001106 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1107 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001108
1109 mFakePolicy->setPointerCapture(false);
1110 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1111 mReader->loopOnce();
1112 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001113 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001114
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001115 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001116 // does not change.
1117 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1118 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001119 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001120}
1121
Chris Ye87143712020-11-10 05:05:58 +00001122class FakeVibratorInputMapper : public FakeInputMapper {
1123public:
1124 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1125 : FakeInputMapper(deviceContext, sources) {}
1126
1127 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1128};
1129
1130TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1131 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001132 ftl::Flags<InputDeviceClass> deviceClass =
1133 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001134 constexpr int32_t eventHubId = 1;
1135 const char* DEVICE_LOCATION = "BLUETOOTH";
1136 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1137 FakeVibratorInputMapper& mapper =
1138 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1139 mReader->pushNextDevice(device);
1140
1141 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1142 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1143
1144 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1145 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1146}
1147
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001148// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001149
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001150class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001151public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001152 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001153
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001154 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001155
Andy Chenf9f1a022022-08-29 20:07:10 -04001156 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1157
Chris Yee2b1e5c2021-03-10 22:45:12 -08001158 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1159
1160 void dump(std::string& dump) override {}
1161
1162 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1163 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001164 }
1165
Chris Yee2b1e5c2021-03-10 22:45:12 -08001166 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1167 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001168 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001169
1170 bool setLightColor(int32_t lightId, int32_t color) override {
1171 getDeviceContext().setLightBrightness(lightId, color >> 24);
1172 return true;
1173 }
1174
1175 std::optional<int32_t> getLightColor(int32_t lightId) override {
1176 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1177 if (!result.has_value()) {
1178 return std::nullopt;
1179 }
1180 return result.value() << 24;
1181 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001182
1183 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1184
1185 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1186
1187private:
1188 InputDeviceContext& mDeviceContext;
1189 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1190 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001191 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001192};
1193
Chris Yee2b1e5c2021-03-10 22:45:12 -08001194TEST_F(InputReaderTest, BatteryGetCapacity) {
1195 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001196 ftl::Flags<InputDeviceClass> deviceClass =
1197 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001198 constexpr int32_t eventHubId = 1;
1199 const char* DEVICE_LOCATION = "BLUETOOTH";
1200 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001201 FakePeripheralController& controller =
1202 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001203 mReader->pushNextDevice(device);
1204
1205 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1206
Harry Cuttsa5b71292022-11-28 12:56:17 +00001207 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1208 FakeEventHub::BATTERY_CAPACITY);
1209 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001210}
1211
1212TEST_F(InputReaderTest, BatteryGetStatus) {
1213 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001214 ftl::Flags<InputDeviceClass> deviceClass =
1215 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001216 constexpr int32_t eventHubId = 1;
1217 const char* DEVICE_LOCATION = "BLUETOOTH";
1218 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001219 FakePeripheralController& controller =
1220 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001221 mReader->pushNextDevice(device);
1222
1223 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1224
Harry Cuttsa5b71292022-11-28 12:56:17 +00001225 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1226 FakeEventHub::BATTERY_STATUS);
1227 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001228}
1229
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001230TEST_F(InputReaderTest, BatteryGetDevicePath) {
1231 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1232 ftl::Flags<InputDeviceClass> deviceClass =
1233 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1234 constexpr int32_t eventHubId = 1;
1235 const char* DEVICE_LOCATION = "BLUETOOTH";
1236 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1237 device->addController<FakePeripheralController>(eventHubId);
1238 mReader->pushNextDevice(device);
1239
1240 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1241
Harry Cuttsa5b71292022-11-28 12:56:17 +00001242 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001243}
1244
Chris Ye3fdbfef2021-01-06 18:45:18 -08001245TEST_F(InputReaderTest, LightGetColor) {
1246 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001247 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001248 constexpr int32_t eventHubId = 1;
1249 const char* DEVICE_LOCATION = "BLUETOOTH";
1250 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001251 FakePeripheralController& controller =
1252 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001253 mReader->pushNextDevice(device);
1254 RawLightInfo info = {.id = 1,
1255 .name = "Mono",
1256 .maxBrightness = 255,
1257 .flags = InputLightClass::BRIGHTNESS,
1258 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001259 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1260 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001261
1262 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001263
Harry Cutts33476232023-01-30 19:57:29 +00001264 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1265 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1266 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1267 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001268}
1269
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001270// --- InputReaderIntegrationTest ---
1271
1272// These tests create and interact with the InputReader only through its interface.
1273// The InputReader is started during SetUp(), which starts its processing in its own
1274// thread. The tests use linux uinput to emulate input devices.
1275// NOTE: Interacting with the physical device while these tests are running may cause
1276// the tests to fail.
1277class InputReaderIntegrationTest : public testing::Test {
1278protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001279 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001280 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001281 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001282
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001283 std::shared_ptr<FakePointerController> mFakePointerController;
1284
Chris Yea52ade12020-08-27 16:49:20 -07001285 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001286#if !defined(__ANDROID__)
1287 GTEST_SKIP();
1288#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001289 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001290 mFakePointerController = std::make_shared<FakePointerController>();
1291 mFakePolicy->setPointerController(mFakePointerController);
Harry Cutts33476232023-01-30 19:57:29 +00001292 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1293 /*eventDidNotHappenTimeout=*/30ms);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001294
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001295 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1296 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001297 ASSERT_EQ(mReader->start(), OK);
1298
1299 // Since this test is run on a real device, all the input devices connected
1300 // to the test device will show up in mReader. We wait for those input devices to
1301 // show up before beginning the tests.
1302 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1303 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1304 }
1305
Chris Yea52ade12020-08-27 16:49:20 -07001306 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001307#if !defined(__ANDROID__)
1308 return;
1309#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001310 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001311 mReader.reset();
1312 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001313 mFakePolicy.clear();
1314 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001315
1316 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1317 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1318 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1319 [&name](const InputDeviceInfo& info) {
1320 return info.getIdentifier().name == name;
1321 });
1322 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1323 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001324};
1325
1326TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1327 // An invalid input device that is only used for this test.
1328 class InvalidUinputDevice : public UinputDevice {
1329 public:
Harry Cutts33476232023-01-30 19:57:29 +00001330 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001331
1332 private:
1333 void configureDevice(int fd, uinput_user_dev* device) override {}
1334 };
1335
1336 const size_t numDevices = mFakePolicy->getInputDevices().size();
1337
1338 // UinputDevice does not set any event or key bits, so InputReader should not
1339 // consider it as a valid device.
1340 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1341 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1342 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1343 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1344
1345 invalidDevice.reset();
1346 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1347 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1348 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1349}
1350
1351TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1352 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1353
1354 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1355 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1356 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1357 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1358
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001359 const auto device = findDeviceByName(keyboard->getName());
1360 ASSERT_TRUE(device.has_value());
1361 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1362 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1363 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001364
1365 keyboard.reset();
1366 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1367 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1368 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1369}
1370
1371TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1372 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1373 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1374
1375 NotifyConfigurationChangedArgs configChangedArgs;
1376 ASSERT_NO_FATAL_FAILURE(
1377 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001378 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001379 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1380
1381 NotifyKeyArgs keyArgs;
1382 keyboard->pressAndReleaseHomeKey();
1383 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1384 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001385 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001386 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001387 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001388 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001389 prevTimestamp = keyArgs.eventTime;
1390
1391 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1392 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001393 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001394 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001395 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001396}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001397
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001398TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1399 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1400 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1401
1402 const auto device = findDeviceByName(stylus->getName());
1403 ASSERT_TRUE(device.has_value());
1404
Prabir Pradhana3621852022-10-14 18:57:23 +00001405 // An external stylus with buttons should also be recognized as a keyboard.
1406 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001407 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1408 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1409
1410 const auto DOWN =
1411 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1412 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1413
1414 stylus->pressAndReleaseKey(BTN_STYLUS);
1415 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1416 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1417 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1418 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1419
1420 stylus->pressAndReleaseKey(BTN_STYLUS2);
1421 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1422 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1423 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1424 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1425
1426 stylus->pressAndReleaseKey(BTN_STYLUS3);
1427 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1428 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1430 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1431}
1432
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001433/**
1434 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1435 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1436 * are passed to the listener.
1437 */
1438static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1439TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1440 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1441 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1442 NotifyKeyArgs keyArgs;
1443
1444 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1445 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1446 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1447 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1448
1449 controller->pressAndReleaseKey(BTN_GEAR_UP);
1450 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1451 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1452 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1453}
1454
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001455// --- TouchIntegrationTest ---
1456
Arthur Hungaab25622020-01-16 11:22:11 +08001457class TouchIntegrationTest : public InputReaderIntegrationTest {
1458protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001459 const std::string UNIQUE_ID = "local:0";
1460
Chris Yea52ade12020-08-27 16:49:20 -07001461 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001462#if !defined(__ANDROID__)
1463 GTEST_SKIP();
1464#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001465 InputReaderIntegrationTest::SetUp();
1466 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001467 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1468 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001469
1470 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1471 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1472 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001473 const auto info = findDeviceByName(mDevice->getName());
1474 ASSERT_TRUE(info);
1475 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001476 }
1477
1478 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001479 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001480 std::optional<uint8_t> physicalPort,
1481 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001482 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001483 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08001484 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1485 }
1486
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001487 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1488 NotifyMotionArgs args;
1489 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1490 EXPECT_EQ(action, args.action);
1491 ASSERT_EQ(points.size(), args.pointerCount);
1492 for (size_t i = 0; i < args.pointerCount; i++) {
1493 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1494 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1495 }
1496 }
1497
Arthur Hungaab25622020-01-16 11:22:11 +08001498 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001499 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001500};
1501
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001502TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1503 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1504 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1505 // presses).
1506 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1507 mDeviceInfo.getSources());
1508}
1509
Arthur Hungaab25622020-01-16 11:22:11 +08001510TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1511 NotifyMotionArgs args;
1512 const Point centerPoint = mDevice->getCenterPoint();
1513
1514 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001515 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001516 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001517 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001518 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1519 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1520
1521 // ACTION_MOVE
1522 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001523 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001524 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1526
1527 // ACTION_UP
1528 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001529 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001530 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1531 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1532}
1533
1534TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1535 NotifyMotionArgs args;
1536 const Point centerPoint = mDevice->getCenterPoint();
1537
1538 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001539 mDevice->sendSlot(FIRST_SLOT);
1540 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001541 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001542 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001543 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1544 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1545
1546 // ACTION_POINTER_DOWN (Second slot)
1547 const Point secondPoint = centerPoint + Point(100, 100);
1548 mDevice->sendSlot(SECOND_SLOT);
1549 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001550 mDevice->sendDown(secondPoint);
1551 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001552 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001553 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001554
1555 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001556 mDevice->sendMove(secondPoint + Point(1, 1));
1557 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001558 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1560
1561 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001562 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001563 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001564 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001565 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001566
1567 // ACTION_UP
1568 mDevice->sendSlot(FIRST_SLOT);
1569 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001570 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001571 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1572 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1573}
1574
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001575/**
1576 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1577 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1578 * data?
1579 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1580 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1581 * for Pointer 0 only is generated after.
1582 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1583 * events, we will not miss any information.
1584 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1585 * event generated afterwards that contains the newest movement of pointer 0.
1586 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1587 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1588 * losing information about non-palm pointers.
1589 */
1590TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1591 NotifyMotionArgs args;
1592 const Point centerPoint = mDevice->getCenterPoint();
1593
1594 // ACTION_DOWN
1595 mDevice->sendSlot(FIRST_SLOT);
1596 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1597 mDevice->sendDown(centerPoint);
1598 mDevice->sendSync();
1599 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1600
1601 // ACTION_POINTER_DOWN (Second slot)
1602 const Point secondPoint = centerPoint + Point(100, 100);
1603 mDevice->sendSlot(SECOND_SLOT);
1604 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1605 mDevice->sendDown(secondPoint);
1606 mDevice->sendSync();
1607 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1608
1609 // ACTION_MOVE (First slot)
1610 mDevice->sendSlot(FIRST_SLOT);
1611 mDevice->sendMove(centerPoint + Point(5, 5));
1612 // ACTION_POINTER_UP (Second slot)
1613 mDevice->sendSlot(SECOND_SLOT);
1614 mDevice->sendPointerUp();
1615 // Send a single sync for the above 2 pointer updates
1616 mDevice->sendSync();
1617
1618 // First, we should get POINTER_UP for the second pointer
1619 assertReceivedMotion(ACTION_POINTER_1_UP,
1620 {/*first pointer */ centerPoint + Point(5, 5),
1621 /*second pointer*/ secondPoint});
1622
1623 // Next, the MOVE event for the first pointer
1624 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1625}
1626
1627/**
1628 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1629 * move, and then it will go up, all in the same frame.
1630 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1631 * gets sent to the listener.
1632 */
1633TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1634 NotifyMotionArgs args;
1635 const Point centerPoint = mDevice->getCenterPoint();
1636
1637 // ACTION_DOWN
1638 mDevice->sendSlot(FIRST_SLOT);
1639 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1640 mDevice->sendDown(centerPoint);
1641 mDevice->sendSync();
1642 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1643
1644 // ACTION_POINTER_DOWN (Second slot)
1645 const Point secondPoint = centerPoint + Point(100, 100);
1646 mDevice->sendSlot(SECOND_SLOT);
1647 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1648 mDevice->sendDown(secondPoint);
1649 mDevice->sendSync();
1650 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1651
1652 // ACTION_MOVE (First slot)
1653 mDevice->sendSlot(FIRST_SLOT);
1654 mDevice->sendMove(centerPoint + Point(5, 5));
1655 // ACTION_POINTER_UP (Second slot)
1656 mDevice->sendSlot(SECOND_SLOT);
1657 mDevice->sendMove(secondPoint + Point(6, 6));
1658 mDevice->sendPointerUp();
1659 // Send a single sync for the above 2 pointer updates
1660 mDevice->sendSync();
1661
1662 // First, we should get POINTER_UP for the second pointer
1663 // The movement of the second pointer during the liftoff frame is ignored.
1664 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1665 assertReceivedMotion(ACTION_POINTER_1_UP,
1666 {/*first pointer */ centerPoint + Point(5, 5),
1667 /*second pointer*/ secondPoint});
1668
1669 // Next, the MOVE event for the first pointer
1670 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1671}
1672
Arthur Hungaab25622020-01-16 11:22:11 +08001673TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1674 NotifyMotionArgs args;
1675 const Point centerPoint = mDevice->getCenterPoint();
1676
1677 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001678 mDevice->sendSlot(FIRST_SLOT);
1679 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001680 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001681 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001682 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1683 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1684
arthurhungcc7f9802020-04-30 17:55:40 +08001685 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001686 const Point secondPoint = centerPoint + Point(100, 100);
1687 mDevice->sendSlot(SECOND_SLOT);
1688 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1689 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001690 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001691 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001692 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001693
arthurhungcc7f9802020-04-30 17:55:40 +08001694 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001695 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001696 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001697 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1699
arthurhungcc7f9802020-04-30 17:55:40 +08001700 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1701 // a palm event.
1702 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001703 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001704 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001705 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001706 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001707 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001708
arthurhungcc7f9802020-04-30 17:55:40 +08001709 // Send up to second slot, expect first slot send moving.
1710 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001711 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001712 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001714
arthurhungcc7f9802020-04-30 17:55:40 +08001715 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001716 mDevice->sendSlot(FIRST_SLOT);
1717 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001718 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001719
arthurhungcc7f9802020-04-30 17:55:40 +08001720 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1721 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001722}
1723
Prabir Pradhanda20b172022-09-26 17:01:18 +00001724TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1725 const Point centerPoint = mDevice->getCenterPoint();
1726
1727 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1728 mDevice->sendSlot(FIRST_SLOT);
1729 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1730 mDevice->sendToolType(MT_TOOL_PEN);
1731 mDevice->sendDown(centerPoint);
1732 mDevice->sendSync();
1733 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1734 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1735 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1736
1737 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1738
1739 // Release the stylus touch.
1740 mDevice->sendUp();
1741 mDevice->sendSync();
1742 ASSERT_NO_FATAL_FAILURE(
1743 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1744
1745 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1746
1747 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1748 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1749 mDevice->sendToolType(MT_TOOL_FINGER);
1750 mDevice->sendDown(centerPoint);
1751 mDevice->sendSync();
1752 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1753 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1754 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
1755
1756 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1757
1758 mDevice->sendUp();
1759 mDevice->sendSync();
1760 ASSERT_NO_FATAL_FAILURE(
1761 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1762
1763 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1764 // The policy should be notified of the stylus presence.
1765 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1766 mDevice->sendToolType(MT_TOOL_PEN);
1767 mDevice->sendMove(centerPoint);
1768 mDevice->sendSync();
1769 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1770 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1771 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1772
1773 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1774}
1775
Prabir Pradhan124ea442022-10-28 20:27:44 +00001776// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001777
Prabir Pradhan124ea442022-10-28 20:27:44 +00001778// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1779// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1780// stylus.
1781template <typename UinputStylusDevice>
1782class StylusButtonIntegrationTest : public TouchIntegrationTest {
1783protected:
1784 void SetUp() override {
1785#if !defined(__ANDROID__)
1786 GTEST_SKIP();
1787#endif
1788 TouchIntegrationTest::SetUp();
1789 mTouchscreen = mDevice.get();
1790 mTouchscreenInfo = mDeviceInfo;
1791
1792 setUpStylusDevice();
1793 }
1794
1795 UinputStylusDevice* mStylus{nullptr};
1796 InputDeviceInfo mStylusInfo{};
1797
1798 UinputTouchScreen* mTouchscreen{nullptr};
1799 InputDeviceInfo mTouchscreenInfo{};
1800
1801private:
1802 // When we are attempting to test stylus button events that are sent from the touchscreen,
1803 // use the same Uinput device for the touchscreen and the stylus.
1804 template <typename T = UinputStylusDevice>
1805 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1806 mStylus = mDevice.get();
1807 mStylusInfo = mDeviceInfo;
1808 }
1809
1810 // When we are attempting to stylus buttons from an external stylus being merged with touches
1811 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1812 template <typename T = UinputStylusDevice>
1813 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1814 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1815 mStylus = mStylusDeviceLifecycleTracker.get();
1816 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1817 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1818 const auto info = findDeviceByName(mStylus->getName());
1819 ASSERT_TRUE(info);
1820 mStylusInfo = *info;
1821 }
1822
1823 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1824
1825 // Hide the base class's device to expose it with a different name for readability.
1826 using TouchIntegrationTest::mDevice;
1827 using TouchIntegrationTest::mDeviceInfo;
1828};
1829
1830using StylusButtonIntegrationTestTypes =
1831 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1832TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1833
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001834TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001835 const auto stylusId = TestFixture::mStylusInfo.getId();
1836
1837 TestFixture::mStylus->pressKey(BTN_STYLUS);
1838 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1839 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1840 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1841
1842 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1843 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001844 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001845 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001846}
1847
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001848TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001849 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1850 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1851 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001852
1853 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001854 TestFixture::mStylus->pressKey(BTN_STYLUS);
1855 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001856 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001857 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001858
1859 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001860 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1861 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1862 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1863 TestFixture::mTouchscreen->sendDown(centerPoint);
1864 TestFixture::mTouchscreen->sendSync();
1865 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001866 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1867 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001868 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1869 WithDeviceId(touchscreenId))));
1870 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001871 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1872 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001873 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1874 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001875
Prabir Pradhan124ea442022-10-28 20:27:44 +00001876 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1877 TestFixture::mTouchscreen->sendSync();
1878 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001879 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001880 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1881 WithDeviceId(touchscreenId))));
1882 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001883 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001884 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1885 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001886
1887 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001888 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1889 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001890 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001891 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001892}
1893
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001894TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001895 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1896 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1897 const auto stylusId = TestFixture::mStylusInfo.getId();
1898 auto toolTypeDevice =
1899 AllOf(WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithDeviceId(touchscreenId));
1900
1901 // Press the stylus button.
1902 TestFixture::mStylus->pressKey(BTN_STYLUS);
1903 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1904 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1905 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1906
1907 // Start hovering with the stylus.
1908 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1909 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1910 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1911 TestFixture::mTouchscreen->sendMove(centerPoint);
1912 TestFixture::mTouchscreen->sendSync();
1913 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1914 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1915 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1916 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1917 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1918 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1919 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1920 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1921 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1922
1923 // Touch down with the stylus.
1924 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1925 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1926 TestFixture::mTouchscreen->sendDown(centerPoint);
1927 TestFixture::mTouchscreen->sendSync();
1928 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1929 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1930 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1931
1932 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1933 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1934 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1935
1936 // Stop touching with the stylus, and start hovering.
1937 TestFixture::mTouchscreen->sendUp();
1938 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1939 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1940 TestFixture::mTouchscreen->sendMove(centerPoint);
1941 TestFixture::mTouchscreen->sendSync();
1942 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1943 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
1944 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1945 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1946 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1947 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1948 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1949 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1950 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1951
1952 // Stop hovering.
1953 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1954 TestFixture::mTouchscreen->sendSync();
1955 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1956 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1957 WithButtonState(0))));
1958 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
1959 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1960 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1961 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1962
1963 // Release the stylus button.
1964 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1965 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1966 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
1967 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1968}
1969
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001970TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001971 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1972 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1973 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001974
1975 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001976 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1977 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1978 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1979 TestFixture::mTouchscreen->sendDown(centerPoint);
1980 TestFixture::mTouchscreen->sendSync();
1981 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001982 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001983 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1984 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001985
1986 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001987 TestFixture::mStylus->pressKey(BTN_STYLUS);
1988 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001989 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001990 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1991 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001992 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1993 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001994 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1995 WithDeviceId(touchscreenId))));
1996 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001997 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1998 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001999 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2000 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002001
Prabir Pradhan124ea442022-10-28 20:27:44 +00002002 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2003 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002004 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002005 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2006 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002007 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002008 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2009 WithDeviceId(touchscreenId))));
2010 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002011 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002012 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2013 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002014
2015 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002016 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2017 TestFixture::mTouchscreen->sendSync();
2018 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002019 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002020 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2021 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002022}
2023
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002024TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002025 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2026 TestFixture::mReader->requestRefreshConfiguration(
2027 InputReaderConfiguration::CHANGE_STYLUS_BUTTON_REPORTING);
2028
2029 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2030 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2031 const auto stylusId = TestFixture::mStylusInfo.getId();
2032
2033 // Start a stylus gesture. By the time this event is processed, the configuration change that
2034 // was requested is guaranteed to be completed.
2035 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2036 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2037 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2038 TestFixture::mTouchscreen->sendDown(centerPoint);
2039 TestFixture::mTouchscreen->sendSync();
2040 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2041 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2042 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2043 WithDeviceId(touchscreenId))));
2044
2045 // Press and release a stylus button. Each change only generates a MOVE motion event.
2046 // Key events are unaffected.
2047 TestFixture::mStylus->pressKey(BTN_STYLUS);
2048 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2049 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2050 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2051 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2052 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2053 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2054 WithDeviceId(touchscreenId))));
2055
2056 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2057 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2058 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2059 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2060 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2061 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2062 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2063 WithDeviceId(touchscreenId))));
2064
2065 // Finish the stylus gesture.
2066 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2067 TestFixture::mTouchscreen->sendSync();
2068 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2069 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2070 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2071 WithDeviceId(touchscreenId))));
2072}
2073
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002074// --- ExternalStylusIntegrationTest ---
2075
2076// Verify the behavior of an external stylus. An external stylus can report pressure or button
2077// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2078// ongoing stylus gesture that is being emitted by the touchscreen.
2079using ExternalStylusIntegrationTest = TouchIntegrationTest;
2080
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002081TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002082 const Point centerPoint = mDevice->getCenterPoint();
2083
2084 // Create an external stylus capable of reporting pressure data that
2085 // should be fused with a touch pointer.
2086 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2087 createUinputDevice<UinputExternalStylusWithPressure>();
2088 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2089 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2090 const auto stylusInfo = findDeviceByName(stylus->getName());
2091 ASSERT_TRUE(stylusInfo);
2092
2093 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2094
2095 const auto touchscreenId = mDeviceInfo.getId();
2096
2097 // Set a pressure value on the stylus. It doesn't generate any events.
2098 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2099 stylus->setPressure(100);
2100 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2101
2102 // Start a finger gesture, and ensure it shows up as stylus gesture
2103 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002104 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002105 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002106 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002107 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002108 mDevice->sendSync();
2109 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2110 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002111 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2112 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002113
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002114 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2115 // event with the updated pressure.
2116 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002117 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2118 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002119 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2120 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002121
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002122 // The external stylus did not generate any events.
2123 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2124 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2125}
2126
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002127TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002128 const Point centerPoint = mDevice->getCenterPoint();
2129
2130 // Create an external stylus capable of reporting pressure data that
2131 // should be fused with a touch pointer.
2132 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2133 createUinputDevice<UinputExternalStylusWithPressure>();
2134 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2135 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2136 const auto stylusInfo = findDeviceByName(stylus->getName());
2137 ASSERT_TRUE(stylusInfo);
2138
2139 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2140
2141 const auto touchscreenId = mDeviceInfo.getId();
2142
2143 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2144 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002145 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2146 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002147 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002148 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002149
2150 // Start a finger gesture. The touch device will withhold generating any touches for
2151 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2152 mDevice->sendSlot(FIRST_SLOT);
2153 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2154 mDevice->sendToolType(MT_TOOL_FINGER);
2155 mDevice->sendDown(centerPoint);
2156 auto waitUntil = std::chrono::system_clock::now() +
2157 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002158 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002159 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002160
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002161 // Since the external stylus did not report a pressure value within the timeout,
2162 // it shows up as a finger pointer.
2163 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2164 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2165 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDeviceId(touchscreenId),
2166 WithPressure(1.f))));
2167
2168 // Change the pressure on the external stylus. Since the pressure was not present at the start
2169 // of the gesture, it is ignored for now.
2170 stylus->setPressure(200);
2171 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2172
2173 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002174 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2175 mDevice->sendSync();
2176 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2177 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002178 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2179
2180 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2181 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2182 mDevice->sendToolType(MT_TOOL_FINGER);
2183 mDevice->sendDown(centerPoint);
2184 mDevice->sendSync();
2185 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2186 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2187 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2188 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2189
2190 // The external stylus did not generate any events.
2191 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2192 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002193}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002194
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002195TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002196 const Point centerPoint = mDevice->getCenterPoint();
2197
2198 // Create an external stylus device that does not support pressure. It should not affect any
2199 // touch pointers.
2200 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2201 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2202 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2203 const auto stylusInfo = findDeviceByName(stylus->getName());
2204 ASSERT_TRUE(stylusInfo);
2205
2206 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2207
2208 const auto touchscreenId = mDeviceInfo.getId();
2209
2210 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2211 // pressure data from the external stylus.
2212 mDevice->sendSlot(FIRST_SLOT);
2213 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2214 mDevice->sendToolType(MT_TOOL_FINGER);
2215 mDevice->sendDown(centerPoint);
2216 auto waitUntil = std::chrono::system_clock::now() +
2217 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2218 mDevice->sendSync();
2219 ASSERT_NO_FATAL_FAILURE(
2220 mTestListener
2221 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2222 WithToolType(
2223 AMOTION_EVENT_TOOL_TYPE_FINGER),
2224 WithButtonState(0),
2225 WithDeviceId(touchscreenId),
2226 WithPressure(1.f)),
2227 waitUntil));
2228
2229 // The external stylus did not generate any events.
2230 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2231 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2232}
2233
Michael Wrightd02c5b62014-02-10 15:10:22 -08002234// --- InputDeviceTest ---
2235class InputDeviceTest : public testing::Test {
2236protected:
2237 static const char* DEVICE_NAME;
2238 static const char* DEVICE_LOCATION;
2239 static const int32_t DEVICE_ID;
2240 static const int32_t DEVICE_GENERATION;
2241 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002242 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002243 static const int32_t EVENTHUB_ID;
2244 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2245
2246 std::shared_ptr<FakeEventHub> mFakeEventHub;
2247 sp<FakeInputReaderPolicy> mFakePolicy;
2248 std::unique_ptr<TestInputListener> mFakeListener;
2249 std::unique_ptr<InstrumentedInputReader> mReader;
2250 std::shared_ptr<InputDevice> mDevice;
2251
2252 void SetUp() override {
2253 mFakeEventHub = std::make_unique<FakeEventHub>();
2254 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2255 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002256 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002257 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002258 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002259 identifier.name = DEVICE_NAME;
2260 identifier.location = DEVICE_LOCATION;
2261 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2262 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2263 identifier);
2264 mReader->pushNextDevice(mDevice);
2265 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002266 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002267 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002268
2269 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002270 mFakeListener.reset();
2271 mFakePolicy.clear();
2272 }
2273};
2274
2275const char* InputDeviceTest::DEVICE_NAME = "device";
2276const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2277const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2278const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002279const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002280const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2281 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002282const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002283const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2284
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002285TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002286 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002287 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2288 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002289}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002290
Michael Wrightd02c5b62014-02-10 15:10:22 -08002291TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2292 ASSERT_EQ(mDevice->isEnabled(), false);
2293}
2294
2295TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2296 // Configuration.
2297 InputReaderConfiguration config;
2298 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2299
2300 // Reset.
2301 unused += mDevice->reset(ARBITRARY_TIME);
2302
2303 NotifyDeviceResetArgs resetArgs;
2304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2305 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2306 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2307
2308 // Metadata.
2309 ASSERT_TRUE(mDevice->isIgnored());
2310 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2311
2312 InputDeviceInfo info = mDevice->getDeviceInfo();
2313 ASSERT_EQ(DEVICE_ID, info.getId());
2314 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2315 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2316 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2317
2318 // State queries.
2319 ASSERT_EQ(0, mDevice->getMetaState());
2320
2321 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2322 << "Ignored device should return unknown key code state.";
2323 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2324 << "Ignored device should return unknown scan code state.";
2325 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2326 << "Ignored device should return unknown switch state.";
2327
2328 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2329 uint8_t flags[2] = { 0, 1 };
2330 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2331 << "Ignored device should never mark any key codes.";
2332 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2333 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2334}
2335
2336TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2337 // Configuration.
2338 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002339
2340 FakeInputMapper& mapper1 =
2341 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2342 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002343 mapper1.setMetaState(AMETA_ALT_ON);
2344 mapper1.addSupportedKeyCode(AKEYCODE_A);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002345 mapper1.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002346 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2347 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2348 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2349 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2350 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2351
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002352 FakeInputMapper& mapper2 =
Michael Wrightd02c5b62014-02-10 15:10:22 -08002353 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
2354 mapper2.setMetaState(AMETA_SHIFT_ON);
2355
2356 InputReaderConfiguration config;
2357 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2358
Harry Cuttsf13161a2023-03-08 14:15:49 +00002359 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2360 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002361 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002362 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002363
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002364 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2365 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002366
2367 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002368 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002369 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2370 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002371
2372 NotifyDeviceResetArgs resetArgs;
2373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2374 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2375 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2376
2377 // Metadata.
2378 ASSERT_FALSE(mDevice->isIgnored());
2379 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2380
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002381 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002382 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002383 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002384 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2385 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2386
2387 // State queries.
2388 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2389 << "Should query mappers and combine meta states.";
2390
2391 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2392 << "Should return unknown key code state when source not supported.";
2393 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2394 << "Should return unknown scan code state when source not supported.";
2395 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2396 << "Should return unknown switch state when source not supported.";
2397
2398 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2399 << "Should query mapper when source is supported.";
2400 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2401 << "Should query mapper when source is supported.";
2402 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2403 << "Should query mapper when source is supported.";
2404
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002405 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002406 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002407 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002408 << "Should do nothing when source is unsupported.";
2409 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2410 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2411 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2412 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2413
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002414 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002415 << "Should query mapper when source is supported.";
2416 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2417 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2418 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2419 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2420
2421 // Event handling.
2422 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002423 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002424 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002426 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2427 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002428}
2429
Arthur Hung2c9a3342019-07-23 14:18:59 +08002430// A single input device is associated with a specific display. Check that:
2431// 1. Device is disabled if the viewport corresponding to the associated display is not found
2432// 2. Device is disabled when setEnabled API is called
2433TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002434 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002435
2436 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002437 std::list<NotifyArgs> unused =
2438 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002439
2440 // Device should be enabled by default.
2441 ASSERT_TRUE(mDevice->isEnabled());
2442
2443 // Prepare associated info.
2444 constexpr uint8_t hdmi = 1;
2445 const std::string UNIQUE_ID = "local:1";
2446
2447 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002448 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2449 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002450 // Device should be disabled because it is associated with a specific display via
2451 // input port <-> display port association, but the corresponding display is not found
2452 ASSERT_FALSE(mDevice->isEnabled());
2453
2454 // Prepare displays.
2455 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002456 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002457 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002458 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2459 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002460 ASSERT_TRUE(mDevice->isEnabled());
2461
2462 // Device should be disabled after set disable.
2463 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002464 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2465 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002466 ASSERT_FALSE(mDevice->isEnabled());
2467
2468 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002469 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2470 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002471 ASSERT_FALSE(mDevice->isEnabled());
2472}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473
Christine Franks1ba71cc2021-04-07 14:37:42 -07002474TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2475 // Device should be enabled by default.
2476 mFakePolicy->clearViewports();
2477 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002478 std::list<NotifyArgs> unused =
2479 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002480 ASSERT_TRUE(mDevice->isEnabled());
2481
2482 // Device should be disabled because it is associated with a specific display, but the
2483 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002484 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002485 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2486 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002487 ASSERT_FALSE(mDevice->isEnabled());
2488
2489 // Device should be enabled when a display is found.
2490 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002491 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002492 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002493 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2494 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002495 ASSERT_TRUE(mDevice->isEnabled());
2496
2497 // Device should be disabled after set disable.
2498 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002499 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2500 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002501 ASSERT_FALSE(mDevice->isEnabled());
2502
2503 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002504 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2505 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002506 ASSERT_FALSE(mDevice->isEnabled());
2507}
2508
Christine Franks2a2293c2022-01-18 11:51:16 -08002509TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2510 mFakePolicy->clearViewports();
2511 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002512 std::list<NotifyArgs> unused =
2513 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08002514
Christine Franks2a2293c2022-01-18 11:51:16 -08002515 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2516 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002517 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002518 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002519 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2520 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002521 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2522}
2523
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002524/**
2525 * This test reproduces a crash caused by a dangling reference that remains after device is added
2526 * and removed. The reference is accessed in InputDevice::dump(..);
2527 */
2528TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2529 constexpr int32_t TEST_EVENTHUB_ID = 10;
2530 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2531
Harry Cutts33476232023-01-30 19:57:29 +00002532 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
2533 device.addEventHubDevice(TEST_EVENTHUB_ID, /*populateMappers=*/true);
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002534 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2535 std::string dumpStr, eventHubDevStr;
2536 device.dump(dumpStr, eventHubDevStr);
2537}
2538
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002539TEST_F(InputDeviceTest, GetBluetoothAddress) {
2540 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2541 ASSERT_TRUE(address);
2542 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2543}
2544
Michael Wrightd02c5b62014-02-10 15:10:22 -08002545// --- SwitchInputMapperTest ---
2546
2547class SwitchInputMapperTest : public InputMapperTest {
2548protected:
2549};
2550
2551TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002552 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002553
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002554 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002555}
2556
2557TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002558 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002559
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002560 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002561 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002562
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002563 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002564 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002565}
2566
2567TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002568 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002569 std::list<NotifyArgs> out;
2570 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2571 ASSERT_TRUE(out.empty());
2572 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2573 ASSERT_TRUE(out.empty());
2574 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2575 ASSERT_TRUE(out.empty());
2576 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002577
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002578 ASSERT_EQ(1u, out.size());
2579 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002580 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002581 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2582 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002583 args.switchMask);
2584 ASSERT_EQ(uint32_t(0), args.policyFlags);
2585}
2586
Chris Ye87143712020-11-10 05:05:58 +00002587// --- VibratorInputMapperTest ---
2588class VibratorInputMapperTest : public InputMapperTest {
2589protected:
2590 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2591};
2592
2593TEST_F(VibratorInputMapperTest, GetSources) {
2594 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2595
2596 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2597}
2598
2599TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2600 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2601
2602 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2603}
2604
2605TEST_F(VibratorInputMapperTest, Vibrate) {
2606 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002607 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002608 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2609
2610 VibrationElement pattern(2);
2611 VibrationSequence sequence(2);
2612 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002613 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2614 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002615 sequence.addElement(pattern);
2616 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002617 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2618 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002619 sequence.addElement(pattern);
2620
2621 std::vector<int64_t> timings = {0, 1};
2622 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2623
2624 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002625 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002626 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002627 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002628 // Verify vibrator state listener was notified.
2629 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002630 ASSERT_EQ(1u, out.size());
2631 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2632 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2633 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002634 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002635 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002636 ASSERT_FALSE(mapper.isVibrating());
2637 // Verify vibrator state listener was notified.
2638 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002639 ASSERT_EQ(1u, out.size());
2640 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2641 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2642 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002643}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644
Chris Yef59a2f42020-10-16 12:55:26 -07002645// --- SensorInputMapperTest ---
2646
2647class SensorInputMapperTest : public InputMapperTest {
2648protected:
2649 static const int32_t ACCEL_RAW_MIN;
2650 static const int32_t ACCEL_RAW_MAX;
2651 static const int32_t ACCEL_RAW_FUZZ;
2652 static const int32_t ACCEL_RAW_FLAT;
2653 static const int32_t ACCEL_RAW_RESOLUTION;
2654
2655 static const int32_t GYRO_RAW_MIN;
2656 static const int32_t GYRO_RAW_MAX;
2657 static const int32_t GYRO_RAW_FUZZ;
2658 static const int32_t GYRO_RAW_FLAT;
2659 static const int32_t GYRO_RAW_RESOLUTION;
2660
2661 static const float GRAVITY_MS2_UNIT;
2662 static const float DEGREE_RADIAN_UNIT;
2663
2664 void prepareAccelAxes();
2665 void prepareGyroAxes();
2666 void setAccelProperties();
2667 void setGyroProperties();
2668 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2669};
2670
2671const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2672const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2673const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2674const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2675const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2676
2677const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2678const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2679const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2680const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2681const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2682
2683const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2684const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2685
2686void SensorInputMapperTest::prepareAccelAxes() {
2687 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2688 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2689 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2690 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2691 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2692 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2693}
2694
2695void SensorInputMapperTest::prepareGyroAxes() {
2696 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2697 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2698 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2699 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2700 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2701 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2702}
2703
2704void SensorInputMapperTest::setAccelProperties() {
2705 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2706 /* sensorDataIndex */ 0);
2707 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2708 /* sensorDataIndex */ 1);
2709 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2710 /* sensorDataIndex */ 2);
2711 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2712 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2713 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2714 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2715 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2716}
2717
2718void SensorInputMapperTest::setGyroProperties() {
2719 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2720 /* sensorDataIndex */ 0);
2721 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2722 /* sensorDataIndex */ 1);
2723 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2724 /* sensorDataIndex */ 2);
2725 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2726 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2727 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2728 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2729 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2730}
2731
2732TEST_F(SensorInputMapperTest, GetSources) {
2733 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2734
2735 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2736}
2737
2738TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2739 setAccelProperties();
2740 prepareAccelAxes();
2741 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2742
2743 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2744 std::chrono::microseconds(10000),
2745 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002746 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2749 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2750 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2751 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002752
2753 NotifySensorArgs args;
2754 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2755 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2756 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2757
2758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2759 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2760 ASSERT_EQ(args.deviceId, DEVICE_ID);
2761 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2762 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2763 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2764 ASSERT_EQ(args.values, values);
2765 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2766}
2767
2768TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2769 setGyroProperties();
2770 prepareGyroAxes();
2771 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2772
2773 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2774 std::chrono::microseconds(10000),
2775 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002776 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002782
2783 NotifySensorArgs args;
2784 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2785 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2786 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2787
2788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2789 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2790 ASSERT_EQ(args.deviceId, DEVICE_ID);
2791 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2792 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2793 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2794 ASSERT_EQ(args.values, values);
2795 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2796}
2797
Michael Wrightd02c5b62014-02-10 15:10:22 -08002798// --- KeyboardInputMapperTest ---
2799
2800class KeyboardInputMapperTest : public InputMapperTest {
2801protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002802 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002803 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002804 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002805
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002806 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002807 int32_t originalKeyCode, int32_t rotatedKeyCode,
2808 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002809};
2810
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002811/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2812 * orientation.
2813 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002814void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002815 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2816 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002817}
2818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002819void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002820 int32_t originalScanCode, int32_t originalKeyCode,
2821 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002822 NotifyKeyArgs args;
2823
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2826 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2827 ASSERT_EQ(originalScanCode, args.scanCode);
2828 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002829 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2833 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2834 ASSERT_EQ(originalScanCode, args.scanCode);
2835 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002836 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002837}
2838
Michael Wrightd02c5b62014-02-10 15:10:22 -08002839TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002840 KeyboardInputMapper& mapper =
2841 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2842 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002843
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002844 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002845}
2846
2847TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2848 const int32_t USAGE_A = 0x070004;
2849 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002850 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2851 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002852 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2853 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2854 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002855
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002856 KeyboardInputMapper& mapper =
2857 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2858 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002859 // Initial metastate is AMETA_NONE.
2860 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861
2862 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002863 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002864 NotifyKeyArgs args;
2865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2866 ASSERT_EQ(DEVICE_ID, args.deviceId);
2867 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2868 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2869 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2870 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2871 ASSERT_EQ(KEY_HOME, args.scanCode);
2872 ASSERT_EQ(AMETA_NONE, args.metaState);
2873 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2874 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2875 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2876
2877 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002878 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2880 ASSERT_EQ(DEVICE_ID, args.deviceId);
2881 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2882 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2883 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2884 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2885 ASSERT_EQ(KEY_HOME, args.scanCode);
2886 ASSERT_EQ(AMETA_NONE, args.metaState);
2887 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2888 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2889 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2890
2891 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2895 ASSERT_EQ(DEVICE_ID, args.deviceId);
2896 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2897 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2898 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2899 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2900 ASSERT_EQ(0, args.scanCode);
2901 ASSERT_EQ(AMETA_NONE, args.metaState);
2902 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2903 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2904 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2905
2906 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2908 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2910 ASSERT_EQ(DEVICE_ID, args.deviceId);
2911 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2912 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2913 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2914 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2915 ASSERT_EQ(0, args.scanCode);
2916 ASSERT_EQ(AMETA_NONE, args.metaState);
2917 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2918 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2919 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2920
2921 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002922 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2923 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2925 ASSERT_EQ(DEVICE_ID, args.deviceId);
2926 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2927 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2928 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2929 ASSERT_EQ(0, args.keyCode);
2930 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2931 ASSERT_EQ(AMETA_NONE, args.metaState);
2932 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2933 ASSERT_EQ(0U, args.policyFlags);
2934 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2935
2936 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002937 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2938 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2940 ASSERT_EQ(DEVICE_ID, args.deviceId);
2941 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2942 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2943 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2944 ASSERT_EQ(0, args.keyCode);
2945 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2946 ASSERT_EQ(AMETA_NONE, args.metaState);
2947 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2948 ASSERT_EQ(0U, args.policyFlags);
2949 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2950}
2951
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00002952TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
2953 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
2954 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
2955 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
2956
2957 KeyboardInputMapper& mapper =
2958 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2959 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2960
2961 // Key down by scan code.
2962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
2963 NotifyKeyArgs args;
2964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2965 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2966
2967 // Key up by scan code.
2968 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
2969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2970 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2971}
2972
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002973/**
2974 * Ensure that the readTime is set to the time when the EV_KEY is received.
2975 */
2976TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
2977 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2978
2979 KeyboardInputMapper& mapper =
2980 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2981 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2982 NotifyKeyArgs args;
2983
2984 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00002985 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2987 ASSERT_EQ(12, args.readTime);
2988
2989 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00002990 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2992 ASSERT_EQ(15, args.readTime);
2993}
2994
Michael Wrightd02c5b62014-02-10 15:10:22 -08002995TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002996 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2997 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07002998 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
2999 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3000 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003002 KeyboardInputMapper& mapper =
3003 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3004 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005
Arthur Hung95f68612022-04-07 14:08:22 +08003006 // Initial metastate is AMETA_NONE.
3007 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003008
3009 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003011 NotifyKeyArgs args;
3012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3013 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003014 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003015 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003016
3017 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003018 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3020 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003021 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022
3023 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003024 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3026 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003027 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003028
3029 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003030 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3032 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003033 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003034 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003035}
3036
3037TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003038 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3039 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3040 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3041 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003042
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003043 KeyboardInputMapper& mapper =
3044 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3045 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003046
Michael Wrighta9cf4192022-12-01 23:46:39 +00003047 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003048 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3049 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3050 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3051 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3052 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3053 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3054 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3055 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3056}
3057
3058TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003059 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3060 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3061 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3062 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063
Michael Wrightd02c5b62014-02-10 15:10:22 -08003064 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003065 KeyboardInputMapper& mapper =
3066 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3067 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068
Michael Wrighta9cf4192022-12-01 23:46:39 +00003069 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003070 ASSERT_NO_FATAL_FAILURE(
3071 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3072 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3073 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3074 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3075 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3076 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3077 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003079 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003080 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003081 ASSERT_NO_FATAL_FAILURE(
3082 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3083 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3084 AKEYCODE_DPAD_UP, DISPLAY_ID));
3085 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3086 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3087 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3088 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003090 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003091 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003092 ASSERT_NO_FATAL_FAILURE(
3093 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3094 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3095 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3096 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3097 AKEYCODE_DPAD_UP, DISPLAY_ID));
3098 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3099 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003101 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003102 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003103 ASSERT_NO_FATAL_FAILURE(
3104 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3105 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3106 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3107 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3108 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3109 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3110 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003111
3112 // Special case: if orientation changes while key is down, we still emit the same keycode
3113 // in the key up as we did in the key down.
3114 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003115 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003116 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3119 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3120 ASSERT_EQ(KEY_UP, args.scanCode);
3121 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3122
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003123 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003124 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3127 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3128 ASSERT_EQ(KEY_UP, args.scanCode);
3129 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3130}
3131
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003132TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3133 // If the keyboard is not orientation aware,
3134 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003135 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003136
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003137 KeyboardInputMapper& mapper =
3138 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3139 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003140 NotifyKeyArgs args;
3141
3142 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3147 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3148
Michael Wrighta9cf4192022-12-01 23:46:39 +00003149 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3154 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3155}
3156
3157TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3158 // If the keyboard is orientation aware,
3159 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003160 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003161
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003162 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003163 KeyboardInputMapper& mapper =
3164 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3165 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003166 NotifyKeyArgs args;
3167
3168 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3169 // ^--- already checked by the previous test
3170
Michael Wrighta9cf4192022-12-01 23:46:39 +00003171 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003172 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3177 ASSERT_EQ(DISPLAY_ID, args.displayId);
3178
3179 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003180 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003181 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003182 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3187 ASSERT_EQ(newDisplayId, args.displayId);
3188}
3189
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003191 KeyboardInputMapper& mapper =
3192 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3193 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003195 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003196 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003197
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003198 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003199 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003200}
3201
Philip Junker4af3b3d2021-12-14 10:36:55 +01003202TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3203 KeyboardInputMapper& mapper =
3204 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3205 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3206
3207 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3208 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3209 << "If a mapping is available, the result is equal to the mapping";
3210
3211 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3212 << "If no mapping is available, the result is the key location";
3213}
3214
Michael Wrightd02c5b62014-02-10 15:10:22 -08003215TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003216 KeyboardInputMapper& mapper =
3217 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3218 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003219
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003220 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003221 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003222
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003223 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003224 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225}
3226
3227TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003228 KeyboardInputMapper& mapper =
3229 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3230 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003232 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003233
Michael Wrightd02c5b62014-02-10 15:10:22 -08003234 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003235 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003236 ASSERT_TRUE(flags[0]);
3237 ASSERT_FALSE(flags[1]);
3238}
3239
3240TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003241 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3242 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3243 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3244 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3245 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3246 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003248 KeyboardInputMapper& mapper =
3249 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3250 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003251 // Initial metastate is AMETA_NONE.
3252 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003253
3254 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003255 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3256 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3257 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258
3259 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003262 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3263 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3264 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266
3267 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003270 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3271 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3272 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003273 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274
3275 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003278 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3279 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3280 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003281 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
3283 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003286 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3287 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3288 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003289 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290
3291 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003294 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3295 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3296 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003297 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298
3299 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003302 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3303 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3304 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003305 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306}
3307
Chris Yea52ade12020-08-27 16:49:20 -07003308TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3309 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3310 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3311 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3312 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3313
3314 KeyboardInputMapper& mapper =
3315 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3316 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3317
Chris Yea52ade12020-08-27 16:49:20 -07003318 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003319 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003320 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3321 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3322 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3323 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3324
3325 NotifyKeyArgs args;
3326 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3329 ASSERT_EQ(AMETA_NONE, args.metaState);
3330 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3331 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3332 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3333
3334 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003335 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3337 ASSERT_EQ(AMETA_NONE, args.metaState);
3338 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3339 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3340 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3341}
3342
Arthur Hung2c9a3342019-07-23 14:18:59 +08003343TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3344 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003345 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3346 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3347 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3348 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003349
3350 // keyboard 2.
3351 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003352 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003353 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003354 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003355 std::shared_ptr<InputDevice> device2 =
3356 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003357 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003358
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003359 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3360 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3361 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3362 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003363
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003364 KeyboardInputMapper& mapper =
3365 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3366 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003367
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003368 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003369 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003370 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003371 std::list<NotifyArgs> unused =
3372 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003373 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003374 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003375
3376 // Prepared displays and associated info.
3377 constexpr uint8_t hdmi1 = 0;
3378 constexpr uint8_t hdmi2 = 1;
3379 const std::string SECONDARY_UNIQUE_ID = "local:1";
3380
3381 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3382 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3383
3384 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003385 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3386 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003387 ASSERT_FALSE(device2->isEnabled());
3388
3389 // Prepare second display.
3390 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003391 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003392 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003393 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003394 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003395 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003396 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3397 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003398
3399 // Device should be enabled after the associated display is found.
3400 ASSERT_TRUE(mDevice->isEnabled());
3401 ASSERT_TRUE(device2->isEnabled());
3402
3403 // Test pad key events
3404 ASSERT_NO_FATAL_FAILURE(
3405 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3406 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3407 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3408 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3409 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3410 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3411 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3412
3413 ASSERT_NO_FATAL_FAILURE(
3414 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3415 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3416 AKEYCODE_DPAD_RIGHT, newDisplayId));
3417 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3418 AKEYCODE_DPAD_DOWN, newDisplayId));
3419 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3420 AKEYCODE_DPAD_LEFT, newDisplayId));
3421}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003422
arthurhungc903df12020-08-11 15:08:42 +08003423TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3424 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3425 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3426 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3427 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3428 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3429 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3430
3431 KeyboardInputMapper& mapper =
3432 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3433 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003434 // Initial metastate is AMETA_NONE.
3435 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003436
3437 // Initialization should have turned all of the lights off.
3438 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3439 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3440 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3441
3442 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003445 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3446 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3447
3448 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003451 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3452 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3453
3454 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003457 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3458 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3459
3460 mFakeEventHub->removeDevice(EVENTHUB_ID);
3461 mReader->loopOnce();
3462
3463 // keyboard 2 should default toggle keys.
3464 const std::string USB2 = "USB2";
3465 const std::string DEVICE_NAME2 = "KEYBOARD2";
3466 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3467 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3468 std::shared_ptr<InputDevice> device2 =
3469 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003470 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003471 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3472 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3473 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3474 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3475 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3476 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3477
arthurhung6fe95782020-10-05 22:41:16 +08003478 KeyboardInputMapper& mapper2 =
3479 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3480 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003481 std::list<NotifyArgs> unused =
3482 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003483 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003484 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003485
3486 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3487 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3488 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003489 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3490 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003491}
3492
Arthur Hungcb40a002021-08-03 14:31:01 +00003493TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3494 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3495 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3496 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3497
3498 // Suppose we have two mappers. (DPAD + KEYBOARD)
3499 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3500 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3501 KeyboardInputMapper& mapper =
3502 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3503 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003504 // Initial metastate is AMETA_NONE.
3505 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003506
3507 mReader->toggleCapsLockState(DEVICE_ID);
3508 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3509}
3510
Arthur Hungfb3cc112022-04-13 07:39:50 +00003511TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3512 // keyboard 1.
3513 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3514 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3515 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3516 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3517 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3518 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3519
3520 KeyboardInputMapper& mapper1 =
3521 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3522 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3523
3524 // keyboard 2.
3525 const std::string USB2 = "USB2";
3526 const std::string DEVICE_NAME2 = "KEYBOARD2";
3527 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3528 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3529 std::shared_ptr<InputDevice> device2 =
3530 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3531 ftl::Flags<InputDeviceClass>(0));
3532 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3533 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3534 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3535 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3536 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3537 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3538
3539 KeyboardInputMapper& mapper2 =
3540 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3541 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003542 std::list<NotifyArgs> unused =
3543 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003544 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003545 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003546
Arthur Hung95f68612022-04-07 14:08:22 +08003547 // Initial metastate is AMETA_NONE.
3548 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3549 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3550
3551 // Toggle num lock on and off.
3552 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3553 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003554 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3555 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3556 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3557
3558 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3559 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3560 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3561 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3562 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3563
3564 // Toggle caps lock on and off.
3565 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3566 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3567 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3568 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3569 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3570
3571 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3572 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3573 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3574 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3575 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3576
3577 // Toggle scroll lock on and off.
3578 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3579 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3580 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3581 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3582 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3583
3584 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3585 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3586 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3587 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3588 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3589}
3590
Arthur Hung2141d542022-08-23 07:45:21 +00003591TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3592 const int32_t USAGE_A = 0x070004;
3593 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3594 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3595
3596 KeyboardInputMapper& mapper =
3597 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3598 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3599 // Key down by scan code.
3600 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3601 NotifyKeyArgs args;
3602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3603 ASSERT_EQ(DEVICE_ID, args.deviceId);
3604 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3605 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3606 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3607 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3608 ASSERT_EQ(KEY_HOME, args.scanCode);
3609 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3610
3611 // Disable device, it should synthesize cancellation events for down events.
3612 mFakePolicy->addDisabledDevice(DEVICE_ID);
3613 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
3614
3615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3616 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3617 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3618 ASSERT_EQ(KEY_HOME, args.scanCode);
3619 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3620}
3621
Zixuan Qufecb6062022-11-12 04:44:31 +00003622TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
3623 mDevice->addMapper<KeyboardInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3624 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3625 std::list<NotifyArgs> unused =
3626 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
3627
3628 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3629
3630 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3631 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUT_ASSOCIATION);
3632
3633 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3634 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3635 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3636 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3637 deviceInfo.getKeyboardLayoutInfo()->layoutType);
3638}
3639
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003640TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3641 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3642 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3643
3644 // Configuration
3645 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3646 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3647 InputReaderConfiguration config;
3648 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
3649
3650 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3651 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3652}
3653
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003654// --- KeyboardInputMapperTest_ExternalDevice ---
3655
3656class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3657protected:
Chris Yea52ade12020-08-27 16:49:20 -07003658 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003659};
3660
3661TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003662 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3663 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003664
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003665 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3666 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3667 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3668 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003669
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003670 KeyboardInputMapper& mapper =
3671 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3672 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003673
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003675 NotifyKeyArgs args;
3676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3677 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3678
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003679 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3681 ASSERT_EQ(uint32_t(0), args.policyFlags);
3682
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003685 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003686
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003687 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3689 ASSERT_EQ(uint32_t(0), args.policyFlags);
3690
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3693 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3694
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003695 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3697 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3698}
3699
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003700TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003701 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003702
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003703 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3704 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3705 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003706
Powei Fengd041c5d2019-05-03 17:11:33 -07003707 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003708 KeyboardInputMapper& mapper =
3709 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3710 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003711
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003713 NotifyKeyArgs args;
3714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3715 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3716
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003717 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3719 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3720
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003721 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3723 ASSERT_EQ(uint32_t(0), args.policyFlags);
3724
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003725 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3727 ASSERT_EQ(uint32_t(0), args.policyFlags);
3728
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003729 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3731 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3732
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003733 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3735 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3736}
3737
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738// --- CursorInputMapperTest ---
3739
3740class CursorInputMapperTest : public InputMapperTest {
3741protected:
3742 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3743
Michael Wright17db18e2020-06-26 20:51:44 +01003744 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003745
Chris Yea52ade12020-08-27 16:49:20 -07003746 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003747 InputMapperTest::SetUp();
3748
Michael Wright17db18e2020-06-26 20:51:44 +01003749 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003750 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751 }
3752
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003753 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3754 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003755
Michael Wrighta9cf4192022-12-01 23:46:39 +00003756 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003757 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3758 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3759 }
3760
3761 void prepareSecondaryDisplay() {
3762 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003763 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003764 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003765 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003766
3767 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3768 float pressure) {
3769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3770 0.0f, 0.0f, 0.0f, EPSILON));
3771 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003772};
3773
3774const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3775
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003776void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3777 int32_t originalY, int32_t rotatedX,
3778 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003779 NotifyMotionArgs args;
3780
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003786 ASSERT_NO_FATAL_FAILURE(
3787 assertCursorPointerCoords(args.pointerCoords[0],
3788 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3789 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790}
3791
3792TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003793 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003794 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003795
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003796 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797}
3798
3799TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003800 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003801 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003802
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003803 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804}
3805
3806TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003808 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003809
3810 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003811 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003812
3813 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003814 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3815 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003816 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3817 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3818
3819 // When the bounds are set, then there should be a valid motion range.
3820 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3821
3822 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003823 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003824
3825 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3826 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3827 1, 800 - 1, 0.0f, 0.0f));
3828 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3829 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3830 2, 480 - 1, 0.0f, 0.0f));
3831 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3832 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3833 0.0f, 1.0f, 0.0f, 0.0f));
3834}
3835
3836TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003837 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003838 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003839
3840 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003841 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003842
3843 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3844 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3845 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3846 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3847 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3848 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3849 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3850 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3851 0.0f, 1.0f, 0.0f, 0.0f));
3852}
3853
3854TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003855 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003856 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003857
arthurhungdcef2dc2020-08-11 14:47:50 +08003858 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859
3860 NotifyMotionArgs args;
3861
3862 // Button press.
3863 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3867 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3868 ASSERT_EQ(DEVICE_ID, args.deviceId);
3869 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3870 ASSERT_EQ(uint32_t(0), args.policyFlags);
3871 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3872 ASSERT_EQ(0, args.flags);
3873 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3874 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3875 ASSERT_EQ(0, args.edgeFlags);
3876 ASSERT_EQ(uint32_t(1), args.pointerCount);
3877 ASSERT_EQ(0, args.pointerProperties[0].id);
3878 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003879 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003880 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3881 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3882 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3883
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3885 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3886 ASSERT_EQ(DEVICE_ID, args.deviceId);
3887 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3888 ASSERT_EQ(uint32_t(0), args.policyFlags);
3889 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3890 ASSERT_EQ(0, args.flags);
3891 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3892 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3893 ASSERT_EQ(0, args.edgeFlags);
3894 ASSERT_EQ(uint32_t(1), args.pointerCount);
3895 ASSERT_EQ(0, args.pointerProperties[0].id);
3896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003897 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003898 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3899 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3900 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3901
Michael Wrightd02c5b62014-02-10 15:10:22 -08003902 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003903 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3904 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3906 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3907 ASSERT_EQ(DEVICE_ID, args.deviceId);
3908 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3909 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003910 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3911 ASSERT_EQ(0, args.flags);
3912 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3913 ASSERT_EQ(0, args.buttonState);
3914 ASSERT_EQ(0, args.edgeFlags);
3915 ASSERT_EQ(uint32_t(1), args.pointerCount);
3916 ASSERT_EQ(0, args.pointerProperties[0].id);
3917 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003918 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003919 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3920 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3921 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3922
3923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3924 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3925 ASSERT_EQ(DEVICE_ID, args.deviceId);
3926 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3927 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003928 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3929 ASSERT_EQ(0, args.flags);
3930 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3931 ASSERT_EQ(0, args.buttonState);
3932 ASSERT_EQ(0, args.edgeFlags);
3933 ASSERT_EQ(uint32_t(1), args.pointerCount);
3934 ASSERT_EQ(0, args.pointerProperties[0].id);
3935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003936 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003937 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3938 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3939 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3940}
3941
3942TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003943 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003944 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003945
3946 NotifyMotionArgs args;
3947
3948 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003953 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3954 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
3955 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003956
3957 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
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(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003962 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
3963 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003964}
3965
3966TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003967 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003968 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969
3970 NotifyMotionArgs args;
3971
3972 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3976 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003977 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003978
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003981 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003982
Michael Wrightd02c5b62014-02-10 15:10:22 -08003983 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3985 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003988 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003989
3990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003991 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003992 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003993}
3994
3995TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003996 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003997 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003998
3999 NotifyMotionArgs args;
4000
4001 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4005 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4007 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004008 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4009 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4010 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004011
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4013 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004014 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4015 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4016 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004017
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004024 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4025 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4026 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004027
4028 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004032 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004033 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004034
4035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004036 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004037 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004038}
4039
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004040TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004041 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004043 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4044 // need to be rotated.
4045 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004046 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004047
Michael Wrighta9cf4192022-12-01 23:46:39 +00004048 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4050 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4051 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4052 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4053 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4054 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4055 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4056 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4057}
4058
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004059TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004060 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004062 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4063 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004064 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004066 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004067 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4069 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4070 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4071 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4072 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4073 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4074 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4075 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4076
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004077 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004078 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004079 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4080 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4081 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4082 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4083 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4084 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4085 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4086 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004088 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004089 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4091 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4092 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4093 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4094 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4095 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4096 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4097 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4098
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004099 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004100 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004101 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4102 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4103 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4104 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4105 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4106 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4107 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4108 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109}
4110
4111TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004113 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114
4115 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4116 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117
4118 NotifyMotionArgs motionArgs;
4119 NotifyKeyArgs keyArgs;
4120
4121 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4125 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4126 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004127 ASSERT_NO_FATAL_FAILURE(
4128 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004129
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4131 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4132 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004133 ASSERT_NO_FATAL_FAILURE(
4134 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004135
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004139 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004141 ASSERT_NO_FATAL_FAILURE(
4142 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143
4144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004145 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004147 ASSERT_NO_FATAL_FAILURE(
4148 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004149
4150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004152 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004153 ASSERT_NO_FATAL_FAILURE(
4154 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155
4156 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4161 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4162 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004163 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004164 ASSERT_NO_FATAL_FAILURE(
4165 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4168 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4169 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004170 ASSERT_NO_FATAL_FAILURE(
4171 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004172
4173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4174 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4175 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004176 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004177 ASSERT_NO_FATAL_FAILURE(
4178 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004179
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004185 ASSERT_NO_FATAL_FAILURE(
4186 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004187
4188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004190 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004191 ASSERT_NO_FATAL_FAILURE(
4192 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004197 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4198 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004199 ASSERT_NO_FATAL_FAILURE(
4200 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004203
4204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004207 ASSERT_NO_FATAL_FAILURE(
4208 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004209
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4211 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004213 ASSERT_NO_FATAL_FAILURE(
4214 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215
4216 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4218 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4220 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4221 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004222
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004224 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004226 ASSERT_NO_FATAL_FAILURE(
4227 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004228
4229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4231 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004232 ASSERT_NO_FATAL_FAILURE(
4233 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004238 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004240 ASSERT_NO_FATAL_FAILURE(
4241 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004242
4243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004245 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004246
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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4250 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4251 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4252
4253 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004254 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4257 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4258 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004259
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004261 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004263 ASSERT_NO_FATAL_FAILURE(
4264 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004265
4266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4268 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004269 ASSERT_NO_FATAL_FAILURE(
4270 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004275 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004277 ASSERT_NO_FATAL_FAILURE(
4278 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004279
4280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4281 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4282 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004283 ASSERT_NO_FATAL_FAILURE(
4284 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004285
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4287 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4288 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4289
4290 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4294 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4295 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004296
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004298 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004300 ASSERT_NO_FATAL_FAILURE(
4301 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004302
4303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4304 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4305 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004306 ASSERT_NO_FATAL_FAILURE(
4307 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004312 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004314 ASSERT_NO_FATAL_FAILURE(
4315 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004316
4317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4318 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4319 ASSERT_EQ(0, 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4324 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4325 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4326
4327 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4331 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4332 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004333
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004335 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004337 ASSERT_NO_FATAL_FAILURE(
4338 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004339
4340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4341 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4342 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004343 ASSERT_NO_FATAL_FAILURE(
4344 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004349 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004351 ASSERT_NO_FATAL_FAILURE(
4352 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004353
4354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4355 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4356 ASSERT_EQ(0, 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4361 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4362 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4363}
4364
4365TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004367 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368
4369 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4370 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371
4372 NotifyMotionArgs args;
4373
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004378 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4379 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4381 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 +00004382 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004383}
4384
4385TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004386 addConfigurationProperty("cursor.mode", "pointer");
4387 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004388 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004389
4390 NotifyDeviceResetArgs resetArgs;
4391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4392 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4393 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4394
4395 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4396 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004397
4398 NotifyMotionArgs args;
4399
4400 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4403 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4405 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4408 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 +00004409 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004410
4411 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4415 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4416 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4418 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4420 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4421 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4423 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4424
4425 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004426 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4427 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4429 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4430 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4432 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4434 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4435 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4437 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4438
4439 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4444 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4447 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 +00004448 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004449
4450 // Disable pointer capture and check that the device generation got bumped
4451 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004452 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004453 mFakePolicy->setPointerCapture(false);
4454 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004455 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004456
4457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004458 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4459
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4461 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4464 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4467 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 +00004468 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469}
4470
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004471/**
4472 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4473 * pointer acceleration or speed processing should not be applied.
4474 */
4475TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4476 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004477 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4478 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004479 mFakePolicy->setVelocityControlParams(testParams);
4480 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4481
4482 NotifyDeviceResetArgs resetArgs;
4483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4484 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4485 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4486
4487 NotifyMotionArgs args;
4488
4489 // Move and verify scale is applied.
4490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4492 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4494 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4495 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4496 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4497 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4498 ASSERT_GT(relX, 10);
4499 ASSERT_GT(relY, 20);
4500
4501 // Enable Pointer Capture
4502 mFakePolicy->setPointerCapture(true);
4503 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4504 NotifyPointerCaptureChangedArgs captureArgs;
4505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4506 ASSERT_TRUE(captureArgs.request.enable);
4507
4508 // Move and verify scale is not applied.
4509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4513 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4515 ASSERT_EQ(10, args.pointerCoords[0].getX());
4516 ASSERT_EQ(20, args.pointerCoords[0].getY());
4517}
4518
Prabir Pradhan208360b2022-06-24 18:37:04 +00004519TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4520 addConfigurationProperty("cursor.mode", "pointer");
4521 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4522
4523 NotifyDeviceResetArgs resetArgs;
4524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4525 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4526 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4527
4528 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004529 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004530
4531 NotifyMotionArgs args;
4532
4533 // Verify that the coordinates are rotated.
4534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4538 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4539 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4540 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4541 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4542
4543 // Enable Pointer Capture.
4544 mFakePolicy->setPointerCapture(true);
4545 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4546 NotifyPointerCaptureChangedArgs captureArgs;
4547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4548 ASSERT_TRUE(captureArgs.request.enable);
4549
4550 // Move and verify rotation is not applied.
4551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4555 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4557 ASSERT_EQ(10, args.pointerCoords[0].getX());
4558 ASSERT_EQ(20, args.pointerCoords[0].getY());
4559}
4560
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004561TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004562 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004563
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004564 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004565 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004566
4567 // Set up the secondary display as the display on which the pointer should be shown.
4568 // The InputDevice is not associated with any display.
4569 prepareSecondaryDisplay();
4570 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08004571 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4572
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004573 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004574 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004575
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004576 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4579 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004581 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4582 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4583 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004584 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004585}
4586
4587TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4588 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4589
4590 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004591 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004592
4593 // Set up the secondary display as the display on which the pointer should be shown,
4594 // and associate the InputDevice with the secondary display.
4595 prepareSecondaryDisplay();
4596 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4597 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4598 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4599
4600 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4601 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004602
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(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004607 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4608 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4609 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004610 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004611}
4612
4613TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4614 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4615
4616 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004617 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004618 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4619
4620 // Associate the InputDevice with the secondary display.
4621 prepareSecondaryDisplay();
4622 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4623 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4624
4625 // The mapper should not generate any events because it is associated with a display that is
4626 // different from the pointer display.
4627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004631}
4632
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004633// --- BluetoothCursorInputMapperTest ---
4634
4635class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4636protected:
4637 void SetUp() override {
4638 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4639
4640 mFakePointerController = std::make_shared<FakePointerController>();
4641 mFakePolicy->setPointerController(mFakePointerController);
4642 }
4643};
4644
4645TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4646 addConfigurationProperty("cursor.mode", "pointer");
4647 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4648
4649 nsecs_t kernelEventTime = ARBITRARY_TIME;
4650 nsecs_t expectedEventTime = ARBITRARY_TIME;
4651 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4652 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4654 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4655 WithEventTime(expectedEventTime))));
4656
4657 // Process several events that come in quick succession, according to their timestamps.
4658 for (int i = 0; i < 3; i++) {
4659 constexpr static nsecs_t delta = ms2ns(1);
4660 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4661 kernelEventTime += delta;
4662 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4663
4664 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4665 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4667 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4668 WithEventTime(expectedEventTime))));
4669 }
4670}
4671
4672TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4673 addConfigurationProperty("cursor.mode", "pointer");
4674 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4675
4676 nsecs_t expectedEventTime = ARBITRARY_TIME;
4677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4680 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4681 WithEventTime(expectedEventTime))));
4682
4683 // Process several events with the same timestamp from the kernel.
4684 // Ensure that we do not generate events too far into the future.
4685 constexpr static int32_t numEvents =
4686 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4687 for (int i = 0; i < numEvents; i++) {
4688 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4689
4690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4693 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4694 WithEventTime(expectedEventTime))));
4695 }
4696
4697 // By processing more events with the same timestamp, we should not generate events with a
4698 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4699 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4700 for (int i = 0; i < 3; i++) {
4701 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4702 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4704 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4705 WithEventTime(cappedEventTime))));
4706 }
4707}
4708
4709TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4710 addConfigurationProperty("cursor.mode", "pointer");
4711 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4712
4713 nsecs_t kernelEventTime = ARBITRARY_TIME;
4714 nsecs_t expectedEventTime = ARBITRARY_TIME;
4715 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4716 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4718 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4719 WithEventTime(expectedEventTime))));
4720
4721 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4722 // smoothening is not needed, its timestamp is not affected.
4723 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4724 expectedEventTime = kernelEventTime;
4725
4726 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4727 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4729 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4730 WithEventTime(expectedEventTime))));
4731}
4732
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733// --- TouchInputMapperTest ---
4734
4735class TouchInputMapperTest : public InputMapperTest {
4736protected:
4737 static const int32_t RAW_X_MIN;
4738 static const int32_t RAW_X_MAX;
4739 static const int32_t RAW_Y_MIN;
4740 static const int32_t RAW_Y_MAX;
4741 static const int32_t RAW_TOUCH_MIN;
4742 static const int32_t RAW_TOUCH_MAX;
4743 static const int32_t RAW_TOOL_MIN;
4744 static const int32_t RAW_TOOL_MAX;
4745 static const int32_t RAW_PRESSURE_MIN;
4746 static const int32_t RAW_PRESSURE_MAX;
4747 static const int32_t RAW_ORIENTATION_MIN;
4748 static const int32_t RAW_ORIENTATION_MAX;
4749 static const int32_t RAW_DISTANCE_MIN;
4750 static const int32_t RAW_DISTANCE_MAX;
4751 static const int32_t RAW_TILT_MIN;
4752 static const int32_t RAW_TILT_MAX;
4753 static const int32_t RAW_ID_MIN;
4754 static const int32_t RAW_ID_MAX;
4755 static const int32_t RAW_SLOT_MIN;
4756 static const int32_t RAW_SLOT_MAX;
4757 static const float X_PRECISION;
4758 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004759 static const float X_PRECISION_VIRTUAL;
4760 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004761
4762 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004763 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004764
4765 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4766
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004767 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004768 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004769
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770 enum Axes {
4771 POSITION = 1 << 0,
4772 TOUCH = 1 << 1,
4773 TOOL = 1 << 2,
4774 PRESSURE = 1 << 3,
4775 ORIENTATION = 1 << 4,
4776 MINOR = 1 << 5,
4777 ID = 1 << 6,
4778 DISTANCE = 1 << 7,
4779 TILT = 1 << 8,
4780 SLOT = 1 << 9,
4781 TOOL_TYPE = 1 << 10,
4782 };
4783
Michael Wrighta9cf4192022-12-01 23:46:39 +00004784 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004785 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004786 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004788 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789 int32_t toRawX(float displayX);
4790 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004791 int32_t toRotatedRawX(float displayX);
4792 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004793 float toCookedX(float rawX, float rawY);
4794 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004796 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004798 float toDisplayY(int32_t rawY, int32_t displayHeight);
4799
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800};
4801
4802const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4803const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4804const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4805const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4806const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4807const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4808const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4809const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004810const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4811const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004812const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4813const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4814const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4815const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4816const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4817const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4818const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4819const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4820const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4821const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4822const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4823const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004824const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4825 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4826const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4827 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004828const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4829 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004830
4831const float TouchInputMapperTest::GEOMETRIC_SCALE =
4832 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4833 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4834
4835const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4836 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4837 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4838};
4839
Michael Wrighta9cf4192022-12-01 23:46:39 +00004840void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004841 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4842 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004843}
4844
4845void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4846 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004847 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848}
4849
Michael Wrighta9cf4192022-12-01 23:46:39 +00004850void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004851 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4852 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4853 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004854}
4855
Michael Wrightd02c5b62014-02-10 15:10:22 -08004856void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004857 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4858 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4859 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4860 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861}
4862
Jason Gerecke489fda82012-09-07 17:19:40 -07004863void TouchInputMapperTest::prepareLocationCalibration() {
4864 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4865}
4866
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867int32_t TouchInputMapperTest::toRawX(float displayX) {
4868 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4869}
4870
4871int32_t TouchInputMapperTest::toRawY(float displayY) {
4872 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4873}
4874
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004875int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4876 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4877}
4878
4879int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4880 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4881}
4882
Jason Gerecke489fda82012-09-07 17:19:40 -07004883float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4884 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4885 return rawX;
4886}
4887
4888float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4889 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4890 return rawY;
4891}
4892
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004894 return toDisplayX(rawX, DISPLAY_WIDTH);
4895}
4896
4897float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4898 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899}
4900
4901float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004902 return toDisplayY(rawY, DISPLAY_HEIGHT);
4903}
4904
4905float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4906 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907}
4908
4909
4910// --- SingleTouchInputMapperTest ---
4911
4912class SingleTouchInputMapperTest : public TouchInputMapperTest {
4913protected:
4914 void prepareButtons();
4915 void prepareAxes(int axes);
4916
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004917 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4918 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4919 void processUp(SingleTouchInputMapper& mappery);
4920 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4921 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4922 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4923 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4924 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4925 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004926};
4927
4928void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004929 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930}
4931
4932void SingleTouchInputMapperTest::prepareAxes(int axes) {
4933 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004934 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4935 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936 }
4937 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004938 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4939 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940 }
4941 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004942 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4943 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944 }
4945 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004946 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4947 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004948 }
4949 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004950 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4951 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952 }
4953}
4954
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004955void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004959}
4960
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004961void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964}
4965
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004966void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968}
4969
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004970void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972}
4973
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004974void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4975 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004977}
4978
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004979void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981}
4982
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004983void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4984 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004985 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4986 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987}
4988
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004989void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4990 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992}
4993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004994void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996}
4997
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004999 prepareButtons();
5000 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005001 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002
Josep del Río2d8c79a2023-01-23 19:33:50 +00005003 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004}
5005
Michael Wrightd02c5b62014-02-10 15:10:22 -08005006TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005007 prepareButtons();
5008 prepareAxes(POSITION);
5009 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005010 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005011
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005012 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013}
5014
5015TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005017 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005018 prepareButtons();
5019 prepareAxes(POSITION);
5020 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005021 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022
5023 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005024 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005025
5026 // Virtual key is down.
5027 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5028 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5029 processDown(mapper, x, y);
5030 processSync(mapper);
5031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5032
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005033 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034
5035 // Virtual key is up.
5036 processUp(mapper);
5037 processSync(mapper);
5038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5039
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005040 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041}
5042
5043TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005044 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005045 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046 prepareButtons();
5047 prepareAxes(POSITION);
5048 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005049 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050
5051 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005052 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005053
5054 // Virtual key is down.
5055 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5056 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5057 processDown(mapper, x, y);
5058 processSync(mapper);
5059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5060
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005061 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062
5063 // Virtual key is up.
5064 processUp(mapper);
5065 processSync(mapper);
5066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5067
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005068 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005069}
5070
5071TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005073 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005074 prepareButtons();
5075 prepareAxes(POSITION);
5076 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005077 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005080 ASSERT_TRUE(
5081 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082 ASSERT_TRUE(flags[0]);
5083 ASSERT_FALSE(flags[1]);
5084}
5085
5086TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005088 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005089 prepareButtons();
5090 prepareAxes(POSITION);
5091 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005092 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005093
arthurhungdcef2dc2020-08-11 14:47:50 +08005094 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095
5096 NotifyKeyArgs args;
5097
5098 // Press virtual key.
5099 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5100 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5101 processDown(mapper, x, y);
5102 processSync(mapper);
5103
5104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5105 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5106 ASSERT_EQ(DEVICE_ID, args.deviceId);
5107 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5108 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5109 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5110 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5111 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5112 ASSERT_EQ(KEY_HOME, args.scanCode);
5113 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5114 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5115
5116 // Release virtual key.
5117 processUp(mapper);
5118 processSync(mapper);
5119
5120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5121 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5122 ASSERT_EQ(DEVICE_ID, args.deviceId);
5123 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5124 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5125 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5126 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5127 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5128 ASSERT_EQ(KEY_HOME, args.scanCode);
5129 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5130 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5131
5132 // Should not have sent any motions.
5133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5134}
5135
5136TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005138 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005139 prepareButtons();
5140 prepareAxes(POSITION);
5141 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005142 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143
arthurhungdcef2dc2020-08-11 14:47:50 +08005144 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145
5146 NotifyKeyArgs keyArgs;
5147
5148 // Press virtual key.
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
5154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5155 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5156 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5157 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5158 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5159 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5160 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5161 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5162 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5163 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5164 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5165
5166 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5167 // into the display area.
5168 y -= 100;
5169 processMove(mapper, x, y);
5170 processSync(mapper);
5171
5172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5173 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5174 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5175 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5176 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5177 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5178 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5179 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5180 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5181 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5182 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5183 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5184
5185 NotifyMotionArgs motionArgs;
5186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5187 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5188 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5189 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5190 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5191 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5192 ASSERT_EQ(0, motionArgs.flags);
5193 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5194 ASSERT_EQ(0, motionArgs.buttonState);
5195 ASSERT_EQ(0, motionArgs.edgeFlags);
5196 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5197 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5200 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5201 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5202 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5203 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5204
5205 // Keep moving out of bounds. Should generate a pointer move.
5206 y -= 50;
5207 processMove(mapper, x, y);
5208 processSync(mapper);
5209
5210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5211 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5212 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5213 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5214 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5216 ASSERT_EQ(0, motionArgs.flags);
5217 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5218 ASSERT_EQ(0, motionArgs.buttonState);
5219 ASSERT_EQ(0, motionArgs.edgeFlags);
5220 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5221 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5222 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5224 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5225 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5226 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5227 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5228
5229 // Release out of bounds. Should generate a pointer up.
5230 processUp(mapper);
5231 processSync(mapper);
5232
5233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5234 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5235 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5236 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5237 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5238 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5239 ASSERT_EQ(0, motionArgs.flags);
5240 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5241 ASSERT_EQ(0, motionArgs.buttonState);
5242 ASSERT_EQ(0, motionArgs.edgeFlags);
5243 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5244 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5247 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5248 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5249 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5250 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5251
5252 // Should not have sent any more keys or motions.
5253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5255}
5256
5257TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005258 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005259 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005260 prepareButtons();
5261 prepareAxes(POSITION);
5262 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005263 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264
arthurhungdcef2dc2020-08-11 14:47:50 +08005265 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005266
5267 NotifyMotionArgs motionArgs;
5268
5269 // Initially go down out of bounds.
5270 int32_t x = -10;
5271 int32_t y = -10;
5272 processDown(mapper, x, y);
5273 processSync(mapper);
5274
5275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5276
5277 // Move into the display area. Should generate a pointer down.
5278 x = 50;
5279 y = 75;
5280 processMove(mapper, x, y);
5281 processSync(mapper);
5282
5283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5284 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5285 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5286 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5287 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5288 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5289 ASSERT_EQ(0, motionArgs.flags);
5290 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5291 ASSERT_EQ(0, motionArgs.buttonState);
5292 ASSERT_EQ(0, motionArgs.edgeFlags);
5293 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5294 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5295 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5297 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5298 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5299 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5300 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5301
5302 // Release. Should generate a pointer up.
5303 processUp(mapper);
5304 processSync(mapper);
5305
5306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5307 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5308 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5309 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5310 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5311 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5312 ASSERT_EQ(0, motionArgs.flags);
5313 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5314 ASSERT_EQ(0, motionArgs.buttonState);
5315 ASSERT_EQ(0, motionArgs.edgeFlags);
5316 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5317 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5320 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5321 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5322 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5323 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5324
5325 // Should not have sent any more keys or motions.
5326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5328}
5329
Santos Cordonfa5cf462017-04-05 10:37:00 -07005330TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005331 addConfigurationProperty("touch.deviceType", "touchScreen");
5332 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5333
Michael Wrighta9cf4192022-12-01 23:46:39 +00005334 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005335 prepareButtons();
5336 prepareAxes(POSITION);
5337 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005338 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005339
arthurhungdcef2dc2020-08-11 14:47:50 +08005340 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005341
5342 NotifyMotionArgs motionArgs;
5343
5344 // Down.
5345 int32_t x = 100;
5346 int32_t y = 125;
5347 processDown(mapper, x, y);
5348 processSync(mapper);
5349
5350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5351 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5352 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5353 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5354 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5355 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5356 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5357 ASSERT_EQ(0, motionArgs.flags);
5358 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5359 ASSERT_EQ(0, motionArgs.buttonState);
5360 ASSERT_EQ(0, motionArgs.edgeFlags);
5361 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5362 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5365 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5366 1, 0, 0, 0, 0, 0, 0, 0));
5367 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5368 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5369 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5370
5371 // Move.
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(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5381 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5382 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5384 ASSERT_EQ(0, motionArgs.flags);
5385 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5386 ASSERT_EQ(0, motionArgs.buttonState);
5387 ASSERT_EQ(0, motionArgs.edgeFlags);
5388 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5389 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5392 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5393 1, 0, 0, 0, 0, 0, 0, 0));
5394 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5395 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5396 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5397
5398 // Up.
5399 processUp(mapper);
5400 processSync(mapper);
5401
5402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5403 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5404 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5405 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5406 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5407 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5408 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5409 ASSERT_EQ(0, motionArgs.flags);
5410 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5411 ASSERT_EQ(0, motionArgs.buttonState);
5412 ASSERT_EQ(0, motionArgs.edgeFlags);
5413 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5414 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5417 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5418 1, 0, 0, 0, 0, 0, 0, 0));
5419 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5420 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5421 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5422
5423 // Should not have sent any more keys or motions.
5424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5426}
5427
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005429 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005430 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431 prepareButtons();
5432 prepareAxes(POSITION);
5433 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005434 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005435
arthurhungdcef2dc2020-08-11 14:47:50 +08005436 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437
5438 NotifyMotionArgs motionArgs;
5439
5440 // Down.
5441 int32_t x = 100;
5442 int32_t y = 125;
5443 processDown(mapper, x, y);
5444 processSync(mapper);
5445
5446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5447 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5448 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5449 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5450 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5451 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5452 ASSERT_EQ(0, motionArgs.flags);
5453 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5454 ASSERT_EQ(0, motionArgs.buttonState);
5455 ASSERT_EQ(0, motionArgs.edgeFlags);
5456 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5457 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5460 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5461 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5462 ASSERT_NEAR(Y_PRECISION, 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(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5475 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5476 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5477 ASSERT_EQ(0, motionArgs.flags);
5478 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5479 ASSERT_EQ(0, motionArgs.buttonState);
5480 ASSERT_EQ(0, motionArgs.edgeFlags);
5481 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5482 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5483 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5485 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5486 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5487 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5488 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5489
5490 // Up.
5491 processUp(mapper);
5492 processSync(mapper);
5493
5494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5495 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5496 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5497 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5498 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5499 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5500 ASSERT_EQ(0, motionArgs.flags);
5501 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5502 ASSERT_EQ(0, motionArgs.buttonState);
5503 ASSERT_EQ(0, motionArgs.edgeFlags);
5504 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5505 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5508 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5509 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5510 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5511 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5512
5513 // Should not have sent any more keys or motions.
5514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5516}
5517
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005518TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519 addConfigurationProperty("touch.deviceType", "touchScreen");
5520 prepareButtons();
5521 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005522 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5523 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005524 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005525
5526 NotifyMotionArgs args;
5527
5528 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005529 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005530 processDown(mapper, toRawX(50), toRawY(75));
5531 processSync(mapper);
5532
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5534 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5535 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5536
5537 processUp(mapper);
5538 processSync(mapper);
5539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5540}
5541
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005542TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543 addConfigurationProperty("touch.deviceType", "touchScreen");
5544 prepareButtons();
5545 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005546 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5547 // orientation-aware are affected by display rotation.
5548 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005549 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005550
5551 NotifyMotionArgs args;
5552
5553 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005554 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005555 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556 processDown(mapper, toRawX(50), toRawY(75));
5557 processSync(mapper);
5558
5559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5560 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5561 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5562
5563 processUp(mapper);
5564 processSync(mapper);
5565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5566
5567 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005568 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005569 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005570 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005571 processSync(mapper);
5572
5573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5574 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5575 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5576
5577 processUp(mapper);
5578 processSync(mapper);
5579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5580
5581 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005582 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005583 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5585 processSync(mapper);
5586
5587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5588 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5589 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5590
5591 processUp(mapper);
5592 processSync(mapper);
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5594
5595 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005596 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005597 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005598 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599 processSync(mapper);
5600
5601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5602 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5603 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5604
5605 processUp(mapper);
5606 processSync(mapper);
5607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5608}
5609
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005610TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5611 addConfigurationProperty("touch.deviceType", "touchScreen");
5612 prepareButtons();
5613 prepareAxes(POSITION);
5614 addConfigurationProperty("touch.orientationAware", "1");
5615 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5616 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005617 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005618 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5619 NotifyMotionArgs args;
5620
5621 // Orientation 0.
5622 processDown(mapper, toRawX(50), toRawY(75));
5623 processSync(mapper);
5624
5625 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5626 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5627 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5628
5629 processUp(mapper);
5630 processSync(mapper);
5631 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5632}
5633
5634TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5635 addConfigurationProperty("touch.deviceType", "touchScreen");
5636 prepareButtons();
5637 prepareAxes(POSITION);
5638 addConfigurationProperty("touch.orientationAware", "1");
5639 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5640 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005641 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005642 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5643 NotifyMotionArgs args;
5644
5645 // Orientation 90.
5646 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5647 processSync(mapper);
5648
5649 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5650 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5651 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5652
5653 processUp(mapper);
5654 processSync(mapper);
5655 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5656}
5657
5658TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5659 addConfigurationProperty("touch.deviceType", "touchScreen");
5660 prepareButtons();
5661 prepareAxes(POSITION);
5662 addConfigurationProperty("touch.orientationAware", "1");
5663 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5664 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005665 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005666 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5667 NotifyMotionArgs args;
5668
5669 // Orientation 180.
5670 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5671 processSync(mapper);
5672
5673 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5674 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5675 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5676
5677 processUp(mapper);
5678 processSync(mapper);
5679 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5680}
5681
5682TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5683 addConfigurationProperty("touch.deviceType", "touchScreen");
5684 prepareButtons();
5685 prepareAxes(POSITION);
5686 addConfigurationProperty("touch.orientationAware", "1");
5687 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5688 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005689 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005690 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5691 NotifyMotionArgs args;
5692
5693 // Orientation 270.
5694 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5695 processSync(mapper);
5696
5697 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5698 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5699 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5700
5701 processUp(mapper);
5702 processSync(mapper);
5703 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5704}
5705
5706TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5707 addConfigurationProperty("touch.deviceType", "touchScreen");
5708 prepareButtons();
5709 prepareAxes(POSITION);
5710 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5711 // orientation-aware are affected by display rotation.
5712 addConfigurationProperty("touch.orientationAware", "0");
5713 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5714 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5715
5716 NotifyMotionArgs args;
5717
5718 // Orientation 90, Rotation 0.
5719 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005720 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005721 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5722 processSync(mapper);
5723
5724 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5725 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5726 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5727
5728 processUp(mapper);
5729 processSync(mapper);
5730 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5731
5732 // Orientation 90, Rotation 90.
5733 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005734 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005735 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005736 processSync(mapper);
5737
5738 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5739 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5740 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5741
5742 processUp(mapper);
5743 processSync(mapper);
5744 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5745
5746 // Orientation 90, Rotation 180.
5747 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005748 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005749 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5750 processSync(mapper);
5751
5752 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5753 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5754 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5755
5756 processUp(mapper);
5757 processSync(mapper);
5758 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5759
5760 // Orientation 90, Rotation 270.
5761 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005762 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005763 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 -07005764 processSync(mapper);
5765
5766 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5767 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5768 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5769
5770 processUp(mapper);
5771 processSync(mapper);
5772 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5773}
5774
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005775TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5776 addConfigurationProperty("touch.deviceType", "touchScreen");
5777 prepareButtons();
5778 prepareAxes(POSITION);
5779 addConfigurationProperty("touch.orientationAware", "1");
5780 prepareDisplay(ui::ROTATION_0);
5781 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5782
5783 // Set a physical frame in the display viewport.
5784 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5785 viewport->physicalLeft = 20;
5786 viewport->physicalTop = 600;
5787 viewport->physicalRight = 30;
5788 viewport->physicalBottom = 610;
5789 mFakePolicy->updateViewport(*viewport);
5790 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5791
5792 // Start the touch.
5793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5794 processSync(mapper);
5795
5796 // Expect all input starting outside the physical frame to be ignored.
5797 const std::array<Point, 6> outsidePoints = {
5798 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5799 for (const auto& p : outsidePoints) {
5800 processMove(mapper, toRawX(p.x), toRawY(p.y));
5801 processSync(mapper);
5802 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5803 }
5804
5805 // Move the touch into the physical frame.
5806 processMove(mapper, toRawX(25), toRawY(605));
5807 processSync(mapper);
5808 NotifyMotionArgs args;
5809 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5810 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5811 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5812 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5813
5814 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5815 for (const auto& p : outsidePoints) {
5816 processMove(mapper, toRawX(p.x), toRawY(p.y));
5817 processSync(mapper);
5818 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5819 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5820 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5821 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5822 }
5823
5824 processUp(mapper);
5825 processSync(mapper);
5826 EXPECT_NO_FATAL_FAILURE(
5827 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5828}
5829
Michael Wrightd02c5b62014-02-10 15:10:22 -08005830TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005831 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005832 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005833 prepareButtons();
5834 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005835 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005836
5837 // These calculations are based on the input device calibration documentation.
5838 int32_t rawX = 100;
5839 int32_t rawY = 200;
5840 int32_t rawPressure = 10;
5841 int32_t rawToolMajor = 12;
5842 int32_t rawDistance = 2;
5843 int32_t rawTiltX = 30;
5844 int32_t rawTiltY = 110;
5845
5846 float x = toDisplayX(rawX);
5847 float y = toDisplayY(rawY);
5848 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5849 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5850 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5851 float distance = float(rawDistance);
5852
5853 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5854 float tiltScale = M_PI / 180;
5855 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5856 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5857 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5858 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5859
5860 processDown(mapper, rawX, rawY);
5861 processPressure(mapper, rawPressure);
5862 processToolMajor(mapper, rawToolMajor);
5863 processDistance(mapper, rawDistance);
5864 processTilt(mapper, rawTiltX, rawTiltY);
5865 processSync(mapper);
5866
5867 NotifyMotionArgs args;
5868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5870 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5871 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5872}
5873
Jason Gerecke489fda82012-09-07 17:19:40 -07005874TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005875 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005876 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005877 prepareLocationCalibration();
5878 prepareButtons();
5879 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005880 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005881
5882 int32_t rawX = 100;
5883 int32_t rawY = 200;
5884
5885 float x = toDisplayX(toCookedX(rawX, rawY));
5886 float y = toDisplayY(toCookedY(rawX, rawY));
5887
5888 processDown(mapper, rawX, rawY);
5889 processSync(mapper);
5890
5891 NotifyMotionArgs args;
5892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5894 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5895}
5896
Michael Wrightd02c5b62014-02-10 15:10:22 -08005897TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005898 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005899 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005900 prepareButtons();
5901 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005902 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005903
5904 NotifyMotionArgs motionArgs;
5905 NotifyKeyArgs keyArgs;
5906
5907 processDown(mapper, 100, 200);
5908 processSync(mapper);
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5910 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5911 ASSERT_EQ(0, motionArgs.buttonState);
5912
5913 // press BTN_LEFT, release BTN_LEFT
5914 processKey(mapper, BTN_LEFT, 1);
5915 processSync(mapper);
5916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5918 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5919
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5921 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5922 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5923
Michael Wrightd02c5b62014-02-10 15:10:22 -08005924 processKey(mapper, BTN_LEFT, 0);
5925 processSync(mapper);
5926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005927 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005928 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005929
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005931 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005932 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005933
5934 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5935 processKey(mapper, BTN_RIGHT, 1);
5936 processKey(mapper, BTN_MIDDLE, 1);
5937 processSync(mapper);
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5940 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5941 motionArgs.buttonState);
5942
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5944 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5945 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5946
5947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5948 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5949 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5950 motionArgs.buttonState);
5951
Michael Wrightd02c5b62014-02-10 15:10:22 -08005952 processKey(mapper, BTN_RIGHT, 0);
5953 processSync(mapper);
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005955 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005956 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005957
5958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005960 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005961
5962 processKey(mapper, BTN_MIDDLE, 0);
5963 processSync(mapper);
5964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005965 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005966 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005967
5968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005970 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005971
5972 // press BTN_BACK, release BTN_BACK
5973 processKey(mapper, BTN_BACK, 1);
5974 processSync(mapper);
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5976 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5977 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005978
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005981 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5984 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5985 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005986
5987 processKey(mapper, BTN_BACK, 0);
5988 processSync(mapper);
5989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005990 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005991 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005992
5993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005995 ASSERT_EQ(0, motionArgs.buttonState);
5996
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5998 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5999 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6000
6001 // press BTN_SIDE, release BTN_SIDE
6002 processKey(mapper, BTN_SIDE, 1);
6003 processSync(mapper);
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6005 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6006 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006007
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006010 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6011
6012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6013 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6014 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006015
6016 processKey(mapper, BTN_SIDE, 0);
6017 processSync(mapper);
6018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006019 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006020 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006021
6022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006024 ASSERT_EQ(0, motionArgs.buttonState);
6025
Michael Wrightd02c5b62014-02-10 15:10:22 -08006026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6027 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6028 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6029
6030 // press BTN_FORWARD, release BTN_FORWARD
6031 processKey(mapper, BTN_FORWARD, 1);
6032 processSync(mapper);
6033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6034 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6035 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006036
Michael Wrightd02c5b62014-02-10 15:10:22 -08006037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006039 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6040
6041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6042 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6043 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044
6045 processKey(mapper, BTN_FORWARD, 0);
6046 processSync(mapper);
6047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006048 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006049 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006050
6051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006053 ASSERT_EQ(0, motionArgs.buttonState);
6054
Michael Wrightd02c5b62014-02-10 15:10:22 -08006055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6056 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6057 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6058
6059 // press BTN_EXTRA, release BTN_EXTRA
6060 processKey(mapper, BTN_EXTRA, 1);
6061 processSync(mapper);
6062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6063 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6064 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006065
Michael Wrightd02c5b62014-02-10 15:10:22 -08006066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006068 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6069
6070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6071 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6072 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073
6074 processKey(mapper, BTN_EXTRA, 0);
6075 processSync(mapper);
6076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006077 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006078 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006079
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006082 ASSERT_EQ(0, motionArgs.buttonState);
6083
Michael Wrightd02c5b62014-02-10 15:10:22 -08006084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6085 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6086 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6087
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6089
Michael Wrightd02c5b62014-02-10 15:10:22 -08006090 // press BTN_STYLUS, release BTN_STYLUS
6091 processKey(mapper, BTN_STYLUS, 1);
6092 processSync(mapper);
6093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006095 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6096
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6099 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100
6101 processKey(mapper, BTN_STYLUS, 0);
6102 processSync(mapper);
6103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006104 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006105 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006106
6107 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(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006110
6111 // press BTN_STYLUS2, release BTN_STYLUS2
6112 processKey(mapper, BTN_STYLUS2, 1);
6113 processSync(mapper);
6114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006116 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6117
6118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6119 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6120 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121
6122 processKey(mapper, BTN_STYLUS2, 0);
6123 processSync(mapper);
6124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006127
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006130 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006131
6132 // release touch
6133 processUp(mapper);
6134 processSync(mapper);
6135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6136 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6137 ASSERT_EQ(0, motionArgs.buttonState);
6138}
6139
6140TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006142 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006143 prepareButtons();
6144 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006145 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146
6147 NotifyMotionArgs motionArgs;
6148
6149 // default tool type is finger
6150 processDown(mapper, 100, 200);
6151 processSync(mapper);
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6153 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6155
6156 // eraser
6157 processKey(mapper, BTN_TOOL_RUBBER, 1);
6158 processSync(mapper);
6159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6160 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6161 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6162
6163 // stylus
6164 processKey(mapper, BTN_TOOL_RUBBER, 0);
6165 processKey(mapper, BTN_TOOL_PEN, 1);
6166 processSync(mapper);
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6170
6171 // brush
6172 processKey(mapper, BTN_TOOL_PEN, 0);
6173 processKey(mapper, BTN_TOOL_BRUSH, 1);
6174 processSync(mapper);
6175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6177 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6178
6179 // pencil
6180 processKey(mapper, BTN_TOOL_BRUSH, 0);
6181 processKey(mapper, BTN_TOOL_PENCIL, 1);
6182 processSync(mapper);
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6186
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006187 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006188 processKey(mapper, BTN_TOOL_PENCIL, 0);
6189 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6190 processSync(mapper);
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6193 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6194
6195 // mouse
6196 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6197 processKey(mapper, BTN_TOOL_MOUSE, 1);
6198 processSync(mapper);
6199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6202
6203 // lens
6204 processKey(mapper, BTN_TOOL_MOUSE, 0);
6205 processKey(mapper, BTN_TOOL_LENS, 1);
6206 processSync(mapper);
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6210
6211 // double-tap
6212 processKey(mapper, BTN_TOOL_LENS, 0);
6213 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6214 processSync(mapper);
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6218
6219 // triple-tap
6220 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6221 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6222 processSync(mapper);
6223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6226
6227 // quad-tap
6228 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6229 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6230 processSync(mapper);
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6233 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6234
6235 // finger
6236 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6237 processKey(mapper, BTN_TOOL_FINGER, 1);
6238 processSync(mapper);
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6241 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6242
6243 // stylus trumps finger
6244 processKey(mapper, BTN_TOOL_PEN, 1);
6245 processSync(mapper);
6246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6249
6250 // eraser trumps stylus
6251 processKey(mapper, BTN_TOOL_RUBBER, 1);
6252 processSync(mapper);
6253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6255 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6256
6257 // mouse trumps eraser
6258 processKey(mapper, BTN_TOOL_MOUSE, 1);
6259 processSync(mapper);
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6263
6264 // back to default tool type
6265 processKey(mapper, BTN_TOOL_MOUSE, 0);
6266 processKey(mapper, BTN_TOOL_RUBBER, 0);
6267 processKey(mapper, BTN_TOOL_PEN, 0);
6268 processKey(mapper, BTN_TOOL_FINGER, 0);
6269 processSync(mapper);
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6273}
6274
6275TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006276 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006277 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278 prepareButtons();
6279 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006280 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006281 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282
6283 NotifyMotionArgs motionArgs;
6284
6285 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6286 processKey(mapper, BTN_TOOL_FINGER, 1);
6287 processMove(mapper, 100, 200);
6288 processSync(mapper);
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6290 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6292 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6293
6294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6295 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6297 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6298
6299 // move a little
6300 processMove(mapper, 150, 250);
6301 processSync(mapper);
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6303 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6305 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6306
6307 // down when BTN_TOUCH is pressed, pressure defaults to 1
6308 processKey(mapper, BTN_TOUCH, 1);
6309 processSync(mapper);
6310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6311 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6313 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6314
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6316 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6318 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6319
6320 // up when BTN_TOUCH is released, hover restored
6321 processKey(mapper, BTN_TOUCH, 0);
6322 processSync(mapper);
6323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6324 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6326 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6327
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6329 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6331 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6332
6333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6334 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6336 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6337
6338 // exit hover when pointer goes away
6339 processKey(mapper, BTN_TOOL_FINGER, 0);
6340 processSync(mapper);
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6342 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6344 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6345}
6346
6347TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006349 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006350 prepareButtons();
6351 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006352 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006353
6354 NotifyMotionArgs motionArgs;
6355
6356 // initially hovering because pressure is 0
6357 processDown(mapper, 100, 200);
6358 processPressure(mapper, 0);
6359 processSync(mapper);
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6361 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6363 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6364
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6366 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6368 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6369
6370 // move a little
6371 processMove(mapper, 150, 250);
6372 processSync(mapper);
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6374 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6376 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6377
6378 // down when pressure is non-zero
6379 processPressure(mapper, RAW_PRESSURE_MAX);
6380 processSync(mapper);
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6384 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6385
6386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6387 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6389 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6390
6391 // up when pressure becomes 0, hover restored
6392 processPressure(mapper, 0);
6393 processSync(mapper);
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6395 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6397 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6398
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6400 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6402 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6403
6404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6405 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6407 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6408
6409 // exit hover when pointer goes away
6410 processUp(mapper);
6411 processSync(mapper);
6412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6413 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6415 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6416}
6417
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006418TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6419 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006420 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006421 prepareButtons();
6422 prepareAxes(POSITION | PRESSURE);
6423 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6424
6425 // Touch down.
6426 processDown(mapper, 100, 200);
6427 processPressure(mapper, 1);
6428 processSync(mapper);
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6430 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6431
6432 // Reset the mapper. This should cancel the ongoing gesture.
6433 resetMapper(mapper, ARBITRARY_TIME);
6434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6435 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6436
6437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6438}
6439
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006440TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6441 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006442 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006443 prepareButtons();
6444 prepareAxes(POSITION | PRESSURE);
6445 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6446
6447 // Set the initial state for the touch pointer.
6448 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6449 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6450 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6451 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6452
6453 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006454 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6455 // does not generate any events.
6456 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006457
6458 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6459 // the recreated touch state to generate a down event.
6460 processSync(mapper);
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6462 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6463
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6465}
6466
lilinnan687e58f2022-07-19 16:00:50 +08006467TEST_F(SingleTouchInputMapperTest,
6468 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6469 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006470 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006471 prepareButtons();
6472 prepareAxes(POSITION);
6473 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6474 NotifyMotionArgs motionArgs;
6475
6476 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006477 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006478 processSync(mapper);
6479
6480 // We should receive a down event
6481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6482 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6483
6484 // Change display id
6485 clearViewports();
6486 prepareSecondaryDisplay(ViewportType::INTERNAL);
6487
6488 // We should receive a cancel event
6489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6490 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6491 // Then receive reset called
6492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6493}
6494
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006495TEST_F(SingleTouchInputMapperTest,
6496 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6497 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006498 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006499 prepareButtons();
6500 prepareAxes(POSITION);
6501 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6503 NotifyMotionArgs motionArgs;
6504
6505 // Start a new gesture.
6506 processDown(mapper, 100, 200);
6507 processSync(mapper);
6508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6509 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6510
6511 // Make the viewport inactive. This will put the device in disabled mode.
6512 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6513 viewport->isActive = false;
6514 mFakePolicy->updateViewport(*viewport);
6515 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6516
6517 // We should receive a cancel event for the ongoing gesture.
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6519 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6520 // Then we should be notified that the device was reset.
6521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6522
6523 // No events are generated while the viewport is inactive.
6524 processMove(mapper, 101, 201);
6525 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006526 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006527 processSync(mapper);
6528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6529
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006530 // Start a new gesture while the viewport is still inactive.
6531 processDown(mapper, 300, 400);
6532 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6533 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6534 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6535 processSync(mapper);
6536
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006537 // Make the viewport active again. The device should resume processing events.
6538 viewport->isActive = true;
6539 mFakePolicy->updateViewport(*viewport);
6540 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6541
6542 // The device is reset because it changes back to direct mode, without generating any events.
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6545
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006546 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006547 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6549 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006550
6551 // No more events.
6552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6554}
6555
Prabir Pradhan211ba622022-10-31 21:09:21 +00006556TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6557 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006558 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006559 prepareButtons();
6560 prepareAxes(POSITION);
6561 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6563
6564 // Press a stylus button.
6565 processKey(mapper, BTN_STYLUS, 1);
6566 processSync(mapper);
6567
6568 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6569 processDown(mapper, 100, 200);
6570 processSync(mapper);
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6572 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6573 WithCoords(toDisplayX(100), toDisplayY(200)),
6574 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6576 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6577 WithCoords(toDisplayX(100), toDisplayY(200)),
6578 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6579
6580 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6581 // the button has not actually been released, since there will be no pointers through which the
6582 // button state can be reported. The event is generated at the location of the pointer before
6583 // it went up.
6584 processUp(mapper);
6585 processSync(mapper);
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6587 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6588 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6590 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6591 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6592}
6593
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006594TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6595 addConfigurationProperty("touch.deviceType", "touchScreen");
6596 prepareDisplay(ui::ROTATION_0);
6597 prepareButtons();
6598 prepareAxes(POSITION);
6599
6600 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6601
6602 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6604
6605 // Press a stylus button.
6606 processKey(mapper, BTN_STYLUS, 1);
6607 processSync(mapper);
6608
6609 // Start a touch gesture and ensure that the stylus button is not reported.
6610 processDown(mapper, 100, 200);
6611 processSync(mapper);
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6613 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6614
6615 // Release and press the stylus button again.
6616 processKey(mapper, BTN_STYLUS, 0);
6617 processSync(mapper);
6618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6619 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6620 processKey(mapper, BTN_STYLUS, 1);
6621 processSync(mapper);
6622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6623 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6624
6625 // Release the touch gesture.
6626 processUp(mapper);
6627 processSync(mapper);
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6629 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6630
6631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6632}
6633
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006634TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6635 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6636 prepareDisplay(ui::ROTATION_0);
6637 prepareButtons();
6638 prepareAxes(POSITION);
6639 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6641
6642 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6643}
6644
Seunghwan Choi356026c2023-02-01 14:37:25 +09006645TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6646 std::shared_ptr<FakePointerController> fakePointerController =
6647 std::make_shared<FakePointerController>();
6648 addConfigurationProperty("touch.deviceType", "touchScreen");
6649 prepareDisplay(ui::ROTATION_0);
6650 prepareButtons();
6651 prepareAxes(POSITION);
6652 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6653 mFakePolicy->setPointerController(fakePointerController);
6654 mFakePolicy->setStylusPointerIconEnabled(true);
6655 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6656
6657 processKey(mapper, BTN_TOOL_PEN, 1);
6658 processMove(mapper, 100, 200);
6659 processSync(mapper);
6660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6661 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
6662 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
6663 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6664 ASSERT_TRUE(fakePointerController->isPointerShown());
6665 ASSERT_NO_FATAL_FAILURE(
6666 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6667}
6668
6669TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6670 std::shared_ptr<FakePointerController> fakePointerController =
6671 std::make_shared<FakePointerController>();
6672 addConfigurationProperty("touch.deviceType", "touchScreen");
6673 prepareDisplay(ui::ROTATION_0);
6674 prepareButtons();
6675 prepareAxes(POSITION);
6676 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6677 mFakePolicy->setPointerController(fakePointerController);
6678 mFakePolicy->setStylusPointerIconEnabled(false);
6679 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6680
6681 processKey(mapper, BTN_TOOL_PEN, 1);
6682 processMove(mapper, 100, 200);
6683 processSync(mapper);
6684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6685 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
6686 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
6687 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6688 ASSERT_FALSE(fakePointerController->isPointerShown());
6689}
6690
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006691TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6692 // Initialize the device without setting device source to touch navigation.
6693 addConfigurationProperty("touch.deviceType", "touchScreen");
6694 prepareDisplay(ui::ROTATION_0);
6695 prepareButtons();
6696 prepareAxes(POSITION);
6697 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6698
6699 // Ensure that the device is created as a touchscreen, not touch navigation.
6700 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6701
6702 // Add device type association after the device was created.
6703 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6704
6705 // Send update to the mapper.
6706 std::list<NotifyArgs> unused2 =
6707 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6708 InputReaderConfiguration::CHANGE_DEVICE_TYPE /*changes*/);
6709
6710 // Check whether device type update was successful.
6711 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6712}
6713
Prabir Pradhan5632d622021-09-06 07:57:20 -07006714// --- TouchDisplayProjectionTest ---
6715
6716class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6717public:
6718 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6719 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6720 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006721 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6722 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6723 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006724 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006725 auto rotatedWidth = naturalDisplayWidth;
6726 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006727 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006728 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006729 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006730 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006731 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006732 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006733 inverseRotationFlags = ui::Transform::ROT_180;
6734 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006735 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006736 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006737 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006738 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006739 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006740 inverseRotationFlags = ui::Transform::ROT_0;
6741 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006742 }
6743
Prabir Pradhana9df3162022-12-05 23:57:27 +00006744 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006745 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6746
6747 std::optional<DisplayViewport> internalViewport =
6748 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6749 DisplayViewport& v = *internalViewport;
6750 v.displayId = DISPLAY_ID;
6751 v.orientation = orientation;
6752
6753 v.logicalLeft = 0;
6754 v.logicalTop = 0;
6755 v.logicalRight = 100;
6756 v.logicalBottom = 100;
6757
6758 v.physicalLeft = rotatedPhysicalDisplay.left;
6759 v.physicalTop = rotatedPhysicalDisplay.top;
6760 v.physicalRight = rotatedPhysicalDisplay.right;
6761 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6762
Prabir Pradhana9df3162022-12-05 23:57:27 +00006763 v.deviceWidth = rotatedWidth;
6764 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006765
6766 v.isActive = true;
6767 v.uniqueId = UNIQUE_ID;
6768 v.type = ViewportType::INTERNAL;
6769 mFakePolicy->updateViewport(v);
6770 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6771 }
6772
6773 void assertReceivedMove(const Point& point) {
6774 NotifyMotionArgs motionArgs;
6775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6777 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6779 1, 0, 0, 0, 0, 0, 0, 0));
6780 }
6781};
6782
6783TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6784 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006785 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006786
6787 prepareButtons();
6788 prepareAxes(POSITION);
6789 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6790
6791 NotifyMotionArgs motionArgs;
6792
6793 // Configure the DisplayViewport such that the logical display maps to a subsection of
6794 // the display panel called the physical display. Here, the physical display is bounded by the
6795 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6796 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6797 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6798 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6799
Michael Wrighta9cf4192022-12-01 23:46:39 +00006800 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006801 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6802
6803 // Touches outside the physical display should be ignored, and should not generate any
6804 // events. Ensure touches at the following points that lie outside of the physical display
6805 // area do not generate any events.
6806 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6807 processDown(mapper, toRawX(point.x), toRawY(point.y));
6808 processSync(mapper);
6809 processUp(mapper);
6810 processSync(mapper);
6811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6812 << "Unexpected event generated for touch outside physical display at point: "
6813 << point.x << ", " << point.y;
6814 }
6815 }
6816}
6817
6818TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6819 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006820 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006821
6822 prepareButtons();
6823 prepareAxes(POSITION);
6824 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6825
6826 NotifyMotionArgs motionArgs;
6827
6828 // Configure the DisplayViewport such that the logical display maps to a subsection of
6829 // the display panel called the physical display. Here, the physical display is bounded by the
6830 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6831 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6832
Michael Wrighta9cf4192022-12-01 23:46:39 +00006833 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006834 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6835
6836 // Touches that start outside the physical display should be ignored until it enters the
6837 // physical display bounds, at which point it should generate a down event. Start a touch at
6838 // the point (5, 100), which is outside the physical display bounds.
6839 static const Point kOutsidePoint{5, 100};
6840 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6841 processSync(mapper);
6842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6843
6844 // Move the touch into the physical display area. This should generate a pointer down.
6845 processMove(mapper, toRawX(11), toRawY(21));
6846 processSync(mapper);
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6848 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6849 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6850 ASSERT_NO_FATAL_FAILURE(
6851 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6852
6853 // Move the touch inside the physical display area. This should generate a pointer move.
6854 processMove(mapper, toRawX(69), toRawY(159));
6855 processSync(mapper);
6856 assertReceivedMove({69, 159});
6857
6858 // Move outside the physical display area. Since the pointer is already down, this should
6859 // now continue generating events.
6860 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6861 processSync(mapper);
6862 assertReceivedMove(kOutsidePoint);
6863
6864 // Release. This should generate a pointer up.
6865 processUp(mapper);
6866 processSync(mapper);
6867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6868 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6870 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6871
6872 // Ensure no more events were generated.
6873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6875 }
6876}
6877
Prabir Pradhana9df3162022-12-05 23:57:27 +00006878// --- TouchscreenPrecisionTests ---
6879
6880// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6881// in various orientations and with different display rotations. We configure the touchscreen to
6882// have a higher resolution than that of the display by an integer scale factor in each axis so that
6883// we can enforce that coordinates match precisely as expected.
6884class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6885 public ::testing::WithParamInterface<ui::Rotation> {
6886public:
6887 void SetUp() override {
6888 SingleTouchInputMapperTest::SetUp();
6889
6890 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6891 // four times the resolution of the display in the Y axis.
6892 prepareButtons();
6893 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006894 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6895 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006896 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006897 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6898 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006899 }
6900
6901 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6902 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6903 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6904 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6905
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006906 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6907 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6908
6909 static const int32_t PRECISION_RAW_X_FLAT = 16;
6910 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6911
6912 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6913 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6914
Prabir Pradhana9df3162022-12-05 23:57:27 +00006915 static const std::array<Point, 4> kRawCorners;
6916};
6917
6918const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6919 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6920 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6921 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6922 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6923}};
6924
6925// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6926// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6927// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6928TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6929 enum class Orientation {
6930 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6931 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6932 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6933 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6934 ftl_last = ORIENTATION_270,
6935 };
6936 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6937 Orientation::ORIENTATION_270;
6938 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6939 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6940 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6941 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6942 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6943 };
6944
6945 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6946
6947 // Configure the touchscreen as being installed in the one of the four different orientations
6948 // relative to the display.
6949 addConfigurationProperty("touch.deviceType", "touchScreen");
6950 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6951 prepareDisplay(ui::ROTATION_0);
6952
6953 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6954
6955 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6956 // orientations of either 90 or 270) this means the display's natural resolution will be
6957 // flipped.
6958 const bool displayRotated =
6959 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6960 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6961 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6962 const Rect physicalFrame{0, 0, width, height};
6963 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6964
6965 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6966 const float expectedPrecisionX = displayRotated ? 4 : 2;
6967 const float expectedPrecisionY = displayRotated ? 2 : 4;
6968
6969 // Test all four corners.
6970 for (int i = 0; i < 4; i++) {
6971 const auto& raw = kRawCorners[i];
6972 processDown(mapper, raw.x, raw.y);
6973 processSync(mapper);
6974 const auto& expected = expectedPoints[i];
6975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6976 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6977 WithCoords(expected.x, expected.y),
6978 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6979 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6980 << "with touchscreen orientation "
6981 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6982 << expected.x << ", " << expected.y << ").";
6983 processUp(mapper);
6984 processSync(mapper);
6985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6986 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6987 WithCoords(expected.x, expected.y))));
6988 }
6989}
6990
Prabir Pradhan82687402022-12-06 01:32:53 +00006991TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6992 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6993 kMappedCorners = {
6994 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6995 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6996 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6997 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
6998 };
6999
7000 const ui::Rotation displayRotation = GetParam();
7001
7002 addConfigurationProperty("touch.deviceType", "touchScreen");
7003 prepareDisplay(displayRotation);
7004
7005 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7006
7007 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7008
7009 // Test all four corners.
7010 for (int i = 0; i < 4; i++) {
7011 const auto& expected = expectedPoints[i];
7012 const auto& raw = kRawCorners[i];
7013 processDown(mapper, raw.x, raw.y);
7014 processSync(mapper);
7015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7016 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7017 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7018 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7019 << "with display rotation " << ui::toCString(displayRotation)
7020 << ", expected point (" << expected.x << ", " << expected.y << ").";
7021 processUp(mapper);
7022 processSync(mapper);
7023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7024 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7025 WithCoords(expected.x, expected.y))));
7026 }
7027}
7028
Prabir Pradhan3e798762022-12-02 21:02:11 +00007029TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7030 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7031 kMappedCorners = {
7032 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7033 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7034 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7035 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7036 };
7037
7038 const ui::Rotation displayRotation = GetParam();
7039
7040 addConfigurationProperty("touch.deviceType", "touchScreen");
7041 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7042
7043 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7044
7045 // Ori 270, so width and height swapped
7046 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7047 prepareDisplay(displayRotation);
7048 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7049
7050 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7051
7052 // Test all four corners.
7053 for (int i = 0; i < 4; i++) {
7054 const auto& expected = expectedPoints[i];
7055 const auto& raw = kRawCorners[i];
7056 processDown(mapper, raw.x, raw.y);
7057 processSync(mapper);
7058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7059 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7060 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7061 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7062 << "with display rotation " << ui::toCString(displayRotation)
7063 << ", expected point (" << expected.x << ", " << expected.y << ").";
7064 processUp(mapper);
7065 processSync(mapper);
7066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7067 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7068 WithCoords(expected.x, expected.y))));
7069 }
7070}
7071
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007072TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7073 const ui::Rotation displayRotation = GetParam();
7074
7075 addConfigurationProperty("touch.deviceType", "touchScreen");
7076 prepareDisplay(displayRotation);
7077
7078 __attribute__((unused)) SingleTouchInputMapper& mapper =
7079 addMapperAndConfigure<SingleTouchInputMapper>();
7080
7081 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7082 // MotionRanges use display pixels as their units
7083 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7084 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7085
7086 // The MotionRanges should be oriented in the rotated display's coordinate space
7087 const bool displayRotated =
7088 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7089
7090 constexpr float MAX_X = 479.5;
7091 constexpr float MAX_Y = 799.75;
7092 EXPECT_EQ(xRange->min, 0.f);
7093 EXPECT_EQ(yRange->min, 0.f);
7094 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7095 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7096
7097 EXPECT_EQ(xRange->flat, 8.f);
7098 EXPECT_EQ(yRange->flat, 8.f);
7099
7100 EXPECT_EQ(xRange->fuzz, 2.f);
7101 EXPECT_EQ(yRange->fuzz, 2.f);
7102
7103 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7104 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7105}
7106
Prabir Pradhana9df3162022-12-05 23:57:27 +00007107// Run the precision tests for all rotations.
7108INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7109 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7110 ui::ROTATION_270),
7111 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7112 return ftl::enum_string(testParamInfo.param);
7113 });
7114
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007115// --- ExternalStylusFusionTest ---
7116
7117class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7118public:
7119 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7120 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007121 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007122 prepareButtons();
7123 prepareAxes(POSITION);
7124 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7125
7126 mStylusState.when = ARBITRARY_TIME;
7127 mStylusState.pressure = 0.f;
7128 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7129 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
7130 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
7131 processExternalStylusState(mapper);
7132 return mapper;
7133 }
7134
7135 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7136 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7137 for (const NotifyArgs& args : generatedArgs) {
7138 mFakeListener->notify(args);
7139 }
7140 // Loop the reader to flush the input listener queue.
7141 mReader->loopOnce();
7142 return generatedArgs;
7143 }
7144
7145protected:
7146 StylusState mStylusState{};
7147 static constexpr uint32_t EXPECTED_SOURCE =
7148 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7149
7150 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7151 auto toolTypeSource =
7152 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7153
7154 // The first pointer is withheld.
7155 processDown(mapper, 100, 200);
7156 processSync(mapper);
7157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7158 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7159 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7160
7161 // The external stylus reports pressure. The withheld finger pointer is released as a
7162 // stylus.
7163 mStylusState.pressure = 1.f;
7164 processExternalStylusState(mapper);
7165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7166 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7167 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7168
7169 // Subsequent pointer events are not withheld.
7170 processMove(mapper, 101, 201);
7171 processSync(mapper);
7172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7173 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7174
7175 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7177 }
7178
7179 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7180 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7181
7182 // Releasing the touch pointer ends the gesture.
7183 processUp(mapper);
7184 processSync(mapper);
7185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7186 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7187 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7188
7189 mStylusState.pressure = 0.f;
7190 processExternalStylusState(mapper);
7191 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7193 }
7194
7195 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7196 auto toolTypeSource =
7197 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
7198
7199 // The first pointer is withheld when an external stylus is connected,
7200 // and a timeout is requested.
7201 processDown(mapper, 100, 200);
7202 processSync(mapper);
7203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7204 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7205 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7206
7207 // If the timeout expires early, it is requested again.
7208 handleTimeout(mapper, ARBITRARY_TIME + 1);
7209 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7210 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7211
7212 // When the timeout expires, the withheld touch is released as a finger pointer.
7213 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7215 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7216
7217 // Subsequent pointer events are not withheld.
7218 processMove(mapper, 101, 201);
7219 processSync(mapper);
7220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7221 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7222 processUp(mapper);
7223 processSync(mapper);
7224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7225 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7226
7227 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7229 }
7230
7231private:
7232 InputDeviceInfo mExternalStylusDeviceInfo{};
7233};
7234
7235TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7236 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7237 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7238}
7239
7240TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7241 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7242 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7243}
7244
7245TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7246 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7247 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7248}
7249
7250// Test a successful stylus fusion gesture where the pressure is reported by the external
7251// before the touch is reported by the touchscreen.
7252TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7253 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7254 auto toolTypeSource =
7255 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7256
7257 // The external stylus reports pressure first. It is ignored for now.
7258 mStylusState.pressure = 1.f;
7259 processExternalStylusState(mapper);
7260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7261 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7262
7263 // When the touch goes down afterwards, it is reported as a stylus pointer.
7264 processDown(mapper, 100, 200);
7265 processSync(mapper);
7266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7267 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7268 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7269
7270 processMove(mapper, 101, 201);
7271 processSync(mapper);
7272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7273 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7274 processUp(mapper);
7275 processSync(mapper);
7276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7277 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7278
7279 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7281}
7282
7283TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7284 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7285
7286 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7287 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7288
7289 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7290 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7291 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7292 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7293}
7294
7295TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7296 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7297 auto toolTypeSource =
7298 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7299
7300 mStylusState.pressure = 0.8f;
7301 processExternalStylusState(mapper);
7302 processDown(mapper, 100, 200);
7303 processSync(mapper);
7304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7305 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7306 WithPressure(0.8f))));
7307 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7308
7309 // The external stylus reports a pressure change. We wait for some time for a touch event.
7310 mStylusState.pressure = 0.6f;
7311 processExternalStylusState(mapper);
7312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7313 ASSERT_NO_FATAL_FAILURE(
7314 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7315
7316 // If a touch is reported within the timeout, it reports the updated pressure.
7317 processMove(mapper, 101, 201);
7318 processSync(mapper);
7319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7320 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7321 WithPressure(0.6f))));
7322 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7323
7324 // There is another pressure change.
7325 mStylusState.pressure = 0.5f;
7326 processExternalStylusState(mapper);
7327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7328 ASSERT_NO_FATAL_FAILURE(
7329 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7330
7331 // If a touch is not reported within the timeout, a move event is generated to report
7332 // the new pressure.
7333 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7335 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7336 WithPressure(0.5f))));
7337
7338 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7339 // repeated indefinitely.
7340 mStylusState.pressure = 0.0f;
7341 processExternalStylusState(mapper);
7342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7343 ASSERT_NO_FATAL_FAILURE(
7344 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7345 processMove(mapper, 102, 202);
7346 processSync(mapper);
7347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7348 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7349 WithPressure(0.5f))));
7350 processMove(mapper, 103, 203);
7351 processSync(mapper);
7352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7353 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7354 WithPressure(0.5f))));
7355
7356 processUp(mapper);
7357 processSync(mapper);
7358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7359 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7360 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7361
7362 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7364}
7365
7366TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7367 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7368 auto source = WithSource(EXPECTED_SOURCE);
7369
7370 mStylusState.pressure = 1.f;
7371 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
7372 processExternalStylusState(mapper);
7373 processDown(mapper, 100, 200);
7374 processSync(mapper);
7375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7376 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7377 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
7378 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7379
7380 // The external stylus reports a tool change. We wait for some time for a touch event.
7381 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7382 processExternalStylusState(mapper);
7383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7384 ASSERT_NO_FATAL_FAILURE(
7385 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7386
7387 // If a touch is reported within the timeout, it reports the updated pressure.
7388 processMove(mapper, 101, 201);
7389 processSync(mapper);
7390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7391 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7392 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7393 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7394
7395 // There is another tool type change.
7396 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
7397 processExternalStylusState(mapper);
7398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7399 ASSERT_NO_FATAL_FAILURE(
7400 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7401
7402 // If a touch is not reported within the timeout, a move event is generated to report
7403 // the new tool type.
7404 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7406 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7407 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7408
7409 processUp(mapper);
7410 processSync(mapper);
7411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7412 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
7413 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7414
7415 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7417}
7418
7419TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7420 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7421 auto toolTypeSource =
7422 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7423
7424 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7425
7426 // The external stylus reports a button change. We wait for some time for a touch event.
7427 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7428 processExternalStylusState(mapper);
7429 ASSERT_NO_FATAL_FAILURE(
7430 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7431
7432 // If a touch is reported within the timeout, it reports the updated button state.
7433 processMove(mapper, 101, 201);
7434 processSync(mapper);
7435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7436 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7437 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7439 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7440 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7441 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7442
7443 // The button is now released.
7444 mStylusState.buttons = 0;
7445 processExternalStylusState(mapper);
7446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7447 ASSERT_NO_FATAL_FAILURE(
7448 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7449
7450 // If a touch is not reported within the timeout, a move event is generated to report
7451 // the new button state.
7452 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7454 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7455 WithButtonState(0))));
7456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007457 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7458 WithButtonState(0))));
7459
7460 processUp(mapper);
7461 processSync(mapper);
7462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007463 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7464
7465 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7467}
7468
Michael Wrightd02c5b62014-02-10 15:10:22 -08007469// --- MultiTouchInputMapperTest ---
7470
7471class MultiTouchInputMapperTest : public TouchInputMapperTest {
7472protected:
7473 void prepareAxes(int axes);
7474
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007475 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7476 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7477 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7478 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7479 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7480 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7481 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7482 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7483 void processId(MultiTouchInputMapper& mapper, int32_t id);
7484 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7485 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7486 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007487 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007488 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007489 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7490 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007491};
7492
7493void MultiTouchInputMapperTest::prepareAxes(int axes) {
7494 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007495 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7496 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007497 }
7498 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007499 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7500 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007501 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007502 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7503 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007504 }
7505 }
7506 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007507 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7508 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007509 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007510 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007511 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007512 }
7513 }
7514 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007515 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7516 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007517 }
7518 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007519 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7520 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007521 }
7522 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007523 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7524 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007525 }
7526 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007527 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7528 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007529 }
7530 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007531 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7532 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007533 }
7534 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007535 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007536 }
7537}
7538
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007539void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7540 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007543}
7544
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007545void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7546 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007548}
7549
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007550void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7551 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007553}
7554
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007555void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007557}
7558
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007559void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007561}
7562
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007563void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7564 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007566}
7567
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007568void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007570}
7571
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007572void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007573 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007574}
7575
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007576void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007578}
7579
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007580void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007581 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007582}
7583
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007584void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007586}
7587
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007588void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7589 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007590 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007591}
7592
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007593void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7594 int32_t value) {
7595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7597}
7598
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007599void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007600 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007601}
7602
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007603void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7604 nsecs_t readTime) {
7605 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007606}
7607
Michael Wrightd02c5b62014-02-10 15:10:22 -08007608TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007609 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007610 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007611 prepareAxes(POSITION);
7612 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007613 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007614
arthurhungdcef2dc2020-08-11 14:47:50 +08007615 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007616
7617 NotifyMotionArgs motionArgs;
7618
7619 // Two fingers down at once.
7620 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7621 processPosition(mapper, x1, y1);
7622 processMTSync(mapper);
7623 processPosition(mapper, x2, y2);
7624 processMTSync(mapper);
7625 processSync(mapper);
7626
7627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7628 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7629 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7630 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7631 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7632 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7633 ASSERT_EQ(0, motionArgs.flags);
7634 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7635 ASSERT_EQ(0, motionArgs.buttonState);
7636 ASSERT_EQ(0, motionArgs.edgeFlags);
7637 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7638 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7641 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7642 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7643 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7644 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7645
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7647 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7648 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7649 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7650 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007651 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007652 ASSERT_EQ(0, motionArgs.flags);
7653 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7654 ASSERT_EQ(0, motionArgs.buttonState);
7655 ASSERT_EQ(0, motionArgs.edgeFlags);
7656 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7657 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7659 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7660 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7662 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7664 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7665 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7666 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7667 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7668
7669 // Move.
7670 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7671 processPosition(mapper, x1, y1);
7672 processMTSync(mapper);
7673 processPosition(mapper, x2, y2);
7674 processMTSync(mapper);
7675 processSync(mapper);
7676
7677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7678 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7679 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7680 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7681 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7682 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7683 ASSERT_EQ(0, motionArgs.flags);
7684 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7685 ASSERT_EQ(0, motionArgs.buttonState);
7686 ASSERT_EQ(0, motionArgs.edgeFlags);
7687 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7688 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7689 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7690 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7691 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7693 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7695 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7696 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7697 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7698 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7699
7700 // First finger up.
7701 x2 += 15; y2 -= 20;
7702 processPosition(mapper, x2, y2);
7703 processMTSync(mapper);
7704 processSync(mapper);
7705
7706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7707 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7708 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7709 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7710 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007711 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007712 ASSERT_EQ(0, motionArgs.flags);
7713 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7714 ASSERT_EQ(0, motionArgs.buttonState);
7715 ASSERT_EQ(0, motionArgs.edgeFlags);
7716 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7717 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7719 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7722 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7724 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7725 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7726 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7727 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7728
7729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7730 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7731 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7732 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7733 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7734 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7735 ASSERT_EQ(0, motionArgs.flags);
7736 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7737 ASSERT_EQ(0, motionArgs.buttonState);
7738 ASSERT_EQ(0, motionArgs.edgeFlags);
7739 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7740 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7741 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7743 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7744 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7745 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7746 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7747
7748 // Move.
7749 x2 += 20; y2 -= 25;
7750 processPosition(mapper, x2, y2);
7751 processMTSync(mapper);
7752 processSync(mapper);
7753
7754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7755 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7756 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7757 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7758 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7760 ASSERT_EQ(0, motionArgs.flags);
7761 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7762 ASSERT_EQ(0, motionArgs.buttonState);
7763 ASSERT_EQ(0, motionArgs.edgeFlags);
7764 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7765 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7766 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7768 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7769 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7770 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7771 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7772
7773 // New finger down.
7774 int32_t x3 = 700, y3 = 300;
7775 processPosition(mapper, x2, y2);
7776 processMTSync(mapper);
7777 processPosition(mapper, x3, y3);
7778 processMTSync(mapper);
7779 processSync(mapper);
7780
7781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7782 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7783 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7784 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7785 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007786 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007787 ASSERT_EQ(0, motionArgs.flags);
7788 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7789 ASSERT_EQ(0, motionArgs.buttonState);
7790 ASSERT_EQ(0, motionArgs.edgeFlags);
7791 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7792 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7793 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7794 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7797 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7799 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7800 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7801 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7802 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7803
7804 // Second finger up.
7805 x3 += 30; y3 -= 20;
7806 processPosition(mapper, x3, y3);
7807 processMTSync(mapper);
7808 processSync(mapper);
7809
7810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7811 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7812 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7813 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7814 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007815 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007816 ASSERT_EQ(0, motionArgs.flags);
7817 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7818 ASSERT_EQ(0, motionArgs.buttonState);
7819 ASSERT_EQ(0, motionArgs.edgeFlags);
7820 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7821 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7822 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7823 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7824 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7826 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7828 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7829 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7830 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7831 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7832
7833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7834 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7835 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7836 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7837 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7839 ASSERT_EQ(0, motionArgs.flags);
7840 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7841 ASSERT_EQ(0, motionArgs.buttonState);
7842 ASSERT_EQ(0, motionArgs.edgeFlags);
7843 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7844 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7845 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7847 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7848 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7849 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7850 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7851
7852 // Last finger up.
7853 processMTSync(mapper);
7854 processSync(mapper);
7855
7856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7857 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7858 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7859 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7860 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7861 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7862 ASSERT_EQ(0, motionArgs.flags);
7863 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7864 ASSERT_EQ(0, motionArgs.buttonState);
7865 ASSERT_EQ(0, motionArgs.edgeFlags);
7866 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7867 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7868 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7870 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7871 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7872 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7873 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7874
7875 // Should not have sent any more keys or motions.
7876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7878}
7879
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007880TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7881 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007882 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007883
7884 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7885 /*fuzz*/ 0, /*resolution*/ 10);
7886 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7887 /*fuzz*/ 0, /*resolution*/ 11);
7888 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7889 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7890 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7891 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7892 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7893 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7894 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7895 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7896
7897 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7898
7899 // X and Y axes
7900 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7901 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7902 // Touch major and minor
7903 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7904 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7905 // Tool major and minor
7906 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7907 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7908}
7909
7910TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7911 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007912 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007913
7914 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7915 /*fuzz*/ 0, /*resolution*/ 10);
7916 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7917 /*fuzz*/ 0, /*resolution*/ 11);
7918
7919 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7920
7921 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7922
7923 // Touch major and minor
7924 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7925 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7926 // Tool major and minor
7927 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7928 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7929}
7930
Michael Wrightd02c5b62014-02-10 15:10:22 -08007931TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007932 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007933 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007934 prepareAxes(POSITION | ID);
7935 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007936 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007937
arthurhungdcef2dc2020-08-11 14:47:50 +08007938 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007939
7940 NotifyMotionArgs motionArgs;
7941
7942 // Two fingers down at once.
7943 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7944 processPosition(mapper, x1, y1);
7945 processId(mapper, 1);
7946 processMTSync(mapper);
7947 processPosition(mapper, x2, y2);
7948 processId(mapper, 2);
7949 processMTSync(mapper);
7950 processSync(mapper);
7951
7952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7953 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7954 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7955 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7956 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7957 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7958 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7959
7960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007961 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007962 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7963 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7964 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7965 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7966 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7968 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7970 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7971
7972 // Move.
7973 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7974 processPosition(mapper, x1, y1);
7975 processId(mapper, 1);
7976 processMTSync(mapper);
7977 processPosition(mapper, x2, y2);
7978 processId(mapper, 2);
7979 processMTSync(mapper);
7980 processSync(mapper);
7981
7982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7984 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7985 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7986 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7987 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7988 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7990 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7992 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7993
7994 // First finger up.
7995 x2 += 15; y2 -= 20;
7996 processPosition(mapper, x2, y2);
7997 processId(mapper, 2);
7998 processMTSync(mapper);
7999 processSync(mapper);
8000
8001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008002 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008003 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8004 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8005 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8006 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8007 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8009 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8010 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8011 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8012
8013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8015 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8016 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8017 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8019 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8020
8021 // Move.
8022 x2 += 20; y2 -= 25;
8023 processPosition(mapper, x2, y2);
8024 processId(mapper, 2);
8025 processMTSync(mapper);
8026 processSync(mapper);
8027
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8029 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8030 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8031 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8032 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8034 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8035
8036 // New finger down.
8037 int32_t x3 = 700, y3 = 300;
8038 processPosition(mapper, x2, y2);
8039 processId(mapper, 2);
8040 processMTSync(mapper);
8041 processPosition(mapper, x3, y3);
8042 processId(mapper, 3);
8043 processMTSync(mapper);
8044 processSync(mapper);
8045
8046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008047 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008048 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8049 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8051 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8052 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8053 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8054 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8056 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8057
8058 // Second finger up.
8059 x3 += 30; y3 -= 20;
8060 processPosition(mapper, x3, y3);
8061 processId(mapper, 3);
8062 processMTSync(mapper);
8063 processSync(mapper);
8064
8065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008066 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008067 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8068 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8070 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8073 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8075 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8076
8077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8078 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8079 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8080 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8081 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8082 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8083 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8084
8085 // Last finger up.
8086 processMTSync(mapper);
8087 processSync(mapper);
8088
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8090 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8091 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8092 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8095 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8096
8097 // Should not have sent any more keys or motions.
8098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8100}
8101
8102TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008103 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008104 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008105 prepareAxes(POSITION | ID | SLOT);
8106 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008107 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008108
arthurhungdcef2dc2020-08-11 14:47:50 +08008109 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008110
8111 NotifyMotionArgs motionArgs;
8112
8113 // Two fingers down at once.
8114 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8115 processPosition(mapper, x1, y1);
8116 processId(mapper, 1);
8117 processSlot(mapper, 1);
8118 processPosition(mapper, x2, y2);
8119 processId(mapper, 2);
8120 processSync(mapper);
8121
8122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8123 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8124 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8125 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8126 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8128 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8129
8130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008131 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008132 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8133 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8135 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8136 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8138 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8140 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8141
8142 // Move.
8143 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8144 processSlot(mapper, 0);
8145 processPosition(mapper, x1, y1);
8146 processSlot(mapper, 1);
8147 processPosition(mapper, x2, y2);
8148 processSync(mapper);
8149
8150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8152 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8153 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8155 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8156 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8158 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8159 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8160 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8161
8162 // First finger up.
8163 x2 += 15; y2 -= 20;
8164 processSlot(mapper, 0);
8165 processId(mapper, -1);
8166 processSlot(mapper, 1);
8167 processPosition(mapper, x2, y2);
8168 processSync(mapper);
8169
8170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008171 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008172 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8173 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8175 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8178 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8180 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8181
8182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8184 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8185 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8186 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8188 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8189
8190 // Move.
8191 x2 += 20; y2 -= 25;
8192 processPosition(mapper, x2, y2);
8193 processSync(mapper);
8194
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8197 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8198 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8201 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8202
8203 // New finger down.
8204 int32_t x3 = 700, y3 = 300;
8205 processPosition(mapper, x2, y2);
8206 processSlot(mapper, 0);
8207 processId(mapper, 3);
8208 processPosition(mapper, x3, y3);
8209 processSync(mapper);
8210
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008212 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008213 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8214 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8215 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8216 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8219 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8221 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8222
8223 // Second finger up.
8224 x3 += 30; y3 -= 20;
8225 processSlot(mapper, 1);
8226 processId(mapper, -1);
8227 processSlot(mapper, 0);
8228 processPosition(mapper, x3, y3);
8229 processSync(mapper);
8230
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008232 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008233 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8234 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8235 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8236 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8237 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8238 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8239 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8241 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8242
8243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8245 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8246 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8247 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8249 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8250
8251 // Last finger up.
8252 processId(mapper, -1);
8253 processSync(mapper);
8254
8255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8256 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8257 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8258 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8261 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8262
8263 // Should not have sent any more keys or motions.
8264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8266}
8267
8268TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008269 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008270 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008271 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008272 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008273
8274 // These calculations are based on the input device calibration documentation.
8275 int32_t rawX = 100;
8276 int32_t rawY = 200;
8277 int32_t rawTouchMajor = 7;
8278 int32_t rawTouchMinor = 6;
8279 int32_t rawToolMajor = 9;
8280 int32_t rawToolMinor = 8;
8281 int32_t rawPressure = 11;
8282 int32_t rawDistance = 0;
8283 int32_t rawOrientation = 3;
8284 int32_t id = 5;
8285
8286 float x = toDisplayX(rawX);
8287 float y = toDisplayY(rawY);
8288 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8289 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8290 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8291 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8292 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8293 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8294 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8295 float distance = float(rawDistance);
8296
8297 processPosition(mapper, rawX, rawY);
8298 processTouchMajor(mapper, rawTouchMajor);
8299 processTouchMinor(mapper, rawTouchMinor);
8300 processToolMajor(mapper, rawToolMajor);
8301 processToolMinor(mapper, rawToolMinor);
8302 processPressure(mapper, rawPressure);
8303 processOrientation(mapper, rawOrientation);
8304 processDistance(mapper, rawDistance);
8305 processId(mapper, id);
8306 processMTSync(mapper);
8307 processSync(mapper);
8308
8309 NotifyMotionArgs args;
8310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8311 ASSERT_EQ(0, args.pointerProperties[0].id);
8312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8313 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8314 orientation, distance));
8315}
8316
8317TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008318 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008319 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008320 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8321 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008322 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008323
8324 // These calculations are based on the input device calibration documentation.
8325 int32_t rawX = 100;
8326 int32_t rawY = 200;
8327 int32_t rawTouchMajor = 140;
8328 int32_t rawTouchMinor = 120;
8329 int32_t rawToolMajor = 180;
8330 int32_t rawToolMinor = 160;
8331
8332 float x = toDisplayX(rawX);
8333 float y = toDisplayY(rawY);
8334 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8335 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8336 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8337 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8338 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8339
8340 processPosition(mapper, rawX, rawY);
8341 processTouchMajor(mapper, rawTouchMajor);
8342 processTouchMinor(mapper, rawTouchMinor);
8343 processToolMajor(mapper, rawToolMajor);
8344 processToolMinor(mapper, rawToolMinor);
8345 processMTSync(mapper);
8346 processSync(mapper);
8347
8348 NotifyMotionArgs args;
8349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8351 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8352}
8353
8354TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008355 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008356 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008357 prepareAxes(POSITION | TOUCH | TOOL);
8358 addConfigurationProperty("touch.size.calibration", "diameter");
8359 addConfigurationProperty("touch.size.scale", "10");
8360 addConfigurationProperty("touch.size.bias", "160");
8361 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008362 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008363
8364 // These calculations are based on the input device calibration documentation.
8365 // Note: We only provide a single common touch/tool value because the device is assumed
8366 // not to emit separate values for each pointer (isSummed = 1).
8367 int32_t rawX = 100;
8368 int32_t rawY = 200;
8369 int32_t rawX2 = 150;
8370 int32_t rawY2 = 250;
8371 int32_t rawTouchMajor = 5;
8372 int32_t rawToolMajor = 8;
8373
8374 float x = toDisplayX(rawX);
8375 float y = toDisplayY(rawY);
8376 float x2 = toDisplayX(rawX2);
8377 float y2 = toDisplayY(rawY2);
8378 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8379 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8380 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8381
8382 processPosition(mapper, rawX, rawY);
8383 processTouchMajor(mapper, rawTouchMajor);
8384 processToolMajor(mapper, rawToolMajor);
8385 processMTSync(mapper);
8386 processPosition(mapper, rawX2, rawY2);
8387 processTouchMajor(mapper, rawTouchMajor);
8388 processToolMajor(mapper, rawToolMajor);
8389 processMTSync(mapper);
8390 processSync(mapper);
8391
8392 NotifyMotionArgs args;
8393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8394 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8395
8396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008397 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008398 ASSERT_EQ(size_t(2), args.pointerCount);
8399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8400 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8402 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8403}
8404
8405TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008406 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008407 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008408 prepareAxes(POSITION | TOUCH | TOOL);
8409 addConfigurationProperty("touch.size.calibration", "area");
8410 addConfigurationProperty("touch.size.scale", "43");
8411 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008412 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008413
8414 // These calculations are based on the input device calibration documentation.
8415 int32_t rawX = 100;
8416 int32_t rawY = 200;
8417 int32_t rawTouchMajor = 5;
8418 int32_t rawToolMajor = 8;
8419
8420 float x = toDisplayX(rawX);
8421 float y = toDisplayY(rawY);
8422 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8423 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8424 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8425
8426 processPosition(mapper, rawX, rawY);
8427 processTouchMajor(mapper, rawTouchMajor);
8428 processToolMajor(mapper, rawToolMajor);
8429 processMTSync(mapper);
8430 processSync(mapper);
8431
8432 NotifyMotionArgs args;
8433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8435 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8436}
8437
8438TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008439 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008440 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008441 prepareAxes(POSITION | PRESSURE);
8442 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8443 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008444 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008445
Michael Wrightaa449c92017-12-13 21:21:43 +00008446 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008447 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008448 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8449 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8450 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8451
Michael Wrightd02c5b62014-02-10 15:10:22 -08008452 // These calculations are based on the input device calibration documentation.
8453 int32_t rawX = 100;
8454 int32_t rawY = 200;
8455 int32_t rawPressure = 60;
8456
8457 float x = toDisplayX(rawX);
8458 float y = toDisplayY(rawY);
8459 float pressure = float(rawPressure) * 0.01f;
8460
8461 processPosition(mapper, rawX, rawY);
8462 processPressure(mapper, rawPressure);
8463 processMTSync(mapper);
8464 processSync(mapper);
8465
8466 NotifyMotionArgs args;
8467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8469 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8470}
8471
8472TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008473 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008474 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008475 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008476 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008477
8478 NotifyMotionArgs motionArgs;
8479 NotifyKeyArgs keyArgs;
8480
8481 processId(mapper, 1);
8482 processPosition(mapper, 100, 200);
8483 processSync(mapper);
8484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8485 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8486 ASSERT_EQ(0, motionArgs.buttonState);
8487
8488 // press BTN_LEFT, release BTN_LEFT
8489 processKey(mapper, BTN_LEFT, 1);
8490 processSync(mapper);
8491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8492 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8493 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8494
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8496 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8497 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8498
Michael Wrightd02c5b62014-02-10 15:10:22 -08008499 processKey(mapper, BTN_LEFT, 0);
8500 processSync(mapper);
8501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008502 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008503 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008504
8505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008506 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008507 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008508
8509 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8510 processKey(mapper, BTN_RIGHT, 1);
8511 processKey(mapper, BTN_MIDDLE, 1);
8512 processSync(mapper);
8513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8515 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8516 motionArgs.buttonState);
8517
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8519 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8520 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8521
8522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8523 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8524 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8525 motionArgs.buttonState);
8526
Michael Wrightd02c5b62014-02-10 15:10:22 -08008527 processKey(mapper, BTN_RIGHT, 0);
8528 processSync(mapper);
8529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008530 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008531 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008532
8533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008535 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008536
8537 processKey(mapper, BTN_MIDDLE, 0);
8538 processSync(mapper);
8539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008540 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008541 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008542
8543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008545 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008546
8547 // press BTN_BACK, release BTN_BACK
8548 processKey(mapper, BTN_BACK, 1);
8549 processSync(mapper);
8550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8551 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8552 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008553
Michael Wrightd02c5b62014-02-10 15:10:22 -08008554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008556 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8557
8558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8559 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8560 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008561
8562 processKey(mapper, BTN_BACK, 0);
8563 processSync(mapper);
8564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008566 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008567
8568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008570 ASSERT_EQ(0, motionArgs.buttonState);
8571
Michael Wrightd02c5b62014-02-10 15:10:22 -08008572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8573 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8574 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8575
8576 // press BTN_SIDE, release BTN_SIDE
8577 processKey(mapper, BTN_SIDE, 1);
8578 processSync(mapper);
8579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8580 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8581 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008582
Michael Wrightd02c5b62014-02-10 15:10:22 -08008583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008584 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008585 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8586
8587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8588 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8589 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008590
8591 processKey(mapper, BTN_SIDE, 0);
8592 processSync(mapper);
8593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008594 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008595 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008596
8597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008599 ASSERT_EQ(0, motionArgs.buttonState);
8600
Michael Wrightd02c5b62014-02-10 15:10:22 -08008601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8602 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8603 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8604
8605 // press BTN_FORWARD, release BTN_FORWARD
8606 processKey(mapper, BTN_FORWARD, 1);
8607 processSync(mapper);
8608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8609 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8610 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008611
Michael Wrightd02c5b62014-02-10 15:10:22 -08008612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008614 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8615
8616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8617 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8618 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008619
8620 processKey(mapper, BTN_FORWARD, 0);
8621 processSync(mapper);
8622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008623 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008624 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008625
8626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008627 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008628 ASSERT_EQ(0, motionArgs.buttonState);
8629
Michael Wrightd02c5b62014-02-10 15:10:22 -08008630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8631 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8632 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8633
8634 // press BTN_EXTRA, release BTN_EXTRA
8635 processKey(mapper, BTN_EXTRA, 1);
8636 processSync(mapper);
8637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8638 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8639 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008640
Michael Wrightd02c5b62014-02-10 15:10:22 -08008641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008643 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8644
8645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8646 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8647 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008648
8649 processKey(mapper, BTN_EXTRA, 0);
8650 processSync(mapper);
8651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008652 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008653 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008654
8655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008657 ASSERT_EQ(0, motionArgs.buttonState);
8658
Michael Wrightd02c5b62014-02-10 15:10:22 -08008659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8660 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8661 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8662
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8664
Michael Wrightd02c5b62014-02-10 15:10:22 -08008665 // press BTN_STYLUS, release BTN_STYLUS
8666 processKey(mapper, BTN_STYLUS, 1);
8667 processSync(mapper);
8668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8669 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008670 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8671
8672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8673 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8674 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008675
8676 processKey(mapper, BTN_STYLUS, 0);
8677 processSync(mapper);
8678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008679 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008680 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008681
8682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008684 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008685
8686 // press BTN_STYLUS2, release BTN_STYLUS2
8687 processKey(mapper, BTN_STYLUS2, 1);
8688 processSync(mapper);
8689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008691 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8692
8693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8694 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8695 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008696
8697 processKey(mapper, BTN_STYLUS2, 0);
8698 processSync(mapper);
8699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008700 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008701 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008702
8703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008704 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008705 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008706
8707 // release touch
8708 processId(mapper, -1);
8709 processSync(mapper);
8710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8711 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8712 ASSERT_EQ(0, motionArgs.buttonState);
8713}
8714
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008715TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8716 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008717 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008718 prepareAxes(POSITION | ID | SLOT);
8719 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8720
8721 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8722 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8723
8724 // Touch down.
8725 processId(mapper, 1);
8726 processPosition(mapper, 100, 200);
8727 processSync(mapper);
8728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8729 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8730
8731 // Press and release button mapped to the primary stylus button.
8732 processKey(mapper, BTN_A, 1);
8733 processSync(mapper);
8734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8735 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8736 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8738 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8739 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8740
8741 processKey(mapper, BTN_A, 0);
8742 processSync(mapper);
8743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8744 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8746 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8747
8748 // Press and release the HID usage mapped to the secondary stylus button.
8749 processHidUsage(mapper, 0xabcd, 1);
8750 processSync(mapper);
8751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8752 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8753 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8755 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8756 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8757
8758 processHidUsage(mapper, 0xabcd, 0);
8759 processSync(mapper);
8760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8761 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8763 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8764
8765 // Release touch.
8766 processId(mapper, -1);
8767 processSync(mapper);
8768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8769 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8770}
8771
Michael Wrightd02c5b62014-02-10 15:10:22 -08008772TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008773 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008774 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008775 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008776 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008777
8778 NotifyMotionArgs motionArgs;
8779
8780 // default tool type is finger
8781 processId(mapper, 1);
8782 processPosition(mapper, 100, 200);
8783 processSync(mapper);
8784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8785 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8786 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8787
8788 // eraser
8789 processKey(mapper, BTN_TOOL_RUBBER, 1);
8790 processSync(mapper);
8791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8793 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8794
8795 // stylus
8796 processKey(mapper, BTN_TOOL_RUBBER, 0);
8797 processKey(mapper, BTN_TOOL_PEN, 1);
8798 processSync(mapper);
8799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8802
8803 // brush
8804 processKey(mapper, BTN_TOOL_PEN, 0);
8805 processKey(mapper, BTN_TOOL_BRUSH, 1);
8806 processSync(mapper);
8807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8808 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8810
8811 // pencil
8812 processKey(mapper, BTN_TOOL_BRUSH, 0);
8813 processKey(mapper, BTN_TOOL_PENCIL, 1);
8814 processSync(mapper);
8815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8816 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8817 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8818
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008819 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008820 processKey(mapper, BTN_TOOL_PENCIL, 0);
8821 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8822 processSync(mapper);
8823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8824 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8825 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8826
8827 // mouse
8828 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8829 processKey(mapper, BTN_TOOL_MOUSE, 1);
8830 processSync(mapper);
8831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8833 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8834
8835 // lens
8836 processKey(mapper, BTN_TOOL_MOUSE, 0);
8837 processKey(mapper, BTN_TOOL_LENS, 1);
8838 processSync(mapper);
8839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8842
8843 // double-tap
8844 processKey(mapper, BTN_TOOL_LENS, 0);
8845 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8846 processSync(mapper);
8847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8849 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8850
8851 // triple-tap
8852 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8853 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8854 processSync(mapper);
8855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8856 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8857 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8858
8859 // quad-tap
8860 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8861 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8862 processSync(mapper);
8863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8865 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8866
8867 // finger
8868 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8869 processKey(mapper, BTN_TOOL_FINGER, 1);
8870 processSync(mapper);
8871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8873 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8874
8875 // stylus trumps finger
8876 processKey(mapper, BTN_TOOL_PEN, 1);
8877 processSync(mapper);
8878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8879 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8880 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8881
8882 // eraser trumps stylus
8883 processKey(mapper, BTN_TOOL_RUBBER, 1);
8884 processSync(mapper);
8885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8886 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8887 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8888
8889 // mouse trumps eraser
8890 processKey(mapper, BTN_TOOL_MOUSE, 1);
8891 processSync(mapper);
8892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8893 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8894 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8895
8896 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8897 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8898 processSync(mapper);
8899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8901 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8902
8903 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8904 processToolType(mapper, MT_TOOL_PEN);
8905 processSync(mapper);
8906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8908 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8909
8910 // back to default tool type
8911 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8912 processKey(mapper, BTN_TOOL_MOUSE, 0);
8913 processKey(mapper, BTN_TOOL_RUBBER, 0);
8914 processKey(mapper, BTN_TOOL_PEN, 0);
8915 processKey(mapper, BTN_TOOL_FINGER, 0);
8916 processSync(mapper);
8917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8919 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8920}
8921
8922TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008923 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008924 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008925 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008926 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008927 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008928
8929 NotifyMotionArgs motionArgs;
8930
8931 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8932 processId(mapper, 1);
8933 processPosition(mapper, 100, 200);
8934 processSync(mapper);
8935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8936 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8938 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8939
8940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8941 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8943 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8944
8945 // move a little
8946 processPosition(mapper, 150, 250);
8947 processSync(mapper);
8948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8949 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8951 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8952
8953 // down when BTN_TOUCH is pressed, pressure defaults to 1
8954 processKey(mapper, BTN_TOUCH, 1);
8955 processSync(mapper);
8956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8957 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8959 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8960
8961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8962 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8963 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8964 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8965
8966 // up when BTN_TOUCH is released, hover restored
8967 processKey(mapper, BTN_TOUCH, 0);
8968 processSync(mapper);
8969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8970 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8972 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8973
8974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8975 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8977 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8978
8979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8980 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8982 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8983
8984 // exit hover when pointer goes away
8985 processId(mapper, -1);
8986 processSync(mapper);
8987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8988 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8990 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8991}
8992
8993TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008994 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008995 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008996 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008997 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008998
8999 NotifyMotionArgs motionArgs;
9000
9001 // initially hovering because pressure is 0
9002 processId(mapper, 1);
9003 processPosition(mapper, 100, 200);
9004 processPressure(mapper, 0);
9005 processSync(mapper);
9006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9007 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9009 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9010
9011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9012 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9014 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9015
9016 // move a little
9017 processPosition(mapper, 150, 250);
9018 processSync(mapper);
9019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9020 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9022 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9023
9024 // down when pressure becomes non-zero
9025 processPressure(mapper, RAW_PRESSURE_MAX);
9026 processSync(mapper);
9027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9028 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9029 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9030 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9031
9032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9033 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9035 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9036
9037 // up when pressure becomes 0, hover restored
9038 processPressure(mapper, 0);
9039 processSync(mapper);
9040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9041 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9043 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9044
9045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9046 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9048 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9049
9050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9051 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9053 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9054
9055 // exit hover when pointer goes away
9056 processId(mapper, -1);
9057 processSync(mapper);
9058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9059 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9060 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9061 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9062}
9063
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009064/**
9065 * Set the input device port <--> display port associations, and check that the
9066 * events are routed to the display that matches the display port.
9067 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9068 */
9069TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009070 const std::string usb2 = "USB2";
9071 const uint8_t hdmi1 = 0;
9072 const uint8_t hdmi2 = 1;
9073 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009074 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009075
9076 addConfigurationProperty("touch.deviceType", "touchScreen");
9077 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009078 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009079
9080 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9081 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9082
9083 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9084 // for this input device is specified, and the matching viewport is not present,
9085 // the input device should be disabled (at the mapper level).
9086
9087 // Add viewport for display 2 on hdmi2
9088 prepareSecondaryDisplay(type, hdmi2);
9089 // Send a touch event
9090 processPosition(mapper, 100, 100);
9091 processSync(mapper);
9092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9093
9094 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009095 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009096 // Send a touch event again
9097 processPosition(mapper, 100, 100);
9098 processSync(mapper);
9099
9100 NotifyMotionArgs args;
9101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9102 ASSERT_EQ(DISPLAY_ID, args.displayId);
9103}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009104
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009105TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9106 addConfigurationProperty("touch.deviceType", "touchScreen");
9107 prepareAxes(POSITION);
9108 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9109
9110 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9111
Michael Wrighta9cf4192022-12-01 23:46:39 +00009112 prepareDisplay(ui::ROTATION_0);
9113 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009114
9115 // Send a touch event
9116 processPosition(mapper, 100, 100);
9117 processSync(mapper);
9118
9119 NotifyMotionArgs args;
9120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9121 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9122}
9123
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009124TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009125 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009126 std::shared_ptr<FakePointerController> fakePointerController =
9127 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009128 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009129 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009130 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009131
Garfield Tan888a6a42020-01-09 11:39:16 -08009132 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009133 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009134
Michael Wrighta9cf4192022-12-01 23:46:39 +00009135 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009136 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009137 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009138
Josep del Río2d8c79a2023-01-23 19:33:50 +00009139 // Check source is mouse that would obtain the PointerController.
9140 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009141
9142 NotifyMotionArgs motionArgs;
9143 processPosition(mapper, 100, 100);
9144 processSync(mapper);
9145
9146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9147 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9148 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9149}
9150
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009151/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009152 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9153 */
9154TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9155 addConfigurationProperty("touch.deviceType", "touchScreen");
9156 prepareAxes(POSITION);
9157 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9158
Michael Wrighta9cf4192022-12-01 23:46:39 +00009159 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009160 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9161 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9162 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9163 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009164
9165 NotifyMotionArgs args;
9166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9167 ASSERT_EQ(26, args.readTime);
9168
Harry Cutts33476232023-01-30 19:57:29 +00009169 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9170 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9171 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009172
9173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9174 ASSERT_EQ(33, args.readTime);
9175}
9176
9177/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009178 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9179 * events should not be delivered to the listener.
9180 */
9181TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9182 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009183 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009184 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009185 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009186 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9187 prepareAxes(POSITION);
9188 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9189
9190 NotifyMotionArgs motionArgs;
9191 processPosition(mapper, 100, 100);
9192 processSync(mapper);
9193
9194 mFakeListener->assertNotifyMotionWasNotCalled();
9195}
9196
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009197/**
9198 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9199 * the touch mapper can process the events and the events can be delivered to the listener.
9200 */
9201TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9202 addConfigurationProperty("touch.deviceType", "touchScreen");
9203 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009204 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009205 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009206 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9207 prepareAxes(POSITION);
9208 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9209
9210 NotifyMotionArgs motionArgs;
9211 processPosition(mapper, 100, 100);
9212 processSync(mapper);
9213
9214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9215 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9216}
9217
Garfield Tanc734e4f2021-01-15 20:01:39 -08009218TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9219 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009220 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009221 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009222 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009223 std::optional<DisplayViewport> optionalDisplayViewport =
9224 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9225 ASSERT_TRUE(optionalDisplayViewport.has_value());
9226 DisplayViewport displayViewport = *optionalDisplayViewport;
9227
9228 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9229 prepareAxes(POSITION);
9230 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9231
9232 // Finger down
9233 int32_t x = 100, y = 100;
9234 processPosition(mapper, x, y);
9235 processSync(mapper);
9236
9237 NotifyMotionArgs motionArgs;
9238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9239 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9240
9241 // Deactivate display viewport
9242 displayViewport.isActive = false;
9243 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9244 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9245
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009246 // The ongoing touch should be canceled immediately
9247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9248 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9249
9250 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009251 x += 10, y += 10;
9252 processPosition(mapper, x, y);
9253 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009255
9256 // Reactivate display viewport
9257 displayViewport.isActive = true;
9258 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9259 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9260
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009261 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009262 x += 10, y += 10;
9263 processPosition(mapper, x, y);
9264 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9266 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009267}
9268
Arthur Hung7c645402019-01-25 17:45:42 +08009269TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9270 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009271 prepareAxes(POSITION | ID | SLOT);
9272 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009273 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009274
9275 // Create the second touch screen device, and enable multi fingers.
9276 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009277 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009278 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009279 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009280 std::shared_ptr<InputDevice> device2 =
9281 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009282 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009283
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009284 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009285 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009286 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009287 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009288 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009289 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009290 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009291 /*flat=*/0, /*fuzz=*/0);
9292 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009293 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9294 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009295
9296 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009297 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009298 std::list<NotifyArgs> unused =
9299 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00009300 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009301 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009302
9303 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009304 std::shared_ptr<FakePointerController> fakePointerController =
9305 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009306 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009307
9308 // Setup policy for associated displays and show touches.
9309 const uint8_t hdmi1 = 0;
9310 const uint8_t hdmi2 = 1;
9311 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9312 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9313 mFakePolicy->setShowTouches(true);
9314
9315 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009316 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009317 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009318
9319 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009320 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9321 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9322 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009323
9324 // Two fingers down at default display.
9325 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9326 processPosition(mapper, x1, y1);
9327 processId(mapper, 1);
9328 processSlot(mapper, 1);
9329 processPosition(mapper, x2, y2);
9330 processId(mapper, 2);
9331 processSync(mapper);
9332
9333 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9334 fakePointerController->getSpots().find(DISPLAY_ID);
9335 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9336 ASSERT_EQ(size_t(2), iter->second.size());
9337
9338 // Two fingers down at second display.
9339 processPosition(mapper2, x1, y1);
9340 processId(mapper2, 1);
9341 processSlot(mapper2, 1);
9342 processPosition(mapper2, x2, y2);
9343 processId(mapper2, 2);
9344 processSync(mapper2);
9345
9346 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9347 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9348 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009349
9350 // Disable the show touches configuration and ensure the spots are cleared.
9351 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009352 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9353 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009354
9355 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009356}
9357
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009358TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009359 prepareAxes(POSITION);
9360 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009361 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009362 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009363
9364 NotifyMotionArgs motionArgs;
9365 // Unrotated video frame
9366 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9367 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009368 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009369 processPosition(mapper, 100, 200);
9370 processSync(mapper);
9371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9372 ASSERT_EQ(frames, motionArgs.videoFrames);
9373
9374 // Subsequent touch events should not have any videoframes
9375 // This is implemented separately in FakeEventHub,
9376 // but that should match the behaviour of TouchVideoDevice.
9377 processPosition(mapper, 200, 200);
9378 processSync(mapper);
9379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9380 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9381}
9382
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009383TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009384 prepareAxes(POSITION);
9385 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009386 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009387 // Unrotated video frame
9388 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9389 NotifyMotionArgs motionArgs;
9390
9391 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009392 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009393 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9394 clearViewports();
9395 prepareDisplay(orientation);
9396 std::vector<TouchVideoFrame> frames{frame};
9397 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9398 processPosition(mapper, 100, 200);
9399 processSync(mapper);
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9401 ASSERT_EQ(frames, motionArgs.videoFrames);
9402 }
9403}
9404
9405TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9406 prepareAxes(POSITION);
9407 addConfigurationProperty("touch.deviceType", "touchScreen");
9408 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9409 // orientation-aware are affected by display rotation.
9410 addConfigurationProperty("touch.orientationAware", "0");
9411 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9412 // Unrotated video frame
9413 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9414 NotifyMotionArgs motionArgs;
9415
9416 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009417 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009418 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9419 clearViewports();
9420 prepareDisplay(orientation);
9421 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009422 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009423 processPosition(mapper, 100, 200);
9424 processSync(mapper);
9425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009426 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9427 // compared to the display. This is so that when the window transform (which contains the
9428 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9429 // window's coordinate space.
9430 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009431 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009432
9433 // Release finger.
9434 processSync(mapper);
9435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009436 }
9437}
9438
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009439TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009440 prepareAxes(POSITION);
9441 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009442 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009443 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9444 // so mix these.
9445 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9446 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9447 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9448 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9449 NotifyMotionArgs motionArgs;
9450
Michael Wrighta9cf4192022-12-01 23:46:39 +00009451 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009452 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009453 processPosition(mapper, 100, 200);
9454 processSync(mapper);
9455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009456 ASSERT_EQ(frames, motionArgs.videoFrames);
9457}
9458
9459TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9460 prepareAxes(POSITION);
9461 addConfigurationProperty("touch.deviceType", "touchScreen");
9462 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9463 // orientation-aware are affected by display rotation.
9464 addConfigurationProperty("touch.orientationAware", "0");
9465 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9466 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9467 // so mix these.
9468 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9469 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9470 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9471 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9472 NotifyMotionArgs motionArgs;
9473
Michael Wrighta9cf4192022-12-01 23:46:39 +00009474 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009475 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9476 processPosition(mapper, 100, 200);
9477 processSync(mapper);
9478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9479 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9480 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9481 // compared to the display. This is so that when the window transform (which contains the
9482 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9483 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009484 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009485 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009486 ASSERT_EQ(frames, motionArgs.videoFrames);
9487}
9488
Arthur Hung9da14732019-09-02 16:16:58 +08009489/**
9490 * If we had defined port associations, but the viewport is not ready, the touch device would be
9491 * expected to be disabled, and it should be enabled after the viewport has found.
9492 */
9493TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009494 constexpr uint8_t hdmi2 = 1;
9495 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009496 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009497
9498 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9499
9500 addConfigurationProperty("touch.deviceType", "touchScreen");
9501 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009502 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009503
9504 ASSERT_EQ(mDevice->isEnabled(), false);
9505
9506 // Add display on hdmi2, the device should be enabled and can receive touch event.
9507 prepareSecondaryDisplay(type, hdmi2);
9508 ASSERT_EQ(mDevice->isEnabled(), true);
9509
9510 // Send a touch event.
9511 processPosition(mapper, 100, 100);
9512 processSync(mapper);
9513
9514 NotifyMotionArgs args;
9515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9516 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9517}
9518
Arthur Hung421eb1c2020-01-16 00:09:42 +08009519TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009520 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009521 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009522 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009523 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009524
9525 NotifyMotionArgs motionArgs;
9526
9527 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9528 // finger down
9529 processId(mapper, 1);
9530 processPosition(mapper, x1, y1);
9531 processSync(mapper);
9532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9533 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9535
9536 // finger move
9537 processId(mapper, 1);
9538 processPosition(mapper, x2, y2);
9539 processSync(mapper);
9540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9543
9544 // finger up.
9545 processId(mapper, -1);
9546 processSync(mapper);
9547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9548 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9549 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9550
9551 // new finger down
9552 processId(mapper, 1);
9553 processPosition(mapper, x3, y3);
9554 processSync(mapper);
9555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9556 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9558}
9559
9560/**
arthurhungcc7f9802020-04-30 17:55:40 +08009561 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9562 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009563 */
arthurhungcc7f9802020-04-30 17:55:40 +08009564TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009565 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009566 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009567 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009568 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009569
9570 NotifyMotionArgs motionArgs;
9571
9572 // default tool type is finger
9573 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009574 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009575 processPosition(mapper, x1, y1);
9576 processSync(mapper);
9577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9578 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9580
9581 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9582 processToolType(mapper, MT_TOOL_PALM);
9583 processSync(mapper);
9584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9585 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9586
9587 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009588 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009589 processPosition(mapper, x2, y2);
9590 processSync(mapper);
9591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9592
9593 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009594 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009595 processSync(mapper);
9596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9597
9598 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009599 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009600 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009601 processPosition(mapper, x3, y3);
9602 processSync(mapper);
9603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9604 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9605 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9606}
9607
arthurhungbf89a482020-04-17 17:37:55 +08009608/**
arthurhungcc7f9802020-04-30 17:55:40 +08009609 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9610 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009611 */
arthurhungcc7f9802020-04-30 17:55:40 +08009612TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009613 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009614 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009615 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9616 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9617
9618 NotifyMotionArgs motionArgs;
9619
9620 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009621 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9622 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009623 processPosition(mapper, x1, y1);
9624 processSync(mapper);
9625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9626 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9628
9629 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009630 processSlot(mapper, SECOND_SLOT);
9631 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009632 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009633 processSync(mapper);
9634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009635 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9637
9638 // If the tool type of the first finger changes to MT_TOOL_PALM,
9639 // we expect to receive ACTION_POINTER_UP with cancel flag.
9640 processSlot(mapper, FIRST_SLOT);
9641 processId(mapper, FIRST_TRACKING_ID);
9642 processToolType(mapper, MT_TOOL_PALM);
9643 processSync(mapper);
9644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009645 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009646 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9647
9648 // The following MOVE events of second finger should be processed.
9649 processSlot(mapper, SECOND_SLOT);
9650 processId(mapper, SECOND_TRACKING_ID);
9651 processPosition(mapper, x2 + 1, y2 + 1);
9652 processSync(mapper);
9653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9655 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9656
9657 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9658 // it. Second finger receive move.
9659 processSlot(mapper, FIRST_SLOT);
9660 processId(mapper, INVALID_TRACKING_ID);
9661 processSync(mapper);
9662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9663 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9664 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9665
9666 // Second finger keeps moving.
9667 processSlot(mapper, SECOND_SLOT);
9668 processId(mapper, SECOND_TRACKING_ID);
9669 processPosition(mapper, x2 + 2, y2 + 2);
9670 processSync(mapper);
9671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9673 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9674
9675 // Second finger up.
9676 processId(mapper, INVALID_TRACKING_ID);
9677 processSync(mapper);
9678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9679 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9680 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9681}
9682
9683/**
9684 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9685 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9686 */
9687TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9688 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009689 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009690 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9691 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9692
9693 NotifyMotionArgs motionArgs;
9694
9695 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9696 // First finger down.
9697 processId(mapper, FIRST_TRACKING_ID);
9698 processPosition(mapper, x1, y1);
9699 processSync(mapper);
9700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9701 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9703
9704 // Second finger down.
9705 processSlot(mapper, SECOND_SLOT);
9706 processId(mapper, SECOND_TRACKING_ID);
9707 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009708 processSync(mapper);
9709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009710 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009711 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9712
arthurhungcc7f9802020-04-30 17:55:40 +08009713 // If the tool type of the first finger changes to MT_TOOL_PALM,
9714 // we expect to receive ACTION_POINTER_UP with cancel flag.
9715 processSlot(mapper, FIRST_SLOT);
9716 processId(mapper, FIRST_TRACKING_ID);
9717 processToolType(mapper, MT_TOOL_PALM);
9718 processSync(mapper);
9719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009720 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009721 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9722
9723 // Second finger keeps moving.
9724 processSlot(mapper, SECOND_SLOT);
9725 processId(mapper, SECOND_TRACKING_ID);
9726 processPosition(mapper, x2 + 1, y2 + 1);
9727 processSync(mapper);
9728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9729 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9730
9731 // second finger becomes palm, receive cancel due to only 1 finger is active.
9732 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009733 processToolType(mapper, MT_TOOL_PALM);
9734 processSync(mapper);
9735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9736 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9737
arthurhungcc7f9802020-04-30 17:55:40 +08009738 // third finger down.
9739 processSlot(mapper, THIRD_SLOT);
9740 processId(mapper, THIRD_TRACKING_ID);
9741 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009742 processPosition(mapper, x3, y3);
9743 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9745 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9746 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009747 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9748
9749 // third finger move
9750 processId(mapper, THIRD_TRACKING_ID);
9751 processPosition(mapper, x3 + 1, y3 + 1);
9752 processSync(mapper);
9753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9754 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9755
9756 // first finger up, third finger receive move.
9757 processSlot(mapper, FIRST_SLOT);
9758 processId(mapper, INVALID_TRACKING_ID);
9759 processSync(mapper);
9760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9761 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9762 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9763
9764 // second finger up, third finger receive move.
9765 processSlot(mapper, SECOND_SLOT);
9766 processId(mapper, INVALID_TRACKING_ID);
9767 processSync(mapper);
9768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9769 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9770 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9771
9772 // third finger up.
9773 processSlot(mapper, THIRD_SLOT);
9774 processId(mapper, INVALID_TRACKING_ID);
9775 processSync(mapper);
9776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9777 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9778 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9779}
9780
9781/**
9782 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9783 * and the active finger could still be allowed to receive the events
9784 */
9785TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9786 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009787 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009788 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9789 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9790
9791 NotifyMotionArgs motionArgs;
9792
9793 // default tool type is finger
9794 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9795 processId(mapper, FIRST_TRACKING_ID);
9796 processPosition(mapper, x1, y1);
9797 processSync(mapper);
9798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9799 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9801
9802 // Second finger down.
9803 processSlot(mapper, SECOND_SLOT);
9804 processId(mapper, SECOND_TRACKING_ID);
9805 processPosition(mapper, x2, y2);
9806 processSync(mapper);
9807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009808 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9810
9811 // If the tool type of the second finger changes to MT_TOOL_PALM,
9812 // we expect to receive ACTION_POINTER_UP with cancel flag.
9813 processId(mapper, SECOND_TRACKING_ID);
9814 processToolType(mapper, MT_TOOL_PALM);
9815 processSync(mapper);
9816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009817 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009818 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9819
9820 // The following MOVE event should be processed.
9821 processSlot(mapper, FIRST_SLOT);
9822 processId(mapper, FIRST_TRACKING_ID);
9823 processPosition(mapper, x1 + 1, y1 + 1);
9824 processSync(mapper);
9825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9827 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9828
9829 // second finger up.
9830 processSlot(mapper, SECOND_SLOT);
9831 processId(mapper, INVALID_TRACKING_ID);
9832 processSync(mapper);
9833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9835
9836 // first finger keep moving
9837 processSlot(mapper, FIRST_SLOT);
9838 processId(mapper, FIRST_TRACKING_ID);
9839 processPosition(mapper, x1 + 2, y1 + 2);
9840 processSync(mapper);
9841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9843
9844 // first finger up.
9845 processId(mapper, INVALID_TRACKING_ID);
9846 processSync(mapper);
9847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9848 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9849 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009850}
9851
Arthur Hung9ad18942021-06-19 02:04:46 +00009852/**
9853 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9854 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9855 * cause slot be valid again.
9856 */
9857TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9858 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009859 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009860 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9861 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9862
9863 NotifyMotionArgs motionArgs;
9864
9865 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9866 // First finger down.
9867 processId(mapper, FIRST_TRACKING_ID);
9868 processPosition(mapper, x1, y1);
9869 processPressure(mapper, RAW_PRESSURE_MAX);
9870 processSync(mapper);
9871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9872 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9873 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9874
9875 // First finger move.
9876 processId(mapper, FIRST_TRACKING_ID);
9877 processPosition(mapper, x1 + 1, y1 + 1);
9878 processPressure(mapper, RAW_PRESSURE_MAX);
9879 processSync(mapper);
9880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9882 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9883
9884 // Second finger down.
9885 processSlot(mapper, SECOND_SLOT);
9886 processId(mapper, SECOND_TRACKING_ID);
9887 processPosition(mapper, x2, y2);
9888 processPressure(mapper, RAW_PRESSURE_MAX);
9889 processSync(mapper);
9890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009891 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009892 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9893
9894 // second finger up with some unexpected data.
9895 processSlot(mapper, SECOND_SLOT);
9896 processId(mapper, INVALID_TRACKING_ID);
9897 processPosition(mapper, x2, y2);
9898 processSync(mapper);
9899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009900 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009901 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9902
9903 // first finger up with some unexpected data.
9904 processSlot(mapper, FIRST_SLOT);
9905 processId(mapper, INVALID_TRACKING_ID);
9906 processPosition(mapper, x2, y2);
9907 processPressure(mapper, RAW_PRESSURE_MAX);
9908 processSync(mapper);
9909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9910 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9911 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9912}
9913
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009914TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9915 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009916 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009917 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9918 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9919
9920 // First finger down.
9921 processId(mapper, FIRST_TRACKING_ID);
9922 processPosition(mapper, 100, 200);
9923 processPressure(mapper, RAW_PRESSURE_MAX);
9924 processSync(mapper);
9925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9926 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9927
9928 // Second finger down.
9929 processSlot(mapper, SECOND_SLOT);
9930 processId(mapper, SECOND_TRACKING_ID);
9931 processPosition(mapper, 300, 400);
9932 processPressure(mapper, RAW_PRESSURE_MAX);
9933 processSync(mapper);
9934 ASSERT_NO_FATAL_FAILURE(
9935 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9936
9937 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009938 // preserved. Resetting should cancel the ongoing gesture.
9939 resetMapper(mapper, ARBITRARY_TIME);
9940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9941 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009942
9943 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9944 // the existing touch state to generate a down event.
9945 processPosition(mapper, 301, 302);
9946 processSync(mapper);
9947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9948 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9950 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9951
9952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9953}
9954
9955TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9956 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009957 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009958 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9959 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9960
9961 // First finger touches down and releases.
9962 processId(mapper, FIRST_TRACKING_ID);
9963 processPosition(mapper, 100, 200);
9964 processPressure(mapper, RAW_PRESSURE_MAX);
9965 processSync(mapper);
9966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9967 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9968 processId(mapper, INVALID_TRACKING_ID);
9969 processSync(mapper);
9970 ASSERT_NO_FATAL_FAILURE(
9971 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9972
9973 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9974 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009975 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9977
9978 // Send an empty sync frame. Since there are no pointers, no events are generated.
9979 processSync(mapper);
9980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9981}
9982
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009983TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009984 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009985 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009986 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
9987 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009989
9990 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9991 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9992 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9993 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9994 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9995
9996 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009997 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009998 processId(mapper, FIRST_TRACKING_ID);
9999 processToolType(mapper, MT_TOOL_PEN);
10000 processPosition(mapper, 100, 200);
10001 processPressure(mapper, RAW_PRESSURE_MAX);
10002 processSync(mapper);
10003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10004 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10005 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
10006 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10007
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010008 // Now that we know the device supports styluses, ensure that the device is re-configured with
10009 // the stylus source.
10010 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10011 {
10012 const auto& devices = mReader->getInputDevices();
10013 auto deviceInfo =
10014 std::find_if(devices.begin(), devices.end(),
10015 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10016 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10017 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10018 }
10019
10020 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10022
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010023 processId(mapper, INVALID_TRACKING_ID);
10024 processSync(mapper);
10025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10026 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10027 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
10028 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010029}
10030
Seunghwan Choi356026c2023-02-01 14:37:25 +090010031TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10032 addConfigurationProperty("touch.deviceType", "touchScreen");
10033 prepareDisplay(ui::ROTATION_0);
10034 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10035 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10036 // indicate stylus presence dynamically.
10037 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10038 std::shared_ptr<FakePointerController> fakePointerController =
10039 std::make_shared<FakePointerController>();
10040 mFakePolicy->setPointerController(fakePointerController);
10041 mFakePolicy->setStylusPointerIconEnabled(true);
10042 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10043
10044 processId(mapper, FIRST_TRACKING_ID);
10045 processPressure(mapper, RAW_PRESSURE_MIN);
10046 processPosition(mapper, 100, 200);
10047 processToolType(mapper, MT_TOOL_PEN);
10048 processSync(mapper);
10049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10050 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
10051 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
10052 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10053 ASSERT_TRUE(fakePointerController->isPointerShown());
10054 ASSERT_NO_FATAL_FAILURE(
10055 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10056}
10057
10058TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10059 addConfigurationProperty("touch.deviceType", "touchScreen");
10060 prepareDisplay(ui::ROTATION_0);
10061 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10062 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10063 // indicate stylus presence dynamically.
10064 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10065 std::shared_ptr<FakePointerController> fakePointerController =
10066 std::make_shared<FakePointerController>();
10067 mFakePolicy->setPointerController(fakePointerController);
10068 mFakePolicy->setStylusPointerIconEnabled(false);
10069 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10070
10071 processId(mapper, FIRST_TRACKING_ID);
10072 processPressure(mapper, RAW_PRESSURE_MIN);
10073 processPosition(mapper, 100, 200);
10074 processToolType(mapper, MT_TOOL_PEN);
10075 processSync(mapper);
10076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10077 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
10078 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
10079 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10080 ASSERT_FALSE(fakePointerController->isPointerShown());
10081}
10082
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010083// --- MultiTouchInputMapperTest_ExternalDevice ---
10084
10085class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10086protected:
Chris Yea52ade12020-08-27 16:49:20 -070010087 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010088};
10089
10090/**
10091 * Expect fallback to internal viewport if device is external and external viewport is not present.
10092 */
10093TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10094 prepareAxes(POSITION);
10095 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010096 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010097 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10098
10099 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10100
10101 NotifyMotionArgs motionArgs;
10102
10103 // Expect the event to be sent to the internal viewport,
10104 // because an external viewport is not present.
10105 processPosition(mapper, 100, 100);
10106 processSync(mapper);
10107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10108 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10109
10110 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010111 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010112 processPosition(mapper, 100, 100);
10113 processSync(mapper);
10114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10115 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10116}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010117
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010118TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10119 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10120 std::shared_ptr<FakePointerController> fakePointerController =
10121 std::make_shared<FakePointerController>();
10122 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10123 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010124
10125 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010126 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010127 prepareAxes(POSITION | ID | SLOT);
10128 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10129 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10130 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010131 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010132 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10133
10134 // captured touchpad should be a touchpad source
10135 NotifyDeviceResetArgs resetArgs;
10136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10137 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10138
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010139 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010140
10141 const InputDeviceInfo::MotionRange* relRangeX =
10142 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10143 ASSERT_NE(relRangeX, nullptr);
10144 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10145 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10146 const InputDeviceInfo::MotionRange* relRangeY =
10147 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10148 ASSERT_NE(relRangeY, nullptr);
10149 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10150 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10151
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010152 // run captured pointer tests - note that this is unscaled, so input listener events should be
10153 // identical to what the hardware sends (accounting for any
10154 // calibration).
10155 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010156 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010157 processId(mapper, 1);
10158 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10159 processKey(mapper, BTN_TOUCH, 1);
10160 processSync(mapper);
10161
10162 // expect coord[0] to contain initial location of touch 0
10163 NotifyMotionArgs args;
10164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10165 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10166 ASSERT_EQ(1U, args.pointerCount);
10167 ASSERT_EQ(0, args.pointerProperties[0].id);
10168 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10169 ASSERT_NO_FATAL_FAILURE(
10170 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10171
10172 // FINGER 1 DOWN
10173 processSlot(mapper, 1);
10174 processId(mapper, 2);
10175 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10176 processSync(mapper);
10177
10178 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010180 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010181 ASSERT_EQ(2U, args.pointerCount);
10182 ASSERT_EQ(0, args.pointerProperties[0].id);
10183 ASSERT_EQ(1, args.pointerProperties[1].id);
10184 ASSERT_NO_FATAL_FAILURE(
10185 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10186 ASSERT_NO_FATAL_FAILURE(
10187 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10188
10189 // FINGER 1 MOVE
10190 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10191 processSync(mapper);
10192
10193 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10194 // from move
10195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10197 ASSERT_NO_FATAL_FAILURE(
10198 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10199 ASSERT_NO_FATAL_FAILURE(
10200 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10201
10202 // FINGER 0 MOVE
10203 processSlot(mapper, 0);
10204 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10205 processSync(mapper);
10206
10207 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10210 ASSERT_NO_FATAL_FAILURE(
10211 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10212 ASSERT_NO_FATAL_FAILURE(
10213 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10214
10215 // BUTTON DOWN
10216 processKey(mapper, BTN_LEFT, 1);
10217 processSync(mapper);
10218
10219 // touchinputmapper design sends a move before button press
10220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10223 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10224
10225 // BUTTON UP
10226 processKey(mapper, BTN_LEFT, 0);
10227 processSync(mapper);
10228
10229 // touchinputmapper design sends a move after button release
10230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10231 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10234
10235 // FINGER 0 UP
10236 processId(mapper, -1);
10237 processSync(mapper);
10238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10239 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10240
10241 // FINGER 1 MOVE
10242 processSlot(mapper, 1);
10243 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10244 processSync(mapper);
10245
10246 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10249 ASSERT_EQ(1U, args.pointerCount);
10250 ASSERT_EQ(1, args.pointerProperties[0].id);
10251 ASSERT_NO_FATAL_FAILURE(
10252 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10253
10254 // FINGER 1 UP
10255 processId(mapper, -1);
10256 processKey(mapper, BTN_TOUCH, 0);
10257 processSync(mapper);
10258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10259 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10260
Josep del Río2d8c79a2023-01-23 19:33:50 +000010261 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010262 mFakePolicy->setPointerCapture(false);
10263 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010265 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010266}
10267
10268TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10269 std::shared_ptr<FakePointerController> fakePointerController =
10270 std::make_shared<FakePointerController>();
10271 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10272 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010273
10274 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010275 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010276 prepareAxes(POSITION | ID | SLOT);
10277 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10278 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010279 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010280 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10281 // run uncaptured pointer tests - pushes out generic events
10282 // FINGER 0 DOWN
10283 processId(mapper, 3);
10284 processPosition(mapper, 100, 100);
10285 processKey(mapper, BTN_TOUCH, 1);
10286 processSync(mapper);
10287
10288 // start at (100,100), cursor should be at (0,0) * scale
10289 NotifyMotionArgs args;
10290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10291 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10292 ASSERT_NO_FATAL_FAILURE(
10293 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10294
10295 // FINGER 0 MOVE
10296 processPosition(mapper, 200, 200);
10297 processSync(mapper);
10298
10299 // compute scaling to help with touch position checking
10300 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10301 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10302 float scale =
10303 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10304
10305 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10307 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10309 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010310
10311 // BUTTON DOWN
10312 processKey(mapper, BTN_LEFT, 1);
10313 processSync(mapper);
10314
10315 // touchinputmapper design sends a move before button press
10316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10317 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10319 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10320
10321 // BUTTON UP
10322 processKey(mapper, BTN_LEFT, 0);
10323 processSync(mapper);
10324
10325 // touchinputmapper design sends a move after button release
10326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10327 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10329 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010330}
10331
10332TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10333 std::shared_ptr<FakePointerController> fakePointerController =
10334 std::make_shared<FakePointerController>();
10335
Michael Wrighta9cf4192022-12-01 23:46:39 +000010336 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010337 prepareAxes(POSITION | ID | SLOT);
10338 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010339 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010340 mFakePolicy->setPointerCapture(false);
10341 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10342
Josep del Río2d8c79a2023-01-23 19:33:50 +000010343 // uncaptured touchpad should be a pointer device
10344 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010345
Josep del Río2d8c79a2023-01-23 19:33:50 +000010346 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010347 mFakePolicy->setPointerCapture(true);
10348 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10349 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10350}
10351
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010352// --- BluetoothMultiTouchInputMapperTest ---
10353
10354class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10355protected:
10356 void SetUp() override {
10357 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10358 }
10359};
10360
10361TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10362 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010363 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010364 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10365 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10366
10367 nsecs_t kernelEventTime = ARBITRARY_TIME;
10368 nsecs_t expectedEventTime = ARBITRARY_TIME;
10369 // Touch down.
10370 processId(mapper, FIRST_TRACKING_ID);
10371 processPosition(mapper, 100, 200);
10372 processPressure(mapper, RAW_PRESSURE_MAX);
10373 processSync(mapper, ARBITRARY_TIME);
10374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10375 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10376
10377 // Process several events that come in quick succession, according to their timestamps.
10378 for (int i = 0; i < 3; i++) {
10379 constexpr static nsecs_t delta = ms2ns(1);
10380 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10381 kernelEventTime += delta;
10382 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10383
10384 processPosition(mapper, 101 + i, 201 + i);
10385 processSync(mapper, kernelEventTime);
10386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10387 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10388 WithEventTime(expectedEventTime))));
10389 }
10390
10391 // Release the touch.
10392 processId(mapper, INVALID_TRACKING_ID);
10393 processPressure(mapper, RAW_PRESSURE_MIN);
10394 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10396 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10397 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10398}
10399
10400// --- MultiTouchPointerModeTest ---
10401
HQ Liue6983c72022-04-19 22:14:56 +000010402class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10403protected:
10404 float mPointerMovementScale;
10405 float mPointerXZoomScale;
10406 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10407 addConfigurationProperty("touch.deviceType", "pointer");
10408 std::shared_ptr<FakePointerController> fakePointerController =
10409 std::make_shared<FakePointerController>();
10410 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10411 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010412 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010413
10414 prepareAxes(POSITION);
10415 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10416 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10417 // needs to be disabled, and the pointer gesture needs to be enabled.
10418 mFakePolicy->setPointerCapture(false);
10419 mFakePolicy->setPointerGestureEnabled(true);
10420 mFakePolicy->setPointerController(fakePointerController);
10421
10422 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10423 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10424 mPointerMovementScale =
10425 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10426 mPointerXZoomScale =
10427 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10428 }
10429
10430 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10431 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10432 /*flat*/ 0,
10433 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10434 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10435 /*flat*/ 0,
10436 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10437 }
10438};
10439
10440/**
10441 * Two fingers down on a pointer mode touch pad. The width
10442 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10443 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10444 * be greater than the both value to be freeform gesture, so that after two
10445 * fingers start to move downwards, the gesture should be swipe.
10446 */
10447TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10448 // The min freeform gesture width is 25units/mm x 30mm = 750
10449 // which is greater than fraction of the diagnal length of the touchpad (349).
10450 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010451 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010452 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10453 NotifyMotionArgs motionArgs;
10454
10455 // Two fingers down at once.
10456 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10457 // Pointer's initial position is used the [0,0] coordinate.
10458 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10459
10460 processId(mapper, FIRST_TRACKING_ID);
10461 processPosition(mapper, x1, y1);
10462 processMTSync(mapper);
10463 processId(mapper, SECOND_TRACKING_ID);
10464 processPosition(mapper, x2, y2);
10465 processMTSync(mapper);
10466 processSync(mapper);
10467
10468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10469 ASSERT_EQ(1U, motionArgs.pointerCount);
10470 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010472 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010473 ASSERT_NO_FATAL_FAILURE(
10474 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10475
10476 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10477 // that there should be 1 pointer.
10478 int32_t movingDistance = 200;
10479 y1 += movingDistance;
10480 y2 += movingDistance;
10481
10482 processId(mapper, FIRST_TRACKING_ID);
10483 processPosition(mapper, x1, y1);
10484 processMTSync(mapper);
10485 processId(mapper, SECOND_TRACKING_ID);
10486 processPosition(mapper, x2, y2);
10487 processMTSync(mapper);
10488 processSync(mapper);
10489
10490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10491 ASSERT_EQ(1U, motionArgs.pointerCount);
10492 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010494 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10496 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10497 0, 0, 0, 0));
10498}
10499
10500/**
10501 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10502 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10503 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10504 * value to be freeform gesture, so that after two fingers start to move downwards,
10505 * the gesture should be swipe.
10506 */
10507TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10508 // The min freeform gesture width is 5units/mm x 30mm = 150
10509 // which is greater than fraction of the diagnal length of the touchpad (349).
10510 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010511 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
HQ Liue6983c72022-04-19 22:14:56 +000010512 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10513 NotifyMotionArgs motionArgs;
10514
10515 // Two fingers down at once.
10516 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10517 // Pointer's initial position is used the [0,0] coordinate.
10518 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10519
10520 processId(mapper, FIRST_TRACKING_ID);
10521 processPosition(mapper, x1, y1);
10522 processMTSync(mapper);
10523 processId(mapper, SECOND_TRACKING_ID);
10524 processPosition(mapper, x2, y2);
10525 processMTSync(mapper);
10526 processSync(mapper);
10527
10528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10529 ASSERT_EQ(1U, motionArgs.pointerCount);
10530 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10531 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010532 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010533 ASSERT_NO_FATAL_FAILURE(
10534 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10535
10536 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10537 // and there should be 1 pointer.
10538 int32_t movingDistance = 200;
10539 y1 += movingDistance;
10540 y2 += movingDistance;
10541
10542 processId(mapper, FIRST_TRACKING_ID);
10543 processPosition(mapper, x1, y1);
10544 processMTSync(mapper);
10545 processId(mapper, SECOND_TRACKING_ID);
10546 processPosition(mapper, x2, y2);
10547 processMTSync(mapper);
10548 processSync(mapper);
10549
10550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10551 ASSERT_EQ(1U, motionArgs.pointerCount);
10552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010554 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010555 // New coordinate is the scaled relative coordinate from the initial coordinate.
10556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10557 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10558 0, 0, 0, 0));
10559}
10560
10561/**
10562 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10563 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10564 * freeform gestures after two fingers start to move downwards.
10565 */
10566TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010567 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010568 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10569
10570 NotifyMotionArgs motionArgs;
10571
10572 // Two fingers down at once. Wider than the max swipe width.
10573 // The gesture is expected to be PRESS, then transformed to FREEFORM
10574 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10575
10576 processId(mapper, FIRST_TRACKING_ID);
10577 processPosition(mapper, x1, y1);
10578 processMTSync(mapper);
10579 processId(mapper, SECOND_TRACKING_ID);
10580 processPosition(mapper, x2, y2);
10581 processMTSync(mapper);
10582 processSync(mapper);
10583
10584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10585 ASSERT_EQ(1U, motionArgs.pointerCount);
10586 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010588 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010589 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10590 ASSERT_NO_FATAL_FAILURE(
10591 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10592
10593 int32_t movingDistance = 200;
10594
10595 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10596 // then two down events for two pointers.
10597 y1 += movingDistance;
10598 y2 += movingDistance;
10599
10600 processId(mapper, FIRST_TRACKING_ID);
10601 processPosition(mapper, x1, y1);
10602 processMTSync(mapper);
10603 processId(mapper, SECOND_TRACKING_ID);
10604 processPosition(mapper, x2, y2);
10605 processMTSync(mapper);
10606 processSync(mapper);
10607
10608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10609 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10610 ASSERT_EQ(1U, motionArgs.pointerCount);
10611 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10614 ASSERT_EQ(1U, motionArgs.pointerCount);
10615 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010618 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010619 ASSERT_EQ(2U, motionArgs.pointerCount);
10620 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010622 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010623 // Two pointers' scaled relative coordinates from their initial centroid.
10624 // Initial y coordinates are 0 as y1 and y2 have the same value.
10625 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10626 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10627 // When pointers move, the new coordinates equal to the initial coordinates plus
10628 // scaled moving distance.
10629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10630 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10631 0, 0, 0, 0));
10632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10633 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10634 0, 0, 0, 0));
10635
10636 // Move two fingers down again, expect one MOVE motion event.
10637 y1 += movingDistance;
10638 y2 += movingDistance;
10639
10640 processId(mapper, FIRST_TRACKING_ID);
10641 processPosition(mapper, x1, y1);
10642 processMTSync(mapper);
10643 processId(mapper, SECOND_TRACKING_ID);
10644 processPosition(mapper, x2, y2);
10645 processMTSync(mapper);
10646 processSync(mapper);
10647
10648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10649 ASSERT_EQ(2U, motionArgs.pointerCount);
10650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010652 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10654 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10655 0, 0, 0, 0, 0));
10656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10657 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10658 0, 0, 0, 0, 0));
10659}
10660
Harry Cutts39b7ca22022-10-05 15:55:48 +000010661TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010662 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Harry Cutts39b7ca22022-10-05 15:55:48 +000010663 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10664 NotifyMotionArgs motionArgs;
10665
10666 // Place two fingers down.
10667 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10668
10669 processId(mapper, FIRST_TRACKING_ID);
10670 processPosition(mapper, x1, y1);
10671 processMTSync(mapper);
10672 processId(mapper, SECOND_TRACKING_ID);
10673 processPosition(mapper, x2, y2);
10674 processMTSync(mapper);
10675 processSync(mapper);
10676
10677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10678 ASSERT_EQ(1U, motionArgs.pointerCount);
10679 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10680 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10681 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10682 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10683
10684 // Move the two fingers down and to the left.
10685 int32_t movingDistance = 200;
10686 x1 -= movingDistance;
10687 y1 += movingDistance;
10688 x2 -= movingDistance;
10689 y2 += movingDistance;
10690
10691 processId(mapper, FIRST_TRACKING_ID);
10692 processPosition(mapper, x1, y1);
10693 processMTSync(mapper);
10694 processId(mapper, SECOND_TRACKING_ID);
10695 processPosition(mapper, x2, y2);
10696 processMTSync(mapper);
10697 processSync(mapper);
10698
10699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10700 ASSERT_EQ(1U, motionArgs.pointerCount);
10701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10702 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10703 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10704 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10705}
10706
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010707TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010708 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010709 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10710 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10712
10713 // Start a stylus gesture.
10714 processKey(mapper, BTN_TOOL_PEN, 1);
10715 processId(mapper, FIRST_TRACKING_ID);
10716 processPosition(mapper, 100, 200);
10717 processSync(mapper);
10718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10719 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10720 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10721 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10722 // TODO(b/257078296): Pointer mode generates extra event.
10723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10724 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10725 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10726 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10728
10729 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10730 // gesture should be disabled.
10731 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10732 viewport->isActive = false;
10733 mFakePolicy->updateViewport(*viewport);
10734 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10736 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10737 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10738 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10739 // TODO(b/257078296): Pointer mode generates extra event.
10740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10741 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10742 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10743 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10745}
10746
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010747// --- JoystickInputMapperTest ---
10748
10749class JoystickInputMapperTest : public InputMapperTest {
10750protected:
10751 static const int32_t RAW_X_MIN;
10752 static const int32_t RAW_X_MAX;
10753 static const int32_t RAW_Y_MIN;
10754 static const int32_t RAW_Y_MAX;
10755
10756 void SetUp() override {
10757 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10758 }
10759 void prepareAxes() {
10760 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10761 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10762 }
10763
10764 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10765 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10766 }
10767
10768 void processSync(JoystickInputMapper& mapper) {
10769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10770 }
10771
Michael Wrighta9cf4192022-12-01 23:46:39 +000010772 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010773 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10774 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10775 NO_PORT, ViewportType::VIRTUAL);
10776 }
10777};
10778
10779const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10780const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10781const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10782const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10783
10784TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10785 prepareAxes();
10786 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10787
10788 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10789
Michael Wrighta9cf4192022-12-01 23:46:39 +000010790 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010791
10792 // Send an axis event
10793 processAxis(mapper, ABS_X, 100);
10794 processSync(mapper);
10795
10796 NotifyMotionArgs args;
10797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10798 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10799
10800 // Send another axis event
10801 processAxis(mapper, ABS_Y, 100);
10802 processSync(mapper);
10803
10804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10805 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10806}
10807
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010808// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010809
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010810class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010811protected:
10812 static const char* DEVICE_NAME;
10813 static const char* DEVICE_LOCATION;
10814 static const int32_t DEVICE_ID;
10815 static const int32_t DEVICE_GENERATION;
10816 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010817 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010818 static const int32_t EVENTHUB_ID;
10819
10820 std::shared_ptr<FakeEventHub> mFakeEventHub;
10821 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010822 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010823 std::unique_ptr<InstrumentedInputReader> mReader;
10824 std::shared_ptr<InputDevice> mDevice;
10825
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010826 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010827 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010828 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010829 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010830 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010831 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010832 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10833 }
10834
10835 void SetUp() override { SetUp(DEVICE_CLASSES); }
10836
10837 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010838 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010839 mFakePolicy.clear();
10840 }
10841
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010842 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010843 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10844 mReader->requestRefreshConfiguration(changes);
10845 mReader->loopOnce();
10846 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010847 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010848 }
10849
10850 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10851 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010852 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010853 InputDeviceIdentifier identifier;
10854 identifier.name = name;
10855 identifier.location = location;
10856 std::shared_ptr<InputDevice> device =
10857 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10858 identifier);
10859 mReader->pushNextDevice(device);
10860 mFakeEventHub->addDevice(eventHubId, name, classes);
10861 mReader->loopOnce();
10862 return device;
10863 }
10864
10865 template <class T, typename... Args>
10866 T& addControllerAndConfigure(Args... args) {
10867 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10868
10869 return controller;
10870 }
10871};
10872
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010873const char* PeripheralControllerTest::DEVICE_NAME = "device";
10874const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10875const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10876const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10877const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010878const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10879 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010880const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010881
10882// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010883class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010884protected:
10885 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010886 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010887 }
10888};
10889
10890TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010891 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010892
Harry Cuttsa5b71292022-11-28 12:56:17 +000010893 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10894 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10895 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010896}
10897
10898TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010899 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010900
Harry Cuttsa5b71292022-11-28 12:56:17 +000010901 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10902 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10903 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010904}
10905
10906// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010907class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010908protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010909 void SetUp() override {
10910 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10911 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010912};
10913
Chris Ye85758332021-05-16 23:05:17 -070010914TEST_F(LightControllerTest, MonoLight) {
10915 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010916 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010917 .maxBrightness = 255,
10918 .flags = InputLightClass::BRIGHTNESS,
10919 .path = ""};
10920 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010921
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010922 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010923 InputDeviceInfo info;
10924 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010925 std::vector<InputDeviceLightInfo> lights = info.getLights();
10926 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010927 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10928 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10929
10930 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10931 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10932}
10933
10934TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10935 RawLightInfo infoMono = {.id = 1,
10936 .name = "mono_keyboard_backlight",
10937 .maxBrightness = 255,
10938 .flags = InputLightClass::BRIGHTNESS |
10939 InputLightClass::KEYBOARD_BACKLIGHT,
10940 .path = ""};
10941 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10942
10943 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10944 InputDeviceInfo info;
10945 controller.populateDeviceInfo(&info);
10946 std::vector<InputDeviceLightInfo> lights = info.getLights();
10947 ASSERT_EQ(1U, lights.size());
10948 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10949 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010950
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010951 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10952 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010953}
10954
10955TEST_F(LightControllerTest, RGBLight) {
10956 RawLightInfo infoRed = {.id = 1,
10957 .name = "red",
10958 .maxBrightness = 255,
10959 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10960 .path = ""};
10961 RawLightInfo infoGreen = {.id = 2,
10962 .name = "green",
10963 .maxBrightness = 255,
10964 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10965 .path = ""};
10966 RawLightInfo infoBlue = {.id = 3,
10967 .name = "blue",
10968 .maxBrightness = 255,
10969 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10970 .path = ""};
10971 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10972 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10973 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10974
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010975 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010976 InputDeviceInfo info;
10977 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010978 std::vector<InputDeviceLightInfo> lights = info.getLights();
10979 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010980 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10981 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10982 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10983
10984 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10985 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10986}
10987
10988TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10989 RawLightInfo infoRed = {.id = 1,
10990 .name = "red_keyboard_backlight",
10991 .maxBrightness = 255,
10992 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10993 InputLightClass::KEYBOARD_BACKLIGHT,
10994 .path = ""};
10995 RawLightInfo infoGreen = {.id = 2,
10996 .name = "green_keyboard_backlight",
10997 .maxBrightness = 255,
10998 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10999 InputLightClass::KEYBOARD_BACKLIGHT,
11000 .path = ""};
11001 RawLightInfo infoBlue = {.id = 3,
11002 .name = "blue_keyboard_backlight",
11003 .maxBrightness = 255,
11004 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11005 InputLightClass::KEYBOARD_BACKLIGHT,
11006 .path = ""};
11007 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11008 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11009 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11010
11011 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11012 InputDeviceInfo info;
11013 controller.populateDeviceInfo(&info);
11014 std::vector<InputDeviceLightInfo> lights = info.getLights();
11015 ASSERT_EQ(1U, lights.size());
11016 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11017 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11018 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11019
11020 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11021 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11022}
11023
11024TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11025 RawLightInfo infoRed = {.id = 1,
11026 .name = "red",
11027 .maxBrightness = 255,
11028 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11029 .path = ""};
11030 RawLightInfo infoGreen = {.id = 2,
11031 .name = "green",
11032 .maxBrightness = 255,
11033 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11034 .path = ""};
11035 RawLightInfo infoBlue = {.id = 3,
11036 .name = "blue",
11037 .maxBrightness = 255,
11038 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11039 .path = ""};
11040 RawLightInfo infoGlobal = {.id = 3,
11041 .name = "global_keyboard_backlight",
11042 .maxBrightness = 255,
11043 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11044 InputLightClass::KEYBOARD_BACKLIGHT,
11045 .path = ""};
11046 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11047 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11048 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11049 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11050
11051 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11052 InputDeviceInfo info;
11053 controller.populateDeviceInfo(&info);
11054 std::vector<InputDeviceLightInfo> lights = info.getLights();
11055 ASSERT_EQ(1U, lights.size());
11056 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11057 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11058 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011059
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011060 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11061 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011062}
11063
11064TEST_F(LightControllerTest, MultiColorRGBLight) {
11065 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011066 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011067 .maxBrightness = 255,
11068 .flags = InputLightClass::BRIGHTNESS |
11069 InputLightClass::MULTI_INTENSITY |
11070 InputLightClass::MULTI_INDEX,
11071 .path = ""};
11072
11073 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11074
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011075 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011076 InputDeviceInfo info;
11077 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011078 std::vector<InputDeviceLightInfo> lights = info.getLights();
11079 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011080 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11081 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11082 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11083
11084 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11085 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11086}
11087
11088TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11089 RawLightInfo infoColor = {.id = 1,
11090 .name = "multi_color_keyboard_backlight",
11091 .maxBrightness = 255,
11092 .flags = InputLightClass::BRIGHTNESS |
11093 InputLightClass::MULTI_INTENSITY |
11094 InputLightClass::MULTI_INDEX |
11095 InputLightClass::KEYBOARD_BACKLIGHT,
11096 .path = ""};
11097
11098 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11099
11100 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11101 InputDeviceInfo info;
11102 controller.populateDeviceInfo(&info);
11103 std::vector<InputDeviceLightInfo> lights = info.getLights();
11104 ASSERT_EQ(1U, lights.size());
11105 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11106 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11107 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011108
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011109 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11110 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011111}
11112
11113TEST_F(LightControllerTest, PlayerIdLight) {
11114 RawLightInfo info1 = {.id = 1,
11115 .name = "player1",
11116 .maxBrightness = 255,
11117 .flags = InputLightClass::BRIGHTNESS,
11118 .path = ""};
11119 RawLightInfo info2 = {.id = 2,
11120 .name = "player2",
11121 .maxBrightness = 255,
11122 .flags = InputLightClass::BRIGHTNESS,
11123 .path = ""};
11124 RawLightInfo info3 = {.id = 3,
11125 .name = "player3",
11126 .maxBrightness = 255,
11127 .flags = InputLightClass::BRIGHTNESS,
11128 .path = ""};
11129 RawLightInfo info4 = {.id = 4,
11130 .name = "player4",
11131 .maxBrightness = 255,
11132 .flags = InputLightClass::BRIGHTNESS,
11133 .path = ""};
11134 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11135 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11136 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11137 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11138
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011139 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011140 InputDeviceInfo info;
11141 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011142 std::vector<InputDeviceLightInfo> lights = info.getLights();
11143 ASSERT_EQ(1U, lights.size());
11144 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011145 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11146 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011147
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011148 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11149 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11150 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011151}
11152
Michael Wrightd02c5b62014-02-10 15:10:22 -080011153} // namespace android