blob: 2223b35183babb8cadedd3935207feffcd2b79c6 [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
Arpit Singh4be4eef2023-03-28 14:26:01 +0000266 std::list<NotifyArgs> reconfigure(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
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000647TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
648 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
649 mFakeEventHub->setSysfsRootPath(1, "xyz");
650
651 // Should also have received a notification describing the new input device.
652 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
653 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
654 ASSERT_EQ(0U, inputDevice.getLights().size());
655
656 RawLightInfo infoMonolight = {.id = 123,
657 .name = "mono_keyboard_backlight",
658 .maxBrightness = 255,
659 .flags = InputLightClass::BRIGHTNESS,
660 .path = ""};
661 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
662 mReader->sysfsNodeChanged("xyz");
663 mReader->loopOnce();
664
665 // Should also have received a notification describing the new recreated input device.
666 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
667 inputDevice = mFakePolicy->getInputDevices()[0];
668 ASSERT_EQ(1U, inputDevice.getLights().size());
669}
670
Chris Yee7310032020-09-22 15:36:28 -0700671TEST_F(InputReaderTest, GetMergedInputDevices) {
672 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
673 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
674 // Add two subdevices to device
675 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
676 // Must add at least one mapper or the device will be ignored!
677 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
678 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
679
680 // Push same device instance for next device to be added, so they'll have same identifier.
681 mReader->pushNextDevice(device);
682 mReader->pushNextDevice(device);
683 ASSERT_NO_FATAL_FAILURE(
684 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
685 ASSERT_NO_FATAL_FAILURE(
686 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
687
688 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000689 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700690}
691
Chris Yee14523a2020-12-19 13:46:00 -0800692TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
693 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
694 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
695 // Add two subdevices to device
696 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
697 // Must add at least one mapper or the device will be ignored!
698 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
699 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
700
701 // Push same device instance for next device to be added, so they'll have same identifier.
702 mReader->pushNextDevice(device);
703 mReader->pushNextDevice(device);
704 // Sensor device is initially disabled
705 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
706 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
707 nullptr));
708 // Device is disabled because the only sub device is a sensor device and disabled initially.
709 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
710 ASSERT_FALSE(device->isEnabled());
711 ASSERT_NO_FATAL_FAILURE(
712 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
713 // The merged device is enabled if any sub device is enabled
714 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
715 ASSERT_TRUE(device->isEnabled());
716}
717
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700718TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800719 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700720 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800721 constexpr int32_t eventHubId = 1;
722 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700723 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800724 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800725 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800726 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700727
Yi Kong9b14ac62018-07-17 13:48:38 -0700728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700729
730 NotifyDeviceResetArgs resetArgs;
731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700732 ASSERT_EQ(deviceId, resetArgs.deviceId);
733
734 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800735 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000736 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700737
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700739 ASSERT_EQ(deviceId, resetArgs.deviceId);
740 ASSERT_EQ(device->isEnabled(), false);
741
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800742 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000743 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700746 ASSERT_EQ(device->isEnabled(), false);
747
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800748 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000749 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700751 ASSERT_EQ(deviceId, resetArgs.deviceId);
752 ASSERT_EQ(device->isEnabled(), true);
753}
754
Michael Wrightd02c5b62014-02-10 15:10:22 -0800755TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800756 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700757 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800758 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800759 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800760 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800761 AINPUT_SOURCE_KEYBOARD, nullptr);
762 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763
764 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
765 AINPUT_SOURCE_ANY, AKEYCODE_A))
766 << "Should return unknown when the device id is >= 0 but unknown.";
767
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800768 ASSERT_EQ(AKEY_STATE_UNKNOWN,
769 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
770 << "Should return unknown when the device id is valid but the sources are not "
771 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800772
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800773 ASSERT_EQ(AKEY_STATE_DOWN,
774 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
775 AKEYCODE_A))
776 << "Should return value provided by mapper when device id is valid and the device "
777 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800778
779 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
780 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
781 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
782
783 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
784 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
785 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
786}
787
Philip Junker4af3b3d2021-12-14 10:36:55 +0100788TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
789 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
790 constexpr int32_t eventHubId = 1;
791 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
792 InputDeviceClass::KEYBOARD,
793 AINPUT_SOURCE_KEYBOARD, nullptr);
794 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
795
796 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
797 << "Should return unknown when the device with the specified id is not found.";
798
799 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
800 << "Should return correct mapping when device id is valid and mapping exists.";
801
802 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
803 << "Should return the location key code when device id is valid and there's no "
804 "mapping.";
805}
806
807TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
808 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
809 constexpr int32_t eventHubId = 1;
810 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
811 InputDeviceClass::JOYSTICK,
812 AINPUT_SOURCE_GAMEPAD, nullptr);
813 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
814
815 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
816 << "Should return unknown when the device id is valid but there is no keyboard mapper";
817}
818
Michael Wrightd02c5b62014-02-10 15:10:22 -0800819TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800820 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700821 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800822 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800823 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800824 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800825 AINPUT_SOURCE_KEYBOARD, nullptr);
826 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827
828 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
829 AINPUT_SOURCE_ANY, KEY_A))
830 << "Should return unknown when the device id is >= 0 but unknown.";
831
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800832 ASSERT_EQ(AKEY_STATE_UNKNOWN,
833 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
834 << "Should return unknown when the device id is valid but the sources are not "
835 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800837 ASSERT_EQ(AKEY_STATE_DOWN,
838 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
839 KEY_A))
840 << "Should return value provided by mapper when device id is valid and the device "
841 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800842
843 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
844 AINPUT_SOURCE_TRACKBALL, KEY_A))
845 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
846
847 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
848 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
849 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
850}
851
852TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800853 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700854 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800855 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800856 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800857 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800858 AINPUT_SOURCE_KEYBOARD, nullptr);
859 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860
861 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
862 AINPUT_SOURCE_ANY, SW_LID))
863 << "Should return unknown when the device id is >= 0 but unknown.";
864
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800865 ASSERT_EQ(AKEY_STATE_UNKNOWN,
866 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
867 << "Should return unknown when the device id is valid but the sources are not "
868 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800870 ASSERT_EQ(AKEY_STATE_DOWN,
871 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
872 SW_LID))
873 << "Should return value provided by mapper when device id is valid and the device "
874 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875
876 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
877 AINPUT_SOURCE_TRACKBALL, SW_LID))
878 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
879
880 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
881 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
882 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
883}
884
885TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800886 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700887 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800888 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800889 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800890 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800891 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100892
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800893 mapper.addSupportedKeyCode(AKEYCODE_A);
894 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700896 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800897 uint8_t flags[4] = { 0, 0, 0, 1 };
898
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700899 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900 << "Should return false when device id is >= 0 but unknown.";
901 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
902
903 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700904 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800905 << "Should return false when device id is valid but the sources are not supported by "
906 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
908
909 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700910 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800911 keyCodes, flags))
912 << "Should return value provided by mapper when device id is valid and the device "
913 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800914 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
915
916 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700917 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
918 << "Should return false when the device id is < 0 but the sources are not supported by "
919 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800920 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
921
922 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700923 ASSERT_TRUE(
924 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
925 << "Should return value provided by mapper when device id is < 0 and one of the "
926 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
928}
929
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000930TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800931 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700932 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933
934 NotifyConfigurationChangedArgs args;
935
936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
937 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
938}
939
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000940TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
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;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000943 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800944 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000945 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800946 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800947 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800948 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000950 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000951 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
953
954 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800955 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000956 ASSERT_EQ(when, event.when);
957 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800958 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959 ASSERT_EQ(EV_KEY, event.type);
960 ASSERT_EQ(KEY_A, event.code);
961 ASSERT_EQ(1, event.value);
962}
963
Garfield Tan1c7bc862020-01-28 13:24:04 -0800964TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800965 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700966 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800967 constexpr int32_t eventHubId = 1;
968 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800969 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800970 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800971 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800972 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800973
974 NotifyDeviceResetArgs resetArgs;
975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800976 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800978 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000979 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800981 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800982 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800983
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800984 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000985 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800987 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800988 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800989
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800990 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000991 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800993 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800994 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800995}
996
Garfield Tan1c7bc862020-01-28 13:24:04 -0800997TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
998 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700999 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001000 constexpr int32_t eventHubId = 1;
1001 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1002 // Must add at least one mapper or the device will be ignored!
1003 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001004 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001005 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1006
1007 NotifyDeviceResetArgs resetArgs;
1008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1009 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1010}
1011
Arthur Hungc23540e2018-11-29 20:42:11 +08001012TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001013 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001014 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001015 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001016 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001017 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1018 FakeInputMapper& mapper =
1019 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001020 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001021
1022 const uint8_t hdmi1 = 1;
1023
1024 // Associated touch screen with second display.
1025 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1026
1027 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001028 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001029 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001030 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001031 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001032 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001033 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001034 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001035 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001036
1037 // Add the device, and make sure all of the callbacks are triggered.
1038 // The device is added after the input port associations are processed since
1039 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001040 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001043 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001044
Arthur Hung2c9a3342019-07-23 14:18:59 +08001045 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001046 ASSERT_EQ(deviceId, device->getId());
1047 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1048 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001049
1050 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001051 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001052 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001053 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001054}
1055
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001056TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1057 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001058 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001059 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1060 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1061 // Must add at least one mapper or the device will be ignored!
1062 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1063 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1064 mReader->pushNextDevice(device);
1065 mReader->pushNextDevice(device);
1066 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1067 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1068
1069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1070
1071 NotifyDeviceResetArgs resetArgs;
1072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1073 ASSERT_EQ(deviceId, resetArgs.deviceId);
1074 ASSERT_TRUE(device->isEnabled());
1075 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1076 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1077
1078 disableDevice(deviceId);
1079 mReader->loopOnce();
1080
1081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1082 ASSERT_EQ(deviceId, resetArgs.deviceId);
1083 ASSERT_FALSE(device->isEnabled());
1084 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1085 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1086
1087 enableDevice(deviceId);
1088 mReader->loopOnce();
1089
1090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1091 ASSERT_EQ(deviceId, resetArgs.deviceId);
1092 ASSERT_TRUE(device->isEnabled());
1093 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1094 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1095}
1096
1097TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1098 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001099 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001100 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1101 // Add two subdevices to device
1102 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1103 FakeInputMapper& mapperDevice1 =
1104 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1105 FakeInputMapper& mapperDevice2 =
1106 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1107 mReader->pushNextDevice(device);
1108 mReader->pushNextDevice(device);
1109 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1110 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1111
1112 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1113 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1114
1115 ASSERT_EQ(AKEY_STATE_DOWN,
1116 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1117 ASSERT_EQ(AKEY_STATE_DOWN,
1118 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1119 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1120 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1121}
1122
Prabir Pradhan7e186182020-11-10 13:56:45 -08001123TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1124 NotifyPointerCaptureChangedArgs args;
1125
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001126 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001127 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1128 mReader->loopOnce();
1129 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001130 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1131 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001132
1133 mFakePolicy->setPointerCapture(false);
1134 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1135 mReader->loopOnce();
1136 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001137 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001138
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001139 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001140 // does not change.
1141 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1142 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001143 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001144}
1145
Chris Ye87143712020-11-10 05:05:58 +00001146class FakeVibratorInputMapper : public FakeInputMapper {
1147public:
1148 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1149 : FakeInputMapper(deviceContext, sources) {}
1150
1151 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1152};
1153
1154TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1155 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001156 ftl::Flags<InputDeviceClass> deviceClass =
1157 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001158 constexpr int32_t eventHubId = 1;
1159 const char* DEVICE_LOCATION = "BLUETOOTH";
1160 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1161 FakeVibratorInputMapper& mapper =
1162 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1163 mReader->pushNextDevice(device);
1164
1165 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1166 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1167
1168 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1169 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1170}
1171
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001172// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001173
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001174class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001175public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001176 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001177
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001178 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001179
Andy Chenf9f1a022022-08-29 20:07:10 -04001180 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1181
Chris Yee2b1e5c2021-03-10 22:45:12 -08001182 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1183
1184 void dump(std::string& dump) override {}
1185
1186 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1187 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001188 }
1189
Chris Yee2b1e5c2021-03-10 22:45:12 -08001190 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1191 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001192 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001193
1194 bool setLightColor(int32_t lightId, int32_t color) override {
1195 getDeviceContext().setLightBrightness(lightId, color >> 24);
1196 return true;
1197 }
1198
1199 std::optional<int32_t> getLightColor(int32_t lightId) override {
1200 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1201 if (!result.has_value()) {
1202 return std::nullopt;
1203 }
1204 return result.value() << 24;
1205 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001206
1207 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1208
1209 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1210
1211private:
1212 InputDeviceContext& mDeviceContext;
1213 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1214 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001215 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001216};
1217
Chris Yee2b1e5c2021-03-10 22:45:12 -08001218TEST_F(InputReaderTest, BatteryGetCapacity) {
1219 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001220 ftl::Flags<InputDeviceClass> deviceClass =
1221 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001222 constexpr int32_t eventHubId = 1;
1223 const char* DEVICE_LOCATION = "BLUETOOTH";
1224 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001225 FakePeripheralController& controller =
1226 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001227 mReader->pushNextDevice(device);
1228
1229 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1230
Harry Cuttsa5b71292022-11-28 12:56:17 +00001231 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1232 FakeEventHub::BATTERY_CAPACITY);
1233 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001234}
1235
1236TEST_F(InputReaderTest, BatteryGetStatus) {
1237 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001238 ftl::Flags<InputDeviceClass> deviceClass =
1239 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001240 constexpr int32_t eventHubId = 1;
1241 const char* DEVICE_LOCATION = "BLUETOOTH";
1242 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001243 FakePeripheralController& controller =
1244 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001245 mReader->pushNextDevice(device);
1246
1247 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1248
Harry Cuttsa5b71292022-11-28 12:56:17 +00001249 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1250 FakeEventHub::BATTERY_STATUS);
1251 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001252}
1253
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001254TEST_F(InputReaderTest, BatteryGetDevicePath) {
1255 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1256 ftl::Flags<InputDeviceClass> deviceClass =
1257 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1258 constexpr int32_t eventHubId = 1;
1259 const char* DEVICE_LOCATION = "BLUETOOTH";
1260 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1261 device->addController<FakePeripheralController>(eventHubId);
1262 mReader->pushNextDevice(device);
1263
1264 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1265
Harry Cuttsa5b71292022-11-28 12:56:17 +00001266 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001267}
1268
Chris Ye3fdbfef2021-01-06 18:45:18 -08001269TEST_F(InputReaderTest, LightGetColor) {
1270 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001271 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001272 constexpr int32_t eventHubId = 1;
1273 const char* DEVICE_LOCATION = "BLUETOOTH";
1274 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001275 FakePeripheralController& controller =
1276 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001277 mReader->pushNextDevice(device);
1278 RawLightInfo info = {.id = 1,
1279 .name = "Mono",
1280 .maxBrightness = 255,
1281 .flags = InputLightClass::BRIGHTNESS,
1282 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001283 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1284 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001285
1286 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001287
Harry Cutts33476232023-01-30 19:57:29 +00001288 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1289 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1290 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1291 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001292}
1293
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001294// --- InputReaderIntegrationTest ---
1295
1296// These tests create and interact with the InputReader only through its interface.
1297// The InputReader is started during SetUp(), which starts its processing in its own
1298// thread. The tests use linux uinput to emulate input devices.
1299// NOTE: Interacting with the physical device while these tests are running may cause
1300// the tests to fail.
1301class InputReaderIntegrationTest : public testing::Test {
1302protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001303 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001304 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001305 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001306
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001307 std::shared_ptr<FakePointerController> mFakePointerController;
1308
Chris Yea52ade12020-08-27 16:49:20 -07001309 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001310#if !defined(__ANDROID__)
1311 GTEST_SKIP();
1312#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001313 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001314 mFakePointerController = std::make_shared<FakePointerController>();
1315 mFakePolicy->setPointerController(mFakePointerController);
Harry Cutts33476232023-01-30 19:57:29 +00001316 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1317 /*eventDidNotHappenTimeout=*/30ms);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001318
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001319 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1320 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001321 ASSERT_EQ(mReader->start(), OK);
1322
1323 // Since this test is run on a real device, all the input devices connected
1324 // to the test device will show up in mReader. We wait for those input devices to
1325 // show up before beginning the tests.
1326 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1327 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1328 }
1329
Chris Yea52ade12020-08-27 16:49:20 -07001330 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001331#if !defined(__ANDROID__)
1332 return;
1333#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001334 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001335 mReader.reset();
1336 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001337 mFakePolicy.clear();
1338 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001339
1340 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1341 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1342 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1343 [&name](const InputDeviceInfo& info) {
1344 return info.getIdentifier().name == name;
1345 });
1346 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1347 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001348};
1349
1350TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1351 // An invalid input device that is only used for this test.
1352 class InvalidUinputDevice : public UinputDevice {
1353 public:
Harry Cutts33476232023-01-30 19:57:29 +00001354 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001355
1356 private:
1357 void configureDevice(int fd, uinput_user_dev* device) override {}
1358 };
1359
1360 const size_t numDevices = mFakePolicy->getInputDevices().size();
1361
1362 // UinputDevice does not set any event or key bits, so InputReader should not
1363 // consider it as a valid device.
1364 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1365 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1366 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1367 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1368
1369 invalidDevice.reset();
1370 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1371 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1372 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1373}
1374
1375TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1376 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1377
1378 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1379 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1380 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1381 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1382
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001383 const auto device = findDeviceByName(keyboard->getName());
1384 ASSERT_TRUE(device.has_value());
1385 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1386 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1387 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001388
1389 keyboard.reset();
1390 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1391 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1392 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1393}
1394
1395TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1396 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1397 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1398
1399 NotifyConfigurationChangedArgs configChangedArgs;
1400 ASSERT_NO_FATAL_FAILURE(
1401 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001402 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001403 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1404
1405 NotifyKeyArgs keyArgs;
1406 keyboard->pressAndReleaseHomeKey();
1407 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1408 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001409 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001410 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001411 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001412 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001413 prevTimestamp = keyArgs.eventTime;
1414
1415 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1416 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001417 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001418 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001419 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001420}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001421
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001422TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1423 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1424 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1425
1426 const auto device = findDeviceByName(stylus->getName());
1427 ASSERT_TRUE(device.has_value());
1428
Prabir Pradhana3621852022-10-14 18:57:23 +00001429 // An external stylus with buttons should also be recognized as a keyboard.
1430 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001431 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1432 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1433
1434 const auto DOWN =
1435 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1436 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1437
1438 stylus->pressAndReleaseKey(BTN_STYLUS);
1439 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1440 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1441 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1442 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1443
1444 stylus->pressAndReleaseKey(BTN_STYLUS2);
1445 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1446 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1447 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1448 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1449
1450 stylus->pressAndReleaseKey(BTN_STYLUS3);
1451 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1452 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1454 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1455}
1456
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001457/**
1458 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1459 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1460 * are passed to the listener.
1461 */
1462static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1463TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1464 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1465 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1466 NotifyKeyArgs keyArgs;
1467
1468 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1469 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1470 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1471 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1472
1473 controller->pressAndReleaseKey(BTN_GEAR_UP);
1474 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1475 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1476 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1477}
1478
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001479// --- TouchIntegrationTest ---
1480
Arthur Hungaab25622020-01-16 11:22:11 +08001481class TouchIntegrationTest : public InputReaderIntegrationTest {
1482protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001483 const std::string UNIQUE_ID = "local:0";
1484
Chris Yea52ade12020-08-27 16:49:20 -07001485 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001486#if !defined(__ANDROID__)
1487 GTEST_SKIP();
1488#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001489 InputReaderIntegrationTest::SetUp();
1490 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001491 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1492 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001493
1494 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1495 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1496 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001497 const auto info = findDeviceByName(mDevice->getName());
1498 ASSERT_TRUE(info);
1499 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001500 }
1501
1502 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001503 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001504 std::optional<uint8_t> physicalPort,
1505 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001506 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001507 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08001508 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1509 }
1510
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001511 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1512 NotifyMotionArgs args;
1513 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1514 EXPECT_EQ(action, args.action);
1515 ASSERT_EQ(points.size(), args.pointerCount);
1516 for (size_t i = 0; i < args.pointerCount; i++) {
1517 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1518 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1519 }
1520 }
1521
Arthur Hungaab25622020-01-16 11:22:11 +08001522 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001523 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001524};
1525
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001526TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1527 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1528 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1529 // presses).
1530 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1531 mDeviceInfo.getSources());
1532}
1533
Arthur Hungaab25622020-01-16 11:22:11 +08001534TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1535 NotifyMotionArgs args;
1536 const Point centerPoint = mDevice->getCenterPoint();
1537
1538 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001539 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001540 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001541 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001542 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1543 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1544
1545 // ACTION_MOVE
1546 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001547 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001548 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1550
1551 // ACTION_UP
1552 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001553 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001554 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1555 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1556}
1557
1558TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1559 NotifyMotionArgs args;
1560 const Point centerPoint = mDevice->getCenterPoint();
1561
1562 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001563 mDevice->sendSlot(FIRST_SLOT);
1564 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001565 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001566 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001567 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1568 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1569
1570 // ACTION_POINTER_DOWN (Second slot)
1571 const Point secondPoint = centerPoint + Point(100, 100);
1572 mDevice->sendSlot(SECOND_SLOT);
1573 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001574 mDevice->sendDown(secondPoint);
1575 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001576 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001577 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001578
1579 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001580 mDevice->sendMove(secondPoint + Point(1, 1));
1581 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001582 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1584
1585 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001586 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001587 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001588 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001589 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001590
1591 // ACTION_UP
1592 mDevice->sendSlot(FIRST_SLOT);
1593 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001594 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001595 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1596 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1597}
1598
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001599/**
1600 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1601 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1602 * data?
1603 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1604 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1605 * for Pointer 0 only is generated after.
1606 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1607 * events, we will not miss any information.
1608 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1609 * event generated afterwards that contains the newest movement of pointer 0.
1610 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1611 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1612 * losing information about non-palm pointers.
1613 */
1614TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1615 NotifyMotionArgs args;
1616 const Point centerPoint = mDevice->getCenterPoint();
1617
1618 // ACTION_DOWN
1619 mDevice->sendSlot(FIRST_SLOT);
1620 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1621 mDevice->sendDown(centerPoint);
1622 mDevice->sendSync();
1623 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1624
1625 // ACTION_POINTER_DOWN (Second slot)
1626 const Point secondPoint = centerPoint + Point(100, 100);
1627 mDevice->sendSlot(SECOND_SLOT);
1628 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1629 mDevice->sendDown(secondPoint);
1630 mDevice->sendSync();
1631 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1632
1633 // ACTION_MOVE (First slot)
1634 mDevice->sendSlot(FIRST_SLOT);
1635 mDevice->sendMove(centerPoint + Point(5, 5));
1636 // ACTION_POINTER_UP (Second slot)
1637 mDevice->sendSlot(SECOND_SLOT);
1638 mDevice->sendPointerUp();
1639 // Send a single sync for the above 2 pointer updates
1640 mDevice->sendSync();
1641
1642 // First, we should get POINTER_UP for the second pointer
1643 assertReceivedMotion(ACTION_POINTER_1_UP,
1644 {/*first pointer */ centerPoint + Point(5, 5),
1645 /*second pointer*/ secondPoint});
1646
1647 // Next, the MOVE event for the first pointer
1648 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1649}
1650
1651/**
1652 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1653 * move, and then it will go up, all in the same frame.
1654 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1655 * gets sent to the listener.
1656 */
1657TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1658 NotifyMotionArgs args;
1659 const Point centerPoint = mDevice->getCenterPoint();
1660
1661 // ACTION_DOWN
1662 mDevice->sendSlot(FIRST_SLOT);
1663 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1664 mDevice->sendDown(centerPoint);
1665 mDevice->sendSync();
1666 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1667
1668 // ACTION_POINTER_DOWN (Second slot)
1669 const Point secondPoint = centerPoint + Point(100, 100);
1670 mDevice->sendSlot(SECOND_SLOT);
1671 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1672 mDevice->sendDown(secondPoint);
1673 mDevice->sendSync();
1674 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1675
1676 // ACTION_MOVE (First slot)
1677 mDevice->sendSlot(FIRST_SLOT);
1678 mDevice->sendMove(centerPoint + Point(5, 5));
1679 // ACTION_POINTER_UP (Second slot)
1680 mDevice->sendSlot(SECOND_SLOT);
1681 mDevice->sendMove(secondPoint + Point(6, 6));
1682 mDevice->sendPointerUp();
1683 // Send a single sync for the above 2 pointer updates
1684 mDevice->sendSync();
1685
1686 // First, we should get POINTER_UP for the second pointer
1687 // The movement of the second pointer during the liftoff frame is ignored.
1688 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1689 assertReceivedMotion(ACTION_POINTER_1_UP,
1690 {/*first pointer */ centerPoint + Point(5, 5),
1691 /*second pointer*/ secondPoint});
1692
1693 // Next, the MOVE event for the first pointer
1694 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1695}
1696
Arthur Hungaab25622020-01-16 11:22:11 +08001697TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1698 NotifyMotionArgs args;
1699 const Point centerPoint = mDevice->getCenterPoint();
1700
1701 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001702 mDevice->sendSlot(FIRST_SLOT);
1703 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001704 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001705 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001706 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1707 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1708
arthurhungcc7f9802020-04-30 17:55:40 +08001709 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001710 const Point secondPoint = centerPoint + Point(100, 100);
1711 mDevice->sendSlot(SECOND_SLOT);
1712 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1713 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001714 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001715 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001716 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001717
arthurhungcc7f9802020-04-30 17:55:40 +08001718 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001719 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001720 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001721 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1722 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1723
arthurhungcc7f9802020-04-30 17:55:40 +08001724 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1725 // a palm event.
1726 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001727 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001728 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001729 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001730 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001731 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001732
arthurhungcc7f9802020-04-30 17:55:40 +08001733 // Send up to second slot, expect first slot send moving.
1734 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001735 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001736 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1737 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001738
arthurhungcc7f9802020-04-30 17:55:40 +08001739 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001740 mDevice->sendSlot(FIRST_SLOT);
1741 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001742 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001743
arthurhungcc7f9802020-04-30 17:55:40 +08001744 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1745 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001746}
1747
Prabir Pradhanda20b172022-09-26 17:01:18 +00001748TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1749 const Point centerPoint = mDevice->getCenterPoint();
1750
1751 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1752 mDevice->sendSlot(FIRST_SLOT);
1753 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1754 mDevice->sendToolType(MT_TOOL_PEN);
1755 mDevice->sendDown(centerPoint);
1756 mDevice->sendSync();
1757 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1758 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001759 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001760
1761 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1762
1763 // Release the stylus touch.
1764 mDevice->sendUp();
1765 mDevice->sendSync();
1766 ASSERT_NO_FATAL_FAILURE(
1767 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1768
1769 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1770
1771 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1772 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1773 mDevice->sendToolType(MT_TOOL_FINGER);
1774 mDevice->sendDown(centerPoint);
1775 mDevice->sendSync();
1776 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1777 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001778 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001779
1780 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1781
1782 mDevice->sendUp();
1783 mDevice->sendSync();
1784 ASSERT_NO_FATAL_FAILURE(
1785 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1786
1787 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1788 // The policy should be notified of the stylus presence.
1789 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1790 mDevice->sendToolType(MT_TOOL_PEN);
1791 mDevice->sendMove(centerPoint);
1792 mDevice->sendSync();
1793 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1794 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001795 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001796
1797 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1798}
1799
Prabir Pradhan124ea442022-10-28 20:27:44 +00001800// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001801
Prabir Pradhan124ea442022-10-28 20:27:44 +00001802// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1803// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1804// stylus.
1805template <typename UinputStylusDevice>
1806class StylusButtonIntegrationTest : public TouchIntegrationTest {
1807protected:
1808 void SetUp() override {
1809#if !defined(__ANDROID__)
1810 GTEST_SKIP();
1811#endif
1812 TouchIntegrationTest::SetUp();
1813 mTouchscreen = mDevice.get();
1814 mTouchscreenInfo = mDeviceInfo;
1815
1816 setUpStylusDevice();
1817 }
1818
1819 UinputStylusDevice* mStylus{nullptr};
1820 InputDeviceInfo mStylusInfo{};
1821
1822 UinputTouchScreen* mTouchscreen{nullptr};
1823 InputDeviceInfo mTouchscreenInfo{};
1824
1825private:
1826 // When we are attempting to test stylus button events that are sent from the touchscreen,
1827 // use the same Uinput device for the touchscreen and the stylus.
1828 template <typename T = UinputStylusDevice>
1829 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1830 mStylus = mDevice.get();
1831 mStylusInfo = mDeviceInfo;
1832 }
1833
1834 // When we are attempting to stylus buttons from an external stylus being merged with touches
1835 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1836 template <typename T = UinputStylusDevice>
1837 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1838 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1839 mStylus = mStylusDeviceLifecycleTracker.get();
1840 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1841 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1842 const auto info = findDeviceByName(mStylus->getName());
1843 ASSERT_TRUE(info);
1844 mStylusInfo = *info;
1845 }
1846
1847 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1848
1849 // Hide the base class's device to expose it with a different name for readability.
1850 using TouchIntegrationTest::mDevice;
1851 using TouchIntegrationTest::mDeviceInfo;
1852};
1853
1854using StylusButtonIntegrationTestTypes =
1855 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1856TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1857
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001858TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001859 const auto stylusId = TestFixture::mStylusInfo.getId();
1860
1861 TestFixture::mStylus->pressKey(BTN_STYLUS);
1862 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1863 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1864 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1865
1866 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1867 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001868 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001869 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001870}
1871
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001872TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001873 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1874 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1875 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001876
1877 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001878 TestFixture::mStylus->pressKey(BTN_STYLUS);
1879 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001880 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001881 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001882
1883 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001884 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1885 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1886 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1887 TestFixture::mTouchscreen->sendDown(centerPoint);
1888 TestFixture::mTouchscreen->sendSync();
1889 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001890 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001891 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001892 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1893 WithDeviceId(touchscreenId))));
1894 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001895 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001896 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001897 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1898 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001899
Prabir Pradhan124ea442022-10-28 20:27:44 +00001900 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1901 TestFixture::mTouchscreen->sendSync();
1902 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001903 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001904 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001905 WithDeviceId(touchscreenId))));
1906 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001907 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001908 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001909 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001910
1911 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001912 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1913 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001914 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001915 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001916}
1917
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001918TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001919 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1920 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1921 const auto stylusId = TestFixture::mStylusInfo.getId();
1922 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001923 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001924
1925 // Press the stylus button.
1926 TestFixture::mStylus->pressKey(BTN_STYLUS);
1927 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1928 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1929 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1930
1931 // Start hovering with the stylus.
1932 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1933 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1934 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1935 TestFixture::mTouchscreen->sendMove(centerPoint);
1936 TestFixture::mTouchscreen->sendSync();
1937 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1938 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1939 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1940 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1941 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1942 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1943 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1944 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1945 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1946
1947 // Touch down with the stylus.
1948 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1949 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1950 TestFixture::mTouchscreen->sendDown(centerPoint);
1951 TestFixture::mTouchscreen->sendSync();
1952 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1953 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1954 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1955
1956 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1957 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1958 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1959
1960 // Stop touching with the stylus, and start hovering.
1961 TestFixture::mTouchscreen->sendUp();
1962 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1963 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1964 TestFixture::mTouchscreen->sendMove(centerPoint);
1965 TestFixture::mTouchscreen->sendSync();
1966 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1967 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
1968 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1969 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1970 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1971 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1972 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1973 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1974 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1975
1976 // Stop hovering.
1977 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1978 TestFixture::mTouchscreen->sendSync();
1979 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1980 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1981 WithButtonState(0))));
1982 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
1983 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1984 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1985 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1986
1987 // Release the stylus button.
1988 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1989 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1990 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
1991 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1992}
1993
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001994TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001995 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1996 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1997 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001998
1999 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002000 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2001 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2002 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2003 TestFixture::mTouchscreen->sendDown(centerPoint);
2004 TestFixture::mTouchscreen->sendSync();
2005 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002006 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002007 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002008 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002009
2010 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002011 TestFixture::mStylus->pressKey(BTN_STYLUS);
2012 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002013 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002014 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2015 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002016 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002017 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002018 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2019 WithDeviceId(touchscreenId))));
2020 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002021 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002022 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002023 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2024 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002025
Prabir Pradhan124ea442022-10-28 20:27:44 +00002026 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2027 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002028 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002029 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2030 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002031 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002032 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002033 WithDeviceId(touchscreenId))));
2034 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002035 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002036 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002037 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002038
2039 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002040 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2041 TestFixture::mTouchscreen->sendSync();
2042 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002043 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002044 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002045 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002046}
2047
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002048TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002049 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2050 TestFixture::mReader->requestRefreshConfiguration(
2051 InputReaderConfiguration::CHANGE_STYLUS_BUTTON_REPORTING);
2052
2053 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2054 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2055 const auto stylusId = TestFixture::mStylusInfo.getId();
2056
2057 // Start a stylus gesture. By the time this event is processed, the configuration change that
2058 // was requested is guaranteed to be completed.
2059 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2060 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2061 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2062 TestFixture::mTouchscreen->sendDown(centerPoint);
2063 TestFixture::mTouchscreen->sendSync();
2064 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2065 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002066 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002067 WithDeviceId(touchscreenId))));
2068
2069 // Press and release a stylus button. Each change only generates a MOVE motion event.
2070 // Key events are unaffected.
2071 TestFixture::mStylus->pressKey(BTN_STYLUS);
2072 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2073 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2074 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2075 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2076 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002077 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002078 WithDeviceId(touchscreenId))));
2079
2080 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2081 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2082 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2083 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2084 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2085 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002086 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002087 WithDeviceId(touchscreenId))));
2088
2089 // Finish the stylus gesture.
2090 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2091 TestFixture::mTouchscreen->sendSync();
2092 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2093 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002094 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002095 WithDeviceId(touchscreenId))));
2096}
2097
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002098// --- ExternalStylusIntegrationTest ---
2099
2100// Verify the behavior of an external stylus. An external stylus can report pressure or button
2101// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2102// ongoing stylus gesture that is being emitted by the touchscreen.
2103using ExternalStylusIntegrationTest = TouchIntegrationTest;
2104
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002105TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002106 const Point centerPoint = mDevice->getCenterPoint();
2107
2108 // Create an external stylus capable of reporting pressure data that
2109 // should be fused with a touch pointer.
2110 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2111 createUinputDevice<UinputExternalStylusWithPressure>();
2112 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2113 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2114 const auto stylusInfo = findDeviceByName(stylus->getName());
2115 ASSERT_TRUE(stylusInfo);
2116
2117 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2118
2119 const auto touchscreenId = mDeviceInfo.getId();
2120
2121 // Set a pressure value on the stylus. It doesn't generate any events.
2122 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2123 stylus->setPressure(100);
2124 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2125
2126 // Start a finger gesture, and ensure it shows up as stylus gesture
2127 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002128 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002129 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002130 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002131 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002132 mDevice->sendSync();
2133 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2134 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002135 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002136 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002137
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002138 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2139 // event with the updated pressure.
2140 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002141 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2142 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002143 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002144 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002145
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002146 // The external stylus did not generate any events.
2147 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2148 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2149}
2150
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002151TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002152 const Point centerPoint = mDevice->getCenterPoint();
2153
2154 // Create an external stylus capable of reporting pressure data that
2155 // should be fused with a touch pointer.
2156 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2157 createUinputDevice<UinputExternalStylusWithPressure>();
2158 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2159 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2160 const auto stylusInfo = findDeviceByName(stylus->getName());
2161 ASSERT_TRUE(stylusInfo);
2162
2163 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2164
2165 const auto touchscreenId = mDeviceInfo.getId();
2166
2167 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2168 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002169 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2170 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002171 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002172 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002173
2174 // Start a finger gesture. The touch device will withhold generating any touches for
2175 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2176 mDevice->sendSlot(FIRST_SLOT);
2177 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2178 mDevice->sendToolType(MT_TOOL_FINGER);
2179 mDevice->sendDown(centerPoint);
2180 auto waitUntil = std::chrono::system_clock::now() +
2181 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002182 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002183 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002184
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002185 // Since the external stylus did not report a pressure value within the timeout,
2186 // it shows up as a finger pointer.
2187 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2188 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002189 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002190 WithPressure(1.f))));
2191
2192 // Change the pressure on the external stylus. Since the pressure was not present at the start
2193 // of the gesture, it is ignored for now.
2194 stylus->setPressure(200);
2195 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2196
2197 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002198 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2199 mDevice->sendSync();
2200 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2201 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002202 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002203
2204 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2205 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2206 mDevice->sendToolType(MT_TOOL_FINGER);
2207 mDevice->sendDown(centerPoint);
2208 mDevice->sendSync();
2209 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2210 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002211 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002212 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2213
2214 // The external stylus did not generate any events.
2215 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2216 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002217}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002218
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002219TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002220 const Point centerPoint = mDevice->getCenterPoint();
2221
2222 // Create an external stylus device that does not support pressure. It should not affect any
2223 // touch pointers.
2224 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2225 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2226 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2227 const auto stylusInfo = findDeviceByName(stylus->getName());
2228 ASSERT_TRUE(stylusInfo);
2229
2230 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2231
2232 const auto touchscreenId = mDeviceInfo.getId();
2233
2234 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2235 // pressure data from the external stylus.
2236 mDevice->sendSlot(FIRST_SLOT);
2237 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2238 mDevice->sendToolType(MT_TOOL_FINGER);
2239 mDevice->sendDown(centerPoint);
2240 auto waitUntil = std::chrono::system_clock::now() +
2241 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2242 mDevice->sendSync();
2243 ASSERT_NO_FATAL_FAILURE(
2244 mTestListener
2245 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2246 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002247 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002248 WithButtonState(0),
2249 WithDeviceId(touchscreenId),
2250 WithPressure(1.f)),
2251 waitUntil));
2252
2253 // The external stylus did not generate any events.
2254 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2255 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2256}
2257
Michael Wrightd02c5b62014-02-10 15:10:22 -08002258// --- InputDeviceTest ---
2259class InputDeviceTest : public testing::Test {
2260protected:
2261 static const char* DEVICE_NAME;
2262 static const char* DEVICE_LOCATION;
2263 static const int32_t DEVICE_ID;
2264 static const int32_t DEVICE_GENERATION;
2265 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002266 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002267 static const int32_t EVENTHUB_ID;
2268 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2269
2270 std::shared_ptr<FakeEventHub> mFakeEventHub;
2271 sp<FakeInputReaderPolicy> mFakePolicy;
2272 std::unique_ptr<TestInputListener> mFakeListener;
2273 std::unique_ptr<InstrumentedInputReader> mReader;
2274 std::shared_ptr<InputDevice> mDevice;
2275
2276 void SetUp() override {
2277 mFakeEventHub = std::make_unique<FakeEventHub>();
2278 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2279 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002280 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002281 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002282 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002283 identifier.name = DEVICE_NAME;
2284 identifier.location = DEVICE_LOCATION;
2285 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2286 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2287 identifier);
2288 mReader->pushNextDevice(mDevice);
2289 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002290 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002291 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002292
2293 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002294 mFakeListener.reset();
2295 mFakePolicy.clear();
2296 }
2297};
2298
2299const char* InputDeviceTest::DEVICE_NAME = "device";
2300const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2301const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2302const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002303const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002304const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2305 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002306const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002307const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2308
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002309TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002310 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002311 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2312 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002313}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002314
Michael Wrightd02c5b62014-02-10 15:10:22 -08002315TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2316 ASSERT_EQ(mDevice->isEnabled(), false);
2317}
2318
2319TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2320 // Configuration.
2321 InputReaderConfiguration config;
2322 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2323
2324 // Reset.
2325 unused += mDevice->reset(ARBITRARY_TIME);
2326
2327 NotifyDeviceResetArgs resetArgs;
2328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2329 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2330 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2331
2332 // Metadata.
2333 ASSERT_TRUE(mDevice->isIgnored());
2334 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2335
2336 InputDeviceInfo info = mDevice->getDeviceInfo();
2337 ASSERT_EQ(DEVICE_ID, info.getId());
2338 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2339 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2340 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2341
2342 // State queries.
2343 ASSERT_EQ(0, mDevice->getMetaState());
2344
2345 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2346 << "Ignored device should return unknown key code state.";
2347 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2348 << "Ignored device should return unknown scan code state.";
2349 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2350 << "Ignored device should return unknown switch state.";
2351
2352 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2353 uint8_t flags[2] = { 0, 1 };
2354 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2355 << "Ignored device should never mark any key codes.";
2356 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2357 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2358}
2359
2360TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2361 // Configuration.
2362 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002363
2364 FakeInputMapper& mapper1 =
2365 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2366 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002367 mapper1.setMetaState(AMETA_ALT_ON);
2368 mapper1.addSupportedKeyCode(AKEYCODE_A);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002369 mapper1.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002370 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2371 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2372 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2373 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2374 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2375
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002376 FakeInputMapper& mapper2 =
Michael Wrightd02c5b62014-02-10 15:10:22 -08002377 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
2378 mapper2.setMetaState(AMETA_SHIFT_ON);
2379
2380 InputReaderConfiguration config;
2381 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2382
Harry Cuttsf13161a2023-03-08 14:15:49 +00002383 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2384 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002385 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002386 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002387
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002388 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2389 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002390
2391 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002392 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002393 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2394 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002395
2396 NotifyDeviceResetArgs resetArgs;
2397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2398 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2399 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2400
2401 // Metadata.
2402 ASSERT_FALSE(mDevice->isIgnored());
2403 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2404
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002405 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002406 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002407 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002408 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2409 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2410
2411 // State queries.
2412 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2413 << "Should query mappers and combine meta states.";
2414
2415 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2416 << "Should return unknown key code state when source not supported.";
2417 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2418 << "Should return unknown scan code state when source not supported.";
2419 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2420 << "Should return unknown switch state when source not supported.";
2421
2422 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2423 << "Should query mapper when source is supported.";
2424 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2425 << "Should query mapper when source is supported.";
2426 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2427 << "Should query mapper when source is supported.";
2428
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002429 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002431 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432 << "Should do nothing when source is unsupported.";
2433 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2434 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2435 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2436 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2437
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002438 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439 << "Should query mapper when source is supported.";
2440 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2441 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2442 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2443 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2444
2445 // Event handling.
2446 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002447 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002448 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002449
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002450 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2451 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002452}
2453
Arthur Hung2c9a3342019-07-23 14:18:59 +08002454// A single input device is associated with a specific display. Check that:
2455// 1. Device is disabled if the viewport corresponding to the associated display is not found
2456// 2. Device is disabled when setEnabled API is called
2457TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002458 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002459
2460 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002461 std::list<NotifyArgs> unused =
2462 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002463
2464 // Device should be enabled by default.
2465 ASSERT_TRUE(mDevice->isEnabled());
2466
2467 // Prepare associated info.
2468 constexpr uint8_t hdmi = 1;
2469 const std::string UNIQUE_ID = "local:1";
2470
2471 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002472 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2473 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002474 // Device should be disabled because it is associated with a specific display via
2475 // input port <-> display port association, but the corresponding display is not found
2476 ASSERT_FALSE(mDevice->isEnabled());
2477
2478 // Prepare displays.
2479 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002480 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002481 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002482 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2483 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002484 ASSERT_TRUE(mDevice->isEnabled());
2485
2486 // Device should be disabled after set disable.
2487 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002488 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2489 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002490 ASSERT_FALSE(mDevice->isEnabled());
2491
2492 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002493 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2494 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002495 ASSERT_FALSE(mDevice->isEnabled());
2496}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002497
Christine Franks1ba71cc2021-04-07 14:37:42 -07002498TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2499 // Device should be enabled by default.
2500 mFakePolicy->clearViewports();
2501 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002502 std::list<NotifyArgs> unused =
2503 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002504 ASSERT_TRUE(mDevice->isEnabled());
2505
2506 // Device should be disabled because it is associated with a specific display, but the
2507 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002508 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002509 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2510 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002511 ASSERT_FALSE(mDevice->isEnabled());
2512
2513 // Device should be enabled when a display is found.
2514 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002515 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002516 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002517 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2518 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002519 ASSERT_TRUE(mDevice->isEnabled());
2520
2521 // Device should be disabled after set disable.
2522 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002523 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2524 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002525 ASSERT_FALSE(mDevice->isEnabled());
2526
2527 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002528 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2529 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002530 ASSERT_FALSE(mDevice->isEnabled());
2531}
2532
Christine Franks2a2293c2022-01-18 11:51:16 -08002533TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2534 mFakePolicy->clearViewports();
2535 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002536 std::list<NotifyArgs> unused =
2537 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08002538
Christine Franks2a2293c2022-01-18 11:51:16 -08002539 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2540 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002541 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002542 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002543 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2544 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002545 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2546}
2547
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002548/**
2549 * This test reproduces a crash caused by a dangling reference that remains after device is added
2550 * and removed. The reference is accessed in InputDevice::dump(..);
2551 */
2552TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2553 constexpr int32_t TEST_EVENTHUB_ID = 10;
2554 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2555
Harry Cutts33476232023-01-30 19:57:29 +00002556 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
2557 device.addEventHubDevice(TEST_EVENTHUB_ID, /*populateMappers=*/true);
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002558 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2559 std::string dumpStr, eventHubDevStr;
2560 device.dump(dumpStr, eventHubDevStr);
2561}
2562
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002563TEST_F(InputDeviceTest, GetBluetoothAddress) {
2564 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2565 ASSERT_TRUE(address);
2566 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2567}
2568
Michael Wrightd02c5b62014-02-10 15:10:22 -08002569// --- SwitchInputMapperTest ---
2570
2571class SwitchInputMapperTest : public InputMapperTest {
2572protected:
2573};
2574
2575TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002576 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002577
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002578 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002579}
2580
2581TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002582 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002583
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002584 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002585 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002586
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002587 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002588 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002589}
2590
2591TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002592 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002593 std::list<NotifyArgs> out;
2594 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2595 ASSERT_TRUE(out.empty());
2596 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2597 ASSERT_TRUE(out.empty());
2598 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2599 ASSERT_TRUE(out.empty());
2600 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002601
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002602 ASSERT_EQ(1u, out.size());
2603 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002604 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002605 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2606 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607 args.switchMask);
2608 ASSERT_EQ(uint32_t(0), args.policyFlags);
2609}
2610
Chris Ye87143712020-11-10 05:05:58 +00002611// --- VibratorInputMapperTest ---
2612class VibratorInputMapperTest : public InputMapperTest {
2613protected:
2614 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2615};
2616
2617TEST_F(VibratorInputMapperTest, GetSources) {
2618 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2619
2620 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2621}
2622
2623TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2624 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2625
2626 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2627}
2628
2629TEST_F(VibratorInputMapperTest, Vibrate) {
2630 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002631 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002632 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2633
2634 VibrationElement pattern(2);
2635 VibrationSequence sequence(2);
2636 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002637 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2638 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002639 sequence.addElement(pattern);
2640 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002641 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2642 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002643 sequence.addElement(pattern);
2644
2645 std::vector<int64_t> timings = {0, 1};
2646 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2647
2648 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002649 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002650 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002651 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002652 // Verify vibrator state listener was notified.
2653 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002654 ASSERT_EQ(1u, out.size());
2655 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2656 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2657 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002658 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002659 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002660 ASSERT_FALSE(mapper.isVibrating());
2661 // Verify vibrator state listener was notified.
2662 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002663 ASSERT_EQ(1u, out.size());
2664 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2665 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2666 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002667}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668
Chris Yef59a2f42020-10-16 12:55:26 -07002669// --- SensorInputMapperTest ---
2670
2671class SensorInputMapperTest : public InputMapperTest {
2672protected:
2673 static const int32_t ACCEL_RAW_MIN;
2674 static const int32_t ACCEL_RAW_MAX;
2675 static const int32_t ACCEL_RAW_FUZZ;
2676 static const int32_t ACCEL_RAW_FLAT;
2677 static const int32_t ACCEL_RAW_RESOLUTION;
2678
2679 static const int32_t GYRO_RAW_MIN;
2680 static const int32_t GYRO_RAW_MAX;
2681 static const int32_t GYRO_RAW_FUZZ;
2682 static const int32_t GYRO_RAW_FLAT;
2683 static const int32_t GYRO_RAW_RESOLUTION;
2684
2685 static const float GRAVITY_MS2_UNIT;
2686 static const float DEGREE_RADIAN_UNIT;
2687
2688 void prepareAccelAxes();
2689 void prepareGyroAxes();
2690 void setAccelProperties();
2691 void setGyroProperties();
2692 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2693};
2694
2695const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2696const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2697const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2698const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2699const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2700
2701const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2702const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2703const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2704const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2705const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2706
2707const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2708const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2709
2710void SensorInputMapperTest::prepareAccelAxes() {
2711 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2712 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2713 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2714 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2715 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2716 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2717}
2718
2719void SensorInputMapperTest::prepareGyroAxes() {
2720 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2721 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2722 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2723 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2724 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2725 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2726}
2727
2728void SensorInputMapperTest::setAccelProperties() {
2729 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2730 /* sensorDataIndex */ 0);
2731 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2732 /* sensorDataIndex */ 1);
2733 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2734 /* sensorDataIndex */ 2);
2735 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2736 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2737 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2738 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2739 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2740}
2741
2742void SensorInputMapperTest::setGyroProperties() {
2743 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2744 /* sensorDataIndex */ 0);
2745 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2746 /* sensorDataIndex */ 1);
2747 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2748 /* sensorDataIndex */ 2);
2749 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2750 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2751 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2752 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2753 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2754}
2755
2756TEST_F(SensorInputMapperTest, GetSources) {
2757 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2758
2759 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2760}
2761
2762TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2763 setAccelProperties();
2764 prepareAccelAxes();
2765 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2766
2767 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2768 std::chrono::microseconds(10000),
2769 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002770 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002776
2777 NotifySensorArgs args;
2778 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2779 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2780 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2781
2782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2783 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2784 ASSERT_EQ(args.deviceId, DEVICE_ID);
2785 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2786 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2787 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2788 ASSERT_EQ(args.values, values);
2789 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2790}
2791
2792TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2793 setGyroProperties();
2794 prepareGyroAxes();
2795 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2796
2797 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2798 std::chrono::microseconds(10000),
2799 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002800 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2805 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002806
2807 NotifySensorArgs args;
2808 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2809 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2810 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2811
2812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2813 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2814 ASSERT_EQ(args.deviceId, DEVICE_ID);
2815 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2816 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2817 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2818 ASSERT_EQ(args.values, values);
2819 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2820}
2821
Michael Wrightd02c5b62014-02-10 15:10:22 -08002822// --- KeyboardInputMapperTest ---
2823
2824class KeyboardInputMapperTest : public InputMapperTest {
2825protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002826 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002827 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002828 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002829
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002830 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002831 int32_t originalKeyCode, int32_t rotatedKeyCode,
2832 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002833};
2834
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002835/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2836 * orientation.
2837 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002838void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002839 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2840 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002841}
2842
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002843void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002844 int32_t originalScanCode, int32_t originalKeyCode,
2845 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002846 NotifyKeyArgs args;
2847
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002848 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2850 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2851 ASSERT_EQ(originalScanCode, args.scanCode);
2852 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002853 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2857 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2858 ASSERT_EQ(originalScanCode, args.scanCode);
2859 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002860 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861}
2862
Michael Wrightd02c5b62014-02-10 15:10:22 -08002863TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002864 KeyboardInputMapper& mapper =
2865 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2866 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002867
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002868 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002869}
2870
2871TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2872 const int32_t USAGE_A = 0x070004;
2873 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002874 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2875 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002876 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2877 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2878 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002880 KeyboardInputMapper& mapper =
2881 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2882 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002883 // Initial metastate is AMETA_NONE.
2884 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002885
2886 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002887 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002888 NotifyKeyArgs args;
2889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2890 ASSERT_EQ(DEVICE_ID, args.deviceId);
2891 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2892 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2893 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2894 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2895 ASSERT_EQ(KEY_HOME, args.scanCode);
2896 ASSERT_EQ(AMETA_NONE, args.metaState);
2897 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2898 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2899 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2900
2901 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002902 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2904 ASSERT_EQ(DEVICE_ID, args.deviceId);
2905 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2906 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2907 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2908 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2909 ASSERT_EQ(KEY_HOME, args.scanCode);
2910 ASSERT_EQ(AMETA_NONE, args.metaState);
2911 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2912 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2913 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2914
2915 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002916 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2919 ASSERT_EQ(DEVICE_ID, args.deviceId);
2920 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2921 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2922 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2923 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2924 ASSERT_EQ(0, args.scanCode);
2925 ASSERT_EQ(AMETA_NONE, args.metaState);
2926 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2927 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2928 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2929
2930 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2932 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2934 ASSERT_EQ(DEVICE_ID, args.deviceId);
2935 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2936 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2937 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2938 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2939 ASSERT_EQ(0, args.scanCode);
2940 ASSERT_EQ(AMETA_NONE, args.metaState);
2941 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2942 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2943 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2944
2945 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2949 ASSERT_EQ(DEVICE_ID, args.deviceId);
2950 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2951 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2952 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2953 ASSERT_EQ(0, args.keyCode);
2954 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2955 ASSERT_EQ(AMETA_NONE, args.metaState);
2956 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2957 ASSERT_EQ(0U, args.policyFlags);
2958 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2959
2960 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2962 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2964 ASSERT_EQ(DEVICE_ID, args.deviceId);
2965 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2966 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2967 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2968 ASSERT_EQ(0, args.keyCode);
2969 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2970 ASSERT_EQ(AMETA_NONE, args.metaState);
2971 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2972 ASSERT_EQ(0U, args.policyFlags);
2973 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2974}
2975
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00002976TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
2977 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
2978 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
2979 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
2980
2981 KeyboardInputMapper& mapper =
2982 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2983 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2984
2985 // Key down by scan code.
2986 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
2987 NotifyKeyArgs args;
2988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2989 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2990
2991 // Key up by scan code.
2992 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
2993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2994 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2995}
2996
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002997/**
2998 * Ensure that the readTime is set to the time when the EV_KEY is received.
2999 */
3000TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3001 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3002
3003 KeyboardInputMapper& mapper =
3004 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3005 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3006 NotifyKeyArgs args;
3007
3008 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003009 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3011 ASSERT_EQ(12, args.readTime);
3012
3013 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003014 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3016 ASSERT_EQ(15, args.readTime);
3017}
3018
Michael Wrightd02c5b62014-02-10 15:10:22 -08003019TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003020 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3021 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003022 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3023 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3024 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003026 KeyboardInputMapper& mapper =
3027 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3028 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003029
Arthur Hung95f68612022-04-07 14:08:22 +08003030 // Initial metastate is AMETA_NONE.
3031 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003032
3033 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003035 NotifyKeyArgs args;
3036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3037 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003038 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003039 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003040
3041 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003042 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3044 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003045 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003046
3047 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003048 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3050 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003051 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052
3053 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003054 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3056 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003057 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003058 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059}
3060
3061TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003062 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3063 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3064 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3065 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003066
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003067 KeyboardInputMapper& mapper =
3068 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3069 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070
Michael Wrighta9cf4192022-12-01 23:46:39 +00003071 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003072 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3073 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3074 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3075 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3076 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3077 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3078 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3079 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3080}
3081
3082TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003083 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3084 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3085 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3086 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003087
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 KeyboardInputMapper& mapper =
3090 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3091 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092
Michael Wrighta9cf4192022-12-01 23:46:39 +00003093 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003094 ASSERT_NO_FATAL_FAILURE(
3095 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3096 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3097 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3098 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3099 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3100 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3101 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003103 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003104 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003105 ASSERT_NO_FATAL_FAILURE(
3106 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3107 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3108 AKEYCODE_DPAD_UP, DISPLAY_ID));
3109 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3110 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3111 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3112 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003114 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003115 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003116 ASSERT_NO_FATAL_FAILURE(
3117 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3118 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3119 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3120 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3121 AKEYCODE_DPAD_UP, DISPLAY_ID));
3122 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3123 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003124
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003125 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003126 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003127 ASSERT_NO_FATAL_FAILURE(
3128 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3129 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3130 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3131 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3132 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3133 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3134 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135
3136 // Special case: if orientation changes while key is down, we still emit the same keycode
3137 // in the key up as we did in the key down.
3138 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003139 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003140 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3143 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3144 ASSERT_EQ(KEY_UP, args.scanCode);
3145 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3146
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003147 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003148 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3151 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3152 ASSERT_EQ(KEY_UP, args.scanCode);
3153 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3154}
3155
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003156TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3157 // If the keyboard is not orientation aware,
3158 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003159 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003160
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003161 KeyboardInputMapper& mapper =
3162 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3163 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003164 NotifyKeyArgs args;
3165
3166 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3171 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3172
Michael Wrighta9cf4192022-12-01 23:46:39 +00003173 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3178 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3179}
3180
3181TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3182 // If the keyboard is orientation aware,
3183 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003184 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003185
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003186 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003187 KeyboardInputMapper& mapper =
3188 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3189 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003190 NotifyKeyArgs args;
3191
3192 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3193 // ^--- already checked by the previous test
3194
Michael Wrighta9cf4192022-12-01 23:46:39 +00003195 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003196 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3201 ASSERT_EQ(DISPLAY_ID, args.displayId);
3202
3203 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003204 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003205 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003206 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3211 ASSERT_EQ(newDisplayId, args.displayId);
3212}
3213
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003215 KeyboardInputMapper& mapper =
3216 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3217 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003219 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003220 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003222 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003223 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003224}
3225
Philip Junker4af3b3d2021-12-14 10:36:55 +01003226TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3227 KeyboardInputMapper& mapper =
3228 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3229 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3230
3231 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3232 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3233 << "If a mapping is available, the result is equal to the mapping";
3234
3235 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3236 << "If no mapping is available, the result is the key location";
3237}
3238
Michael Wrightd02c5b62014-02-10 15:10:22 -08003239TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003240 KeyboardInputMapper& mapper =
3241 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3242 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003243
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003244 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003245 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003247 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003248 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249}
3250
3251TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003252 KeyboardInputMapper& mapper =
3253 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3254 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003256 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003259 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260 ASSERT_TRUE(flags[0]);
3261 ASSERT_FALSE(flags[1]);
3262}
3263
3264TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003265 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3266 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3267 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3268 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3269 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3270 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003272 KeyboardInputMapper& mapper =
3273 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3274 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003275 // Initial metastate is AMETA_NONE.
3276 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003277
3278 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003279 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3280 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3281 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
3283 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003286 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3287 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3288 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003289 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290
3291 // Toggle num lock on.
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_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3295 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3296 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003297 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298
3299 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003302 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3303 ASSERT_TRUE(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_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306
3307 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003310 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3311 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3312 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003313 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314
3315 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003318 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3319 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3320 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003321 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322
3323 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003326 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3327 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3328 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003329 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330}
3331
Chris Yea52ade12020-08-27 16:49:20 -07003332TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3333 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3334 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3335 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3336 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3337
3338 KeyboardInputMapper& mapper =
3339 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3340 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3341
Chris Yea52ade12020-08-27 16:49:20 -07003342 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003343 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003344 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3345 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3346 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3347 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3348
3349 NotifyKeyArgs args;
3350 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3353 ASSERT_EQ(AMETA_NONE, args.metaState);
3354 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3355 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3356 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3357
3358 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003359 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3361 ASSERT_EQ(AMETA_NONE, args.metaState);
3362 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3363 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3364 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3365}
3366
Arthur Hung2c9a3342019-07-23 14:18:59 +08003367TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3368 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003369 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3370 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3371 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3372 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003373
3374 // keyboard 2.
3375 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003376 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003377 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003378 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003379 std::shared_ptr<InputDevice> device2 =
3380 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003381 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003382
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003383 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3384 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3385 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3386 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003387
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003388 KeyboardInputMapper& mapper =
3389 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3390 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003391
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003392 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003393 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003394 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003395 std::list<NotifyArgs> unused =
3396 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003397 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003398 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003399
3400 // Prepared displays and associated info.
3401 constexpr uint8_t hdmi1 = 0;
3402 constexpr uint8_t hdmi2 = 1;
3403 const std::string SECONDARY_UNIQUE_ID = "local:1";
3404
3405 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3406 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3407
3408 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003409 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3410 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003411 ASSERT_FALSE(device2->isEnabled());
3412
3413 // Prepare second display.
3414 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003415 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003416 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003417 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003418 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003419 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003420 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3421 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003422
3423 // Device should be enabled after the associated display is found.
3424 ASSERT_TRUE(mDevice->isEnabled());
3425 ASSERT_TRUE(device2->isEnabled());
3426
3427 // Test pad key events
3428 ASSERT_NO_FATAL_FAILURE(
3429 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3430 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3431 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3432 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3433 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3434 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3435 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3436
3437 ASSERT_NO_FATAL_FAILURE(
3438 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3439 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3440 AKEYCODE_DPAD_RIGHT, newDisplayId));
3441 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3442 AKEYCODE_DPAD_DOWN, newDisplayId));
3443 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3444 AKEYCODE_DPAD_LEFT, newDisplayId));
3445}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446
arthurhungc903df12020-08-11 15:08:42 +08003447TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3448 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3449 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3450 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3451 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3452 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3453 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3454
3455 KeyboardInputMapper& mapper =
3456 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3457 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003458 // Initial metastate is AMETA_NONE.
3459 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003460
3461 // Initialization should have turned all of the lights off.
3462 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3463 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3464 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3465
3466 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003469 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3470 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3471
3472 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003475 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3476 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3477
3478 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003481 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3482 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3483
3484 mFakeEventHub->removeDevice(EVENTHUB_ID);
3485 mReader->loopOnce();
3486
3487 // keyboard 2 should default toggle keys.
3488 const std::string USB2 = "USB2";
3489 const std::string DEVICE_NAME2 = "KEYBOARD2";
3490 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3491 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3492 std::shared_ptr<InputDevice> device2 =
3493 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003494 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003495 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3496 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3497 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3498 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3499 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3500 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3501
arthurhung6fe95782020-10-05 22:41:16 +08003502 KeyboardInputMapper& mapper2 =
3503 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3504 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003505 std::list<NotifyArgs> unused =
3506 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003507 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003508 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003509
3510 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3511 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3512 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003513 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3514 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003515}
3516
Arthur Hungcb40a002021-08-03 14:31:01 +00003517TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3518 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3519 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3520 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3521
3522 // Suppose we have two mappers. (DPAD + KEYBOARD)
3523 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3524 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3525 KeyboardInputMapper& mapper =
3526 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3527 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003528 // Initial metastate is AMETA_NONE.
3529 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003530
3531 mReader->toggleCapsLockState(DEVICE_ID);
3532 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3533}
3534
Arthur Hungfb3cc112022-04-13 07:39:50 +00003535TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3536 // keyboard 1.
3537 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3538 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3539 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3540 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3541 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3542 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3543
3544 KeyboardInputMapper& mapper1 =
3545 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3546 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3547
3548 // keyboard 2.
3549 const std::string USB2 = "USB2";
3550 const std::string DEVICE_NAME2 = "KEYBOARD2";
3551 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3552 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3553 std::shared_ptr<InputDevice> device2 =
3554 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3555 ftl::Flags<InputDeviceClass>(0));
3556 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3557 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3558 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3559 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3560 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3561 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3562
3563 KeyboardInputMapper& mapper2 =
3564 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3565 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003566 std::list<NotifyArgs> unused =
3567 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003568 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003569 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003570
Arthur Hung95f68612022-04-07 14:08:22 +08003571 // Initial metastate is AMETA_NONE.
3572 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3573 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3574
3575 // Toggle num lock on and off.
3576 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3577 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003578 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3579 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3580 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3581
3582 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3583 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3584 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3585 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3586 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3587
3588 // Toggle caps lock on and off.
3589 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3590 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3591 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3592 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3593 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3594
3595 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3596 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3597 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3598 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3599 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3600
3601 // Toggle scroll lock on and off.
3602 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3603 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3604 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3605 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3606 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3607
3608 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3609 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3610 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3611 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3612 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3613}
3614
Arthur Hung2141d542022-08-23 07:45:21 +00003615TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3616 const int32_t USAGE_A = 0x070004;
3617 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3618 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3619
3620 KeyboardInputMapper& mapper =
3621 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3622 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3623 // Key down by scan code.
3624 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3625 NotifyKeyArgs args;
3626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3627 ASSERT_EQ(DEVICE_ID, args.deviceId);
3628 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3629 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3630 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3631 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3632 ASSERT_EQ(KEY_HOME, args.scanCode);
3633 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3634
3635 // Disable device, it should synthesize cancellation events for down events.
3636 mFakePolicy->addDisabledDevice(DEVICE_ID);
3637 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
3638
3639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3640 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3641 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3642 ASSERT_EQ(KEY_HOME, args.scanCode);
3643 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3644}
3645
Zixuan Qufecb6062022-11-12 04:44:31 +00003646TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
3647 mDevice->addMapper<KeyboardInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3648 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3649 std::list<NotifyArgs> unused =
3650 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
3651
3652 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3653
3654 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3655 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUT_ASSOCIATION);
3656
3657 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3658 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3659 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3660 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3661 deviceInfo.getKeyboardLayoutInfo()->layoutType);
3662}
3663
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003664TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3665 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3666 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3667
3668 // Configuration
3669 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3670 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3671 InputReaderConfiguration config;
3672 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
3673
3674 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3675 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3676}
3677
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003678// --- KeyboardInputMapperTest_ExternalDevice ---
3679
3680class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3681protected:
Chris Yea52ade12020-08-27 16:49:20 -07003682 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003683};
3684
3685TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003686 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3687 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003688
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003689 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3690 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3691 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3692 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003693
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003694 KeyboardInputMapper& mapper =
3695 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3696 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003697
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003698 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003699 NotifyKeyArgs args;
3700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3701 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3702
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003703 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3705 ASSERT_EQ(uint32_t(0), args.policyFlags);
3706
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003709 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003710
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003711 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3713 ASSERT_EQ(uint32_t(0), args.policyFlags);
3714
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3717 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3718
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003719 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3721 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3722}
3723
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003724TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003725 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003726
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003727 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3728 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3729 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003730
Powei Fengd041c5d2019-05-03 17:11:33 -07003731 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003732 KeyboardInputMapper& mapper =
3733 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3734 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003735
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003737 NotifyKeyArgs args;
3738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3739 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3740
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003741 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3743 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3744
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3747 ASSERT_EQ(uint32_t(0), args.policyFlags);
3748
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003749 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3751 ASSERT_EQ(uint32_t(0), args.policyFlags);
3752
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3755 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3756
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003757 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3759 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3760}
3761
Michael Wrightd02c5b62014-02-10 15:10:22 -08003762// --- CursorInputMapperTest ---
3763
3764class CursorInputMapperTest : public InputMapperTest {
3765protected:
3766 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3767
Michael Wright17db18e2020-06-26 20:51:44 +01003768 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769
Chris Yea52ade12020-08-27 16:49:20 -07003770 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771 InputMapperTest::SetUp();
3772
Michael Wright17db18e2020-06-26 20:51:44 +01003773 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003774 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775 }
3776
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003777 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3778 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003779
Michael Wrighta9cf4192022-12-01 23:46:39 +00003780 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003781 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3782 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3783 }
3784
3785 void prepareSecondaryDisplay() {
3786 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003787 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003788 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003789 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003790
3791 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3792 float pressure) {
3793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3794 0.0f, 0.0f, 0.0f, EPSILON));
3795 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796};
3797
3798const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3799
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003800void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3801 int32_t originalY, int32_t rotatedX,
3802 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003803 NotifyMotionArgs args;
3804
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003805 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3809 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003810 ASSERT_NO_FATAL_FAILURE(
3811 assertCursorPointerCoords(args.pointerCoords[0],
3812 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3813 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003814}
3815
3816TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003817 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003818 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003819
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003820 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003821}
3822
3823TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003824 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003825 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003826
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003827 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003828}
3829
3830TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003831 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003832 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003833
3834 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003835 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836
3837 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003838 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3839 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003840 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3841 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3842
3843 // When the bounds are set, then there should be a valid motion range.
3844 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3845
3846 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003847 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003848
3849 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3850 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3851 1, 800 - 1, 0.0f, 0.0f));
3852 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3853 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3854 2, 480 - 1, 0.0f, 0.0f));
3855 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3856 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3857 0.0f, 1.0f, 0.0f, 0.0f));
3858}
3859
3860TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003861 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003862 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863
3864 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003865 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866
3867 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3868 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3869 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3870 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3871 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3872 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3873 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3874 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3875 0.0f, 1.0f, 0.0f, 0.0f));
3876}
3877
3878TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003880 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881
arthurhungdcef2dc2020-08-11 14:47:50 +08003882 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003883
3884 NotifyMotionArgs args;
3885
3886 // Button press.
3887 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3891 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3892 ASSERT_EQ(DEVICE_ID, args.deviceId);
3893 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3894 ASSERT_EQ(uint32_t(0), args.policyFlags);
3895 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3896 ASSERT_EQ(0, args.flags);
3897 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3898 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3899 ASSERT_EQ(0, args.edgeFlags);
3900 ASSERT_EQ(uint32_t(1), args.pointerCount);
3901 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003902 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003903 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003904 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3905 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3906 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3907
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3909 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3910 ASSERT_EQ(DEVICE_ID, args.deviceId);
3911 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3912 ASSERT_EQ(uint32_t(0), args.policyFlags);
3913 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3914 ASSERT_EQ(0, args.flags);
3915 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3916 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3917 ASSERT_EQ(0, args.edgeFlags);
3918 ASSERT_EQ(uint32_t(1), args.pointerCount);
3919 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003920 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003921 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003922 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3923 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3924 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3925
Michael Wrightd02c5b62014-02-10 15:10:22 -08003926 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003927 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3928 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3930 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3931 ASSERT_EQ(DEVICE_ID, args.deviceId);
3932 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3933 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003934 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3935 ASSERT_EQ(0, args.flags);
3936 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3937 ASSERT_EQ(0, args.buttonState);
3938 ASSERT_EQ(0, args.edgeFlags);
3939 ASSERT_EQ(uint32_t(1), args.pointerCount);
3940 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003941 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003942 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003943 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3944 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3945 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3946
3947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3948 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3949 ASSERT_EQ(DEVICE_ID, args.deviceId);
3950 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3951 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003952 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3953 ASSERT_EQ(0, args.flags);
3954 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3955 ASSERT_EQ(0, args.buttonState);
3956 ASSERT_EQ(0, args.edgeFlags);
3957 ASSERT_EQ(uint32_t(1), args.pointerCount);
3958 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003959 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003960 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003961 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3962 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3963 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3964}
3965
3966TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
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 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 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_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003977 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3978 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
3979 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003980
3981 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003982 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3985 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003986 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
3987 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003988}
3989
3990TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003991 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003992 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003993
3994 NotifyMotionArgs args;
3995
3996 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4000 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004001 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004002
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4004 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004005 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004006
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004011 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004012 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004013
4014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004016 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017}
4018
4019TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004020 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004021 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022
4023 NotifyMotionArgs args;
4024
4025 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4031 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004032 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4033 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4034 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004035
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4037 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004038 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4039 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4040 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004041
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4047 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004048 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4049 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4050 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051
4052 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004056 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004057 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004058
4059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004061 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062}
4063
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004064TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004065 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004067 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4068 // need to be rotated.
4069 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004070 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004071
Michael Wrighta9cf4192022-12-01 23:46:39 +00004072 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4074 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4075 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4076 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4077 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4078 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4079 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4080 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4081}
4082
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004083TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004084 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004086 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4087 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004088 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004090 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004091 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4093 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4094 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4095 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4096 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4097 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4098 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4099 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4100
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004101 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004102 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004103 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4104 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4105 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4106 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4107 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4108 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4109 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4110 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004112 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004113 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4115 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4116 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4117 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4118 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4119 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4120 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4121 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4122
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004123 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004124 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004125 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4130 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133}
4134
4135TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004136 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004137 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138
4139 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4140 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141
4142 NotifyMotionArgs motionArgs;
4143 NotifyKeyArgs keyArgs;
4144
4145 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4149 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4150 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004151 ASSERT_NO_FATAL_FAILURE(
4152 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004153
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4155 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4156 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004157 ASSERT_NO_FATAL_FAILURE(
4158 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004159
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004163 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004165 ASSERT_NO_FATAL_FAILURE(
4166 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167
4168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004169 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004171 ASSERT_NO_FATAL_FAILURE(
4172 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004173
4174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004176 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004177 ASSERT_NO_FATAL_FAILURE(
4178 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179
4180 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4185 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4186 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004187 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004188 ASSERT_NO_FATAL_FAILURE(
4189 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004190
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4192 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4193 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004194 ASSERT_NO_FATAL_FAILURE(
4195 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004196
4197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4198 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4199 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004200 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004201 ASSERT_NO_FATAL_FAILURE(
4202 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004203
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004207 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004209 ASSERT_NO_FATAL_FAILURE(
4210 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004211
4212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004214 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004215 ASSERT_NO_FATAL_FAILURE(
4216 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004218 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004221 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4222 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004223 ASSERT_NO_FATAL_FAILURE(
4224 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004227
4228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004231 ASSERT_NO_FATAL_FAILURE(
4232 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004233
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4235 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004237 ASSERT_NO_FATAL_FAILURE(
4238 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239
4240 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004241 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4242 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4244 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4245 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004246
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004248 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004250 ASSERT_NO_FATAL_FAILURE(
4251 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004252
4253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4254 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4255 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004256 ASSERT_NO_FATAL_FAILURE(
4257 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004262 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004264 ASSERT_NO_FATAL_FAILURE(
4265 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004266
4267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004269 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004270
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004271 ASSERT_NO_FATAL_FAILURE(
4272 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4274 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4275 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4276
4277 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4281 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4282 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004283
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004285 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004287 ASSERT_NO_FATAL_FAILURE(
4288 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004289
4290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4291 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4292 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004293 ASSERT_NO_FATAL_FAILURE(
4294 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004301 ASSERT_NO_FATAL_FAILURE(
4302 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004303
4304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4305 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4306 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004307 ASSERT_NO_FATAL_FAILURE(
4308 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004309
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4311 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4312 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4313
4314 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4318 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4319 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004320
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004322 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004324 ASSERT_NO_FATAL_FAILURE(
4325 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004326
4327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4328 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4329 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004330 ASSERT_NO_FATAL_FAILURE(
4331 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004336 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004338 ASSERT_NO_FATAL_FAILURE(
4339 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004340
4341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4342 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4343 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004344 ASSERT_NO_FATAL_FAILURE(
4345 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004346
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4348 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4349 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4350
4351 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4355 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4356 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004357
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004359 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004361 ASSERT_NO_FATAL_FAILURE(
4362 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004363
4364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4365 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4366 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004367 ASSERT_NO_FATAL_FAILURE(
4368 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004373 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004375 ASSERT_NO_FATAL_FAILURE(
4376 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004377
4378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4379 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4380 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004381 ASSERT_NO_FATAL_FAILURE(
4382 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004383
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4385 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4386 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4387}
4388
4389TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004391 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392
4393 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4394 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004395
4396 NotifyMotionArgs args;
4397
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004402 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4403 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4405 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 +00004406 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004407}
4408
4409TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004410 addConfigurationProperty("cursor.mode", "pointer");
4411 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004412 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004413
4414 NotifyDeviceResetArgs resetArgs;
4415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4416 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4417 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4418
4419 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4420 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004421
4422 NotifyMotionArgs args;
4423
4424 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4427 process(mapper, ARBITRARY_TIME, 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_MOVE, args.action);
4431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4432 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 +00004433 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004434
4435 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4439 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4440 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4442 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4444 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4445 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4447 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4448
4449 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004450 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4451 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4453 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4454 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4456 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4458 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4459 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4461 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4462
4463 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004464 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4465 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4468 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4471 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 +00004472 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004473
4474 // Disable pointer capture and check that the device generation got bumped
4475 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004476 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004477 mFakePolicy->setPointerCapture(false);
4478 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004479 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004480
4481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004482 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4483
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4486 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4488 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4491 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 +00004492 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493}
4494
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004495/**
4496 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4497 * pointer acceleration or speed processing should not be applied.
4498 */
4499TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4500 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004501 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4502 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004503 mFakePolicy->setVelocityControlParams(testParams);
4504 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4505
4506 NotifyDeviceResetArgs resetArgs;
4507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4508 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4509 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4510
4511 NotifyMotionArgs args;
4512
4513 // Move and verify scale is applied.
4514 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4515 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4518 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4519 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4520 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4521 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4522 ASSERT_GT(relX, 10);
4523 ASSERT_GT(relY, 20);
4524
4525 // Enable Pointer Capture
4526 mFakePolicy->setPointerCapture(true);
4527 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4528 NotifyPointerCaptureChangedArgs captureArgs;
4529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4530 ASSERT_TRUE(captureArgs.request.enable);
4531
4532 // Move and verify scale is not applied.
4533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4537 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4539 ASSERT_EQ(10, args.pointerCoords[0].getX());
4540 ASSERT_EQ(20, args.pointerCoords[0].getY());
4541}
4542
Prabir Pradhan208360b2022-06-24 18:37:04 +00004543TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4544 addConfigurationProperty("cursor.mode", "pointer");
4545 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4546
4547 NotifyDeviceResetArgs resetArgs;
4548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4549 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4550 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4551
4552 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004553 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004554
4555 NotifyMotionArgs args;
4556
4557 // Verify that the coordinates are rotated.
4558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4562 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4563 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4564 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4565 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4566
4567 // Enable Pointer Capture.
4568 mFakePolicy->setPointerCapture(true);
4569 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4570 NotifyPointerCaptureChangedArgs captureArgs;
4571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4572 ASSERT_TRUE(captureArgs.request.enable);
4573
4574 // Move and verify rotation is not applied.
4575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4579 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4581 ASSERT_EQ(10, args.pointerCoords[0].getX());
4582 ASSERT_EQ(20, args.pointerCoords[0].getY());
4583}
4584
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004585TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004586 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004587
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004588 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004589 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004590
4591 // Set up the secondary display as the display on which the pointer should be shown.
4592 // The InputDevice is not associated with any display.
4593 prepareSecondaryDisplay();
4594 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08004595 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4596
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004597 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004598 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004599
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004600 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004601 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4602 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4603 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004605 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4606 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4607 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004608 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004609}
4610
4611TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4612 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4613
4614 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004615 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004616
4617 // Set up the secondary display as the display on which the pointer should be shown,
4618 // and associate the InputDevice with the secondary display.
4619 prepareSecondaryDisplay();
4620 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4621 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4622 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4623
4624 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4625 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004626
4627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004631 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4632 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4633 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004634 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004635}
4636
4637TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4638 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4639
4640 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004641 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004642 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4643
4644 // Associate the InputDevice with the secondary display.
4645 prepareSecondaryDisplay();
4646 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4647 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4648
4649 // The mapper should not generate any events because it is associated with a display that is
4650 // different from the pointer display.
4651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4653 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004655}
4656
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004657// --- BluetoothCursorInputMapperTest ---
4658
4659class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4660protected:
4661 void SetUp() override {
4662 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4663
4664 mFakePointerController = std::make_shared<FakePointerController>();
4665 mFakePolicy->setPointerController(mFakePointerController);
4666 }
4667};
4668
4669TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4670 addConfigurationProperty("cursor.mode", "pointer");
4671 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4672
4673 nsecs_t kernelEventTime = ARBITRARY_TIME;
4674 nsecs_t expectedEventTime = ARBITRARY_TIME;
4675 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4676 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4678 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4679 WithEventTime(expectedEventTime))));
4680
4681 // Process several events that come in quick succession, according to their timestamps.
4682 for (int i = 0; i < 3; i++) {
4683 constexpr static nsecs_t delta = ms2ns(1);
4684 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4685 kernelEventTime += delta;
4686 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4687
4688 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4689 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4691 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4692 WithEventTime(expectedEventTime))));
4693 }
4694}
4695
4696TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4697 addConfigurationProperty("cursor.mode", "pointer");
4698 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4699
4700 nsecs_t expectedEventTime = ARBITRARY_TIME;
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(expectedEventTime))));
4706
4707 // Process several events with the same timestamp from the kernel.
4708 // Ensure that we do not generate events too far into the future.
4709 constexpr static int32_t numEvents =
4710 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4711 for (int i = 0; i < numEvents; i++) {
4712 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4713
4714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4717 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4718 WithEventTime(expectedEventTime))));
4719 }
4720
4721 // By processing more events with the same timestamp, we should not generate events with a
4722 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4723 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4724 for (int i = 0; i < 3; i++) {
4725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4726 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4728 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4729 WithEventTime(cappedEventTime))));
4730 }
4731}
4732
4733TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4734 addConfigurationProperty("cursor.mode", "pointer");
4735 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4736
4737 nsecs_t kernelEventTime = ARBITRARY_TIME;
4738 nsecs_t expectedEventTime = ARBITRARY_TIME;
4739 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4740 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4742 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4743 WithEventTime(expectedEventTime))));
4744
4745 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4746 // smoothening is not needed, its timestamp is not affected.
4747 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4748 expectedEventTime = kernelEventTime;
4749
4750 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4751 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4753 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4754 WithEventTime(expectedEventTime))));
4755}
4756
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757// --- TouchInputMapperTest ---
4758
4759class TouchInputMapperTest : public InputMapperTest {
4760protected:
4761 static const int32_t RAW_X_MIN;
4762 static const int32_t RAW_X_MAX;
4763 static const int32_t RAW_Y_MIN;
4764 static const int32_t RAW_Y_MAX;
4765 static const int32_t RAW_TOUCH_MIN;
4766 static const int32_t RAW_TOUCH_MAX;
4767 static const int32_t RAW_TOOL_MIN;
4768 static const int32_t RAW_TOOL_MAX;
4769 static const int32_t RAW_PRESSURE_MIN;
4770 static const int32_t RAW_PRESSURE_MAX;
4771 static const int32_t RAW_ORIENTATION_MIN;
4772 static const int32_t RAW_ORIENTATION_MAX;
4773 static const int32_t RAW_DISTANCE_MIN;
4774 static const int32_t RAW_DISTANCE_MAX;
4775 static const int32_t RAW_TILT_MIN;
4776 static const int32_t RAW_TILT_MAX;
4777 static const int32_t RAW_ID_MIN;
4778 static const int32_t RAW_ID_MAX;
4779 static const int32_t RAW_SLOT_MIN;
4780 static const int32_t RAW_SLOT_MAX;
4781 static const float X_PRECISION;
4782 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004783 static const float X_PRECISION_VIRTUAL;
4784 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785
4786 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004787 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788
4789 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4790
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004791 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004792 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004793
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794 enum Axes {
4795 POSITION = 1 << 0,
4796 TOUCH = 1 << 1,
4797 TOOL = 1 << 2,
4798 PRESSURE = 1 << 3,
4799 ORIENTATION = 1 << 4,
4800 MINOR = 1 << 5,
4801 ID = 1 << 6,
4802 DISTANCE = 1 << 7,
4803 TILT = 1 << 8,
4804 SLOT = 1 << 9,
4805 TOOL_TYPE = 1 << 10,
4806 };
4807
Michael Wrighta9cf4192022-12-01 23:46:39 +00004808 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004809 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004810 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004811 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004812 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813 int32_t toRawX(float displayX);
4814 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004815 int32_t toRotatedRawX(float displayX);
4816 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004817 float toCookedX(float rawX, float rawY);
4818 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004820 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004822 float toDisplayY(int32_t rawY, int32_t displayHeight);
4823
Michael Wrightd02c5b62014-02-10 15:10:22 -08004824};
4825
4826const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4827const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4828const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4829const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4830const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4831const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4832const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4833const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004834const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4835const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4837const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4838const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4839const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4840const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4841const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4842const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4843const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4844const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4845const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4846const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4847const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004848const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4849 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4850const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4851 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004852const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4853 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004854
4855const float TouchInputMapperTest::GEOMETRIC_SCALE =
4856 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4857 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4858
4859const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4860 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4861 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4862};
4863
Michael Wrighta9cf4192022-12-01 23:46:39 +00004864void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004865 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4866 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004867}
4868
4869void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4870 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004871 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872}
4873
Michael Wrighta9cf4192022-12-01 23:46:39 +00004874void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004875 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4876 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4877 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004878}
4879
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004881 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4882 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4883 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4884 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004885}
4886
Jason Gerecke489fda82012-09-07 17:19:40 -07004887void TouchInputMapperTest::prepareLocationCalibration() {
4888 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4889}
4890
Michael Wrightd02c5b62014-02-10 15:10:22 -08004891int32_t TouchInputMapperTest::toRawX(float displayX) {
4892 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4893}
4894
4895int32_t TouchInputMapperTest::toRawY(float displayY) {
4896 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4897}
4898
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004899int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4900 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4901}
4902
4903int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4904 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4905}
4906
Jason Gerecke489fda82012-09-07 17:19:40 -07004907float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4908 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4909 return rawX;
4910}
4911
4912float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4913 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4914 return rawY;
4915}
4916
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004918 return toDisplayX(rawX, DISPLAY_WIDTH);
4919}
4920
4921float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4922 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923}
4924
4925float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004926 return toDisplayY(rawY, DISPLAY_HEIGHT);
4927}
4928
4929float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4930 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931}
4932
4933
4934// --- SingleTouchInputMapperTest ---
4935
4936class SingleTouchInputMapperTest : public TouchInputMapperTest {
4937protected:
4938 void prepareButtons();
4939 void prepareAxes(int axes);
4940
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004941 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4942 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4943 void processUp(SingleTouchInputMapper& mappery);
4944 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4945 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4946 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4947 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4948 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4949 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950};
4951
4952void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004953 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954}
4955
4956void SingleTouchInputMapperTest::prepareAxes(int axes) {
4957 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004958 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4959 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004960 }
4961 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004962 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4963 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964 }
4965 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004966 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4967 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968 }
4969 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004970 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4971 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972 }
4973 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004974 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4975 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976 }
4977}
4978
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004979void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4981 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4982 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983}
4984
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004985void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004986 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988}
4989
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004990void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992}
4993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004994void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996}
4997
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004998void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4999 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001}
5002
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005003void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005005}
5006
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005007void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5008 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005011}
5012
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005013void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5014 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016}
5017
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005018void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020}
5021
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023 prepareButtons();
5024 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005025 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005026
Josep del Río2d8c79a2023-01-23 19:33:50 +00005027 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005028}
5029
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005031 prepareButtons();
5032 prepareAxes(POSITION);
5033 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005034 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005036 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005037}
5038
5039TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005041 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042 prepareButtons();
5043 prepareAxes(POSITION);
5044 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005045 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046
5047 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005048 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049
5050 // Virtual key is down.
5051 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5052 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5053 processDown(mapper, x, y);
5054 processSync(mapper);
5055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5056
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005057 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058
5059 // Virtual key is up.
5060 processUp(mapper);
5061 processSync(mapper);
5062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5063
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005064 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065}
5066
5067TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005069 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070 prepareButtons();
5071 prepareAxes(POSITION);
5072 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005073 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005074
5075 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005076 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077
5078 // Virtual key is down.
5079 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5080 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5081 processDown(mapper, x, y);
5082 processSync(mapper);
5083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5084
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005085 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086
5087 // Virtual key is up.
5088 processUp(mapper);
5089 processSync(mapper);
5090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5091
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005092 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005093}
5094
5095TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005096 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005097 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005098 prepareButtons();
5099 prepareAxes(POSITION);
5100 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005101 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102
Michael Wrightd02c5b62014-02-10 15:10:22 -08005103 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005104 ASSERT_TRUE(
5105 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005106 ASSERT_TRUE(flags[0]);
5107 ASSERT_FALSE(flags[1]);
5108}
5109
5110TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005112 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113 prepareButtons();
5114 prepareAxes(POSITION);
5115 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005116 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005117
arthurhungdcef2dc2020-08-11 14:47:50 +08005118 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119
5120 NotifyKeyArgs args;
5121
5122 // Press virtual key.
5123 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5124 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5125 processDown(mapper, x, y);
5126 processSync(mapper);
5127
5128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5129 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5130 ASSERT_EQ(DEVICE_ID, args.deviceId);
5131 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5132 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5133 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5134 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5135 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5136 ASSERT_EQ(KEY_HOME, args.scanCode);
5137 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5138 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5139
5140 // Release virtual key.
5141 processUp(mapper);
5142 processSync(mapper);
5143
5144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5145 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5146 ASSERT_EQ(DEVICE_ID, args.deviceId);
5147 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5148 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5149 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5150 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5151 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5152 ASSERT_EQ(KEY_HOME, args.scanCode);
5153 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5154 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5155
5156 // Should not have sent any motions.
5157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5158}
5159
5160TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005162 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005163 prepareButtons();
5164 prepareAxes(POSITION);
5165 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005166 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005167
arthurhungdcef2dc2020-08-11 14:47:50 +08005168 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169
5170 NotifyKeyArgs keyArgs;
5171
5172 // Press virtual key.
5173 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5174 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5175 processDown(mapper, x, y);
5176 processSync(mapper);
5177
5178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5179 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5180 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5181 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5182 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5183 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5184 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5185 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5186 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5187 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5188 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5189
5190 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5191 // into the display area.
5192 y -= 100;
5193 processMove(mapper, x, y);
5194 processSync(mapper);
5195
5196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5197 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5198 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5199 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5200 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5201 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5202 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5203 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5204 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5205 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5206 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5207 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5208
5209 NotifyMotionArgs motionArgs;
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_DOWN, 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);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005222 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223 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 // Keep moving out of bounds. Should generate a pointer move.
5230 y -= 50;
5231 processMove(mapper, x, y);
5232 processSync(mapper);
5233
5234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5235 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5236 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5237 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5238 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5240 ASSERT_EQ(0, motionArgs.flags);
5241 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5242 ASSERT_EQ(0, motionArgs.buttonState);
5243 ASSERT_EQ(0, motionArgs.edgeFlags);
5244 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5245 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005246 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5248 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5249 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5250 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5251 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5252
5253 // Release out of bounds. Should generate a pointer up.
5254 processUp(mapper);
5255 processSync(mapper);
5256
5257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5258 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5259 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5260 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5261 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5262 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5263 ASSERT_EQ(0, motionArgs.flags);
5264 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5265 ASSERT_EQ(0, motionArgs.buttonState);
5266 ASSERT_EQ(0, motionArgs.edgeFlags);
5267 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5268 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005269 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5271 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5272 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5273 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5274 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5275
5276 // Should not have sent any more keys or motions.
5277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5279}
5280
5281TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005283 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005284 prepareButtons();
5285 prepareAxes(POSITION);
5286 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005287 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005288
arthurhungdcef2dc2020-08-11 14:47:50 +08005289 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290
5291 NotifyMotionArgs motionArgs;
5292
5293 // Initially go down out of bounds.
5294 int32_t x = -10;
5295 int32_t y = -10;
5296 processDown(mapper, x, y);
5297 processSync(mapper);
5298
5299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5300
5301 // Move into the display area. Should generate a pointer down.
5302 x = 50;
5303 y = 75;
5304 processMove(mapper, x, y);
5305 processSync(mapper);
5306
5307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5308 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5309 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5310 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5311 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5312 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5313 ASSERT_EQ(0, motionArgs.flags);
5314 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5315 ASSERT_EQ(0, motionArgs.buttonState);
5316 ASSERT_EQ(0, motionArgs.edgeFlags);
5317 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5318 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005319 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5321 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5322 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5323 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5324 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5325
5326 // Release. Should generate a pointer up.
5327 processUp(mapper);
5328 processSync(mapper);
5329
5330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5331 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5332 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5333 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5334 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5335 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5336 ASSERT_EQ(0, motionArgs.flags);
5337 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5338 ASSERT_EQ(0, motionArgs.buttonState);
5339 ASSERT_EQ(0, motionArgs.edgeFlags);
5340 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5341 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005342 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5344 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5345 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5346 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5347 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5348
5349 // Should not have sent any more keys or motions.
5350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5352}
5353
Santos Cordonfa5cf462017-04-05 10:37:00 -07005354TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005355 addConfigurationProperty("touch.deviceType", "touchScreen");
5356 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5357
Michael Wrighta9cf4192022-12-01 23:46:39 +00005358 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005359 prepareButtons();
5360 prepareAxes(POSITION);
5361 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005362 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005363
arthurhungdcef2dc2020-08-11 14:47:50 +08005364 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005365
5366 NotifyMotionArgs motionArgs;
5367
5368 // Down.
5369 int32_t x = 100;
5370 int32_t y = 125;
5371 processDown(mapper, x, y);
5372 processSync(mapper);
5373
5374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5375 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5376 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5377 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5378 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5379 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5380 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5381 ASSERT_EQ(0, motionArgs.flags);
5382 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5383 ASSERT_EQ(0, motionArgs.buttonState);
5384 ASSERT_EQ(0, motionArgs.edgeFlags);
5385 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5386 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005387 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5389 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5390 1, 0, 0, 0, 0, 0, 0, 0));
5391 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5392 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5393 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5394
5395 // Move.
5396 x += 50;
5397 y += 75;
5398 processMove(mapper, x, y);
5399 processSync(mapper);
5400
5401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5402 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5403 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5404 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5405 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5406 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5408 ASSERT_EQ(0, motionArgs.flags);
5409 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5410 ASSERT_EQ(0, motionArgs.buttonState);
5411 ASSERT_EQ(0, motionArgs.edgeFlags);
5412 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5413 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005414 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5416 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5417 1, 0, 0, 0, 0, 0, 0, 0));
5418 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5419 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5420 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5421
5422 // Up.
5423 processUp(mapper);
5424 processSync(mapper);
5425
5426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5427 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5428 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5429 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5430 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5431 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5432 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5433 ASSERT_EQ(0, motionArgs.flags);
5434 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5435 ASSERT_EQ(0, motionArgs.buttonState);
5436 ASSERT_EQ(0, motionArgs.edgeFlags);
5437 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5438 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005439 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5441 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5442 1, 0, 0, 0, 0, 0, 0, 0));
5443 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5444 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5445 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5446
5447 // Should not have sent any more keys or motions.
5448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5450}
5451
Michael Wrightd02c5b62014-02-10 15:10:22 -08005452TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005453 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005454 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455 prepareButtons();
5456 prepareAxes(POSITION);
5457 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005458 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005459
arthurhungdcef2dc2020-08-11 14:47:50 +08005460 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461
5462 NotifyMotionArgs motionArgs;
5463
5464 // Down.
5465 int32_t x = 100;
5466 int32_t y = 125;
5467 processDown(mapper, x, y);
5468 processSync(mapper);
5469
5470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5471 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5472 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5473 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5474 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5475 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5476 ASSERT_EQ(0, motionArgs.flags);
5477 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5478 ASSERT_EQ(0, motionArgs.buttonState);
5479 ASSERT_EQ(0, motionArgs.edgeFlags);
5480 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5481 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005482 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5484 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5485 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5486 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5487 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5488
5489 // Move.
5490 x += 50;
5491 y += 75;
5492 processMove(mapper, x, y);
5493 processSync(mapper);
5494
5495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5496 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5497 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5498 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5499 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5500 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5501 ASSERT_EQ(0, motionArgs.flags);
5502 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5503 ASSERT_EQ(0, motionArgs.buttonState);
5504 ASSERT_EQ(0, motionArgs.edgeFlags);
5505 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5506 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005507 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5509 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5510 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5511 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5512 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5513
5514 // Up.
5515 processUp(mapper);
5516 processSync(mapper);
5517
5518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5519 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5520 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5521 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5522 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5523 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5524 ASSERT_EQ(0, motionArgs.flags);
5525 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5526 ASSERT_EQ(0, motionArgs.buttonState);
5527 ASSERT_EQ(0, motionArgs.edgeFlags);
5528 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5529 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005530 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5532 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5533 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5534 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5535 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5536
5537 // Should not have sent any more keys or motions.
5538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5540}
5541
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005542TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
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 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5547 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005548 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005549
5550 NotifyMotionArgs args;
5551
5552 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005553 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005554 processDown(mapper, toRawX(50), toRawY(75));
5555 processSync(mapper);
5556
5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5558 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5559 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5560
5561 processUp(mapper);
5562 processSync(mapper);
5563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5564}
5565
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005566TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005567 addConfigurationProperty("touch.deviceType", "touchScreen");
5568 prepareButtons();
5569 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005570 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5571 // orientation-aware are affected by display rotation.
5572 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005573 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005574
5575 NotifyMotionArgs args;
5576
5577 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005578 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005579 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005580 processDown(mapper, toRawX(50), toRawY(75));
5581 processSync(mapper);
5582
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5584 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5585 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5586
5587 processUp(mapper);
5588 processSync(mapper);
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5590
5591 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005592 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005593 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005594 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005595 processSync(mapper);
5596
5597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5598 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5599 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5600
5601 processUp(mapper);
5602 processSync(mapper);
5603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5604
5605 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005606 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005607 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005608 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5609 processSync(mapper);
5610
5611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5612 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5613 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5614
5615 processUp(mapper);
5616 processSync(mapper);
5617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5618
5619 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005620 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005621 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005622 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623 processSync(mapper);
5624
5625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5626 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5627 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5628
5629 processUp(mapper);
5630 processSync(mapper);
5631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5632}
5633
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005634TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5635 addConfigurationProperty("touch.deviceType", "touchScreen");
5636 prepareButtons();
5637 prepareAxes(POSITION);
5638 addConfigurationProperty("touch.orientationAware", "1");
5639 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
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 0.
5646 processDown(mapper, toRawX(50), toRawY(75));
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_WhenOrientation90_RotatesMotions) {
5659 addConfigurationProperty("touch.deviceType", "touchScreen");
5660 prepareButtons();
5661 prepareAxes(POSITION);
5662 addConfigurationProperty("touch.orientationAware", "1");
5663 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
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 90.
5670 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
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_WhenOrientation180_RotatesMotions) {
5683 addConfigurationProperty("touch.deviceType", "touchScreen");
5684 prepareButtons();
5685 prepareAxes(POSITION);
5686 addConfigurationProperty("touch.orientationAware", "1");
5687 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
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 180.
5694 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + 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_WhenOrientation270_RotatesMotions) {
5707 addConfigurationProperty("touch.deviceType", "touchScreen");
5708 prepareButtons();
5709 prepareAxes(POSITION);
5710 addConfigurationProperty("touch.orientationAware", "1");
5711 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5712 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005713 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005714 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5715 NotifyMotionArgs args;
5716
5717 // Orientation 270.
5718 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5719 processSync(mapper);
5720
5721 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5722 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5723 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5724
5725 processUp(mapper);
5726 processSync(mapper);
5727 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5728}
5729
5730TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5731 addConfigurationProperty("touch.deviceType", "touchScreen");
5732 prepareButtons();
5733 prepareAxes(POSITION);
5734 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5735 // orientation-aware are affected by display rotation.
5736 addConfigurationProperty("touch.orientationAware", "0");
5737 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5738 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5739
5740 NotifyMotionArgs args;
5741
5742 // Orientation 90, Rotation 0.
5743 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005744 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005745 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5746 processSync(mapper);
5747
5748 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5749 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5750 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5751
5752 processUp(mapper);
5753 processSync(mapper);
5754 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5755
5756 // Orientation 90, Rotation 90.
5757 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005758 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005759 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005760 processSync(mapper);
5761
5762 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5763 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5764 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5765
5766 processUp(mapper);
5767 processSync(mapper);
5768 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5769
5770 // Orientation 90, Rotation 180.
5771 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005772 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005773 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5774 processSync(mapper);
5775
5776 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5777 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5778 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5779
5780 processUp(mapper);
5781 processSync(mapper);
5782 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5783
5784 // Orientation 90, Rotation 270.
5785 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005786 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005787 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 -07005788 processSync(mapper);
5789
5790 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5791 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5792 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5793
5794 processUp(mapper);
5795 processSync(mapper);
5796 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5797}
5798
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005799TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5800 addConfigurationProperty("touch.deviceType", "touchScreen");
5801 prepareButtons();
5802 prepareAxes(POSITION);
5803 addConfigurationProperty("touch.orientationAware", "1");
5804 prepareDisplay(ui::ROTATION_0);
5805 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5806
5807 // Set a physical frame in the display viewport.
5808 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5809 viewport->physicalLeft = 20;
5810 viewport->physicalTop = 600;
5811 viewport->physicalRight = 30;
5812 viewport->physicalBottom = 610;
5813 mFakePolicy->updateViewport(*viewport);
5814 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5815
5816 // Start the touch.
5817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5818 processSync(mapper);
5819
5820 // Expect all input starting outside the physical frame to be ignored.
5821 const std::array<Point, 6> outsidePoints = {
5822 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5823 for (const auto& p : outsidePoints) {
5824 processMove(mapper, toRawX(p.x), toRawY(p.y));
5825 processSync(mapper);
5826 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5827 }
5828
5829 // Move the touch into the physical frame.
5830 processMove(mapper, toRawX(25), toRawY(605));
5831 processSync(mapper);
5832 NotifyMotionArgs args;
5833 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5834 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5835 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5836 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5837
5838 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5839 for (const auto& p : outsidePoints) {
5840 processMove(mapper, toRawX(p.x), toRawY(p.y));
5841 processSync(mapper);
5842 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5843 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5844 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5845 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5846 }
5847
5848 processUp(mapper);
5849 processSync(mapper);
5850 EXPECT_NO_FATAL_FAILURE(
5851 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5852}
5853
Michael Wrightd02c5b62014-02-10 15:10:22 -08005854TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005855 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005856 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005857 prepareButtons();
5858 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005859 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860
5861 // These calculations are based on the input device calibration documentation.
5862 int32_t rawX = 100;
5863 int32_t rawY = 200;
5864 int32_t rawPressure = 10;
5865 int32_t rawToolMajor = 12;
5866 int32_t rawDistance = 2;
5867 int32_t rawTiltX = 30;
5868 int32_t rawTiltY = 110;
5869
5870 float x = toDisplayX(rawX);
5871 float y = toDisplayY(rawY);
5872 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5873 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5874 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5875 float distance = float(rawDistance);
5876
5877 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5878 float tiltScale = M_PI / 180;
5879 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5880 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5881 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5882 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5883
5884 processDown(mapper, rawX, rawY);
5885 processPressure(mapper, rawPressure);
5886 processToolMajor(mapper, rawToolMajor);
5887 processDistance(mapper, rawDistance);
5888 processTilt(mapper, rawTiltX, rawTiltY);
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, pressure, size, tool, tool, tool, tool, orientation, distance));
5895 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5896}
5897
Jason Gerecke489fda82012-09-07 17:19:40 -07005898TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005899 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005900 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005901 prepareLocationCalibration();
5902 prepareButtons();
5903 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005904 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005905
5906 int32_t rawX = 100;
5907 int32_t rawY = 200;
5908
5909 float x = toDisplayX(toCookedX(rawX, rawY));
5910 float y = toDisplayY(toCookedY(rawX, rawY));
5911
5912 processDown(mapper, rawX, rawY);
5913 processSync(mapper);
5914
5915 NotifyMotionArgs args;
5916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5918 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5919}
5920
Michael Wrightd02c5b62014-02-10 15:10:22 -08005921TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005922 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005923 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005924 prepareButtons();
5925 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005926 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005927
5928 NotifyMotionArgs motionArgs;
5929 NotifyKeyArgs keyArgs;
5930
5931 processDown(mapper, 100, 200);
5932 processSync(mapper);
5933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5934 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5935 ASSERT_EQ(0, motionArgs.buttonState);
5936
5937 // press BTN_LEFT, release BTN_LEFT
5938 processKey(mapper, BTN_LEFT, 1);
5939 processSync(mapper);
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5941 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5942 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5943
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5945 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5946 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5947
Michael Wrightd02c5b62014-02-10 15:10:22 -08005948 processKey(mapper, BTN_LEFT, 0);
5949 processSync(mapper);
5950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005951 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005952 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005953
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005956 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005957
5958 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5959 processKey(mapper, BTN_RIGHT, 1);
5960 processKey(mapper, BTN_MIDDLE, 1);
5961 processSync(mapper);
5962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5964 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5965 motionArgs.buttonState);
5966
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5968 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5969 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5970
5971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5972 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5973 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5974 motionArgs.buttonState);
5975
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976 processKey(mapper, BTN_RIGHT, 0);
5977 processSync(mapper);
5978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005979 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005980 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005981
5982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005984 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005985
5986 processKey(mapper, BTN_MIDDLE, 0);
5987 processSync(mapper);
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005989 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005990 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005991
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005993 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005994 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995
5996 // press BTN_BACK, release BTN_BACK
5997 processKey(mapper, BTN_BACK, 1);
5998 processSync(mapper);
5999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6000 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6001 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006002
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006004 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006005 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6006
6007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6008 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6009 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006010
6011 processKey(mapper, BTN_BACK, 0);
6012 processSync(mapper);
6013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006014 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006015 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006016
6017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006018 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006019 ASSERT_EQ(0, motionArgs.buttonState);
6020
Michael Wrightd02c5b62014-02-10 15:10:22 -08006021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6022 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6023 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6024
6025 // press BTN_SIDE, release BTN_SIDE
6026 processKey(mapper, BTN_SIDE, 1);
6027 processSync(mapper);
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6029 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6030 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006031
Michael Wrightd02c5b62014-02-10 15:10:22 -08006032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006034 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6035
6036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6037 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6038 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006039
6040 processKey(mapper, BTN_SIDE, 0);
6041 processSync(mapper);
6042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006043 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006045
6046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006047 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006048 ASSERT_EQ(0, motionArgs.buttonState);
6049
Michael Wrightd02c5b62014-02-10 15:10:22 -08006050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6051 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6052 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6053
6054 // press BTN_FORWARD, release BTN_FORWARD
6055 processKey(mapper, BTN_FORWARD, 1);
6056 processSync(mapper);
6057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6058 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6059 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006060
Michael Wrightd02c5b62014-02-10 15:10:22 -08006061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006063 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6064
6065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6066 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6067 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006068
6069 processKey(mapper, BTN_FORWARD, 0);
6070 processSync(mapper);
6071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006072 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006074
6075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006077 ASSERT_EQ(0, motionArgs.buttonState);
6078
Michael Wrightd02c5b62014-02-10 15:10:22 -08006079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6080 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6081 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6082
6083 // press BTN_EXTRA, release BTN_EXTRA
6084 processKey(mapper, BTN_EXTRA, 1);
6085 processSync(mapper);
6086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6087 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6088 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006089
Michael Wrightd02c5b62014-02-10 15:10:22 -08006090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006092 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6093
6094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6095 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6096 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097
6098 processKey(mapper, BTN_EXTRA, 0);
6099 processSync(mapper);
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006101 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006103
6104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006106 ASSERT_EQ(0, motionArgs.buttonState);
6107
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6109 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6110 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6111
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6113
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114 // press BTN_STYLUS, release BTN_STYLUS
6115 processKey(mapper, BTN_STYLUS, 1);
6116 processSync(mapper);
6117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006119 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6120
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6123 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006124
6125 processKey(mapper, BTN_STYLUS, 0);
6126 processSync(mapper);
6127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006128 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006130
6131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006133 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134
6135 // press BTN_STYLUS2, release BTN_STYLUS2
6136 processKey(mapper, BTN_STYLUS2, 1);
6137 processSync(mapper);
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006140 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6141
6142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6143 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6144 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006145
6146 processKey(mapper, BTN_STYLUS2, 0);
6147 processSync(mapper);
6148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006149 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006150 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006151
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006154 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155
6156 // release touch
6157 processUp(mapper);
6158 processSync(mapper);
6159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6160 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6161 ASSERT_EQ(0, motionArgs.buttonState);
6162}
6163
6164TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006166 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167 prepareButtons();
6168 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006169 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006170
6171 NotifyMotionArgs motionArgs;
6172
6173 // default tool type is finger
6174 processDown(mapper, 100, 200);
6175 processSync(mapper);
6176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6177 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006178 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179
6180 // eraser
6181 processKey(mapper, BTN_TOOL_RUBBER, 1);
6182 processSync(mapper);
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006185 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006186
6187 // stylus
6188 processKey(mapper, BTN_TOOL_RUBBER, 0);
6189 processKey(mapper, BTN_TOOL_PEN, 1);
6190 processSync(mapper);
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006193 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194
6195 // brush
6196 processKey(mapper, BTN_TOOL_PEN, 0);
6197 processKey(mapper, BTN_TOOL_BRUSH, 1);
6198 processSync(mapper);
6199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006201 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006202
6203 // pencil
6204 processKey(mapper, BTN_TOOL_BRUSH, 0);
6205 processKey(mapper, BTN_TOOL_PENCIL, 1);
6206 processSync(mapper);
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006209 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006211 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006212 processKey(mapper, BTN_TOOL_PENCIL, 0);
6213 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6214 processSync(mapper);
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006217 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006218
6219 // mouse
6220 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6221 processKey(mapper, BTN_TOOL_MOUSE, 1);
6222 processSync(mapper);
6223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006225 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226
6227 // lens
6228 processKey(mapper, BTN_TOOL_MOUSE, 0);
6229 processKey(mapper, BTN_TOOL_LENS, 1);
6230 processSync(mapper);
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006233 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234
6235 // double-tap
6236 processKey(mapper, BTN_TOOL_LENS, 0);
6237 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6238 processSync(mapper);
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006241 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006242
6243 // triple-tap
6244 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6245 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6246 processSync(mapper);
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006249 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250
6251 // quad-tap
6252 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6253 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6254 processSync(mapper);
6255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006257 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006258
6259 // finger
6260 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6261 processKey(mapper, BTN_TOOL_FINGER, 1);
6262 processSync(mapper);
6263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6264 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006265 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006266
6267 // stylus trumps finger
6268 processKey(mapper, BTN_TOOL_PEN, 1);
6269 processSync(mapper);
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006272 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273
6274 // eraser trumps stylus
6275 processKey(mapper, BTN_TOOL_RUBBER, 1);
6276 processSync(mapper);
6277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006279 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006280
6281 // mouse trumps eraser
6282 processKey(mapper, BTN_TOOL_MOUSE, 1);
6283 processSync(mapper);
6284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006286 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006287
6288 // back to default tool type
6289 processKey(mapper, BTN_TOOL_MOUSE, 0);
6290 processKey(mapper, BTN_TOOL_RUBBER, 0);
6291 processKey(mapper, BTN_TOOL_PEN, 0);
6292 processKey(mapper, BTN_TOOL_FINGER, 0);
6293 processSync(mapper);
6294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6295 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006296 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006297}
6298
6299TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006301 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006302 prepareButtons();
6303 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006304 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006305 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006306
6307 NotifyMotionArgs motionArgs;
6308
6309 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6310 processKey(mapper, BTN_TOOL_FINGER, 1);
6311 processMove(mapper, 100, 200);
6312 processSync(mapper);
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6314 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6316 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6317
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6319 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6321 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6322
6323 // move a little
6324 processMove(mapper, 150, 250);
6325 processSync(mapper);
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6327 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6329 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6330
6331 // down when BTN_TOUCH is pressed, pressure defaults to 1
6332 processKey(mapper, BTN_TOUCH, 1);
6333 processSync(mapper);
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6335 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6337 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6338
6339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6340 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6342 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6343
6344 // up when BTN_TOUCH is released, hover restored
6345 processKey(mapper, BTN_TOUCH, 0);
6346 processSync(mapper);
6347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6348 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6350 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6351
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6353 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6355 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6356
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6358 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6360 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6361
6362 // exit hover when pointer goes away
6363 processKey(mapper, BTN_TOOL_FINGER, 0);
6364 processSync(mapper);
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6366 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6368 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6369}
6370
6371TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006373 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006374 prepareButtons();
6375 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006376 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377
6378 NotifyMotionArgs motionArgs;
6379
6380 // initially hovering because pressure is 0
6381 processDown(mapper, 100, 200);
6382 processPressure(mapper, 0);
6383 processSync(mapper);
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6385 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6387 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6388
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6390 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6392 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6393
6394 // move a little
6395 processMove(mapper, 150, 250);
6396 processSync(mapper);
6397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6398 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6400 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6401
6402 // down when pressure is non-zero
6403 processPressure(mapper, RAW_PRESSURE_MAX);
6404 processSync(mapper);
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6406 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6408 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6409
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6411 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6413 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6414
6415 // up when pressure becomes 0, hover restored
6416 processPressure(mapper, 0);
6417 processSync(mapper);
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6419 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6421 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6422
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6424 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6426 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6427
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6429 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6431 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6432
6433 // exit hover when pointer goes away
6434 processUp(mapper);
6435 processSync(mapper);
6436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6437 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6439 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6440}
6441
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006442TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6443 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006444 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006445 prepareButtons();
6446 prepareAxes(POSITION | PRESSURE);
6447 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6448
6449 // Touch down.
6450 processDown(mapper, 100, 200);
6451 processPressure(mapper, 1);
6452 processSync(mapper);
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6454 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6455
6456 // Reset the mapper. This should cancel the ongoing gesture.
6457 resetMapper(mapper, ARBITRARY_TIME);
6458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6459 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6460
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6462}
6463
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006464TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6465 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006466 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006467 prepareButtons();
6468 prepareAxes(POSITION | PRESSURE);
6469 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6470
6471 // Set the initial state for the touch pointer.
6472 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6473 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6474 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6475 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6476
6477 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006478 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6479 // does not generate any events.
6480 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006481
6482 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6483 // the recreated touch state to generate a down event.
6484 processSync(mapper);
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6486 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6487
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6489}
6490
lilinnan687e58f2022-07-19 16:00:50 +08006491TEST_F(SingleTouchInputMapperTest,
6492 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6493 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006494 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006495 prepareButtons();
6496 prepareAxes(POSITION);
6497 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6498 NotifyMotionArgs motionArgs;
6499
6500 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006501 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006502 processSync(mapper);
6503
6504 // We should receive a down event
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6506 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6507
6508 // Change display id
6509 clearViewports();
6510 prepareSecondaryDisplay(ViewportType::INTERNAL);
6511
6512 // We should receive a cancel event
6513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6514 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6515 // Then receive reset called
6516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6517}
6518
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006519TEST_F(SingleTouchInputMapperTest,
6520 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6521 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006522 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006523 prepareButtons();
6524 prepareAxes(POSITION);
6525 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6527 NotifyMotionArgs motionArgs;
6528
6529 // Start a new gesture.
6530 processDown(mapper, 100, 200);
6531 processSync(mapper);
6532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6533 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6534
6535 // Make the viewport inactive. This will put the device in disabled mode.
6536 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6537 viewport->isActive = false;
6538 mFakePolicy->updateViewport(*viewport);
6539 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6540
6541 // We should receive a cancel event for the ongoing gesture.
6542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6543 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6544 // Then we should be notified that the device was reset.
6545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6546
6547 // No events are generated while the viewport is inactive.
6548 processMove(mapper, 101, 201);
6549 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006550 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006551 processSync(mapper);
6552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6553
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006554 // Start a new gesture while the viewport is still inactive.
6555 processDown(mapper, 300, 400);
6556 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6557 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6558 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6559 processSync(mapper);
6560
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006561 // Make the viewport active again. The device should resume processing events.
6562 viewport->isActive = true;
6563 mFakePolicy->updateViewport(*viewport);
6564 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6565
6566 // The device is reset because it changes back to direct mode, without generating any events.
6567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6569
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006570 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006571 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6573 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006574
6575 // No more events.
6576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6578}
6579
Prabir Pradhan211ba622022-10-31 21:09:21 +00006580TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6581 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006582 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006583 prepareButtons();
6584 prepareAxes(POSITION);
6585 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6587
6588 // Press a stylus button.
6589 processKey(mapper, BTN_STYLUS, 1);
6590 processSync(mapper);
6591
6592 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6593 processDown(mapper, 100, 200);
6594 processSync(mapper);
6595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6596 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6597 WithCoords(toDisplayX(100), toDisplayY(200)),
6598 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6600 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6601 WithCoords(toDisplayX(100), toDisplayY(200)),
6602 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6603
6604 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6605 // the button has not actually been released, since there will be no pointers through which the
6606 // button state can be reported. The event is generated at the location of the pointer before
6607 // it went up.
6608 processUp(mapper);
6609 processSync(mapper);
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6611 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6612 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6614 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6615 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6616}
6617
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006618TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6619 addConfigurationProperty("touch.deviceType", "touchScreen");
6620 prepareDisplay(ui::ROTATION_0);
6621 prepareButtons();
6622 prepareAxes(POSITION);
6623
6624 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6625
6626 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6628
6629 // Press a stylus button.
6630 processKey(mapper, BTN_STYLUS, 1);
6631 processSync(mapper);
6632
6633 // Start a touch gesture and ensure that the stylus button is not reported.
6634 processDown(mapper, 100, 200);
6635 processSync(mapper);
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6637 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6638
6639 // Release and press the stylus button again.
6640 processKey(mapper, BTN_STYLUS, 0);
6641 processSync(mapper);
6642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6643 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6644 processKey(mapper, BTN_STYLUS, 1);
6645 processSync(mapper);
6646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6647 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6648
6649 // Release the touch gesture.
6650 processUp(mapper);
6651 processSync(mapper);
6652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6653 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6654
6655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6656}
6657
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006658TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6659 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6660 prepareDisplay(ui::ROTATION_0);
6661 prepareButtons();
6662 prepareAxes(POSITION);
6663 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6665
6666 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6667}
6668
Seunghwan Choi356026c2023-02-01 14:37:25 +09006669TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
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(true);
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),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006686 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006687 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6688 ASSERT_TRUE(fakePointerController->isPointerShown());
6689 ASSERT_NO_FATAL_FAILURE(
6690 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6691}
6692
6693TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6694 std::shared_ptr<FakePointerController> fakePointerController =
6695 std::make_shared<FakePointerController>();
6696 addConfigurationProperty("touch.deviceType", "touchScreen");
6697 prepareDisplay(ui::ROTATION_0);
6698 prepareButtons();
6699 prepareAxes(POSITION);
6700 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6701 mFakePolicy->setPointerController(fakePointerController);
6702 mFakePolicy->setStylusPointerIconEnabled(false);
6703 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6704
6705 processKey(mapper, BTN_TOOL_PEN, 1);
6706 processMove(mapper, 100, 200);
6707 processSync(mapper);
6708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6709 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006710 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006711 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6712 ASSERT_FALSE(fakePointerController->isPointerShown());
6713}
6714
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006715TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6716 // Initialize the device without setting device source to touch navigation.
6717 addConfigurationProperty("touch.deviceType", "touchScreen");
6718 prepareDisplay(ui::ROTATION_0);
6719 prepareButtons();
6720 prepareAxes(POSITION);
6721 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6722
6723 // Ensure that the device is created as a touchscreen, not touch navigation.
6724 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6725
6726 // Add device type association after the device was created.
6727 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6728
6729 // Send update to the mapper.
6730 std::list<NotifyArgs> unused2 =
6731 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6732 InputReaderConfiguration::CHANGE_DEVICE_TYPE /*changes*/);
6733
6734 // Check whether device type update was successful.
6735 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6736}
6737
Prabir Pradhan5632d622021-09-06 07:57:20 -07006738// --- TouchDisplayProjectionTest ---
6739
6740class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6741public:
6742 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6743 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6744 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006745 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6746 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6747 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006748 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006749 auto rotatedWidth = naturalDisplayWidth;
6750 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006751 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006752 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006753 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006754 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006755 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006756 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006757 inverseRotationFlags = ui::Transform::ROT_180;
6758 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006759 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006760 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006761 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006762 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006763 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006764 inverseRotationFlags = ui::Transform::ROT_0;
6765 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006766 }
6767
Prabir Pradhana9df3162022-12-05 23:57:27 +00006768 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006769 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6770
6771 std::optional<DisplayViewport> internalViewport =
6772 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6773 DisplayViewport& v = *internalViewport;
6774 v.displayId = DISPLAY_ID;
6775 v.orientation = orientation;
6776
6777 v.logicalLeft = 0;
6778 v.logicalTop = 0;
6779 v.logicalRight = 100;
6780 v.logicalBottom = 100;
6781
6782 v.physicalLeft = rotatedPhysicalDisplay.left;
6783 v.physicalTop = rotatedPhysicalDisplay.top;
6784 v.physicalRight = rotatedPhysicalDisplay.right;
6785 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6786
Prabir Pradhana9df3162022-12-05 23:57:27 +00006787 v.deviceWidth = rotatedWidth;
6788 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006789
6790 v.isActive = true;
6791 v.uniqueId = UNIQUE_ID;
6792 v.type = ViewportType::INTERNAL;
6793 mFakePolicy->updateViewport(v);
6794 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6795 }
6796
6797 void assertReceivedMove(const Point& point) {
6798 NotifyMotionArgs motionArgs;
6799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6801 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6803 1, 0, 0, 0, 0, 0, 0, 0));
6804 }
6805};
6806
6807TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6808 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006809 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006810
6811 prepareButtons();
6812 prepareAxes(POSITION);
6813 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6814
6815 NotifyMotionArgs motionArgs;
6816
6817 // Configure the DisplayViewport such that the logical display maps to a subsection of
6818 // the display panel called the physical display. Here, the physical display is bounded by the
6819 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6820 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6821 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6822 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6823
Michael Wrighta9cf4192022-12-01 23:46:39 +00006824 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006825 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6826
6827 // Touches outside the physical display should be ignored, and should not generate any
6828 // events. Ensure touches at the following points that lie outside of the physical display
6829 // area do not generate any events.
6830 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6831 processDown(mapper, toRawX(point.x), toRawY(point.y));
6832 processSync(mapper);
6833 processUp(mapper);
6834 processSync(mapper);
6835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6836 << "Unexpected event generated for touch outside physical display at point: "
6837 << point.x << ", " << point.y;
6838 }
6839 }
6840}
6841
6842TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6843 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006844 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006845
6846 prepareButtons();
6847 prepareAxes(POSITION);
6848 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6849
6850 NotifyMotionArgs motionArgs;
6851
6852 // Configure the DisplayViewport such that the logical display maps to a subsection of
6853 // the display panel called the physical display. Here, the physical display is bounded by the
6854 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6855 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6856
Michael Wrighta9cf4192022-12-01 23:46:39 +00006857 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006858 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6859
6860 // Touches that start outside the physical display should be ignored until it enters the
6861 // physical display bounds, at which point it should generate a down event. Start a touch at
6862 // the point (5, 100), which is outside the physical display bounds.
6863 static const Point kOutsidePoint{5, 100};
6864 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6865 processSync(mapper);
6866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6867
6868 // Move the touch into the physical display area. This should generate a pointer down.
6869 processMove(mapper, toRawX(11), toRawY(21));
6870 processSync(mapper);
6871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6872 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6873 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6874 ASSERT_NO_FATAL_FAILURE(
6875 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6876
6877 // Move the touch inside the physical display area. This should generate a pointer move.
6878 processMove(mapper, toRawX(69), toRawY(159));
6879 processSync(mapper);
6880 assertReceivedMove({69, 159});
6881
6882 // Move outside the physical display area. Since the pointer is already down, this should
6883 // now continue generating events.
6884 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6885 processSync(mapper);
6886 assertReceivedMove(kOutsidePoint);
6887
6888 // Release. This should generate a pointer up.
6889 processUp(mapper);
6890 processSync(mapper);
6891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6892 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6894 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6895
6896 // Ensure no more events were generated.
6897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6899 }
6900}
6901
Prabir Pradhana9df3162022-12-05 23:57:27 +00006902// --- TouchscreenPrecisionTests ---
6903
6904// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6905// in various orientations and with different display rotations. We configure the touchscreen to
6906// have a higher resolution than that of the display by an integer scale factor in each axis so that
6907// we can enforce that coordinates match precisely as expected.
6908class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6909 public ::testing::WithParamInterface<ui::Rotation> {
6910public:
6911 void SetUp() override {
6912 SingleTouchInputMapperTest::SetUp();
6913
6914 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6915 // four times the resolution of the display in the Y axis.
6916 prepareButtons();
6917 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006918 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6919 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006920 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006921 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6922 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006923 }
6924
6925 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6926 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6927 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6928 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6929
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006930 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6931 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6932
6933 static const int32_t PRECISION_RAW_X_FLAT = 16;
6934 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6935
6936 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6937 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6938
Prabir Pradhana9df3162022-12-05 23:57:27 +00006939 static const std::array<Point, 4> kRawCorners;
6940};
6941
6942const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6943 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6944 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6945 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6946 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6947}};
6948
6949// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6950// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6951// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6952TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6953 enum class Orientation {
6954 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6955 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6956 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6957 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6958 ftl_last = ORIENTATION_270,
6959 };
6960 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6961 Orientation::ORIENTATION_270;
6962 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6963 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6964 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6965 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6966 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6967 };
6968
6969 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6970
6971 // Configure the touchscreen as being installed in the one of the four different orientations
6972 // relative to the display.
6973 addConfigurationProperty("touch.deviceType", "touchScreen");
6974 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6975 prepareDisplay(ui::ROTATION_0);
6976
6977 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6978
6979 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6980 // orientations of either 90 or 270) this means the display's natural resolution will be
6981 // flipped.
6982 const bool displayRotated =
6983 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6984 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6985 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6986 const Rect physicalFrame{0, 0, width, height};
6987 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6988
6989 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6990 const float expectedPrecisionX = displayRotated ? 4 : 2;
6991 const float expectedPrecisionY = displayRotated ? 2 : 4;
6992
6993 // Test all four corners.
6994 for (int i = 0; i < 4; i++) {
6995 const auto& raw = kRawCorners[i];
6996 processDown(mapper, raw.x, raw.y);
6997 processSync(mapper);
6998 const auto& expected = expectedPoints[i];
6999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7000 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7001 WithCoords(expected.x, expected.y),
7002 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7003 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7004 << "with touchscreen orientation "
7005 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7006 << expected.x << ", " << expected.y << ").";
7007 processUp(mapper);
7008 processSync(mapper);
7009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7010 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7011 WithCoords(expected.x, expected.y))));
7012 }
7013}
7014
Prabir Pradhan82687402022-12-06 01:32:53 +00007015TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7016 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7017 kMappedCorners = {
7018 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7019 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7020 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7021 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7022 };
7023
7024 const ui::Rotation displayRotation = GetParam();
7025
7026 addConfigurationProperty("touch.deviceType", "touchScreen");
7027 prepareDisplay(displayRotation);
7028
7029 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7030
7031 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7032
7033 // Test all four corners.
7034 for (int i = 0; i < 4; i++) {
7035 const auto& expected = expectedPoints[i];
7036 const auto& raw = kRawCorners[i];
7037 processDown(mapper, raw.x, raw.y);
7038 processSync(mapper);
7039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7040 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7041 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7042 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7043 << "with display rotation " << ui::toCString(displayRotation)
7044 << ", expected point (" << expected.x << ", " << expected.y << ").";
7045 processUp(mapper);
7046 processSync(mapper);
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7048 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7049 WithCoords(expected.x, expected.y))));
7050 }
7051}
7052
Prabir Pradhan3e798762022-12-02 21:02:11 +00007053TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7054 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7055 kMappedCorners = {
7056 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7057 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7058 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7059 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7060 };
7061
7062 const ui::Rotation displayRotation = GetParam();
7063
7064 addConfigurationProperty("touch.deviceType", "touchScreen");
7065 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7066
7067 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7068
7069 // Ori 270, so width and height swapped
7070 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7071 prepareDisplay(displayRotation);
7072 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7073
7074 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7075
7076 // Test all four corners.
7077 for (int i = 0; i < 4; i++) {
7078 const auto& expected = expectedPoints[i];
7079 const auto& raw = kRawCorners[i];
7080 processDown(mapper, raw.x, raw.y);
7081 processSync(mapper);
7082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7083 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7084 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7085 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7086 << "with display rotation " << ui::toCString(displayRotation)
7087 << ", expected point (" << expected.x << ", " << expected.y << ").";
7088 processUp(mapper);
7089 processSync(mapper);
7090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7091 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7092 WithCoords(expected.x, expected.y))));
7093 }
7094}
7095
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007096TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7097 const ui::Rotation displayRotation = GetParam();
7098
7099 addConfigurationProperty("touch.deviceType", "touchScreen");
7100 prepareDisplay(displayRotation);
7101
7102 __attribute__((unused)) SingleTouchInputMapper& mapper =
7103 addMapperAndConfigure<SingleTouchInputMapper>();
7104
7105 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7106 // MotionRanges use display pixels as their units
7107 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7108 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7109
7110 // The MotionRanges should be oriented in the rotated display's coordinate space
7111 const bool displayRotated =
7112 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7113
7114 constexpr float MAX_X = 479.5;
7115 constexpr float MAX_Y = 799.75;
7116 EXPECT_EQ(xRange->min, 0.f);
7117 EXPECT_EQ(yRange->min, 0.f);
7118 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7119 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7120
7121 EXPECT_EQ(xRange->flat, 8.f);
7122 EXPECT_EQ(yRange->flat, 8.f);
7123
7124 EXPECT_EQ(xRange->fuzz, 2.f);
7125 EXPECT_EQ(yRange->fuzz, 2.f);
7126
7127 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7128 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7129}
7130
Prabir Pradhana9df3162022-12-05 23:57:27 +00007131// Run the precision tests for all rotations.
7132INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7133 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7134 ui::ROTATION_270),
7135 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7136 return ftl::enum_string(testParamInfo.param);
7137 });
7138
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007139// --- ExternalStylusFusionTest ---
7140
7141class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7142public:
7143 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7144 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007145 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007146 prepareButtons();
7147 prepareAxes(POSITION);
7148 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7149
7150 mStylusState.when = ARBITRARY_TIME;
7151 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007152 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007153 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
7154 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
7155 processExternalStylusState(mapper);
7156 return mapper;
7157 }
7158
7159 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7160 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7161 for (const NotifyArgs& args : generatedArgs) {
7162 mFakeListener->notify(args);
7163 }
7164 // Loop the reader to flush the input listener queue.
7165 mReader->loopOnce();
7166 return generatedArgs;
7167 }
7168
7169protected:
7170 StylusState mStylusState{};
7171 static constexpr uint32_t EXPECTED_SOURCE =
7172 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7173
7174 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7175 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007176 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007177
7178 // The first pointer is withheld.
7179 processDown(mapper, 100, 200);
7180 processSync(mapper);
7181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7182 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7183 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7184
7185 // The external stylus reports pressure. The withheld finger pointer is released as a
7186 // stylus.
7187 mStylusState.pressure = 1.f;
7188 processExternalStylusState(mapper);
7189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7190 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7191 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7192
7193 // Subsequent pointer events are not withheld.
7194 processMove(mapper, 101, 201);
7195 processSync(mapper);
7196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7197 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7198
7199 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7201 }
7202
7203 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7204 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7205
7206 // Releasing the touch pointer ends the gesture.
7207 processUp(mapper);
7208 processSync(mapper);
7209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7210 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007211 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007212
7213 mStylusState.pressure = 0.f;
7214 processExternalStylusState(mapper);
7215 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7217 }
7218
7219 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7220 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007221 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007222
7223 // The first pointer is withheld when an external stylus is connected,
7224 // and a timeout is requested.
7225 processDown(mapper, 100, 200);
7226 processSync(mapper);
7227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7228 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7229 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7230
7231 // If the timeout expires early, it is requested again.
7232 handleTimeout(mapper, ARBITRARY_TIME + 1);
7233 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7234 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7235
7236 // When the timeout expires, the withheld touch is released as a finger pointer.
7237 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7239 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7240
7241 // Subsequent pointer events are not withheld.
7242 processMove(mapper, 101, 201);
7243 processSync(mapper);
7244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7245 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7246 processUp(mapper);
7247 processSync(mapper);
7248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7249 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7250
7251 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7253 }
7254
7255private:
7256 InputDeviceInfo mExternalStylusDeviceInfo{};
7257};
7258
7259TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7260 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7261 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7262}
7263
7264TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7265 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7266 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7267}
7268
7269TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7270 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7271 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7272}
7273
7274// Test a successful stylus fusion gesture where the pressure is reported by the external
7275// before the touch is reported by the touchscreen.
7276TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7277 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7278 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007279 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007280
7281 // The external stylus reports pressure first. It is ignored for now.
7282 mStylusState.pressure = 1.f;
7283 processExternalStylusState(mapper);
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7285 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7286
7287 // When the touch goes down afterwards, it is reported as a stylus pointer.
7288 processDown(mapper, 100, 200);
7289 processSync(mapper);
7290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7291 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7292 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7293
7294 processMove(mapper, 101, 201);
7295 processSync(mapper);
7296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7297 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7298 processUp(mapper);
7299 processSync(mapper);
7300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7301 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7302
7303 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7305}
7306
7307TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7308 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7309
7310 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7311 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7312
7313 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7314 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7315 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7316 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7317}
7318
7319TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7320 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7321 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007322 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007323
7324 mStylusState.pressure = 0.8f;
7325 processExternalStylusState(mapper);
7326 processDown(mapper, 100, 200);
7327 processSync(mapper);
7328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7329 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7330 WithPressure(0.8f))));
7331 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7332
7333 // The external stylus reports a pressure change. We wait for some time for a touch event.
7334 mStylusState.pressure = 0.6f;
7335 processExternalStylusState(mapper);
7336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7337 ASSERT_NO_FATAL_FAILURE(
7338 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7339
7340 // If a touch is reported within the timeout, it reports the updated pressure.
7341 processMove(mapper, 101, 201);
7342 processSync(mapper);
7343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7344 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7345 WithPressure(0.6f))));
7346 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7347
7348 // There is another pressure change.
7349 mStylusState.pressure = 0.5f;
7350 processExternalStylusState(mapper);
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7352 ASSERT_NO_FATAL_FAILURE(
7353 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7354
7355 // If a touch is not reported within the timeout, a move event is generated to report
7356 // the new pressure.
7357 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7359 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7360 WithPressure(0.5f))));
7361
7362 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7363 // repeated indefinitely.
7364 mStylusState.pressure = 0.0f;
7365 processExternalStylusState(mapper);
7366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7367 ASSERT_NO_FATAL_FAILURE(
7368 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7369 processMove(mapper, 102, 202);
7370 processSync(mapper);
7371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7372 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7373 WithPressure(0.5f))));
7374 processMove(mapper, 103, 203);
7375 processSync(mapper);
7376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7377 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7378 WithPressure(0.5f))));
7379
7380 processUp(mapper);
7381 processSync(mapper);
7382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7383 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007384 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007385
7386 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7388}
7389
7390TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7391 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7392 auto source = WithSource(EXPECTED_SOURCE);
7393
7394 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007395 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007396 processExternalStylusState(mapper);
7397 processDown(mapper, 100, 200);
7398 processSync(mapper);
7399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7400 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007401 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007402 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7403
7404 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007405 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007406 processExternalStylusState(mapper);
7407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7408 ASSERT_NO_FATAL_FAILURE(
7409 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7410
7411 // If a touch is reported within the timeout, it reports the updated pressure.
7412 processMove(mapper, 101, 201);
7413 processSync(mapper);
7414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7415 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007416 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007417 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7418
7419 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007420 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007421 processExternalStylusState(mapper);
7422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7423 ASSERT_NO_FATAL_FAILURE(
7424 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7425
7426 // If a touch is not reported within the timeout, a move event is generated to report
7427 // the new tool type.
7428 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7430 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007431 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007432
7433 processUp(mapper);
7434 processSync(mapper);
7435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7436 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007437 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007438
7439 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7441}
7442
7443TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7444 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7445 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007446 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007447
7448 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7449
7450 // The external stylus reports a button change. We wait for some time for a touch event.
7451 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7452 processExternalStylusState(mapper);
7453 ASSERT_NO_FATAL_FAILURE(
7454 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7455
7456 // If a touch is reported within the timeout, it reports the updated button state.
7457 processMove(mapper, 101, 201);
7458 processSync(mapper);
7459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7460 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7461 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7463 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7464 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7465 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7466
7467 // The button is now released.
7468 mStylusState.buttons = 0;
7469 processExternalStylusState(mapper);
7470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7471 ASSERT_NO_FATAL_FAILURE(
7472 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7473
7474 // If a touch is not reported within the timeout, a move event is generated to report
7475 // the new button state.
7476 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7478 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7479 WithButtonState(0))));
7480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007481 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7482 WithButtonState(0))));
7483
7484 processUp(mapper);
7485 processSync(mapper);
7486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007487 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7488
7489 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7491}
7492
Michael Wrightd02c5b62014-02-10 15:10:22 -08007493// --- MultiTouchInputMapperTest ---
7494
7495class MultiTouchInputMapperTest : public TouchInputMapperTest {
7496protected:
7497 void prepareAxes(int axes);
7498
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007499 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7500 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7501 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7502 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7503 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7504 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7505 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7506 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7507 void processId(MultiTouchInputMapper& mapper, int32_t id);
7508 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7509 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7510 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007511 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007512 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007513 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7514 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007515};
7516
7517void MultiTouchInputMapperTest::prepareAxes(int axes) {
7518 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007519 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7520 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007521 }
7522 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007523 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7524 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007525 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007526 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7527 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007528 }
7529 }
7530 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007531 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7532 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007533 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007534 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007535 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007536 }
7537 }
7538 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007539 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7540 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007541 }
7542 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007543 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7544 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007545 }
7546 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007547 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7548 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007549 }
7550 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007551 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7552 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007553 }
7554 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007555 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7556 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007557 }
7558 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007559 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007560 }
7561}
7562
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007563void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7564 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007567}
7568
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007569void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7570 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007572}
7573
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007574void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7575 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007577}
7578
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007579void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007581}
7582
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007583void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007585}
7586
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007587void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7588 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007590}
7591
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007592void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007594}
7595
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007596void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007598}
7599
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007600void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007601 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007602}
7603
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007604void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007606}
7607
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007608void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007609 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007610}
7611
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007612void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7613 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007615}
7616
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007617void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7618 int32_t value) {
7619 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7621}
7622
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007623void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007624 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007625}
7626
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007627void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7628 nsecs_t readTime) {
7629 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007630}
7631
Michael Wrightd02c5b62014-02-10 15:10:22 -08007632TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007633 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007634 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007635 prepareAxes(POSITION);
7636 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007637 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007638
arthurhungdcef2dc2020-08-11 14:47:50 +08007639 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007640
7641 NotifyMotionArgs motionArgs;
7642
7643 // Two fingers down at once.
7644 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7645 processPosition(mapper, x1, y1);
7646 processMTSync(mapper);
7647 processPosition(mapper, x2, y2);
7648 processMTSync(mapper);
7649 processSync(mapper);
7650
7651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7652 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7653 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7654 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7655 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7656 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7657 ASSERT_EQ(0, motionArgs.flags);
7658 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7659 ASSERT_EQ(0, motionArgs.buttonState);
7660 ASSERT_EQ(0, motionArgs.edgeFlags);
7661 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7662 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007663 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7665 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7666 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7667 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7668 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7669
7670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7671 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7672 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7673 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7674 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007675 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007676 ASSERT_EQ(0, motionArgs.flags);
7677 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7678 ASSERT_EQ(0, motionArgs.buttonState);
7679 ASSERT_EQ(0, motionArgs.edgeFlags);
7680 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7681 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007682 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007683 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007684 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7686 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7688 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7689 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7690 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7691 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7692
7693 // Move.
7694 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7695 processPosition(mapper, x1, y1);
7696 processMTSync(mapper);
7697 processPosition(mapper, x2, y2);
7698 processMTSync(mapper);
7699 processSync(mapper);
7700
7701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7702 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7703 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7704 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7705 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7706 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7707 ASSERT_EQ(0, motionArgs.flags);
7708 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7709 ASSERT_EQ(0, motionArgs.buttonState);
7710 ASSERT_EQ(0, motionArgs.edgeFlags);
7711 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7712 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007713 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007714 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007715 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7717 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7719 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7720 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7721 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7722 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7723
7724 // First finger up.
7725 x2 += 15; y2 -= 20;
7726 processPosition(mapper, x2, y2);
7727 processMTSync(mapper);
7728 processSync(mapper);
7729
7730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7731 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7732 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7733 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7734 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007735 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007736 ASSERT_EQ(0, motionArgs.flags);
7737 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7738 ASSERT_EQ(0, motionArgs.buttonState);
7739 ASSERT_EQ(0, motionArgs.edgeFlags);
7740 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7741 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007742 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007743 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007744 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7746 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7748 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7749 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7750 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7751 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7752
7753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7754 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7755 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7756 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7757 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7758 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7759 ASSERT_EQ(0, motionArgs.flags);
7760 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7761 ASSERT_EQ(0, motionArgs.buttonState);
7762 ASSERT_EQ(0, motionArgs.edgeFlags);
7763 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7764 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007765 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7767 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7768 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7769 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7770 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7771
7772 // Move.
7773 x2 += 20; y2 -= 25;
7774 processPosition(mapper, x2, y2);
7775 processMTSync(mapper);
7776 processSync(mapper);
7777
7778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7779 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7780 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7781 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7782 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7784 ASSERT_EQ(0, motionArgs.flags);
7785 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7786 ASSERT_EQ(0, motionArgs.buttonState);
7787 ASSERT_EQ(0, motionArgs.edgeFlags);
7788 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7789 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007790 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7792 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7793 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7794 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7795 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7796
7797 // New finger down.
7798 int32_t x3 = 700, y3 = 300;
7799 processPosition(mapper, x2, y2);
7800 processMTSync(mapper);
7801 processPosition(mapper, x3, y3);
7802 processMTSync(mapper);
7803 processSync(mapper);
7804
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7806 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7807 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7808 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7809 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007810 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007811 ASSERT_EQ(0, motionArgs.flags);
7812 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7813 ASSERT_EQ(0, motionArgs.buttonState);
7814 ASSERT_EQ(0, motionArgs.edgeFlags);
7815 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7816 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007817 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007818 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007819 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7821 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7823 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7824 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7825 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7826 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7827
7828 // Second finger up.
7829 x3 += 30; y3 -= 20;
7830 processPosition(mapper, x3, y3);
7831 processMTSync(mapper);
7832 processSync(mapper);
7833
7834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7835 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7836 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7837 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7838 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007839 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007840 ASSERT_EQ(0, motionArgs.flags);
7841 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7842 ASSERT_EQ(0, motionArgs.buttonState);
7843 ASSERT_EQ(0, motionArgs.edgeFlags);
7844 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7845 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007846 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007847 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007848 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7850 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7852 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7853 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7854 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7855 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7856
7857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7858 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7859 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7860 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7861 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7863 ASSERT_EQ(0, motionArgs.flags);
7864 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7865 ASSERT_EQ(0, motionArgs.buttonState);
7866 ASSERT_EQ(0, motionArgs.edgeFlags);
7867 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7868 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007869 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7871 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7872 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7873 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7874 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7875
7876 // Last finger up.
7877 processMTSync(mapper);
7878 processSync(mapper);
7879
7880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7881 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7882 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7883 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7884 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7885 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7886 ASSERT_EQ(0, motionArgs.flags);
7887 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7888 ASSERT_EQ(0, motionArgs.buttonState);
7889 ASSERT_EQ(0, motionArgs.edgeFlags);
7890 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7891 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007892 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7894 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7895 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7896 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7897 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7898
7899 // Should not have sent any more keys or motions.
7900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7902}
7903
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007904TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7905 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007906 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007907
7908 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7909 /*fuzz*/ 0, /*resolution*/ 10);
7910 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7911 /*fuzz*/ 0, /*resolution*/ 11);
7912 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7913 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7914 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7915 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7916 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7917 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7918 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7919 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7920
7921 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7922
7923 // X and Y axes
7924 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7925 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7926 // Touch major and minor
7927 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7928 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7929 // Tool major and minor
7930 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7931 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7932}
7933
7934TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7935 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007936 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007937
7938 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7939 /*fuzz*/ 0, /*resolution*/ 10);
7940 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7941 /*fuzz*/ 0, /*resolution*/ 11);
7942
7943 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7944
7945 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7946
7947 // Touch major and minor
7948 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7949 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7950 // Tool major and minor
7951 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7952 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7953}
7954
Michael Wrightd02c5b62014-02-10 15:10:22 -08007955TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007956 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007957 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007958 prepareAxes(POSITION | ID);
7959 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007960 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007961
arthurhungdcef2dc2020-08-11 14:47:50 +08007962 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007963
7964 NotifyMotionArgs motionArgs;
7965
7966 // Two fingers down at once.
7967 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7968 processPosition(mapper, x1, y1);
7969 processId(mapper, 1);
7970 processMTSync(mapper);
7971 processPosition(mapper, x2, y2);
7972 processId(mapper, 2);
7973 processMTSync(mapper);
7974 processSync(mapper);
7975
7976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7977 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7978 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7979 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007980 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7982 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7983
7984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007985 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007986 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7987 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007988 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007989 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007990 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7992 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7994 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7995
7996 // Move.
7997 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7998 processPosition(mapper, x1, y1);
7999 processId(mapper, 1);
8000 processMTSync(mapper);
8001 processPosition(mapper, x2, y2);
8002 processId(mapper, 2);
8003 processMTSync(mapper);
8004 processSync(mapper);
8005
8006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8008 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8009 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008010 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008011 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008012 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8014 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8016 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8017
8018 // First finger up.
8019 x2 += 15; y2 -= 20;
8020 processPosition(mapper, x2, y2);
8021 processId(mapper, 2);
8022 processMTSync(mapper);
8023 processSync(mapper);
8024
8025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008026 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008027 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8028 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008029 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008030 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008031 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8033 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8035 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8036
8037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8039 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8040 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008041 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8043 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8044
8045 // Move.
8046 x2 += 20; y2 -= 25;
8047 processPosition(mapper, x2, y2);
8048 processId(mapper, 2);
8049 processMTSync(mapper);
8050 processSync(mapper);
8051
8052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8054 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8055 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008056 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8058 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8059
8060 // New finger down.
8061 int32_t x3 = 700, y3 = 300;
8062 processPosition(mapper, x2, y2);
8063 processId(mapper, 2);
8064 processMTSync(mapper);
8065 processPosition(mapper, x3, y3);
8066 processId(mapper, 3);
8067 processMTSync(mapper);
8068 processSync(mapper);
8069
8070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008071 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8073 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008074 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008075 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008076 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8078 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8080 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8081
8082 // Second finger up.
8083 x3 += 30; y3 -= 20;
8084 processPosition(mapper, x3, y3);
8085 processId(mapper, 3);
8086 processMTSync(mapper);
8087 processSync(mapper);
8088
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008090 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008091 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8092 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008093 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008094 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008095 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8097 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8099 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8100
8101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8103 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8104 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008105 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8107 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8108
8109 // Last finger up.
8110 processMTSync(mapper);
8111 processSync(mapper);
8112
8113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8114 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8115 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8116 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008117 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8119 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8120
8121 // Should not have sent any more keys or motions.
8122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8124}
8125
8126TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008127 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008128 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008129 prepareAxes(POSITION | ID | SLOT);
8130 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008131 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008132
arthurhungdcef2dc2020-08-11 14:47:50 +08008133 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008134
8135 NotifyMotionArgs motionArgs;
8136
8137 // Two fingers down at once.
8138 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8139 processPosition(mapper, x1, y1);
8140 processId(mapper, 1);
8141 processSlot(mapper, 1);
8142 processPosition(mapper, x2, y2);
8143 processId(mapper, 2);
8144 processSync(mapper);
8145
8146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8147 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8148 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8149 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008150 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008151 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8152 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8153
8154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008155 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008156 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8157 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008158 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008159 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008160 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8162 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8164 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8165
8166 // Move.
8167 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8168 processSlot(mapper, 0);
8169 processPosition(mapper, x1, y1);
8170 processSlot(mapper, 1);
8171 processPosition(mapper, x2, y2);
8172 processSync(mapper);
8173
8174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8175 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8176 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8177 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008178 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008180 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8182 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8184 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8185
8186 // First finger up.
8187 x2 += 15; y2 -= 20;
8188 processSlot(mapper, 0);
8189 processId(mapper, -1);
8190 processSlot(mapper, 1);
8191 processPosition(mapper, x2, y2);
8192 processSync(mapper);
8193
8194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008195 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008196 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8197 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008198 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008199 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008200 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8202 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8204 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8205
8206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8208 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8209 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008210 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8212 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8213
8214 // Move.
8215 x2 += 20; y2 -= 25;
8216 processPosition(mapper, x2, y2);
8217 processSync(mapper);
8218
8219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8221 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8222 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008223 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8225 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8226
8227 // New finger down.
8228 int32_t x3 = 700, y3 = 300;
8229 processPosition(mapper, x2, y2);
8230 processSlot(mapper, 0);
8231 processId(mapper, 3);
8232 processPosition(mapper, x3, y3);
8233 processSync(mapper);
8234
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008236 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008237 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8238 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008239 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008240 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008241 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8243 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8245 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8246
8247 // Second finger up.
8248 x3 += 30; y3 -= 20;
8249 processSlot(mapper, 1);
8250 processId(mapper, -1);
8251 processSlot(mapper, 0);
8252 processPosition(mapper, x3, y3);
8253 processSync(mapper);
8254
8255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008256 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008257 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8258 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008259 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008260 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008261 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8263 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8265 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8266
8267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8269 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8270 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008271 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8273 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8274
8275 // Last finger up.
8276 processId(mapper, -1);
8277 processSync(mapper);
8278
8279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8280 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8281 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8282 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008283 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8285 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8286
8287 // Should not have sent any more keys or motions.
8288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8290}
8291
8292TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008293 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008294 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008295 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008296 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008297
8298 // These calculations are based on the input device calibration documentation.
8299 int32_t rawX = 100;
8300 int32_t rawY = 200;
8301 int32_t rawTouchMajor = 7;
8302 int32_t rawTouchMinor = 6;
8303 int32_t rawToolMajor = 9;
8304 int32_t rawToolMinor = 8;
8305 int32_t rawPressure = 11;
8306 int32_t rawDistance = 0;
8307 int32_t rawOrientation = 3;
8308 int32_t id = 5;
8309
8310 float x = toDisplayX(rawX);
8311 float y = toDisplayY(rawY);
8312 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8313 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8314 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8315 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8316 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8317 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8318 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8319 float distance = float(rawDistance);
8320
8321 processPosition(mapper, rawX, rawY);
8322 processTouchMajor(mapper, rawTouchMajor);
8323 processTouchMinor(mapper, rawTouchMinor);
8324 processToolMajor(mapper, rawToolMajor);
8325 processToolMinor(mapper, rawToolMinor);
8326 processPressure(mapper, rawPressure);
8327 processOrientation(mapper, rawOrientation);
8328 processDistance(mapper, rawDistance);
8329 processId(mapper, id);
8330 processMTSync(mapper);
8331 processSync(mapper);
8332
8333 NotifyMotionArgs args;
8334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8335 ASSERT_EQ(0, args.pointerProperties[0].id);
8336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8337 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8338 orientation, distance));
8339}
8340
8341TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008342 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008343 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008344 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8345 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008346 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008347
8348 // These calculations are based on the input device calibration documentation.
8349 int32_t rawX = 100;
8350 int32_t rawY = 200;
8351 int32_t rawTouchMajor = 140;
8352 int32_t rawTouchMinor = 120;
8353 int32_t rawToolMajor = 180;
8354 int32_t rawToolMinor = 160;
8355
8356 float x = toDisplayX(rawX);
8357 float y = toDisplayY(rawY);
8358 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8359 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8360 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8361 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8362 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8363
8364 processPosition(mapper, rawX, rawY);
8365 processTouchMajor(mapper, rawTouchMajor);
8366 processTouchMinor(mapper, rawTouchMinor);
8367 processToolMajor(mapper, rawToolMajor);
8368 processToolMinor(mapper, rawToolMinor);
8369 processMTSync(mapper);
8370 processSync(mapper);
8371
8372 NotifyMotionArgs args;
8373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8375 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8376}
8377
8378TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008379 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008380 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008381 prepareAxes(POSITION | TOUCH | TOOL);
8382 addConfigurationProperty("touch.size.calibration", "diameter");
8383 addConfigurationProperty("touch.size.scale", "10");
8384 addConfigurationProperty("touch.size.bias", "160");
8385 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008386 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008387
8388 // These calculations are based on the input device calibration documentation.
8389 // Note: We only provide a single common touch/tool value because the device is assumed
8390 // not to emit separate values for each pointer (isSummed = 1).
8391 int32_t rawX = 100;
8392 int32_t rawY = 200;
8393 int32_t rawX2 = 150;
8394 int32_t rawY2 = 250;
8395 int32_t rawTouchMajor = 5;
8396 int32_t rawToolMajor = 8;
8397
8398 float x = toDisplayX(rawX);
8399 float y = toDisplayY(rawY);
8400 float x2 = toDisplayX(rawX2);
8401 float y2 = toDisplayY(rawY2);
8402 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8403 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8404 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8405
8406 processPosition(mapper, rawX, rawY);
8407 processTouchMajor(mapper, rawTouchMajor);
8408 processToolMajor(mapper, rawToolMajor);
8409 processMTSync(mapper);
8410 processPosition(mapper, rawX2, rawY2);
8411 processTouchMajor(mapper, rawTouchMajor);
8412 processToolMajor(mapper, rawToolMajor);
8413 processMTSync(mapper);
8414 processSync(mapper);
8415
8416 NotifyMotionArgs args;
8417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8418 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8419
8420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008421 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008422 ASSERT_EQ(size_t(2), args.pointerCount);
8423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8424 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8426 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8427}
8428
8429TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008430 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008431 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008432 prepareAxes(POSITION | TOUCH | TOOL);
8433 addConfigurationProperty("touch.size.calibration", "area");
8434 addConfigurationProperty("touch.size.scale", "43");
8435 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008436 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008437
8438 // These calculations are based on the input device calibration documentation.
8439 int32_t rawX = 100;
8440 int32_t rawY = 200;
8441 int32_t rawTouchMajor = 5;
8442 int32_t rawToolMajor = 8;
8443
8444 float x = toDisplayX(rawX);
8445 float y = toDisplayY(rawY);
8446 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8447 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8448 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8449
8450 processPosition(mapper, rawX, rawY);
8451 processTouchMajor(mapper, rawTouchMajor);
8452 processToolMajor(mapper, rawToolMajor);
8453 processMTSync(mapper);
8454 processSync(mapper);
8455
8456 NotifyMotionArgs args;
8457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8459 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8460}
8461
8462TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008463 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008464 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008465 prepareAxes(POSITION | PRESSURE);
8466 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8467 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008468 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008469
Michael Wrightaa449c92017-12-13 21:21:43 +00008470 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008471 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008472 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8473 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8474 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8475
Michael Wrightd02c5b62014-02-10 15:10:22 -08008476 // These calculations are based on the input device calibration documentation.
8477 int32_t rawX = 100;
8478 int32_t rawY = 200;
8479 int32_t rawPressure = 60;
8480
8481 float x = toDisplayX(rawX);
8482 float y = toDisplayY(rawY);
8483 float pressure = float(rawPressure) * 0.01f;
8484
8485 processPosition(mapper, rawX, rawY);
8486 processPressure(mapper, rawPressure);
8487 processMTSync(mapper);
8488 processSync(mapper);
8489
8490 NotifyMotionArgs args;
8491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8493 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8494}
8495
8496TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008497 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008498 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008499 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008500 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008501
8502 NotifyMotionArgs motionArgs;
8503 NotifyKeyArgs keyArgs;
8504
8505 processId(mapper, 1);
8506 processPosition(mapper, 100, 200);
8507 processSync(mapper);
8508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8509 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8510 ASSERT_EQ(0, motionArgs.buttonState);
8511
8512 // press BTN_LEFT, release BTN_LEFT
8513 processKey(mapper, BTN_LEFT, 1);
8514 processSync(mapper);
8515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8516 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8517 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8518
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8520 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8521 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8522
Michael Wrightd02c5b62014-02-10 15:10:22 -08008523 processKey(mapper, BTN_LEFT, 0);
8524 processSync(mapper);
8525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008526 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008527 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008528
8529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008531 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008532
8533 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8534 processKey(mapper, BTN_RIGHT, 1);
8535 processKey(mapper, BTN_MIDDLE, 1);
8536 processSync(mapper);
8537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8539 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8540 motionArgs.buttonState);
8541
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8543 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8544 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8545
8546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8547 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8548 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8549 motionArgs.buttonState);
8550
Michael Wrightd02c5b62014-02-10 15:10:22 -08008551 processKey(mapper, BTN_RIGHT, 0);
8552 processSync(mapper);
8553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008554 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008555 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008556
8557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008559 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008560
8561 processKey(mapper, BTN_MIDDLE, 0);
8562 processSync(mapper);
8563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008564 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008565 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008566
8567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008569 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008570
8571 // press BTN_BACK, release BTN_BACK
8572 processKey(mapper, BTN_BACK, 1);
8573 processSync(mapper);
8574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8575 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8576 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008577
Michael Wrightd02c5b62014-02-10 15:10:22 -08008578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008580 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8581
8582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8583 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8584 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008585
8586 processKey(mapper, BTN_BACK, 0);
8587 processSync(mapper);
8588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008589 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008590 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008591
8592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008594 ASSERT_EQ(0, motionArgs.buttonState);
8595
Michael Wrightd02c5b62014-02-10 15:10:22 -08008596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8597 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8598 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8599
8600 // press BTN_SIDE, release BTN_SIDE
8601 processKey(mapper, BTN_SIDE, 1);
8602 processSync(mapper);
8603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8604 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8605 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008606
Michael Wrightd02c5b62014-02-10 15:10:22 -08008607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008609 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8610
8611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8612 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8613 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008614
8615 processKey(mapper, BTN_SIDE, 0);
8616 processSync(mapper);
8617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008618 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008619 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008620
8621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008623 ASSERT_EQ(0, motionArgs.buttonState);
8624
Michael Wrightd02c5b62014-02-10 15:10:22 -08008625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8626 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8627 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8628
8629 // press BTN_FORWARD, release BTN_FORWARD
8630 processKey(mapper, BTN_FORWARD, 1);
8631 processSync(mapper);
8632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8633 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8634 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008635
Michael Wrightd02c5b62014-02-10 15:10:22 -08008636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008638 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8639
8640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8641 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8642 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008643
8644 processKey(mapper, BTN_FORWARD, 0);
8645 processSync(mapper);
8646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008647 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008648 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008649
8650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008652 ASSERT_EQ(0, motionArgs.buttonState);
8653
Michael Wrightd02c5b62014-02-10 15:10:22 -08008654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8655 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8656 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8657
8658 // press BTN_EXTRA, release BTN_EXTRA
8659 processKey(mapper, BTN_EXTRA, 1);
8660 processSync(mapper);
8661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8662 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8663 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008664
Michael Wrightd02c5b62014-02-10 15:10:22 -08008665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008667 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8668
8669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8670 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8671 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008672
8673 processKey(mapper, BTN_EXTRA, 0);
8674 processSync(mapper);
8675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008676 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008677 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008678
8679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008680 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008681 ASSERT_EQ(0, motionArgs.buttonState);
8682
Michael Wrightd02c5b62014-02-10 15:10:22 -08008683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8684 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8685 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8686
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8688
Michael Wrightd02c5b62014-02-10 15:10:22 -08008689 // press BTN_STYLUS, release BTN_STYLUS
8690 processKey(mapper, BTN_STYLUS, 1);
8691 processSync(mapper);
8692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008694 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8695
8696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8697 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8698 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008699
8700 processKey(mapper, BTN_STYLUS, 0);
8701 processSync(mapper);
8702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008703 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008704 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008705
8706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008708 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008709
8710 // press BTN_STYLUS2, release BTN_STYLUS2
8711 processKey(mapper, BTN_STYLUS2, 1);
8712 processSync(mapper);
8713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008715 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8716
8717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8718 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8719 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008720
8721 processKey(mapper, BTN_STYLUS2, 0);
8722 processSync(mapper);
8723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008724 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008725 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008726
8727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008729 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008730
8731 // release touch
8732 processId(mapper, -1);
8733 processSync(mapper);
8734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8735 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8736 ASSERT_EQ(0, motionArgs.buttonState);
8737}
8738
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008739TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8740 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008741 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008742 prepareAxes(POSITION | ID | SLOT);
8743 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8744
8745 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8746 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8747
8748 // Touch down.
8749 processId(mapper, 1);
8750 processPosition(mapper, 100, 200);
8751 processSync(mapper);
8752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8753 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8754
8755 // Press and release button mapped to the primary stylus button.
8756 processKey(mapper, BTN_A, 1);
8757 processSync(mapper);
8758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8759 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8760 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8762 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8763 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8764
8765 processKey(mapper, BTN_A, 0);
8766 processSync(mapper);
8767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8768 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8770 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8771
8772 // Press and release the HID usage mapped to the secondary stylus button.
8773 processHidUsage(mapper, 0xabcd, 1);
8774 processSync(mapper);
8775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8776 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8777 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8779 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8780 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8781
8782 processHidUsage(mapper, 0xabcd, 0);
8783 processSync(mapper);
8784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8785 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8787 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8788
8789 // Release touch.
8790 processId(mapper, -1);
8791 processSync(mapper);
8792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8793 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8794}
8795
Michael Wrightd02c5b62014-02-10 15:10:22 -08008796TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008797 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008798 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008799 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008800 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008801
8802 NotifyMotionArgs motionArgs;
8803
8804 // default tool type is finger
8805 processId(mapper, 1);
8806 processPosition(mapper, 100, 200);
8807 processSync(mapper);
8808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8809 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008810 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008811
8812 // eraser
8813 processKey(mapper, BTN_TOOL_RUBBER, 1);
8814 processSync(mapper);
8815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8816 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008817 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008818
8819 // stylus
8820 processKey(mapper, BTN_TOOL_RUBBER, 0);
8821 processKey(mapper, BTN_TOOL_PEN, 1);
8822 processSync(mapper);
8823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8824 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008825 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008826
8827 // brush
8828 processKey(mapper, BTN_TOOL_PEN, 0);
8829 processKey(mapper, BTN_TOOL_BRUSH, 1);
8830 processSync(mapper);
8831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008833 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008834
8835 // pencil
8836 processKey(mapper, BTN_TOOL_BRUSH, 0);
8837 processKey(mapper, BTN_TOOL_PENCIL, 1);
8838 processSync(mapper);
8839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008841 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008842
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008843 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008844 processKey(mapper, BTN_TOOL_PENCIL, 0);
8845 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8846 processSync(mapper);
8847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008849 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008850
8851 // mouse
8852 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8853 processKey(mapper, BTN_TOOL_MOUSE, 1);
8854 processSync(mapper);
8855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8856 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008857 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008858
8859 // lens
8860 processKey(mapper, BTN_TOOL_MOUSE, 0);
8861 processKey(mapper, BTN_TOOL_LENS, 1);
8862 processSync(mapper);
8863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008865 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008866
8867 // double-tap
8868 processKey(mapper, BTN_TOOL_LENS, 0);
8869 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8870 processSync(mapper);
8871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008873 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008874
8875 // triple-tap
8876 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8877 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8878 processSync(mapper);
8879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008881 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008882
8883 // quad-tap
8884 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8885 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8886 processSync(mapper);
8887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008889 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008890
8891 // finger
8892 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8893 processKey(mapper, BTN_TOOL_FINGER, 1);
8894 processSync(mapper);
8895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008897 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008898
8899 // stylus trumps finger
8900 processKey(mapper, BTN_TOOL_PEN, 1);
8901 processSync(mapper);
8902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008904 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008905
8906 // eraser trumps stylus
8907 processKey(mapper, BTN_TOOL_RUBBER, 1);
8908 processSync(mapper);
8909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008911 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008912
8913 // mouse trumps eraser
8914 processKey(mapper, BTN_TOOL_MOUSE, 1);
8915 processSync(mapper);
8916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008918 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008919
8920 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8921 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8922 processSync(mapper);
8923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008925 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008926
8927 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8928 processToolType(mapper, MT_TOOL_PEN);
8929 processSync(mapper);
8930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8931 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008932 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008933
8934 // back to default tool type
8935 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8936 processKey(mapper, BTN_TOOL_MOUSE, 0);
8937 processKey(mapper, BTN_TOOL_RUBBER, 0);
8938 processKey(mapper, BTN_TOOL_PEN, 0);
8939 processKey(mapper, BTN_TOOL_FINGER, 0);
8940 processSync(mapper);
8941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8942 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008943 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008944}
8945
8946TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008947 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008948 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008949 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008950 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008951 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008952
8953 NotifyMotionArgs motionArgs;
8954
8955 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8956 processId(mapper, 1);
8957 processPosition(mapper, 100, 200);
8958 processSync(mapper);
8959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8960 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8962 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8963
8964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8965 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8967 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8968
8969 // move a little
8970 processPosition(mapper, 150, 250);
8971 processSync(mapper);
8972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8973 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8975 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8976
8977 // down when BTN_TOUCH is pressed, pressure defaults to 1
8978 processKey(mapper, BTN_TOUCH, 1);
8979 processSync(mapper);
8980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8981 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8983 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8984
8985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8986 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8987 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8988 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8989
8990 // up when BTN_TOUCH is released, hover restored
8991 processKey(mapper, BTN_TOUCH, 0);
8992 processSync(mapper);
8993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8994 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8996 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8997
8998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8999 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9001 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9002
9003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9004 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9006 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9007
9008 // exit hover when pointer goes away
9009 processId(mapper, -1);
9010 processSync(mapper);
9011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9012 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9014 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9015}
9016
9017TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009018 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009019 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009020 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009021 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009022
9023 NotifyMotionArgs motionArgs;
9024
9025 // initially hovering because pressure is 0
9026 processId(mapper, 1);
9027 processPosition(mapper, 100, 200);
9028 processPressure(mapper, 0);
9029 processSync(mapper);
9030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9031 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9033 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9034
9035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9036 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9038 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9039
9040 // move a little
9041 processPosition(mapper, 150, 250);
9042 processSync(mapper);
9043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9044 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9046 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9047
9048 // down when pressure becomes non-zero
9049 processPressure(mapper, RAW_PRESSURE_MAX);
9050 processSync(mapper);
9051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9052 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9053 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9054 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9055
9056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9057 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9058 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9059 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9060
9061 // up when pressure becomes 0, hover restored
9062 processPressure(mapper, 0);
9063 processSync(mapper);
9064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9065 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9067 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9068
9069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9070 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9072 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9073
9074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9075 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9077 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9078
9079 // exit hover when pointer goes away
9080 processId(mapper, -1);
9081 processSync(mapper);
9082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9083 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9085 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9086}
9087
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009088/**
9089 * Set the input device port <--> display port associations, and check that the
9090 * events are routed to the display that matches the display port.
9091 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9092 */
9093TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009094 const std::string usb2 = "USB2";
9095 const uint8_t hdmi1 = 0;
9096 const uint8_t hdmi2 = 1;
9097 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009098 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009099
9100 addConfigurationProperty("touch.deviceType", "touchScreen");
9101 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009102 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009103
9104 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9105 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9106
9107 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9108 // for this input device is specified, and the matching viewport is not present,
9109 // the input device should be disabled (at the mapper level).
9110
9111 // Add viewport for display 2 on hdmi2
9112 prepareSecondaryDisplay(type, hdmi2);
9113 // Send a touch event
9114 processPosition(mapper, 100, 100);
9115 processSync(mapper);
9116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9117
9118 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009119 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009120 // Send a touch event again
9121 processPosition(mapper, 100, 100);
9122 processSync(mapper);
9123
9124 NotifyMotionArgs args;
9125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9126 ASSERT_EQ(DISPLAY_ID, args.displayId);
9127}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009128
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009129TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9130 addConfigurationProperty("touch.deviceType", "touchScreen");
9131 prepareAxes(POSITION);
9132 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9133
9134 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9135
Michael Wrighta9cf4192022-12-01 23:46:39 +00009136 prepareDisplay(ui::ROTATION_0);
9137 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009138
9139 // Send a touch event
9140 processPosition(mapper, 100, 100);
9141 processSync(mapper);
9142
9143 NotifyMotionArgs args;
9144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9145 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9146}
9147
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009148TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009149 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009150 std::shared_ptr<FakePointerController> fakePointerController =
9151 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009152 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009153 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009154 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009155
Garfield Tan888a6a42020-01-09 11:39:16 -08009156 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009157 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009158
Michael Wrighta9cf4192022-12-01 23:46:39 +00009159 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009160 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009161 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009162
Josep del Río2d8c79a2023-01-23 19:33:50 +00009163 // Check source is mouse that would obtain the PointerController.
9164 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009165
9166 NotifyMotionArgs motionArgs;
9167 processPosition(mapper, 100, 100);
9168 processSync(mapper);
9169
9170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9171 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9172 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9173}
9174
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009175/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009176 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9177 */
9178TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9179 addConfigurationProperty("touch.deviceType", "touchScreen");
9180 prepareAxes(POSITION);
9181 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9182
Michael Wrighta9cf4192022-12-01 23:46:39 +00009183 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009184 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9185 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9186 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9187 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009188
9189 NotifyMotionArgs args;
9190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9191 ASSERT_EQ(26, args.readTime);
9192
Harry Cutts33476232023-01-30 19:57:29 +00009193 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9194 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9195 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009196
9197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9198 ASSERT_EQ(33, args.readTime);
9199}
9200
9201/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009202 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9203 * events should not be delivered to the listener.
9204 */
9205TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9206 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009207 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009208 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009209 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009210 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9211 prepareAxes(POSITION);
9212 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9213
9214 NotifyMotionArgs motionArgs;
9215 processPosition(mapper, 100, 100);
9216 processSync(mapper);
9217
9218 mFakeListener->assertNotifyMotionWasNotCalled();
9219}
9220
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009221/**
9222 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9223 * the touch mapper can process the events and the events can be delivered to the listener.
9224 */
9225TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9226 addConfigurationProperty("touch.deviceType", "touchScreen");
9227 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009228 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009229 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009230 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9231 prepareAxes(POSITION);
9232 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9233
9234 NotifyMotionArgs motionArgs;
9235 processPosition(mapper, 100, 100);
9236 processSync(mapper);
9237
9238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9239 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9240}
9241
Garfield Tanc734e4f2021-01-15 20:01:39 -08009242TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9243 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009244 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009245 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009246 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009247 std::optional<DisplayViewport> optionalDisplayViewport =
9248 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9249 ASSERT_TRUE(optionalDisplayViewport.has_value());
9250 DisplayViewport displayViewport = *optionalDisplayViewport;
9251
9252 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9253 prepareAxes(POSITION);
9254 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9255
9256 // Finger down
9257 int32_t x = 100, y = 100;
9258 processPosition(mapper, x, y);
9259 processSync(mapper);
9260
9261 NotifyMotionArgs motionArgs;
9262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9263 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9264
9265 // Deactivate display viewport
9266 displayViewport.isActive = false;
9267 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9268 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9269
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009270 // The ongoing touch should be canceled immediately
9271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9272 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9273
9274 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009275 x += 10, y += 10;
9276 processPosition(mapper, x, y);
9277 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009279
9280 // Reactivate display viewport
9281 displayViewport.isActive = true;
9282 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9283 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9284
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009285 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009286 x += 10, y += 10;
9287 processPosition(mapper, x, y);
9288 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9290 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009291}
9292
Arthur Hung7c645402019-01-25 17:45:42 +08009293TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9294 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009295 prepareAxes(POSITION | ID | SLOT);
9296 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009297 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009298
9299 // Create the second touch screen device, and enable multi fingers.
9300 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009301 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009302 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009303 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009304 std::shared_ptr<InputDevice> device2 =
9305 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009306 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009307
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009308 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009309 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009310 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009311 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009312 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009313 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009314 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009315 /*flat=*/0, /*fuzz=*/0);
9316 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009317 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9318 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009319
9320 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009321 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009322 std::list<NotifyArgs> unused =
9323 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00009324 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009325 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009326
9327 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009328 std::shared_ptr<FakePointerController> fakePointerController =
9329 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009330 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009331
9332 // Setup policy for associated displays and show touches.
9333 const uint8_t hdmi1 = 0;
9334 const uint8_t hdmi2 = 1;
9335 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9336 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9337 mFakePolicy->setShowTouches(true);
9338
9339 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009340 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009341 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009342
9343 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009344 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9345 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9346 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009347
9348 // Two fingers down at default display.
9349 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9350 processPosition(mapper, x1, y1);
9351 processId(mapper, 1);
9352 processSlot(mapper, 1);
9353 processPosition(mapper, x2, y2);
9354 processId(mapper, 2);
9355 processSync(mapper);
9356
9357 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9358 fakePointerController->getSpots().find(DISPLAY_ID);
9359 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9360 ASSERT_EQ(size_t(2), iter->second.size());
9361
9362 // Two fingers down at second display.
9363 processPosition(mapper2, x1, y1);
9364 processId(mapper2, 1);
9365 processSlot(mapper2, 1);
9366 processPosition(mapper2, x2, y2);
9367 processId(mapper2, 2);
9368 processSync(mapper2);
9369
9370 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9371 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9372 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009373
9374 // Disable the show touches configuration and ensure the spots are cleared.
9375 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009376 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9377 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009378
9379 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009380}
9381
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009382TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009383 prepareAxes(POSITION);
9384 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009385 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009386 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009387
9388 NotifyMotionArgs motionArgs;
9389 // Unrotated video frame
9390 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9391 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009392 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009393 processPosition(mapper, 100, 200);
9394 processSync(mapper);
9395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9396 ASSERT_EQ(frames, motionArgs.videoFrames);
9397
9398 // Subsequent touch events should not have any videoframes
9399 // This is implemented separately in FakeEventHub,
9400 // but that should match the behaviour of TouchVideoDevice.
9401 processPosition(mapper, 200, 200);
9402 processSync(mapper);
9403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9404 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9405}
9406
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009407TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009408 prepareAxes(POSITION);
9409 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009410 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009411 // Unrotated video frame
9412 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9413 NotifyMotionArgs motionArgs;
9414
9415 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009416 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009417 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9418 clearViewports();
9419 prepareDisplay(orientation);
9420 std::vector<TouchVideoFrame> frames{frame};
9421 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9422 processPosition(mapper, 100, 200);
9423 processSync(mapper);
9424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9425 ASSERT_EQ(frames, motionArgs.videoFrames);
9426 }
9427}
9428
9429TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9430 prepareAxes(POSITION);
9431 addConfigurationProperty("touch.deviceType", "touchScreen");
9432 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9433 // orientation-aware are affected by display rotation.
9434 addConfigurationProperty("touch.orientationAware", "0");
9435 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9436 // Unrotated video frame
9437 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9438 NotifyMotionArgs motionArgs;
9439
9440 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009441 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009442 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9443 clearViewports();
9444 prepareDisplay(orientation);
9445 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009446 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009447 processPosition(mapper, 100, 200);
9448 processSync(mapper);
9449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009450 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9451 // compared to the display. This is so that when the window transform (which contains the
9452 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9453 // window's coordinate space.
9454 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009455 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009456
9457 // Release finger.
9458 processSync(mapper);
9459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009460 }
9461}
9462
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009463TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009464 prepareAxes(POSITION);
9465 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009466 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009467 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9468 // so mix these.
9469 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9470 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9471 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9472 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9473 NotifyMotionArgs motionArgs;
9474
Michael Wrighta9cf4192022-12-01 23:46:39 +00009475 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009476 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009477 processPosition(mapper, 100, 200);
9478 processSync(mapper);
9479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009480 ASSERT_EQ(frames, motionArgs.videoFrames);
9481}
9482
9483TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9484 prepareAxes(POSITION);
9485 addConfigurationProperty("touch.deviceType", "touchScreen");
9486 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9487 // orientation-aware are affected by display rotation.
9488 addConfigurationProperty("touch.orientationAware", "0");
9489 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9490 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9491 // so mix these.
9492 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9493 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9494 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9495 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9496 NotifyMotionArgs motionArgs;
9497
Michael Wrighta9cf4192022-12-01 23:46:39 +00009498 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009499 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9500 processPosition(mapper, 100, 200);
9501 processSync(mapper);
9502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9503 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9504 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9505 // compared to the display. This is so that when the window transform (which contains the
9506 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9507 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009508 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009509 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009510 ASSERT_EQ(frames, motionArgs.videoFrames);
9511}
9512
Arthur Hung9da14732019-09-02 16:16:58 +08009513/**
9514 * If we had defined port associations, but the viewport is not ready, the touch device would be
9515 * expected to be disabled, and it should be enabled after the viewport has found.
9516 */
9517TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009518 constexpr uint8_t hdmi2 = 1;
9519 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009520 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009521
9522 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9523
9524 addConfigurationProperty("touch.deviceType", "touchScreen");
9525 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009526 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009527
9528 ASSERT_EQ(mDevice->isEnabled(), false);
9529
9530 // Add display on hdmi2, the device should be enabled and can receive touch event.
9531 prepareSecondaryDisplay(type, hdmi2);
9532 ASSERT_EQ(mDevice->isEnabled(), true);
9533
9534 // Send a touch event.
9535 processPosition(mapper, 100, 100);
9536 processSync(mapper);
9537
9538 NotifyMotionArgs args;
9539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9540 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9541}
9542
Arthur Hung421eb1c2020-01-16 00:09:42 +08009543TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009544 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009545 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009546 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009547 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009548
9549 NotifyMotionArgs motionArgs;
9550
9551 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9552 // finger down
9553 processId(mapper, 1);
9554 processPosition(mapper, x1, y1);
9555 processSync(mapper);
9556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9557 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009558 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009559
9560 // finger move
9561 processId(mapper, 1);
9562 processPosition(mapper, x2, y2);
9563 processSync(mapper);
9564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009566 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009567
9568 // finger up.
9569 processId(mapper, -1);
9570 processSync(mapper);
9571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9572 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009573 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009574
9575 // new finger down
9576 processId(mapper, 1);
9577 processPosition(mapper, x3, y3);
9578 processSync(mapper);
9579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9580 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009581 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009582}
9583
9584/**
arthurhungcc7f9802020-04-30 17:55:40 +08009585 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9586 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009587 */
arthurhungcc7f9802020-04-30 17:55:40 +08009588TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009589 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009590 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009591 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009592 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009593
9594 NotifyMotionArgs motionArgs;
9595
9596 // default tool type is finger
9597 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009598 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009599 processPosition(mapper, x1, y1);
9600 processSync(mapper);
9601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9602 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009603 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009604
9605 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9606 processToolType(mapper, MT_TOOL_PALM);
9607 processSync(mapper);
9608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9609 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9610
9611 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009612 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009613 processPosition(mapper, x2, y2);
9614 processSync(mapper);
9615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9616
9617 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009618 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009619 processSync(mapper);
9620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9621
9622 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009623 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009624 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009625 processPosition(mapper, x3, y3);
9626 processSync(mapper);
9627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9628 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009629 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009630}
9631
arthurhungbf89a482020-04-17 17:37:55 +08009632/**
arthurhungcc7f9802020-04-30 17:55:40 +08009633 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9634 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009635 */
arthurhungcc7f9802020-04-30 17:55:40 +08009636TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009637 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009638 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009639 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9640 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9641
9642 NotifyMotionArgs motionArgs;
9643
9644 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009645 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9646 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009647 processPosition(mapper, x1, y1);
9648 processSync(mapper);
9649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9650 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009651 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009652
9653 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009654 processSlot(mapper, SECOND_SLOT);
9655 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009656 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009657 processSync(mapper);
9658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009659 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009660 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009661
9662 // If the tool type of the first finger changes to MT_TOOL_PALM,
9663 // we expect to receive ACTION_POINTER_UP with cancel flag.
9664 processSlot(mapper, FIRST_SLOT);
9665 processId(mapper, FIRST_TRACKING_ID);
9666 processToolType(mapper, MT_TOOL_PALM);
9667 processSync(mapper);
9668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009669 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009670 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9671
9672 // The following MOVE events of second finger should be processed.
9673 processSlot(mapper, SECOND_SLOT);
9674 processId(mapper, SECOND_TRACKING_ID);
9675 processPosition(mapper, x2 + 1, y2 + 1);
9676 processSync(mapper);
9677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9678 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9679 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9680
9681 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9682 // it. Second finger receive move.
9683 processSlot(mapper, FIRST_SLOT);
9684 processId(mapper, INVALID_TRACKING_ID);
9685 processSync(mapper);
9686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9688 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9689
9690 // Second finger keeps moving.
9691 processSlot(mapper, SECOND_SLOT);
9692 processId(mapper, SECOND_TRACKING_ID);
9693 processPosition(mapper, x2 + 2, y2 + 2);
9694 processSync(mapper);
9695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9697 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9698
9699 // Second finger up.
9700 processId(mapper, INVALID_TRACKING_ID);
9701 processSync(mapper);
9702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9703 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9704 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9705}
9706
9707/**
9708 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9709 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9710 */
9711TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9712 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009713 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009714 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9715 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9716
9717 NotifyMotionArgs motionArgs;
9718
9719 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9720 // First finger down.
9721 processId(mapper, FIRST_TRACKING_ID);
9722 processPosition(mapper, x1, y1);
9723 processSync(mapper);
9724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9725 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009726 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009727
9728 // Second finger down.
9729 processSlot(mapper, SECOND_SLOT);
9730 processId(mapper, SECOND_TRACKING_ID);
9731 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009732 processSync(mapper);
9733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009734 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009735 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009736
arthurhungcc7f9802020-04-30 17:55:40 +08009737 // If the tool type of the first finger changes to MT_TOOL_PALM,
9738 // we expect to receive ACTION_POINTER_UP with cancel flag.
9739 processSlot(mapper, FIRST_SLOT);
9740 processId(mapper, FIRST_TRACKING_ID);
9741 processToolType(mapper, MT_TOOL_PALM);
9742 processSync(mapper);
9743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009744 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009745 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9746
9747 // Second finger keeps moving.
9748 processSlot(mapper, SECOND_SLOT);
9749 processId(mapper, SECOND_TRACKING_ID);
9750 processPosition(mapper, x2 + 1, y2 + 1);
9751 processSync(mapper);
9752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9753 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9754
9755 // second finger becomes palm, receive cancel due to only 1 finger is active.
9756 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009757 processToolType(mapper, MT_TOOL_PALM);
9758 processSync(mapper);
9759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9760 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9761
arthurhungcc7f9802020-04-30 17:55:40 +08009762 // third finger down.
9763 processSlot(mapper, THIRD_SLOT);
9764 processId(mapper, THIRD_TRACKING_ID);
9765 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009766 processPosition(mapper, x3, y3);
9767 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9769 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009770 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009771 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9772
9773 // third finger move
9774 processId(mapper, THIRD_TRACKING_ID);
9775 processPosition(mapper, x3 + 1, y3 + 1);
9776 processSync(mapper);
9777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9779
9780 // first finger up, third finger receive move.
9781 processSlot(mapper, FIRST_SLOT);
9782 processId(mapper, INVALID_TRACKING_ID);
9783 processSync(mapper);
9784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9786 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9787
9788 // second finger up, third finger receive move.
9789 processSlot(mapper, SECOND_SLOT);
9790 processId(mapper, INVALID_TRACKING_ID);
9791 processSync(mapper);
9792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9793 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9794 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9795
9796 // third finger up.
9797 processSlot(mapper, THIRD_SLOT);
9798 processId(mapper, INVALID_TRACKING_ID);
9799 processSync(mapper);
9800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9801 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9802 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9803}
9804
9805/**
9806 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9807 * and the active finger could still be allowed to receive the events
9808 */
9809TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9810 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009811 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009812 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9813 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9814
9815 NotifyMotionArgs motionArgs;
9816
9817 // default tool type is finger
9818 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9819 processId(mapper, FIRST_TRACKING_ID);
9820 processPosition(mapper, x1, y1);
9821 processSync(mapper);
9822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9823 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009824 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009825
9826 // Second finger down.
9827 processSlot(mapper, SECOND_SLOT);
9828 processId(mapper, SECOND_TRACKING_ID);
9829 processPosition(mapper, x2, y2);
9830 processSync(mapper);
9831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009832 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009833 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009834
9835 // If the tool type of the second finger changes to MT_TOOL_PALM,
9836 // we expect to receive ACTION_POINTER_UP with cancel flag.
9837 processId(mapper, SECOND_TRACKING_ID);
9838 processToolType(mapper, MT_TOOL_PALM);
9839 processSync(mapper);
9840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009841 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009842 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9843
9844 // The following MOVE event should be processed.
9845 processSlot(mapper, FIRST_SLOT);
9846 processId(mapper, FIRST_TRACKING_ID);
9847 processPosition(mapper, x1 + 1, y1 + 1);
9848 processSync(mapper);
9849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9851 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9852
9853 // second finger up.
9854 processSlot(mapper, SECOND_SLOT);
9855 processId(mapper, INVALID_TRACKING_ID);
9856 processSync(mapper);
9857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9859
9860 // first finger keep moving
9861 processSlot(mapper, FIRST_SLOT);
9862 processId(mapper, FIRST_TRACKING_ID);
9863 processPosition(mapper, x1 + 2, y1 + 2);
9864 processSync(mapper);
9865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9867
9868 // first finger up.
9869 processId(mapper, INVALID_TRACKING_ID);
9870 processSync(mapper);
9871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9872 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9873 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009874}
9875
Arthur Hung9ad18942021-06-19 02:04:46 +00009876/**
9877 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9878 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9879 * cause slot be valid again.
9880 */
9881TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9882 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009883 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009884 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9885 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9886
9887 NotifyMotionArgs motionArgs;
9888
9889 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9890 // First finger down.
9891 processId(mapper, FIRST_TRACKING_ID);
9892 processPosition(mapper, x1, y1);
9893 processPressure(mapper, RAW_PRESSURE_MAX);
9894 processSync(mapper);
9895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9896 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9897 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9898
9899 // First finger move.
9900 processId(mapper, FIRST_TRACKING_ID);
9901 processPosition(mapper, x1 + 1, y1 + 1);
9902 processPressure(mapper, RAW_PRESSURE_MAX);
9903 processSync(mapper);
9904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9905 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9906 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9907
9908 // Second finger down.
9909 processSlot(mapper, SECOND_SLOT);
9910 processId(mapper, SECOND_TRACKING_ID);
9911 processPosition(mapper, x2, y2);
9912 processPressure(mapper, RAW_PRESSURE_MAX);
9913 processSync(mapper);
9914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009915 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009916 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9917
9918 // second finger up with some unexpected data.
9919 processSlot(mapper, SECOND_SLOT);
9920 processId(mapper, INVALID_TRACKING_ID);
9921 processPosition(mapper, x2, y2);
9922 processSync(mapper);
9923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009924 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009925 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9926
9927 // first finger up with some unexpected data.
9928 processSlot(mapper, FIRST_SLOT);
9929 processId(mapper, INVALID_TRACKING_ID);
9930 processPosition(mapper, x2, y2);
9931 processPressure(mapper, RAW_PRESSURE_MAX);
9932 processSync(mapper);
9933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9934 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9935 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9936}
9937
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009938TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9939 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009940 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009941 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9942 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9943
9944 // First finger down.
9945 processId(mapper, FIRST_TRACKING_ID);
9946 processPosition(mapper, 100, 200);
9947 processPressure(mapper, RAW_PRESSURE_MAX);
9948 processSync(mapper);
9949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9950 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9951
9952 // Second finger down.
9953 processSlot(mapper, SECOND_SLOT);
9954 processId(mapper, SECOND_TRACKING_ID);
9955 processPosition(mapper, 300, 400);
9956 processPressure(mapper, RAW_PRESSURE_MAX);
9957 processSync(mapper);
9958 ASSERT_NO_FATAL_FAILURE(
9959 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9960
9961 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009962 // preserved. Resetting should cancel the ongoing gesture.
9963 resetMapper(mapper, ARBITRARY_TIME);
9964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9965 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009966
9967 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9968 // the existing touch state to generate a down event.
9969 processPosition(mapper, 301, 302);
9970 processSync(mapper);
9971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9972 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9974 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9975
9976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9977}
9978
9979TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9980 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009981 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009982 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9983 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9984
9985 // First finger touches down and releases.
9986 processId(mapper, FIRST_TRACKING_ID);
9987 processPosition(mapper, 100, 200);
9988 processPressure(mapper, RAW_PRESSURE_MAX);
9989 processSync(mapper);
9990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9991 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9992 processId(mapper, INVALID_TRACKING_ID);
9993 processSync(mapper);
9994 ASSERT_NO_FATAL_FAILURE(
9995 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9996
9997 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9998 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009999 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10001
10002 // Send an empty sync frame. Since there are no pointers, no events are generated.
10003 processSync(mapper);
10004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10005}
10006
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010007TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010008 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010009 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010010 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
10011 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010013
10014 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10015 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10016 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10017 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10018 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10019
10020 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010021 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010022 processId(mapper, FIRST_TRACKING_ID);
10023 processToolType(mapper, MT_TOOL_PEN);
10024 processPosition(mapper, 100, 200);
10025 processPressure(mapper, RAW_PRESSURE_MAX);
10026 processSync(mapper);
10027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10028 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10029 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010030 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010031
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010032 // Now that we know the device supports styluses, ensure that the device is re-configured with
10033 // the stylus source.
10034 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10035 {
10036 const auto& devices = mReader->getInputDevices();
10037 auto deviceInfo =
10038 std::find_if(devices.begin(), devices.end(),
10039 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10040 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10041 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10042 }
10043
10044 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10046
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010047 processId(mapper, INVALID_TRACKING_ID);
10048 processSync(mapper);
10049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10050 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10051 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010052 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010053}
10054
Seunghwan Choi356026c2023-02-01 14:37:25 +090010055TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10056 addConfigurationProperty("touch.deviceType", "touchScreen");
10057 prepareDisplay(ui::ROTATION_0);
10058 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10059 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10060 // indicate stylus presence dynamically.
10061 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10062 std::shared_ptr<FakePointerController> fakePointerController =
10063 std::make_shared<FakePointerController>();
10064 mFakePolicy->setPointerController(fakePointerController);
10065 mFakePolicy->setStylusPointerIconEnabled(true);
10066 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10067
10068 processId(mapper, FIRST_TRACKING_ID);
10069 processPressure(mapper, RAW_PRESSURE_MIN);
10070 processPosition(mapper, 100, 200);
10071 processToolType(mapper, MT_TOOL_PEN);
10072 processSync(mapper);
10073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10074 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010075 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010076 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10077 ASSERT_TRUE(fakePointerController->isPointerShown());
10078 ASSERT_NO_FATAL_FAILURE(
10079 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10080}
10081
10082TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10083 addConfigurationProperty("touch.deviceType", "touchScreen");
10084 prepareDisplay(ui::ROTATION_0);
10085 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10086 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10087 // indicate stylus presence dynamically.
10088 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10089 std::shared_ptr<FakePointerController> fakePointerController =
10090 std::make_shared<FakePointerController>();
10091 mFakePolicy->setPointerController(fakePointerController);
10092 mFakePolicy->setStylusPointerIconEnabled(false);
10093 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10094
10095 processId(mapper, FIRST_TRACKING_ID);
10096 processPressure(mapper, RAW_PRESSURE_MIN);
10097 processPosition(mapper, 100, 200);
10098 processToolType(mapper, MT_TOOL_PEN);
10099 processSync(mapper);
10100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10101 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010102 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010103 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10104 ASSERT_FALSE(fakePointerController->isPointerShown());
10105}
10106
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010107// --- MultiTouchInputMapperTest_ExternalDevice ---
10108
10109class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10110protected:
Chris Yea52ade12020-08-27 16:49:20 -070010111 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010112};
10113
10114/**
10115 * Expect fallback to internal viewport if device is external and external viewport is not present.
10116 */
10117TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10118 prepareAxes(POSITION);
10119 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010120 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010121 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10122
10123 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10124
10125 NotifyMotionArgs motionArgs;
10126
10127 // Expect the event to be sent to the internal viewport,
10128 // because an external viewport is not present.
10129 processPosition(mapper, 100, 100);
10130 processSync(mapper);
10131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10132 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10133
10134 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010135 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010136 processPosition(mapper, 100, 100);
10137 processSync(mapper);
10138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10139 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10140}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010141
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010142TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10143 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10144 std::shared_ptr<FakePointerController> fakePointerController =
10145 std::make_shared<FakePointerController>();
10146 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10147 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010148
10149 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010150 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010151 prepareAxes(POSITION | ID | SLOT);
10152 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10153 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10154 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010155 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010156 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10157
10158 // captured touchpad should be a touchpad source
10159 NotifyDeviceResetArgs resetArgs;
10160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10161 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10162
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010163 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010164
10165 const InputDeviceInfo::MotionRange* relRangeX =
10166 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10167 ASSERT_NE(relRangeX, nullptr);
10168 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10169 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10170 const InputDeviceInfo::MotionRange* relRangeY =
10171 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10172 ASSERT_NE(relRangeY, nullptr);
10173 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10174 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10175
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010176 // run captured pointer tests - note that this is unscaled, so input listener events should be
10177 // identical to what the hardware sends (accounting for any
10178 // calibration).
10179 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010180 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010181 processId(mapper, 1);
10182 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10183 processKey(mapper, BTN_TOUCH, 1);
10184 processSync(mapper);
10185
10186 // expect coord[0] to contain initial location of touch 0
10187 NotifyMotionArgs args;
10188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10189 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10190 ASSERT_EQ(1U, args.pointerCount);
10191 ASSERT_EQ(0, args.pointerProperties[0].id);
10192 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10193 ASSERT_NO_FATAL_FAILURE(
10194 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10195
10196 // FINGER 1 DOWN
10197 processSlot(mapper, 1);
10198 processId(mapper, 2);
10199 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10200 processSync(mapper);
10201
10202 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010204 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010205 ASSERT_EQ(2U, args.pointerCount);
10206 ASSERT_EQ(0, args.pointerProperties[0].id);
10207 ASSERT_EQ(1, args.pointerProperties[1].id);
10208 ASSERT_NO_FATAL_FAILURE(
10209 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10210 ASSERT_NO_FATAL_FAILURE(
10211 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10212
10213 // FINGER 1 MOVE
10214 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10215 processSync(mapper);
10216
10217 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10218 // from move
10219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10221 ASSERT_NO_FATAL_FAILURE(
10222 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10223 ASSERT_NO_FATAL_FAILURE(
10224 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10225
10226 // FINGER 0 MOVE
10227 processSlot(mapper, 0);
10228 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10229 processSync(mapper);
10230
10231 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10234 ASSERT_NO_FATAL_FAILURE(
10235 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10236 ASSERT_NO_FATAL_FAILURE(
10237 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10238
10239 // BUTTON DOWN
10240 processKey(mapper, BTN_LEFT, 1);
10241 processSync(mapper);
10242
10243 // touchinputmapper design sends a move before button press
10244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10248
10249 // BUTTON UP
10250 processKey(mapper, BTN_LEFT, 0);
10251 processSync(mapper);
10252
10253 // touchinputmapper design sends a move after button release
10254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10255 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10258
10259 // FINGER 0 UP
10260 processId(mapper, -1);
10261 processSync(mapper);
10262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10263 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10264
10265 // FINGER 1 MOVE
10266 processSlot(mapper, 1);
10267 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10268 processSync(mapper);
10269
10270 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10273 ASSERT_EQ(1U, args.pointerCount);
10274 ASSERT_EQ(1, args.pointerProperties[0].id);
10275 ASSERT_NO_FATAL_FAILURE(
10276 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10277
10278 // FINGER 1 UP
10279 processId(mapper, -1);
10280 processKey(mapper, BTN_TOUCH, 0);
10281 processSync(mapper);
10282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10283 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10284
Josep del Río2d8c79a2023-01-23 19:33:50 +000010285 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010286 mFakePolicy->setPointerCapture(false);
10287 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010289 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010290}
10291
10292TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10293 std::shared_ptr<FakePointerController> fakePointerController =
10294 std::make_shared<FakePointerController>();
10295 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10296 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010297
10298 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010299 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010300 prepareAxes(POSITION | ID | SLOT);
10301 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10302 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010303 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010304 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10305 // run uncaptured pointer tests - pushes out generic events
10306 // FINGER 0 DOWN
10307 processId(mapper, 3);
10308 processPosition(mapper, 100, 100);
10309 processKey(mapper, BTN_TOUCH, 1);
10310 processSync(mapper);
10311
10312 // start at (100,100), cursor should be at (0,0) * scale
10313 NotifyMotionArgs args;
10314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10315 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10316 ASSERT_NO_FATAL_FAILURE(
10317 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10318
10319 // FINGER 0 MOVE
10320 processPosition(mapper, 200, 200);
10321 processSync(mapper);
10322
10323 // compute scaling to help with touch position checking
10324 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10325 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10326 float scale =
10327 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10328
10329 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10331 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10333 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010334
10335 // BUTTON DOWN
10336 processKey(mapper, BTN_LEFT, 1);
10337 processSync(mapper);
10338
10339 // touchinputmapper design sends a move before button press
10340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10341 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10343 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10344
10345 // BUTTON UP
10346 processKey(mapper, BTN_LEFT, 0);
10347 processSync(mapper);
10348
10349 // touchinputmapper design sends a move after button release
10350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10351 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10353 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010354}
10355
10356TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10357 std::shared_ptr<FakePointerController> fakePointerController =
10358 std::make_shared<FakePointerController>();
10359
Michael Wrighta9cf4192022-12-01 23:46:39 +000010360 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010361 prepareAxes(POSITION | ID | SLOT);
10362 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010363 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010364 mFakePolicy->setPointerCapture(false);
10365 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10366
Josep del Río2d8c79a2023-01-23 19:33:50 +000010367 // uncaptured touchpad should be a pointer device
10368 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010369
Josep del Río2d8c79a2023-01-23 19:33:50 +000010370 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010371 mFakePolicy->setPointerCapture(true);
10372 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10373 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10374}
10375
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010376// --- BluetoothMultiTouchInputMapperTest ---
10377
10378class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10379protected:
10380 void SetUp() override {
10381 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10382 }
10383};
10384
10385TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10386 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010387 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010388 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10389 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10390
10391 nsecs_t kernelEventTime = ARBITRARY_TIME;
10392 nsecs_t expectedEventTime = ARBITRARY_TIME;
10393 // Touch down.
10394 processId(mapper, FIRST_TRACKING_ID);
10395 processPosition(mapper, 100, 200);
10396 processPressure(mapper, RAW_PRESSURE_MAX);
10397 processSync(mapper, ARBITRARY_TIME);
10398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10399 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10400
10401 // Process several events that come in quick succession, according to their timestamps.
10402 for (int i = 0; i < 3; i++) {
10403 constexpr static nsecs_t delta = ms2ns(1);
10404 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10405 kernelEventTime += delta;
10406 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10407
10408 processPosition(mapper, 101 + i, 201 + i);
10409 processSync(mapper, kernelEventTime);
10410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10411 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10412 WithEventTime(expectedEventTime))));
10413 }
10414
10415 // Release the touch.
10416 processId(mapper, INVALID_TRACKING_ID);
10417 processPressure(mapper, RAW_PRESSURE_MIN);
10418 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10420 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10421 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10422}
10423
10424// --- MultiTouchPointerModeTest ---
10425
HQ Liue6983c72022-04-19 22:14:56 +000010426class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10427protected:
10428 float mPointerMovementScale;
10429 float mPointerXZoomScale;
10430 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10431 addConfigurationProperty("touch.deviceType", "pointer");
10432 std::shared_ptr<FakePointerController> fakePointerController =
10433 std::make_shared<FakePointerController>();
10434 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10435 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010436 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010437
10438 prepareAxes(POSITION);
10439 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10440 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10441 // needs to be disabled, and the pointer gesture needs to be enabled.
10442 mFakePolicy->setPointerCapture(false);
10443 mFakePolicy->setPointerGestureEnabled(true);
10444 mFakePolicy->setPointerController(fakePointerController);
10445
10446 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10447 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10448 mPointerMovementScale =
10449 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10450 mPointerXZoomScale =
10451 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10452 }
10453
10454 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10455 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10456 /*flat*/ 0,
10457 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10458 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10459 /*flat*/ 0,
10460 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10461 }
10462};
10463
10464/**
10465 * Two fingers down on a pointer mode touch pad. The width
10466 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10467 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10468 * be greater than the both value to be freeform gesture, so that after two
10469 * fingers start to move downwards, the gesture should be swipe.
10470 */
10471TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10472 // The min freeform gesture width is 25units/mm x 30mm = 750
10473 // which is greater than fraction of the diagnal length of the touchpad (349).
10474 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010475 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010476 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10477 NotifyMotionArgs motionArgs;
10478
10479 // Two fingers down at once.
10480 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10481 // Pointer's initial position is used the [0,0] coordinate.
10482 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10483
10484 processId(mapper, FIRST_TRACKING_ID);
10485 processPosition(mapper, x1, y1);
10486 processMTSync(mapper);
10487 processId(mapper, SECOND_TRACKING_ID);
10488 processPosition(mapper, x2, y2);
10489 processMTSync(mapper);
10490 processSync(mapper);
10491
10492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10493 ASSERT_EQ(1U, motionArgs.pointerCount);
10494 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010495 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010496 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010497 ASSERT_NO_FATAL_FAILURE(
10498 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10499
10500 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10501 // that there should be 1 pointer.
10502 int32_t movingDistance = 200;
10503 y1 += movingDistance;
10504 y2 += movingDistance;
10505
10506 processId(mapper, FIRST_TRACKING_ID);
10507 processPosition(mapper, x1, y1);
10508 processMTSync(mapper);
10509 processId(mapper, SECOND_TRACKING_ID);
10510 processPosition(mapper, x2, y2);
10511 processMTSync(mapper);
10512 processSync(mapper);
10513
10514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10515 ASSERT_EQ(1U, motionArgs.pointerCount);
10516 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010517 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010518 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10520 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10521 0, 0, 0, 0));
10522}
10523
10524/**
10525 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10526 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10527 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10528 * value to be freeform gesture, so that after two fingers start to move downwards,
10529 * the gesture should be swipe.
10530 */
10531TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10532 // The min freeform gesture width is 5units/mm x 30mm = 150
10533 // which is greater than fraction of the diagnal length of the touchpad (349).
10534 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010535 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
HQ Liue6983c72022-04-19 22:14:56 +000010536 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10537 NotifyMotionArgs motionArgs;
10538
10539 // Two fingers down at once.
10540 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10541 // Pointer's initial position is used the [0,0] coordinate.
10542 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10543
10544 processId(mapper, FIRST_TRACKING_ID);
10545 processPosition(mapper, x1, y1);
10546 processMTSync(mapper);
10547 processId(mapper, SECOND_TRACKING_ID);
10548 processPosition(mapper, x2, y2);
10549 processMTSync(mapper);
10550 processSync(mapper);
10551
10552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10553 ASSERT_EQ(1U, motionArgs.pointerCount);
10554 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010555 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010556 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010557 ASSERT_NO_FATAL_FAILURE(
10558 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10559
10560 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10561 // and there should be 1 pointer.
10562 int32_t movingDistance = 200;
10563 y1 += movingDistance;
10564 y2 += movingDistance;
10565
10566 processId(mapper, FIRST_TRACKING_ID);
10567 processPosition(mapper, x1, y1);
10568 processMTSync(mapper);
10569 processId(mapper, SECOND_TRACKING_ID);
10570 processPosition(mapper, x2, y2);
10571 processMTSync(mapper);
10572 processSync(mapper);
10573
10574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10575 ASSERT_EQ(1U, motionArgs.pointerCount);
10576 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010577 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010578 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010579 // New coordinate is the scaled relative coordinate from the initial coordinate.
10580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10581 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10582 0, 0, 0, 0));
10583}
10584
10585/**
10586 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10587 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10588 * freeform gestures after two fingers start to move downwards.
10589 */
10590TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010591 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010592 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10593
10594 NotifyMotionArgs motionArgs;
10595
10596 // Two fingers down at once. Wider than the max swipe width.
10597 // The gesture is expected to be PRESS, then transformed to FREEFORM
10598 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
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 ASSERT_EQ(1U, motionArgs.pointerCount);
10610 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010611 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010612 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010613 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10614 ASSERT_NO_FATAL_FAILURE(
10615 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10616
10617 int32_t movingDistance = 200;
10618
10619 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10620 // then two down events for two pointers.
10621 y1 += movingDistance;
10622 y2 += movingDistance;
10623
10624 processId(mapper, FIRST_TRACKING_ID);
10625 processPosition(mapper, x1, y1);
10626 processMTSync(mapper);
10627 processId(mapper, SECOND_TRACKING_ID);
10628 processPosition(mapper, x2, y2);
10629 processMTSync(mapper);
10630 processSync(mapper);
10631
10632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10633 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10634 ASSERT_EQ(1U, motionArgs.pointerCount);
10635 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010637 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
HQ Liue6983c72022-04-19 22:14:56 +000010638 ASSERT_EQ(1U, motionArgs.pointerCount);
10639 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010641 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010642 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010643 ASSERT_EQ(2U, motionArgs.pointerCount);
10644 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010645 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010646 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010647 // Two pointers' scaled relative coordinates from their initial centroid.
10648 // Initial y coordinates are 0 as y1 and y2 have the same value.
10649 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10650 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10651 // When pointers move, the new coordinates equal to the initial coordinates plus
10652 // scaled moving distance.
10653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10654 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10655 0, 0, 0, 0));
10656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10657 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10658 0, 0, 0, 0));
10659
10660 // Move two fingers down again, expect one MOVE motion event.
10661 y1 += movingDistance;
10662 y2 += movingDistance;
10663
10664 processId(mapper, FIRST_TRACKING_ID);
10665 processPosition(mapper, x1, y1);
10666 processMTSync(mapper);
10667 processId(mapper, SECOND_TRACKING_ID);
10668 processPosition(mapper, x2, y2);
10669 processMTSync(mapper);
10670 processSync(mapper);
10671
10672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10673 ASSERT_EQ(2U, motionArgs.pointerCount);
10674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010675 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010676 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10678 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10679 0, 0, 0, 0, 0));
10680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10681 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10682 0, 0, 0, 0, 0));
10683}
10684
Harry Cutts39b7ca22022-10-05 15:55:48 +000010685TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010686 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Harry Cutts39b7ca22022-10-05 15:55:48 +000010687 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10688 NotifyMotionArgs motionArgs;
10689
10690 // Place two fingers down.
10691 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10692
10693 processId(mapper, FIRST_TRACKING_ID);
10694 processPosition(mapper, x1, y1);
10695 processMTSync(mapper);
10696 processId(mapper, SECOND_TRACKING_ID);
10697 processPosition(mapper, x2, y2);
10698 processMTSync(mapper);
10699 processSync(mapper);
10700
10701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10702 ASSERT_EQ(1U, motionArgs.pointerCount);
10703 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10704 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10705 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10706 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10707
10708 // Move the two fingers down and to the left.
10709 int32_t movingDistance = 200;
10710 x1 -= movingDistance;
10711 y1 += movingDistance;
10712 x2 -= movingDistance;
10713 y2 += movingDistance;
10714
10715 processId(mapper, FIRST_TRACKING_ID);
10716 processPosition(mapper, x1, y1);
10717 processMTSync(mapper);
10718 processId(mapper, SECOND_TRACKING_ID);
10719 processPosition(mapper, x2, y2);
10720 processMTSync(mapper);
10721 processSync(mapper);
10722
10723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10724 ASSERT_EQ(1U, motionArgs.pointerCount);
10725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10726 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10727 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10728 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10729}
10730
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010731TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010732 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010733 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10734 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10736
10737 // Start a stylus gesture.
10738 processKey(mapper, BTN_TOOL_PEN, 1);
10739 processId(mapper, FIRST_TRACKING_ID);
10740 processPosition(mapper, 100, 200);
10741 processSync(mapper);
10742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10743 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10744 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010745 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010746 // TODO(b/257078296): Pointer mode generates extra event.
10747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10748 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10749 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010750 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10752
10753 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10754 // gesture should be disabled.
10755 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10756 viewport->isActive = false;
10757 mFakePolicy->updateViewport(*viewport);
10758 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10760 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10761 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010762 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010763 // TODO(b/257078296): Pointer mode generates extra event.
10764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10765 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10766 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010767 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10769}
10770
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010771// --- JoystickInputMapperTest ---
10772
10773class JoystickInputMapperTest : public InputMapperTest {
10774protected:
10775 static const int32_t RAW_X_MIN;
10776 static const int32_t RAW_X_MAX;
10777 static const int32_t RAW_Y_MIN;
10778 static const int32_t RAW_Y_MAX;
10779
10780 void SetUp() override {
10781 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10782 }
10783 void prepareAxes() {
10784 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10785 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10786 }
10787
10788 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10790 }
10791
10792 void processSync(JoystickInputMapper& mapper) {
10793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10794 }
10795
Michael Wrighta9cf4192022-12-01 23:46:39 +000010796 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010797 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10798 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10799 NO_PORT, ViewportType::VIRTUAL);
10800 }
10801};
10802
10803const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10804const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10805const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10806const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10807
10808TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10809 prepareAxes();
10810 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10811
10812 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10813
Michael Wrighta9cf4192022-12-01 23:46:39 +000010814 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010815
10816 // Send an axis event
10817 processAxis(mapper, ABS_X, 100);
10818 processSync(mapper);
10819
10820 NotifyMotionArgs args;
10821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10822 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10823
10824 // Send another axis event
10825 processAxis(mapper, ABS_Y, 100);
10826 processSync(mapper);
10827
10828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10829 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10830}
10831
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010832// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010833
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010834class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010835protected:
10836 static const char* DEVICE_NAME;
10837 static const char* DEVICE_LOCATION;
10838 static const int32_t DEVICE_ID;
10839 static const int32_t DEVICE_GENERATION;
10840 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010841 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010842 static const int32_t EVENTHUB_ID;
10843
10844 std::shared_ptr<FakeEventHub> mFakeEventHub;
10845 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010846 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010847 std::unique_ptr<InstrumentedInputReader> mReader;
10848 std::shared_ptr<InputDevice> mDevice;
10849
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010850 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010851 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010852 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010853 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010854 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010855 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010856 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10857 }
10858
10859 void SetUp() override { SetUp(DEVICE_CLASSES); }
10860
10861 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010862 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010863 mFakePolicy.clear();
10864 }
10865
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010866 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010867 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10868 mReader->requestRefreshConfiguration(changes);
10869 mReader->loopOnce();
10870 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010871 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010872 }
10873
10874 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10875 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010876 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010877 InputDeviceIdentifier identifier;
10878 identifier.name = name;
10879 identifier.location = location;
10880 std::shared_ptr<InputDevice> device =
10881 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10882 identifier);
10883 mReader->pushNextDevice(device);
10884 mFakeEventHub->addDevice(eventHubId, name, classes);
10885 mReader->loopOnce();
10886 return device;
10887 }
10888
10889 template <class T, typename... Args>
10890 T& addControllerAndConfigure(Args... args) {
10891 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10892
10893 return controller;
10894 }
10895};
10896
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010897const char* PeripheralControllerTest::DEVICE_NAME = "device";
10898const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10899const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10900const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10901const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010902const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10903 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010904const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010905
10906// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010907class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010908protected:
10909 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010910 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010911 }
10912};
10913
10914TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010915 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010916
Harry Cuttsa5b71292022-11-28 12:56:17 +000010917 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10918 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10919 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010920}
10921
10922TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010923 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010924
Harry Cuttsa5b71292022-11-28 12:56:17 +000010925 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10926 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10927 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010928}
10929
10930// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010931class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010932protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010933 void SetUp() override {
10934 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10935 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010936};
10937
Chris Ye85758332021-05-16 23:05:17 -070010938TEST_F(LightControllerTest, MonoLight) {
10939 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010940 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010941 .maxBrightness = 255,
10942 .flags = InputLightClass::BRIGHTNESS,
10943 .path = ""};
10944 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010945
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010946 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010947 InputDeviceInfo info;
10948 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010949 std::vector<InputDeviceLightInfo> lights = info.getLights();
10950 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010951 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10952 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10953
10954 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10955 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10956}
10957
10958TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10959 RawLightInfo infoMono = {.id = 1,
10960 .name = "mono_keyboard_backlight",
10961 .maxBrightness = 255,
10962 .flags = InputLightClass::BRIGHTNESS |
10963 InputLightClass::KEYBOARD_BACKLIGHT,
10964 .path = ""};
10965 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10966
10967 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10968 InputDeviceInfo info;
10969 controller.populateDeviceInfo(&info);
10970 std::vector<InputDeviceLightInfo> lights = info.getLights();
10971 ASSERT_EQ(1U, lights.size());
10972 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10973 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010974
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010975 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10976 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010977}
10978
10979TEST_F(LightControllerTest, RGBLight) {
10980 RawLightInfo infoRed = {.id = 1,
10981 .name = "red",
10982 .maxBrightness = 255,
10983 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10984 .path = ""};
10985 RawLightInfo infoGreen = {.id = 2,
10986 .name = "green",
10987 .maxBrightness = 255,
10988 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10989 .path = ""};
10990 RawLightInfo infoBlue = {.id = 3,
10991 .name = "blue",
10992 .maxBrightness = 255,
10993 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10994 .path = ""};
10995 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10996 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10997 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10998
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010999 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011000 InputDeviceInfo info;
11001 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011002 std::vector<InputDeviceLightInfo> lights = info.getLights();
11003 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011004 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11005 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11006 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11007
11008 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11009 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11010}
11011
11012TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11013 RawLightInfo infoRed = {.id = 1,
11014 .name = "red_keyboard_backlight",
11015 .maxBrightness = 255,
11016 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11017 InputLightClass::KEYBOARD_BACKLIGHT,
11018 .path = ""};
11019 RawLightInfo infoGreen = {.id = 2,
11020 .name = "green_keyboard_backlight",
11021 .maxBrightness = 255,
11022 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11023 InputLightClass::KEYBOARD_BACKLIGHT,
11024 .path = ""};
11025 RawLightInfo infoBlue = {.id = 3,
11026 .name = "blue_keyboard_backlight",
11027 .maxBrightness = 255,
11028 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11029 InputLightClass::KEYBOARD_BACKLIGHT,
11030 .path = ""};
11031 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11032 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11033 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11034
11035 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11036 InputDeviceInfo info;
11037 controller.populateDeviceInfo(&info);
11038 std::vector<InputDeviceLightInfo> lights = info.getLights();
11039 ASSERT_EQ(1U, lights.size());
11040 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11041 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11042 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11043
11044 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11045 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11046}
11047
11048TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11049 RawLightInfo infoRed = {.id = 1,
11050 .name = "red",
11051 .maxBrightness = 255,
11052 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11053 .path = ""};
11054 RawLightInfo infoGreen = {.id = 2,
11055 .name = "green",
11056 .maxBrightness = 255,
11057 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11058 .path = ""};
11059 RawLightInfo infoBlue = {.id = 3,
11060 .name = "blue",
11061 .maxBrightness = 255,
11062 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11063 .path = ""};
11064 RawLightInfo infoGlobal = {.id = 3,
11065 .name = "global_keyboard_backlight",
11066 .maxBrightness = 255,
11067 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11068 InputLightClass::KEYBOARD_BACKLIGHT,
11069 .path = ""};
11070 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11071 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11072 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11073 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11074
11075 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11076 InputDeviceInfo info;
11077 controller.populateDeviceInfo(&info);
11078 std::vector<InputDeviceLightInfo> lights = info.getLights();
11079 ASSERT_EQ(1U, lights.size());
11080 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));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011083
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011084 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11085 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011086}
11087
11088TEST_F(LightControllerTest, MultiColorRGBLight) {
11089 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011090 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011091 .maxBrightness = 255,
11092 .flags = InputLightClass::BRIGHTNESS |
11093 InputLightClass::MULTI_INTENSITY |
11094 InputLightClass::MULTI_INDEX,
11095 .path = ""};
11096
11097 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11098
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011099 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011100 InputDeviceInfo info;
11101 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011102 std::vector<InputDeviceLightInfo> lights = info.getLights();
11103 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011104 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11105 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11106 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11107
11108 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11109 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11110}
11111
11112TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11113 RawLightInfo infoColor = {.id = 1,
11114 .name = "multi_color_keyboard_backlight",
11115 .maxBrightness = 255,
11116 .flags = InputLightClass::BRIGHTNESS |
11117 InputLightClass::MULTI_INTENSITY |
11118 InputLightClass::MULTI_INDEX |
11119 InputLightClass::KEYBOARD_BACKLIGHT,
11120 .path = ""};
11121
11122 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11123
11124 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11125 InputDeviceInfo info;
11126 controller.populateDeviceInfo(&info);
11127 std::vector<InputDeviceLightInfo> lights = info.getLights();
11128 ASSERT_EQ(1U, lights.size());
11129 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11130 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11131 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011132
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011133 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11134 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011135}
11136
11137TEST_F(LightControllerTest, PlayerIdLight) {
11138 RawLightInfo info1 = {.id = 1,
11139 .name = "player1",
11140 .maxBrightness = 255,
11141 .flags = InputLightClass::BRIGHTNESS,
11142 .path = ""};
11143 RawLightInfo info2 = {.id = 2,
11144 .name = "player2",
11145 .maxBrightness = 255,
11146 .flags = InputLightClass::BRIGHTNESS,
11147 .path = ""};
11148 RawLightInfo info3 = {.id = 3,
11149 .name = "player3",
11150 .maxBrightness = 255,
11151 .flags = InputLightClass::BRIGHTNESS,
11152 .path = ""};
11153 RawLightInfo info4 = {.id = 4,
11154 .name = "player4",
11155 .maxBrightness = 255,
11156 .flags = InputLightClass::BRIGHTNESS,
11157 .path = ""};
11158 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11159 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11160 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11161 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11162
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011163 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011164 InputDeviceInfo info;
11165 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011166 std::vector<InputDeviceLightInfo> lights = info.getLights();
11167 ASSERT_EQ(1U, lights.size());
11168 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011169 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11170 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011171
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011172 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11173 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11174 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011175}
11176
Michael Wrightd02c5b62014-02-10 15:10:22 -080011177} // namespace android