blob: 2b8b70b54cf2b3d4b91b3232105fe07f1bdf1f00 [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());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700607 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800608 }
609
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800610 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700611 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000612 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700613 }
614
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800615 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700616 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000617 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700618 }
619
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800620 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700621 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700622 ftl::Flags<InputDeviceClass> classes,
623 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800624 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800625 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
626 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800627 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800628 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629 return mapper;
630 }
631};
632
Chris Ye98d3f532020-10-01 21:48:59 -0700633TEST_F(InputReaderTest, PolicyGetInputDevices) {
634 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700635 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700636 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637
638 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700639 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800640 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800641 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100642 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
644 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000645 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800646}
647
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000648TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
649 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
650 mFakeEventHub->setSysfsRootPath(1, "xyz");
651
652 // Should also have received a notification describing the new input device.
653 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
654 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
655 ASSERT_EQ(0U, inputDevice.getLights().size());
656
657 RawLightInfo infoMonolight = {.id = 123,
658 .name = "mono_keyboard_backlight",
659 .maxBrightness = 255,
660 .flags = InputLightClass::BRIGHTNESS,
661 .path = ""};
662 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
663 mReader->sysfsNodeChanged("xyz");
664 mReader->loopOnce();
665
666 // Should also have received a notification describing the new recreated input device.
667 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
668 inputDevice = mFakePolicy->getInputDevices()[0];
669 ASSERT_EQ(1U, inputDevice.getLights().size());
670}
671
Chris Yee7310032020-09-22 15:36:28 -0700672TEST_F(InputReaderTest, GetMergedInputDevices) {
673 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
674 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
675 // Add two subdevices to device
676 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
677 // Must add at least one mapper or the device will be ignored!
678 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
679 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
680
681 // Push same device instance for next device to be added, so they'll have same identifier.
682 mReader->pushNextDevice(device);
683 mReader->pushNextDevice(device);
684 ASSERT_NO_FATAL_FAILURE(
685 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
686 ASSERT_NO_FATAL_FAILURE(
687 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
688
689 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000690 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700691}
692
Chris Yee14523a2020-12-19 13:46:00 -0800693TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
694 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
695 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
696 // Add two subdevices to device
697 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
698 // Must add at least one mapper or the device will be ignored!
699 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
700 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
701
702 // Push same device instance for next device to be added, so they'll have same identifier.
703 mReader->pushNextDevice(device);
704 mReader->pushNextDevice(device);
705 // Sensor device is initially disabled
706 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
707 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
708 nullptr));
709 // Device is disabled because the only sub device is a sensor device and disabled initially.
710 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
711 ASSERT_FALSE(device->isEnabled());
712 ASSERT_NO_FATAL_FAILURE(
713 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
714 // The merged device is enabled if any sub device is enabled
715 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
716 ASSERT_TRUE(device->isEnabled());
717}
718
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700719TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800720 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700721 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800722 constexpr int32_t eventHubId = 1;
723 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700724 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800725 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800726 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800727 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700728
Yi Kong9b14ac62018-07-17 13:48:38 -0700729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700730
731 NotifyDeviceResetArgs resetArgs;
732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700733 ASSERT_EQ(deviceId, resetArgs.deviceId);
734
735 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800736 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000737 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700738
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700740 ASSERT_EQ(deviceId, resetArgs.deviceId);
741 ASSERT_EQ(device->isEnabled(), false);
742
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800743 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000744 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700747 ASSERT_EQ(device->isEnabled(), false);
748
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800749 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000750 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700752 ASSERT_EQ(deviceId, resetArgs.deviceId);
753 ASSERT_EQ(device->isEnabled(), true);
754}
755
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800757 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700758 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800759 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800760 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800761 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800762 AINPUT_SOURCE_KEYBOARD, nullptr);
763 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764
765 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
766 AINPUT_SOURCE_ANY, AKEYCODE_A))
767 << "Should return unknown when the device id is >= 0 but unknown.";
768
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800769 ASSERT_EQ(AKEY_STATE_UNKNOWN,
770 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
771 << "Should return unknown when the device id is valid but the sources are not "
772 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800773
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800774 ASSERT_EQ(AKEY_STATE_DOWN,
775 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
776 AKEYCODE_A))
777 << "Should return value provided by mapper when device id is valid and the device "
778 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800779
780 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
781 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
782 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
783
784 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
785 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
786 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
787}
788
Philip Junker4af3b3d2021-12-14 10:36:55 +0100789TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
790 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
791 constexpr int32_t eventHubId = 1;
792 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
793 InputDeviceClass::KEYBOARD,
794 AINPUT_SOURCE_KEYBOARD, nullptr);
795 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
796
797 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
798 << "Should return unknown when the device with the specified id is not found.";
799
800 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
801 << "Should return correct mapping when device id is valid and mapping exists.";
802
803 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
804 << "Should return the location key code when device id is valid and there's no "
805 "mapping.";
806}
807
808TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
809 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
810 constexpr int32_t eventHubId = 1;
811 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
812 InputDeviceClass::JOYSTICK,
813 AINPUT_SOURCE_GAMEPAD, nullptr);
814 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
815
816 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
817 << "Should return unknown when the device id is valid but there is no keyboard mapper";
818}
819
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800821 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700822 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800823 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800824 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800825 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800826 AINPUT_SOURCE_KEYBOARD, nullptr);
827 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828
829 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
830 AINPUT_SOURCE_ANY, KEY_A))
831 << "Should return unknown when the device id is >= 0 but unknown.";
832
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800833 ASSERT_EQ(AKEY_STATE_UNKNOWN,
834 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
835 << "Should return unknown when the device id is valid but the sources are not "
836 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800837
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800838 ASSERT_EQ(AKEY_STATE_DOWN,
839 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
840 KEY_A))
841 << "Should return value provided by mapper when device id is valid and the device "
842 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800843
844 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
845 AINPUT_SOURCE_TRACKBALL, KEY_A))
846 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
847
848 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
849 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
850 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
851}
852
853TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800854 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700855 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800856 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800857 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800858 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800859 AINPUT_SOURCE_KEYBOARD, nullptr);
860 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861
862 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
863 AINPUT_SOURCE_ANY, SW_LID))
864 << "Should return unknown when the device id is >= 0 but unknown.";
865
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800866 ASSERT_EQ(AKEY_STATE_UNKNOWN,
867 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
868 << "Should return unknown when the device id is valid but the sources are not "
869 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800870
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800871 ASSERT_EQ(AKEY_STATE_DOWN,
872 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
873 SW_LID))
874 << "Should return value provided by mapper when device id is valid and the device "
875 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876
877 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
878 AINPUT_SOURCE_TRACKBALL, SW_LID))
879 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
880
881 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
882 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
883 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
884}
885
886TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800887 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700888 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800889 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800890 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800891 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800892 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100893
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800894 mapper.addSupportedKeyCode(AKEYCODE_A);
895 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700897 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898 uint8_t flags[4] = { 0, 0, 0, 1 };
899
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700900 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901 << "Should return false when device id is >= 0 but unknown.";
902 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
903
904 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700905 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800906 << "Should return false when device id is valid but the sources are not supported by "
907 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800908 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
909
910 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700911 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800912 keyCodes, flags))
913 << "Should return value provided by mapper when device id is valid and the device "
914 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
916
917 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700918 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
919 << "Should return false when the device id is < 0 but the sources are not supported by "
920 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
922
923 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700924 ASSERT_TRUE(
925 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
926 << "Should return value provided by mapper when device id is < 0 and one of the "
927 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800928 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
929}
930
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000931TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800932 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700933 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934
935 NotifyConfigurationChangedArgs args;
936
937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
938 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
939}
940
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000941TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800942 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700943 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000944 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800945 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000946 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800947 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800948 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800949 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000951 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000952 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
954
955 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800956 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000957 ASSERT_EQ(when, event.when);
958 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800959 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 ASSERT_EQ(EV_KEY, event.type);
961 ASSERT_EQ(KEY_A, event.code);
962 ASSERT_EQ(1, event.value);
963}
964
Garfield Tan1c7bc862020-01-28 13:24:04 -0800965TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800966 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700967 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800968 constexpr int32_t eventHubId = 1;
969 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800970 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800971 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800972 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800973 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800974
975 NotifyDeviceResetArgs resetArgs;
976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800977 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800978
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800979 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000980 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800982 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800983 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800984
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800985 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000986 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800988 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800989 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800991 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000992 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800994 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800995 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800996}
997
Garfield Tan1c7bc862020-01-28 13:24:04 -0800998TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
999 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001000 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001001 constexpr int32_t eventHubId = 1;
1002 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1003 // Must add at least one mapper or the device will be ignored!
1004 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001005 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001006 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1007
1008 NotifyDeviceResetArgs resetArgs;
1009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1010 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1011}
1012
Arthur Hungc23540e2018-11-29 20:42:11 +08001013TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001014 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001015 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001016 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001017 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001018 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1019 FakeInputMapper& mapper =
1020 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001021 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001022
1023 const uint8_t hdmi1 = 1;
1024
1025 // Associated touch screen with second display.
1026 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1027
1028 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001029 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001030 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001031 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001032 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001033 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001034 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001035 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001036 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001037
1038 // Add the device, and make sure all of the callbacks are triggered.
1039 // The device is added after the input port associations are processed since
1040 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001041 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001044 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001045
Arthur Hung2c9a3342019-07-23 14:18:59 +08001046 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001047 ASSERT_EQ(deviceId, device->getId());
1048 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1049 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001050
1051 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001052 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001053 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001054 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001055}
1056
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001057TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1058 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001059 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001060 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1061 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1062 // Must add at least one mapper or the device will be ignored!
1063 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1064 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1065 mReader->pushNextDevice(device);
1066 mReader->pushNextDevice(device);
1067 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1068 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1069
1070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1071
1072 NotifyDeviceResetArgs resetArgs;
1073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1074 ASSERT_EQ(deviceId, resetArgs.deviceId);
1075 ASSERT_TRUE(device->isEnabled());
1076 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1077 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1078
1079 disableDevice(deviceId);
1080 mReader->loopOnce();
1081
1082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1083 ASSERT_EQ(deviceId, resetArgs.deviceId);
1084 ASSERT_FALSE(device->isEnabled());
1085 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1086 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1087
1088 enableDevice(deviceId);
1089 mReader->loopOnce();
1090
1091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1092 ASSERT_EQ(deviceId, resetArgs.deviceId);
1093 ASSERT_TRUE(device->isEnabled());
1094 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1095 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1096}
1097
1098TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1099 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001100 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001101 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1102 // Add two subdevices to device
1103 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1104 FakeInputMapper& mapperDevice1 =
1105 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1106 FakeInputMapper& mapperDevice2 =
1107 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1108 mReader->pushNextDevice(device);
1109 mReader->pushNextDevice(device);
1110 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1111 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1112
1113 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1114 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1115
1116 ASSERT_EQ(AKEY_STATE_DOWN,
1117 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1118 ASSERT_EQ(AKEY_STATE_DOWN,
1119 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1120 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1121 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1122}
1123
Prabir Pradhan7e186182020-11-10 13:56:45 -08001124TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1125 NotifyPointerCaptureChangedArgs args;
1126
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001127 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001128 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1129 mReader->loopOnce();
1130 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001131 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1132 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001133
1134 mFakePolicy->setPointerCapture(false);
1135 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1136 mReader->loopOnce();
1137 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001138 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001139
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001140 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001141 // does not change.
1142 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1143 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001144 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001145}
1146
Chris Ye87143712020-11-10 05:05:58 +00001147class FakeVibratorInputMapper : public FakeInputMapper {
1148public:
1149 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1150 : FakeInputMapper(deviceContext, sources) {}
1151
1152 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1153};
1154
1155TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1156 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001157 ftl::Flags<InputDeviceClass> deviceClass =
1158 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001159 constexpr int32_t eventHubId = 1;
1160 const char* DEVICE_LOCATION = "BLUETOOTH";
1161 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1162 FakeVibratorInputMapper& mapper =
1163 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1164 mReader->pushNextDevice(device);
1165
1166 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1167 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1168
1169 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1170 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1171}
1172
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001173// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001174
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001175class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001176public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001177 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001178
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001179 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001180
Andy Chenf9f1a022022-08-29 20:07:10 -04001181 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1182
Chris Yee2b1e5c2021-03-10 22:45:12 -08001183 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1184
1185 void dump(std::string& dump) override {}
1186
1187 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1188 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001189 }
1190
Chris Yee2b1e5c2021-03-10 22:45:12 -08001191 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1192 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001193 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001194
1195 bool setLightColor(int32_t lightId, int32_t color) override {
1196 getDeviceContext().setLightBrightness(lightId, color >> 24);
1197 return true;
1198 }
1199
1200 std::optional<int32_t> getLightColor(int32_t lightId) override {
1201 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1202 if (!result.has_value()) {
1203 return std::nullopt;
1204 }
1205 return result.value() << 24;
1206 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001207
1208 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1209
1210 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1211
1212private:
1213 InputDeviceContext& mDeviceContext;
1214 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1215 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001216 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001217};
1218
Chris Yee2b1e5c2021-03-10 22:45:12 -08001219TEST_F(InputReaderTest, BatteryGetCapacity) {
1220 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001221 ftl::Flags<InputDeviceClass> deviceClass =
1222 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001223 constexpr int32_t eventHubId = 1;
1224 const char* DEVICE_LOCATION = "BLUETOOTH";
1225 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001226 FakePeripheralController& controller =
1227 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001228 mReader->pushNextDevice(device);
1229
1230 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1231
Harry Cuttsa5b71292022-11-28 12:56:17 +00001232 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1233 FakeEventHub::BATTERY_CAPACITY);
1234 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001235}
1236
1237TEST_F(InputReaderTest, BatteryGetStatus) {
1238 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001239 ftl::Flags<InputDeviceClass> deviceClass =
1240 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001241 constexpr int32_t eventHubId = 1;
1242 const char* DEVICE_LOCATION = "BLUETOOTH";
1243 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001244 FakePeripheralController& controller =
1245 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001246 mReader->pushNextDevice(device);
1247
1248 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1249
Harry Cuttsa5b71292022-11-28 12:56:17 +00001250 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1251 FakeEventHub::BATTERY_STATUS);
1252 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001253}
1254
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001255TEST_F(InputReaderTest, BatteryGetDevicePath) {
1256 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1257 ftl::Flags<InputDeviceClass> deviceClass =
1258 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1259 constexpr int32_t eventHubId = 1;
1260 const char* DEVICE_LOCATION = "BLUETOOTH";
1261 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1262 device->addController<FakePeripheralController>(eventHubId);
1263 mReader->pushNextDevice(device);
1264
1265 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1266
Harry Cuttsa5b71292022-11-28 12:56:17 +00001267 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001268}
1269
Chris Ye3fdbfef2021-01-06 18:45:18 -08001270TEST_F(InputReaderTest, LightGetColor) {
1271 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001272 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001273 constexpr int32_t eventHubId = 1;
1274 const char* DEVICE_LOCATION = "BLUETOOTH";
1275 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001276 FakePeripheralController& controller =
1277 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001278 mReader->pushNextDevice(device);
1279 RawLightInfo info = {.id = 1,
1280 .name = "Mono",
1281 .maxBrightness = 255,
1282 .flags = InputLightClass::BRIGHTNESS,
1283 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001284 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1285 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001286
1287 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001288
Harry Cutts33476232023-01-30 19:57:29 +00001289 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1290 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1291 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1292 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001293}
1294
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001295// --- InputReaderIntegrationTest ---
1296
1297// These tests create and interact with the InputReader only through its interface.
1298// The InputReader is started during SetUp(), which starts its processing in its own
1299// thread. The tests use linux uinput to emulate input devices.
1300// NOTE: Interacting with the physical device while these tests are running may cause
1301// the tests to fail.
1302class InputReaderIntegrationTest : public testing::Test {
1303protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001304 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001305 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001306 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001307
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001308 std::shared_ptr<FakePointerController> mFakePointerController;
1309
Chris Yea52ade12020-08-27 16:49:20 -07001310 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001311#if !defined(__ANDROID__)
1312 GTEST_SKIP();
1313#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001314 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001315 mFakePointerController = std::make_shared<FakePointerController>();
1316 mFakePolicy->setPointerController(mFakePointerController);
Harry Cutts33476232023-01-30 19:57:29 +00001317 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1318 /*eventDidNotHappenTimeout=*/30ms);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001319
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001320 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1321 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001322 ASSERT_EQ(mReader->start(), OK);
1323
1324 // Since this test is run on a real device, all the input devices connected
1325 // to the test device will show up in mReader. We wait for those input devices to
1326 // show up before beginning the tests.
1327 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +00001328 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001329 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1330 }
1331
Chris Yea52ade12020-08-27 16:49:20 -07001332 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001333#if !defined(__ANDROID__)
1334 return;
1335#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001336 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001337 mReader.reset();
1338 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001339 mFakePolicy.clear();
1340 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001341
1342 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1343 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1344 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1345 [&name](const InputDeviceInfo& info) {
1346 return info.getIdentifier().name == name;
1347 });
1348 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1349 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001350};
1351
1352TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1353 // An invalid input device that is only used for this test.
1354 class InvalidUinputDevice : public UinputDevice {
1355 public:
Harry Cutts33476232023-01-30 19:57:29 +00001356 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001357
1358 private:
1359 void configureDevice(int fd, uinput_user_dev* device) override {}
1360 };
1361
1362 const size_t numDevices = mFakePolicy->getInputDevices().size();
1363
1364 // UinputDevice does not set any event or key bits, so InputReader should not
1365 // consider it as a valid device.
1366 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1367 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1368 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1369 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1370
1371 invalidDevice.reset();
1372 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1373 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1374 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1375}
1376
1377TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1378 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1379
1380 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1381 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1382 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1383 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1384
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001385 const auto device = findDeviceByName(keyboard->getName());
1386 ASSERT_TRUE(device.has_value());
1387 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1388 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1389 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001390
1391 keyboard.reset();
1392 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1394 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1395}
1396
1397TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1398 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1399 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1400
1401 NotifyConfigurationChangedArgs configChangedArgs;
1402 ASSERT_NO_FATAL_FAILURE(
1403 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001404 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001405 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1406
1407 NotifyKeyArgs keyArgs;
1408 keyboard->pressAndReleaseHomeKey();
1409 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1410 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001411 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001412 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001413 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001414 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001415 prevTimestamp = keyArgs.eventTime;
1416
1417 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1418 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001419 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001420 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001421 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001422}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001423
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001424TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1425 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1426 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1427
1428 const auto device = findDeviceByName(stylus->getName());
1429 ASSERT_TRUE(device.has_value());
1430
Prabir Pradhana3621852022-10-14 18:57:23 +00001431 // An external stylus with buttons should also be recognized as a keyboard.
1432 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001433 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1434 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1435
1436 const auto DOWN =
1437 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1438 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1439
1440 stylus->pressAndReleaseKey(BTN_STYLUS);
1441 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1442 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1443 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1444 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1445
1446 stylus->pressAndReleaseKey(BTN_STYLUS2);
1447 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1448 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1449 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1450 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1451
1452 stylus->pressAndReleaseKey(BTN_STYLUS3);
1453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1454 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1455 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1456 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1457}
1458
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001459/**
1460 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1461 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1462 * are passed to the listener.
1463 */
1464static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1465TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1466 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1467 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1468 NotifyKeyArgs keyArgs;
1469
1470 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1471 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1472 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1473 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1474
1475 controller->pressAndReleaseKey(BTN_GEAR_UP);
1476 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1477 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1478 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1479}
1480
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001481// --- TouchIntegrationTest ---
1482
Arthur Hungaab25622020-01-16 11:22:11 +08001483class TouchIntegrationTest : public InputReaderIntegrationTest {
1484protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001485 const std::string UNIQUE_ID = "local:0";
1486
Chris Yea52ade12020-08-27 16:49:20 -07001487 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001488#if !defined(__ANDROID__)
1489 GTEST_SKIP();
1490#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001491 InputReaderIntegrationTest::SetUp();
1492 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001493 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1494 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001495
1496 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1497 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1498 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001499 const auto info = findDeviceByName(mDevice->getName());
1500 ASSERT_TRUE(info);
1501 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001502 }
1503
1504 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001505 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001506 std::optional<uint8_t> physicalPort,
1507 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001508 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001509 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08001510 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1511 }
1512
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001513 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1514 NotifyMotionArgs args;
1515 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1516 EXPECT_EQ(action, args.action);
1517 ASSERT_EQ(points.size(), args.pointerCount);
1518 for (size_t i = 0; i < args.pointerCount; i++) {
1519 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1520 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1521 }
1522 }
1523
Arthur Hungaab25622020-01-16 11:22:11 +08001524 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001525 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001526};
1527
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001528TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1529 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1530 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1531 // presses).
1532 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1533 mDeviceInfo.getSources());
1534}
1535
Arthur Hungaab25622020-01-16 11:22:11 +08001536TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1537 NotifyMotionArgs args;
1538 const Point centerPoint = mDevice->getCenterPoint();
1539
1540 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001541 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001542 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001543 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001544 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1545 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1546
1547 // ACTION_MOVE
1548 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001549 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001550 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1552
1553 // ACTION_UP
1554 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001555 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001556 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1557 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1558}
1559
1560TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1561 NotifyMotionArgs args;
1562 const Point centerPoint = mDevice->getCenterPoint();
1563
1564 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001565 mDevice->sendSlot(FIRST_SLOT);
1566 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001567 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001568 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001569 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1570 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1571
1572 // ACTION_POINTER_DOWN (Second slot)
1573 const Point secondPoint = centerPoint + Point(100, 100);
1574 mDevice->sendSlot(SECOND_SLOT);
1575 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001576 mDevice->sendDown(secondPoint);
1577 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001578 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001579 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001580
1581 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001582 mDevice->sendMove(secondPoint + Point(1, 1));
1583 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001584 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1585 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1586
1587 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001588 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001589 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001590 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001591 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001592
1593 // ACTION_UP
1594 mDevice->sendSlot(FIRST_SLOT);
1595 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001596 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001597 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1598 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1599}
1600
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001601/**
1602 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1603 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1604 * data?
1605 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1606 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1607 * for Pointer 0 only is generated after.
1608 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1609 * events, we will not miss any information.
1610 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1611 * event generated afterwards that contains the newest movement of pointer 0.
1612 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1613 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1614 * losing information about non-palm pointers.
1615 */
1616TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1617 NotifyMotionArgs args;
1618 const Point centerPoint = mDevice->getCenterPoint();
1619
1620 // ACTION_DOWN
1621 mDevice->sendSlot(FIRST_SLOT);
1622 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1623 mDevice->sendDown(centerPoint);
1624 mDevice->sendSync();
1625 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1626
1627 // ACTION_POINTER_DOWN (Second slot)
1628 const Point secondPoint = centerPoint + Point(100, 100);
1629 mDevice->sendSlot(SECOND_SLOT);
1630 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1631 mDevice->sendDown(secondPoint);
1632 mDevice->sendSync();
1633 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1634
1635 // ACTION_MOVE (First slot)
1636 mDevice->sendSlot(FIRST_SLOT);
1637 mDevice->sendMove(centerPoint + Point(5, 5));
1638 // ACTION_POINTER_UP (Second slot)
1639 mDevice->sendSlot(SECOND_SLOT);
1640 mDevice->sendPointerUp();
1641 // Send a single sync for the above 2 pointer updates
1642 mDevice->sendSync();
1643
1644 // First, we should get POINTER_UP for the second pointer
1645 assertReceivedMotion(ACTION_POINTER_1_UP,
1646 {/*first pointer */ centerPoint + Point(5, 5),
1647 /*second pointer*/ secondPoint});
1648
1649 // Next, the MOVE event for the first pointer
1650 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1651}
1652
1653/**
1654 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1655 * move, and then it will go up, all in the same frame.
1656 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1657 * gets sent to the listener.
1658 */
1659TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1660 NotifyMotionArgs args;
1661 const Point centerPoint = mDevice->getCenterPoint();
1662
1663 // ACTION_DOWN
1664 mDevice->sendSlot(FIRST_SLOT);
1665 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1666 mDevice->sendDown(centerPoint);
1667 mDevice->sendSync();
1668 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1669
1670 // ACTION_POINTER_DOWN (Second slot)
1671 const Point secondPoint = centerPoint + Point(100, 100);
1672 mDevice->sendSlot(SECOND_SLOT);
1673 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1674 mDevice->sendDown(secondPoint);
1675 mDevice->sendSync();
1676 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1677
1678 // ACTION_MOVE (First slot)
1679 mDevice->sendSlot(FIRST_SLOT);
1680 mDevice->sendMove(centerPoint + Point(5, 5));
1681 // ACTION_POINTER_UP (Second slot)
1682 mDevice->sendSlot(SECOND_SLOT);
1683 mDevice->sendMove(secondPoint + Point(6, 6));
1684 mDevice->sendPointerUp();
1685 // Send a single sync for the above 2 pointer updates
1686 mDevice->sendSync();
1687
1688 // First, we should get POINTER_UP for the second pointer
1689 // The movement of the second pointer during the liftoff frame is ignored.
1690 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1691 assertReceivedMotion(ACTION_POINTER_1_UP,
1692 {/*first pointer */ centerPoint + Point(5, 5),
1693 /*second pointer*/ secondPoint});
1694
1695 // Next, the MOVE event for the first pointer
1696 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1697}
1698
Arthur Hungaab25622020-01-16 11:22:11 +08001699TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1700 NotifyMotionArgs args;
1701 const Point centerPoint = mDevice->getCenterPoint();
1702
1703 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001704 mDevice->sendSlot(FIRST_SLOT);
1705 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001706 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001707 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001708 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1709 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1710
arthurhungcc7f9802020-04-30 17:55:40 +08001711 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001712 const Point secondPoint = centerPoint + Point(100, 100);
1713 mDevice->sendSlot(SECOND_SLOT);
1714 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1715 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001716 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001717 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001718 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001719
arthurhungcc7f9802020-04-30 17:55:40 +08001720 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001721 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001722 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001723 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1725
arthurhungcc7f9802020-04-30 17:55:40 +08001726 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1727 // a palm event.
1728 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001729 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001730 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001731 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001732 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001733 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001734
arthurhungcc7f9802020-04-30 17:55:40 +08001735 // Send up to second slot, expect first slot send moving.
1736 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001737 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001738 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001740
arthurhungcc7f9802020-04-30 17:55:40 +08001741 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001742 mDevice->sendSlot(FIRST_SLOT);
1743 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001744 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001745
arthurhungcc7f9802020-04-30 17:55:40 +08001746 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1747 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001748}
1749
Prabir Pradhanda20b172022-09-26 17:01:18 +00001750TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1751 const Point centerPoint = mDevice->getCenterPoint();
1752
1753 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1754 mDevice->sendSlot(FIRST_SLOT);
1755 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1756 mDevice->sendToolType(MT_TOOL_PEN);
1757 mDevice->sendDown(centerPoint);
1758 mDevice->sendSync();
1759 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1760 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001761 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001762
1763 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1764
1765 // Release the stylus touch.
1766 mDevice->sendUp();
1767 mDevice->sendSync();
1768 ASSERT_NO_FATAL_FAILURE(
1769 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1770
1771 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1772
1773 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1774 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1775 mDevice->sendToolType(MT_TOOL_FINGER);
1776 mDevice->sendDown(centerPoint);
1777 mDevice->sendSync();
1778 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1779 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001780 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001781
1782 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1783
1784 mDevice->sendUp();
1785 mDevice->sendSync();
1786 ASSERT_NO_FATAL_FAILURE(
1787 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1788
1789 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1790 // The policy should be notified of the stylus presence.
1791 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1792 mDevice->sendToolType(MT_TOOL_PEN);
1793 mDevice->sendMove(centerPoint);
1794 mDevice->sendSync();
1795 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1796 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001797 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001798
1799 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1800}
1801
Prabir Pradhan124ea442022-10-28 20:27:44 +00001802// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001803
Prabir Pradhan124ea442022-10-28 20:27:44 +00001804// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1805// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1806// stylus.
1807template <typename UinputStylusDevice>
1808class StylusButtonIntegrationTest : public TouchIntegrationTest {
1809protected:
1810 void SetUp() override {
1811#if !defined(__ANDROID__)
1812 GTEST_SKIP();
1813#endif
1814 TouchIntegrationTest::SetUp();
1815 mTouchscreen = mDevice.get();
1816 mTouchscreenInfo = mDeviceInfo;
1817
1818 setUpStylusDevice();
1819 }
1820
1821 UinputStylusDevice* mStylus{nullptr};
1822 InputDeviceInfo mStylusInfo{};
1823
1824 UinputTouchScreen* mTouchscreen{nullptr};
1825 InputDeviceInfo mTouchscreenInfo{};
1826
1827private:
1828 // When we are attempting to test stylus button events that are sent from the touchscreen,
1829 // use the same Uinput device for the touchscreen and the stylus.
1830 template <typename T = UinputStylusDevice>
1831 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1832 mStylus = mDevice.get();
1833 mStylusInfo = mDeviceInfo;
1834 }
1835
1836 // When we are attempting to stylus buttons from an external stylus being merged with touches
1837 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1838 template <typename T = UinputStylusDevice>
1839 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1840 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1841 mStylus = mStylusDeviceLifecycleTracker.get();
1842 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1843 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1844 const auto info = findDeviceByName(mStylus->getName());
1845 ASSERT_TRUE(info);
1846 mStylusInfo = *info;
1847 }
1848
1849 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1850
1851 // Hide the base class's device to expose it with a different name for readability.
1852 using TouchIntegrationTest::mDevice;
1853 using TouchIntegrationTest::mDeviceInfo;
1854};
1855
1856using StylusButtonIntegrationTestTypes =
1857 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1858TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1859
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001860TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001861 const auto stylusId = TestFixture::mStylusInfo.getId();
1862
1863 TestFixture::mStylus->pressKey(BTN_STYLUS);
1864 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1865 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1866 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1867
1868 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1869 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001870 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001871 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001872}
1873
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001874TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001875 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1876 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1877 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001878
1879 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001880 TestFixture::mStylus->pressKey(BTN_STYLUS);
1881 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001882 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001883 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001884
1885 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001886 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1887 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1888 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1889 TestFixture::mTouchscreen->sendDown(centerPoint);
1890 TestFixture::mTouchscreen->sendSync();
1891 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001892 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001893 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001894 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1895 WithDeviceId(touchscreenId))));
1896 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001897 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001898 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001899 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1900 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001901
Prabir Pradhan124ea442022-10-28 20:27:44 +00001902 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1903 TestFixture::mTouchscreen->sendSync();
1904 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001905 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001906 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001907 WithDeviceId(touchscreenId))));
1908 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001909 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001910 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001911 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001912
1913 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001914 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1915 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001916 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001917 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001918}
1919
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001920TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001921 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1922 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1923 const auto stylusId = TestFixture::mStylusInfo.getId();
1924 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001925 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001926
1927 // Press the stylus button.
1928 TestFixture::mStylus->pressKey(BTN_STYLUS);
1929 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1930 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1931 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1932
1933 // Start hovering with the stylus.
1934 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1935 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1936 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1937 TestFixture::mTouchscreen->sendMove(centerPoint);
1938 TestFixture::mTouchscreen->sendSync();
1939 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1940 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1941 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1942 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1943 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1944 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1945 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1946 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1947 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1948
1949 // Touch down with the stylus.
1950 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1951 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1952 TestFixture::mTouchscreen->sendDown(centerPoint);
1953 TestFixture::mTouchscreen->sendSync();
1954 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1955 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1956 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1957
1958 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1959 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1960 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1961
1962 // Stop touching with the stylus, and start hovering.
1963 TestFixture::mTouchscreen->sendUp();
1964 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1965 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1966 TestFixture::mTouchscreen->sendMove(centerPoint);
1967 TestFixture::mTouchscreen->sendSync();
1968 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1969 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
1970 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1971 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1972 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1973 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1974 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1975 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1976 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1977
1978 // Stop hovering.
1979 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1980 TestFixture::mTouchscreen->sendSync();
1981 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1982 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1983 WithButtonState(0))));
1984 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
1985 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1986 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1987 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1988
1989 // Release the stylus button.
1990 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1991 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1992 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
1993 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1994}
1995
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001996TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001997 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1998 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1999 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002000
2001 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002002 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2003 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2004 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2005 TestFixture::mTouchscreen->sendDown(centerPoint);
2006 TestFixture::mTouchscreen->sendSync();
2007 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002008 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002009 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002010 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002011
2012 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002013 TestFixture::mStylus->pressKey(BTN_STYLUS);
2014 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002015 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002016 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2017 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002018 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002019 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002020 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2021 WithDeviceId(touchscreenId))));
2022 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002023 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002024 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002025 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2026 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002027
Prabir Pradhan124ea442022-10-28 20:27:44 +00002028 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2029 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002030 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002031 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2032 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002033 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002034 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002035 WithDeviceId(touchscreenId))));
2036 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002037 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002038 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002039 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002040
2041 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002042 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2043 TestFixture::mTouchscreen->sendSync();
2044 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002045 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002046 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002047 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002048}
2049
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002050TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002051 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2052 TestFixture::mReader->requestRefreshConfiguration(
2053 InputReaderConfiguration::CHANGE_STYLUS_BUTTON_REPORTING);
2054
2055 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2056 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2057 const auto stylusId = TestFixture::mStylusInfo.getId();
2058
2059 // Start a stylus gesture. By the time this event is processed, the configuration change that
2060 // was requested is guaranteed to be completed.
2061 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2062 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2063 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2064 TestFixture::mTouchscreen->sendDown(centerPoint);
2065 TestFixture::mTouchscreen->sendSync();
2066 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2067 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002068 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002069 WithDeviceId(touchscreenId))));
2070
2071 // Press and release a stylus button. Each change only generates a MOVE motion event.
2072 // Key events are unaffected.
2073 TestFixture::mStylus->pressKey(BTN_STYLUS);
2074 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2075 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2076 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2077 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2078 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002079 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002080 WithDeviceId(touchscreenId))));
2081
2082 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2083 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2084 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2085 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2086 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2087 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002088 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002089 WithDeviceId(touchscreenId))));
2090
2091 // Finish the stylus gesture.
2092 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2093 TestFixture::mTouchscreen->sendSync();
2094 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2095 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002096 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002097 WithDeviceId(touchscreenId))));
2098}
2099
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002100// --- ExternalStylusIntegrationTest ---
2101
2102// Verify the behavior of an external stylus. An external stylus can report pressure or button
2103// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2104// ongoing stylus gesture that is being emitted by the touchscreen.
2105using ExternalStylusIntegrationTest = TouchIntegrationTest;
2106
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002107TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002108 const Point centerPoint = mDevice->getCenterPoint();
2109
2110 // Create an external stylus capable of reporting pressure data that
2111 // should be fused with a touch pointer.
2112 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2113 createUinputDevice<UinputExternalStylusWithPressure>();
2114 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2115 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2116 const auto stylusInfo = findDeviceByName(stylus->getName());
2117 ASSERT_TRUE(stylusInfo);
2118
2119 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2120
2121 const auto touchscreenId = mDeviceInfo.getId();
2122
2123 // Set a pressure value on the stylus. It doesn't generate any events.
2124 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2125 stylus->setPressure(100);
2126 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2127
2128 // Start a finger gesture, and ensure it shows up as stylus gesture
2129 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002130 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002131 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002132 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002133 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002134 mDevice->sendSync();
2135 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2136 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002137 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002138 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002139
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002140 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2141 // event with the updated pressure.
2142 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002143 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2144 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002145 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002146 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002147
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002148 // The external stylus did not generate any events.
2149 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2150 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2151}
2152
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002153TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002154 const Point centerPoint = mDevice->getCenterPoint();
2155
2156 // Create an external stylus capable of reporting pressure data that
2157 // should be fused with a touch pointer.
2158 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2159 createUinputDevice<UinputExternalStylusWithPressure>();
2160 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2161 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2162 const auto stylusInfo = findDeviceByName(stylus->getName());
2163 ASSERT_TRUE(stylusInfo);
2164
2165 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2166
2167 const auto touchscreenId = mDeviceInfo.getId();
2168
2169 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2170 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002171 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2172 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002173 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002174 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002175
2176 // Start a finger gesture. The touch device will withhold generating any touches for
2177 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2178 mDevice->sendSlot(FIRST_SLOT);
2179 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2180 mDevice->sendToolType(MT_TOOL_FINGER);
2181 mDevice->sendDown(centerPoint);
2182 auto waitUntil = std::chrono::system_clock::now() +
2183 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002184 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002185 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002186
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002187 // Since the external stylus did not report a pressure value within the timeout,
2188 // it shows up as a finger pointer.
2189 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2190 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002191 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002192 WithPressure(1.f))));
2193
2194 // Change the pressure on the external stylus. Since the pressure was not present at the start
2195 // of the gesture, it is ignored for now.
2196 stylus->setPressure(200);
2197 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2198
2199 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002200 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2201 mDevice->sendSync();
2202 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2203 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002204 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002205
2206 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2207 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2208 mDevice->sendToolType(MT_TOOL_FINGER);
2209 mDevice->sendDown(centerPoint);
2210 mDevice->sendSync();
2211 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2212 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002213 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002214 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2215
2216 // The external stylus did not generate any events.
2217 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2218 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002219}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002220
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002221TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002222 const Point centerPoint = mDevice->getCenterPoint();
2223
2224 // Create an external stylus device that does not support pressure. It should not affect any
2225 // touch pointers.
2226 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2227 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2228 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2229 const auto stylusInfo = findDeviceByName(stylus->getName());
2230 ASSERT_TRUE(stylusInfo);
2231
2232 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2233
2234 const auto touchscreenId = mDeviceInfo.getId();
2235
2236 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2237 // pressure data from the external stylus.
2238 mDevice->sendSlot(FIRST_SLOT);
2239 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2240 mDevice->sendToolType(MT_TOOL_FINGER);
2241 mDevice->sendDown(centerPoint);
2242 auto waitUntil = std::chrono::system_clock::now() +
2243 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2244 mDevice->sendSync();
2245 ASSERT_NO_FATAL_FAILURE(
2246 mTestListener
2247 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2248 WithToolType(
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002249 ToolType::FINGER),
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002250 WithButtonState(0),
2251 WithDeviceId(touchscreenId),
2252 WithPressure(1.f)),
2253 waitUntil));
2254
2255 // The external stylus did not generate any events.
2256 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2257 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2258}
2259
Michael Wrightd02c5b62014-02-10 15:10:22 -08002260// --- InputDeviceTest ---
2261class InputDeviceTest : public testing::Test {
2262protected:
2263 static const char* DEVICE_NAME;
2264 static const char* DEVICE_LOCATION;
2265 static const int32_t DEVICE_ID;
2266 static const int32_t DEVICE_GENERATION;
2267 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002268 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002269 static const int32_t EVENTHUB_ID;
2270 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2271
2272 std::shared_ptr<FakeEventHub> mFakeEventHub;
2273 sp<FakeInputReaderPolicy> mFakePolicy;
2274 std::unique_ptr<TestInputListener> mFakeListener;
2275 std::unique_ptr<InstrumentedInputReader> mReader;
2276 std::shared_ptr<InputDevice> mDevice;
2277
2278 void SetUp() override {
2279 mFakeEventHub = std::make_unique<FakeEventHub>();
2280 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2281 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002282 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002283 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002284 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002285 identifier.name = DEVICE_NAME;
2286 identifier.location = DEVICE_LOCATION;
2287 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2288 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2289 identifier);
2290 mReader->pushNextDevice(mDevice);
2291 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002292 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002293 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002294
2295 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002296 mFakeListener.reset();
2297 mFakePolicy.clear();
2298 }
2299};
2300
2301const char* InputDeviceTest::DEVICE_NAME = "device";
2302const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2303const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2304const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002305const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002306const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2307 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002308const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002309const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2310
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002311TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002312 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002313 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2314 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002315}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002316
Michael Wrightd02c5b62014-02-10 15:10:22 -08002317TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2318 ASSERT_EQ(mDevice->isEnabled(), false);
2319}
2320
2321TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2322 // Configuration.
2323 InputReaderConfiguration config;
2324 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2325
2326 // Reset.
2327 unused += mDevice->reset(ARBITRARY_TIME);
2328
2329 NotifyDeviceResetArgs resetArgs;
2330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2331 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2332 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2333
2334 // Metadata.
2335 ASSERT_TRUE(mDevice->isIgnored());
2336 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2337
2338 InputDeviceInfo info = mDevice->getDeviceInfo();
2339 ASSERT_EQ(DEVICE_ID, info.getId());
2340 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2341 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2342 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2343
2344 // State queries.
2345 ASSERT_EQ(0, mDevice->getMetaState());
2346
2347 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2348 << "Ignored device should return unknown key code state.";
2349 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2350 << "Ignored device should return unknown scan code state.";
2351 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2352 << "Ignored device should return unknown switch state.";
2353
2354 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2355 uint8_t flags[2] = { 0, 1 };
2356 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2357 << "Ignored device should never mark any key codes.";
2358 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2359 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2360}
2361
2362TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2363 // Configuration.
2364 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002365
2366 FakeInputMapper& mapper1 =
2367 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2368 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002369 mapper1.setMetaState(AMETA_ALT_ON);
2370 mapper1.addSupportedKeyCode(AKEYCODE_A);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002371 mapper1.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002372 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2373 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2374 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2375 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2376 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2377
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002378 FakeInputMapper& mapper2 =
Michael Wrightd02c5b62014-02-10 15:10:22 -08002379 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
2380 mapper2.setMetaState(AMETA_SHIFT_ON);
2381
2382 InputReaderConfiguration config;
2383 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2384
Harry Cuttsf13161a2023-03-08 14:15:49 +00002385 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2386 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002387 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002388 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002389
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002390 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2391 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002392
2393 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002394 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002395 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2396 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002397
2398 NotifyDeviceResetArgs resetArgs;
2399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2400 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2401 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2402
2403 // Metadata.
2404 ASSERT_FALSE(mDevice->isIgnored());
2405 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2406
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002407 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002408 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002409 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002410 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2411 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2412
2413 // State queries.
2414 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2415 << "Should query mappers and combine meta states.";
2416
2417 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2418 << "Should return unknown key code state when source not supported.";
2419 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2420 << "Should return unknown scan code state when source not supported.";
2421 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2422 << "Should return unknown switch state when source not supported.";
2423
2424 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2425 << "Should query mapper when source is supported.";
2426 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2427 << "Should query mapper when source is supported.";
2428 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2429 << "Should query mapper when source is supported.";
2430
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002431 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002433 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002434 << "Should do nothing when source is unsupported.";
2435 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2436 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2437 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2438 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2439
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002440 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002441 << "Should query mapper when source is supported.";
2442 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2443 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2444 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2445 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2446
2447 // Event handling.
2448 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002449 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002450 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002451
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002452 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2453 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454}
2455
Arthur Hung2c9a3342019-07-23 14:18:59 +08002456// A single input device is associated with a specific display. Check that:
2457// 1. Device is disabled if the viewport corresponding to the associated display is not found
2458// 2. Device is disabled when setEnabled API is called
2459TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002460 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002461
2462 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002463 std::list<NotifyArgs> unused =
2464 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002465
2466 // Device should be enabled by default.
2467 ASSERT_TRUE(mDevice->isEnabled());
2468
2469 // Prepare associated info.
2470 constexpr uint8_t hdmi = 1;
2471 const std::string UNIQUE_ID = "local:1";
2472
2473 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002474 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2475 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002476 // Device should be disabled because it is associated with a specific display via
2477 // input port <-> display port association, but the corresponding display is not found
2478 ASSERT_FALSE(mDevice->isEnabled());
2479
2480 // Prepare displays.
2481 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002482 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002483 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002484 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2485 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002486 ASSERT_TRUE(mDevice->isEnabled());
2487
2488 // Device should be disabled after set disable.
2489 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002490 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2491 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002492 ASSERT_FALSE(mDevice->isEnabled());
2493
2494 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002495 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2496 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002497 ASSERT_FALSE(mDevice->isEnabled());
2498}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002499
Christine Franks1ba71cc2021-04-07 14:37:42 -07002500TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2501 // Device should be enabled by default.
2502 mFakePolicy->clearViewports();
2503 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002504 std::list<NotifyArgs> unused =
2505 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002506 ASSERT_TRUE(mDevice->isEnabled());
2507
2508 // Device should be disabled because it is associated with a specific display, but the
2509 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002510 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002511 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2512 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002513 ASSERT_FALSE(mDevice->isEnabled());
2514
2515 // Device should be enabled when a display is found.
2516 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002517 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002518 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002519 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2520 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002521 ASSERT_TRUE(mDevice->isEnabled());
2522
2523 // Device should be disabled after set disable.
2524 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002525 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2526 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002527 ASSERT_FALSE(mDevice->isEnabled());
2528
2529 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002530 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2531 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002532 ASSERT_FALSE(mDevice->isEnabled());
2533}
2534
Christine Franks2a2293c2022-01-18 11:51:16 -08002535TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2536 mFakePolicy->clearViewports();
2537 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002538 std::list<NotifyArgs> unused =
2539 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08002540
Christine Franks2a2293c2022-01-18 11:51:16 -08002541 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2542 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002543 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002544 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002545 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2546 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002547 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2548}
2549
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002550/**
2551 * This test reproduces a crash caused by a dangling reference that remains after device is added
2552 * and removed. The reference is accessed in InputDevice::dump(..);
2553 */
2554TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2555 constexpr int32_t TEST_EVENTHUB_ID = 10;
2556 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2557
Harry Cutts33476232023-01-30 19:57:29 +00002558 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
2559 device.addEventHubDevice(TEST_EVENTHUB_ID, /*populateMappers=*/true);
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002560 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2561 std::string dumpStr, eventHubDevStr;
2562 device.dump(dumpStr, eventHubDevStr);
2563}
2564
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002565TEST_F(InputDeviceTest, GetBluetoothAddress) {
2566 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2567 ASSERT_TRUE(address);
2568 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2569}
2570
Michael Wrightd02c5b62014-02-10 15:10:22 -08002571// --- SwitchInputMapperTest ---
2572
2573class SwitchInputMapperTest : public InputMapperTest {
2574protected:
2575};
2576
2577TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002578 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002579
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002580 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002581}
2582
2583TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002584 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002585
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002586 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002587 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002588
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002589 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002590 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002591}
2592
2593TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002594 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002595 std::list<NotifyArgs> out;
2596 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2597 ASSERT_TRUE(out.empty());
2598 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2599 ASSERT_TRUE(out.empty());
2600 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2601 ASSERT_TRUE(out.empty());
2602 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002603
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002604 ASSERT_EQ(1u, out.size());
2605 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002606 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002607 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2608 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002609 args.switchMask);
2610 ASSERT_EQ(uint32_t(0), args.policyFlags);
2611}
2612
Chris Ye87143712020-11-10 05:05:58 +00002613// --- VibratorInputMapperTest ---
2614class VibratorInputMapperTest : public InputMapperTest {
2615protected:
2616 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2617};
2618
2619TEST_F(VibratorInputMapperTest, GetSources) {
2620 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2621
2622 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2623}
2624
2625TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2626 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2627
2628 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2629}
2630
2631TEST_F(VibratorInputMapperTest, Vibrate) {
2632 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002633 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002634 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2635
2636 VibrationElement pattern(2);
2637 VibrationSequence sequence(2);
2638 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002639 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2640 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002641 sequence.addElement(pattern);
2642 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002643 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2644 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002645 sequence.addElement(pattern);
2646
2647 std::vector<int64_t> timings = {0, 1};
2648 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2649
2650 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002651 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002652 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002653 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002654 // Verify vibrator state listener was notified.
2655 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002656 ASSERT_EQ(1u, out.size());
2657 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2658 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2659 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002660 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002661 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002662 ASSERT_FALSE(mapper.isVibrating());
2663 // Verify vibrator state listener was notified.
2664 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002665 ASSERT_EQ(1u, out.size());
2666 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2667 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2668 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002669}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002670
Chris Yef59a2f42020-10-16 12:55:26 -07002671// --- SensorInputMapperTest ---
2672
2673class SensorInputMapperTest : public InputMapperTest {
2674protected:
2675 static const int32_t ACCEL_RAW_MIN;
2676 static const int32_t ACCEL_RAW_MAX;
2677 static const int32_t ACCEL_RAW_FUZZ;
2678 static const int32_t ACCEL_RAW_FLAT;
2679 static const int32_t ACCEL_RAW_RESOLUTION;
2680
2681 static const int32_t GYRO_RAW_MIN;
2682 static const int32_t GYRO_RAW_MAX;
2683 static const int32_t GYRO_RAW_FUZZ;
2684 static const int32_t GYRO_RAW_FLAT;
2685 static const int32_t GYRO_RAW_RESOLUTION;
2686
2687 static const float GRAVITY_MS2_UNIT;
2688 static const float DEGREE_RADIAN_UNIT;
2689
2690 void prepareAccelAxes();
2691 void prepareGyroAxes();
2692 void setAccelProperties();
2693 void setGyroProperties();
2694 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2695};
2696
2697const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2698const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2699const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2700const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2701const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2702
2703const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2704const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2705const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2706const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2707const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2708
2709const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2710const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2711
2712void SensorInputMapperTest::prepareAccelAxes() {
2713 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2714 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2715 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2716 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2717 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2718 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2719}
2720
2721void SensorInputMapperTest::prepareGyroAxes() {
2722 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2723 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2724 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2725 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2726 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2727 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2728}
2729
2730void SensorInputMapperTest::setAccelProperties() {
2731 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2732 /* sensorDataIndex */ 0);
2733 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2734 /* sensorDataIndex */ 1);
2735 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2736 /* sensorDataIndex */ 2);
2737 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2738 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2739 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2740 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2741 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2742}
2743
2744void SensorInputMapperTest::setGyroProperties() {
2745 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2746 /* sensorDataIndex */ 0);
2747 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2748 /* sensorDataIndex */ 1);
2749 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2750 /* sensorDataIndex */ 2);
2751 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2752 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2753 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2754 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2755 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2756}
2757
2758TEST_F(SensorInputMapperTest, GetSources) {
2759 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2760
2761 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2762}
2763
2764TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2765 setAccelProperties();
2766 prepareAccelAxes();
2767 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2768
2769 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2770 std::chrono::microseconds(10000),
2771 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002772 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002778
2779 NotifySensorArgs args;
2780 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2781 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2782 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2783
2784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2785 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2786 ASSERT_EQ(args.deviceId, DEVICE_ID);
2787 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2788 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2789 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2790 ASSERT_EQ(args.values, values);
2791 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2792}
2793
2794TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2795 setGyroProperties();
2796 prepareGyroAxes();
2797 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2798
2799 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2800 std::chrono::microseconds(10000),
2801 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002802 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2805 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002808
2809 NotifySensorArgs args;
2810 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2811 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2812 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2813
2814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2815 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2816 ASSERT_EQ(args.deviceId, DEVICE_ID);
2817 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2818 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2819 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2820 ASSERT_EQ(args.values, values);
2821 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2822}
2823
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824// --- KeyboardInputMapperTest ---
2825
2826class KeyboardInputMapperTest : public InputMapperTest {
2827protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002828 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002829 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002830 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002831
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002832 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002833 int32_t originalKeyCode, int32_t rotatedKeyCode,
2834 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002835};
2836
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002837/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2838 * orientation.
2839 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002840void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002841 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2842 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002843}
2844
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002845void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002846 int32_t originalScanCode, int32_t originalKeyCode,
2847 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848 NotifyKeyArgs args;
2849
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002850 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2852 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2853 ASSERT_EQ(originalScanCode, args.scanCode);
2854 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002855 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002856
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2859 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2860 ASSERT_EQ(originalScanCode, args.scanCode);
2861 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002862 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002863}
2864
Michael Wrightd02c5b62014-02-10 15:10:22 -08002865TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002866 KeyboardInputMapper& mapper =
2867 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2868 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002869
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002870 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002871}
2872
2873TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2874 const int32_t USAGE_A = 0x070004;
2875 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002876 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2877 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002878 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2879 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2880 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002881
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002882 KeyboardInputMapper& mapper =
2883 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2884 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002885 // Initial metastate is AMETA_NONE.
2886 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002887
2888 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002890 NotifyKeyArgs args;
2891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2892 ASSERT_EQ(DEVICE_ID, args.deviceId);
2893 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2894 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2895 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2896 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2897 ASSERT_EQ(KEY_HOME, args.scanCode);
2898 ASSERT_EQ(AMETA_NONE, args.metaState);
2899 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2900 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2901 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2902
2903 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002904 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2906 ASSERT_EQ(DEVICE_ID, args.deviceId);
2907 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2908 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2909 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2910 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2911 ASSERT_EQ(KEY_HOME, args.scanCode);
2912 ASSERT_EQ(AMETA_NONE, args.metaState);
2913 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2914 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2915 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2916
2917 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2919 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2921 ASSERT_EQ(DEVICE_ID, args.deviceId);
2922 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2923 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2924 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2925 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2926 ASSERT_EQ(0, args.scanCode);
2927 ASSERT_EQ(AMETA_NONE, args.metaState);
2928 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2929 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2930 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2931
2932 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2934 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2936 ASSERT_EQ(DEVICE_ID, args.deviceId);
2937 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2938 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2939 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2940 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2941 ASSERT_EQ(0, args.scanCode);
2942 ASSERT_EQ(AMETA_NONE, args.metaState);
2943 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2944 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2945 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2946
2947 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2951 ASSERT_EQ(DEVICE_ID, args.deviceId);
2952 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2953 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2954 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2955 ASSERT_EQ(0, args.keyCode);
2956 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2957 ASSERT_EQ(AMETA_NONE, args.metaState);
2958 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2959 ASSERT_EQ(0U, args.policyFlags);
2960 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2961
2962 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2964 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2966 ASSERT_EQ(DEVICE_ID, args.deviceId);
2967 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2968 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2969 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2970 ASSERT_EQ(0, args.keyCode);
2971 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2972 ASSERT_EQ(AMETA_NONE, args.metaState);
2973 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2974 ASSERT_EQ(0U, args.policyFlags);
2975 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2976}
2977
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00002978TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
2979 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
2980 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
2981 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
2982
2983 KeyboardInputMapper& mapper =
2984 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2985 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2986
2987 // Key down by scan code.
2988 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
2989 NotifyKeyArgs args;
2990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2991 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2992
2993 // Key up by scan code.
2994 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
2995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2996 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2997}
2998
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002999/**
3000 * Ensure that the readTime is set to the time when the EV_KEY is received.
3001 */
3002TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3003 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3004
3005 KeyboardInputMapper& mapper =
3006 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3007 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3008 NotifyKeyArgs args;
3009
3010 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003011 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3013 ASSERT_EQ(12, args.readTime);
3014
3015 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003016 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3018 ASSERT_EQ(15, args.readTime);
3019}
3020
Michael Wrightd02c5b62014-02-10 15:10:22 -08003021TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003022 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3023 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003024 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3025 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3026 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003028 KeyboardInputMapper& mapper =
3029 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3030 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031
Arthur Hung95f68612022-04-07 14:08:22 +08003032 // Initial metastate is AMETA_NONE.
3033 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034
3035 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003037 NotifyKeyArgs args;
3038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3039 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003040 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003041 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003042
3043 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003044 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3046 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003047 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003048
3049 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003050 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3052 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003053 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003054
3055 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003056 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3058 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003059 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003060 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003061}
3062
3063TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003064 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3065 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3066 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3067 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003069 KeyboardInputMapper& mapper =
3070 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3071 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003072
Michael Wrighta9cf4192022-12-01 23:46:39 +00003073 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3075 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3076 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3077 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3078 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3079 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3080 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3081 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3082}
3083
3084TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003085 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3086 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3087 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3088 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 KeyboardInputMapper& mapper =
3092 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3093 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003094
Michael Wrighta9cf4192022-12-01 23:46:39 +00003095 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003096 ASSERT_NO_FATAL_FAILURE(
3097 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3098 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3099 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3100 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3101 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3102 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3103 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003105 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003106 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003107 ASSERT_NO_FATAL_FAILURE(
3108 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3109 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3110 AKEYCODE_DPAD_UP, DISPLAY_ID));
3111 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3112 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3113 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3114 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003116 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003117 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003118 ASSERT_NO_FATAL_FAILURE(
3119 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3120 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3121 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3122 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3123 AKEYCODE_DPAD_UP, DISPLAY_ID));
3124 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3125 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003127 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003128 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003129 ASSERT_NO_FATAL_FAILURE(
3130 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3131 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3132 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3133 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3134 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3135 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3136 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003137
3138 // Special case: if orientation changes while key is down, we still emit the same keycode
3139 // in the key up as we did in the key down.
3140 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003141 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003142 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3145 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3146 ASSERT_EQ(KEY_UP, args.scanCode);
3147 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3148
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003149 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003150 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3153 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3154 ASSERT_EQ(KEY_UP, args.scanCode);
3155 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3156}
3157
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003158TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3159 // If the keyboard is not orientation aware,
3160 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003161 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003162
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003163 KeyboardInputMapper& mapper =
3164 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3165 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003166 NotifyKeyArgs args;
3167
3168 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3173 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3174
Michael Wrighta9cf4192022-12-01 23:46:39 +00003175 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3180 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3181}
3182
3183TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3184 // If the keyboard is orientation aware,
3185 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003186 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003187
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003188 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003189 KeyboardInputMapper& mapper =
3190 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3191 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003192 NotifyKeyArgs args;
3193
3194 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3195 // ^--- already checked by the previous test
3196
Michael Wrighta9cf4192022-12-01 23:46:39 +00003197 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003198 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3203 ASSERT_EQ(DISPLAY_ID, args.displayId);
3204
3205 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003206 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003207 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003208 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3213 ASSERT_EQ(newDisplayId, args.displayId);
3214}
3215
Michael Wrightd02c5b62014-02-10 15:10:22 -08003216TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003217 KeyboardInputMapper& mapper =
3218 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3219 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003220
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003221 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003222 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003223
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003224 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003225 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003226}
3227
Philip Junker4af3b3d2021-12-14 10:36:55 +01003228TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3229 KeyboardInputMapper& mapper =
3230 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3231 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3232
3233 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3234 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3235 << "If a mapping is available, the result is equal to the mapping";
3236
3237 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3238 << "If no mapping is available, the result is the key location";
3239}
3240
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003242 KeyboardInputMapper& mapper =
3243 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3244 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003246 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003247 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003249 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003250 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251}
3252
3253TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003254 KeyboardInputMapper& mapper =
3255 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3256 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003258 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003261 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262 ASSERT_TRUE(flags[0]);
3263 ASSERT_FALSE(flags[1]);
3264}
3265
3266TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003267 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3268 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3269 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3270 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3271 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3272 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003274 KeyboardInputMapper& mapper =
3275 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3276 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003277 // Initial metastate is AMETA_NONE.
3278 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279
3280 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003281 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3282 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3283 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284
3285 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003286 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003288 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3289 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3290 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003291 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003292
3293 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003296 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3297 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3298 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003299 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300
3301 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003304 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3305 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3306 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003307 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308
3309 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003312 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3313 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3314 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003315 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003316
3317 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3319 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003320 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3321 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3322 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003323 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324
3325 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003328 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3329 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3330 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003331 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332}
3333
Chris Yea52ade12020-08-27 16:49:20 -07003334TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3335 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3336 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3337 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3338 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3339
3340 KeyboardInputMapper& mapper =
3341 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3342 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3343
Chris Yea52ade12020-08-27 16:49:20 -07003344 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003345 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003346 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3347 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3348 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3349 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3350
3351 NotifyKeyArgs args;
3352 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3355 ASSERT_EQ(AMETA_NONE, args.metaState);
3356 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3357 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3358 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3359
3360 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003361 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3363 ASSERT_EQ(AMETA_NONE, args.metaState);
3364 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3365 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3366 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3367}
3368
Arthur Hung2c9a3342019-07-23 14:18:59 +08003369TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3370 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003371 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3372 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3373 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3374 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003375
3376 // keyboard 2.
3377 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003378 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003379 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003380 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003381 std::shared_ptr<InputDevice> device2 =
3382 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003383 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003384
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003385 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3386 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3387 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3388 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003389
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003390 KeyboardInputMapper& mapper =
3391 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3392 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003393
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003394 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003395 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003396 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003397 std::list<NotifyArgs> unused =
3398 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003399 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003400 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003401
3402 // Prepared displays and associated info.
3403 constexpr uint8_t hdmi1 = 0;
3404 constexpr uint8_t hdmi2 = 1;
3405 const std::string SECONDARY_UNIQUE_ID = "local:1";
3406
3407 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3408 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3409
3410 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003411 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3412 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003413 ASSERT_FALSE(device2->isEnabled());
3414
3415 // Prepare second display.
3416 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003417 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003418 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003419 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003420 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003421 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003422 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3423 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003424
3425 // Device should be enabled after the associated display is found.
3426 ASSERT_TRUE(mDevice->isEnabled());
3427 ASSERT_TRUE(device2->isEnabled());
3428
3429 // Test pad key events
3430 ASSERT_NO_FATAL_FAILURE(
3431 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3432 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3433 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3434 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3435 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3436 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3437 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3438
3439 ASSERT_NO_FATAL_FAILURE(
3440 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3441 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3442 AKEYCODE_DPAD_RIGHT, newDisplayId));
3443 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3444 AKEYCODE_DPAD_DOWN, newDisplayId));
3445 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3446 AKEYCODE_DPAD_LEFT, newDisplayId));
3447}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448
arthurhungc903df12020-08-11 15:08:42 +08003449TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3450 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3451 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3452 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3453 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3454 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3455 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3456
3457 KeyboardInputMapper& mapper =
3458 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3459 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003460 // Initial metastate is AMETA_NONE.
3461 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003462
3463 // Initialization should have turned all of the lights off.
3464 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3465 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3466 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3467
3468 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3470 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003471 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3472 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3473
3474 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003477 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3478 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3479
3480 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003483 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3484 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3485
3486 mFakeEventHub->removeDevice(EVENTHUB_ID);
3487 mReader->loopOnce();
3488
3489 // keyboard 2 should default toggle keys.
3490 const std::string USB2 = "USB2";
3491 const std::string DEVICE_NAME2 = "KEYBOARD2";
3492 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3493 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3494 std::shared_ptr<InputDevice> device2 =
3495 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003496 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003497 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3498 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3499 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3500 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3501 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3502 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3503
arthurhung6fe95782020-10-05 22:41:16 +08003504 KeyboardInputMapper& mapper2 =
3505 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3506 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003507 std::list<NotifyArgs> unused =
3508 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003509 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003510 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003511
3512 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3513 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3514 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003515 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3516 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003517}
3518
Arthur Hungcb40a002021-08-03 14:31:01 +00003519TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3520 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3521 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3522 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3523
3524 // Suppose we have two mappers. (DPAD + KEYBOARD)
3525 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3526 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3527 KeyboardInputMapper& mapper =
3528 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3529 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003530 // Initial metastate is AMETA_NONE.
3531 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003532
3533 mReader->toggleCapsLockState(DEVICE_ID);
3534 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3535}
3536
Arthur Hungfb3cc112022-04-13 07:39:50 +00003537TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3538 // keyboard 1.
3539 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3540 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3541 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3542 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3543 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3544 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3545
3546 KeyboardInputMapper& mapper1 =
3547 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3548 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3549
3550 // keyboard 2.
3551 const std::string USB2 = "USB2";
3552 const std::string DEVICE_NAME2 = "KEYBOARD2";
3553 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3554 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3555 std::shared_ptr<InputDevice> device2 =
3556 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3557 ftl::Flags<InputDeviceClass>(0));
3558 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3559 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3560 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3561 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3562 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3563 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3564
3565 KeyboardInputMapper& mapper2 =
3566 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3567 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003568 std::list<NotifyArgs> unused =
3569 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003570 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003571 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003572
Arthur Hung95f68612022-04-07 14:08:22 +08003573 // Initial metastate is AMETA_NONE.
3574 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3575 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3576
3577 // Toggle num lock on and off.
3578 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3579 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003580 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3581 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3582 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3583
3584 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3585 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3586 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3587 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3588 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3589
3590 // Toggle caps lock on and off.
3591 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3592 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3593 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3594 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3595 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3596
3597 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3598 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3599 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3600 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3601 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3602
3603 // Toggle scroll lock on and off.
3604 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3605 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3606 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3607 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3608 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3609
3610 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3611 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3612 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3613 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3614 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3615}
3616
Arthur Hung2141d542022-08-23 07:45:21 +00003617TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3618 const int32_t USAGE_A = 0x070004;
3619 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3620 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3621
3622 KeyboardInputMapper& mapper =
3623 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3624 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3625 // Key down by scan code.
3626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3627 NotifyKeyArgs args;
3628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3629 ASSERT_EQ(DEVICE_ID, args.deviceId);
3630 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3631 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3632 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3633 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3634 ASSERT_EQ(KEY_HOME, args.scanCode);
3635 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3636
3637 // Disable device, it should synthesize cancellation events for down events.
3638 mFakePolicy->addDisabledDevice(DEVICE_ID);
3639 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
3640
3641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3642 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3643 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3644 ASSERT_EQ(KEY_HOME, args.scanCode);
3645 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3646}
3647
Zixuan Qufecb6062022-11-12 04:44:31 +00003648TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
3649 mDevice->addMapper<KeyboardInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3650 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3651 std::list<NotifyArgs> unused =
3652 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
3653
3654 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3655
3656 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3657 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUT_ASSOCIATION);
3658
3659 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3660 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3661 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3662 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3663 deviceInfo.getKeyboardLayoutInfo()->layoutType);
3664}
3665
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003666TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3667 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3668 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3669
3670 // Configuration
3671 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3672 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3673 InputReaderConfiguration config;
3674 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
3675
3676 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3677 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3678}
3679
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003680// --- KeyboardInputMapperTest_ExternalDevice ---
3681
3682class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3683protected:
Chris Yea52ade12020-08-27 16:49:20 -07003684 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003685};
3686
3687TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003688 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3689 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003690
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003691 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3692 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3693 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3694 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003695
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003696 KeyboardInputMapper& mapper =
3697 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3698 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003699
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003701 NotifyKeyArgs args;
3702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3703 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3704
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003705 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3707 ASSERT_EQ(uint32_t(0), args.policyFlags);
3708
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003709 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003711 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003712
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003713 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3715 ASSERT_EQ(uint32_t(0), args.policyFlags);
3716
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003717 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3719 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3720
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003721 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3723 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3724}
3725
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003726TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003727 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003728
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003729 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3730 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3731 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003732
Powei Fengd041c5d2019-05-03 17:11:33 -07003733 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003734 KeyboardInputMapper& mapper =
3735 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3736 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003737
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003739 NotifyKeyArgs args;
3740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3741 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3742
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003743 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3745 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3746
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3749 ASSERT_EQ(uint32_t(0), args.policyFlags);
3750
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003751 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3753 ASSERT_EQ(uint32_t(0), args.policyFlags);
3754
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003755 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3757 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3758
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003759 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3761 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3762}
3763
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764// --- CursorInputMapperTest ---
3765
3766class CursorInputMapperTest : public InputMapperTest {
3767protected:
3768 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3769
Michael Wright17db18e2020-06-26 20:51:44 +01003770 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771
Chris Yea52ade12020-08-27 16:49:20 -07003772 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773 InputMapperTest::SetUp();
3774
Michael Wright17db18e2020-06-26 20:51:44 +01003775 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003776 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003777 }
3778
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003779 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3780 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003781
Michael Wrighta9cf4192022-12-01 23:46:39 +00003782 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003783 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3784 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3785 }
3786
3787 void prepareSecondaryDisplay() {
3788 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003789 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003790 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003791 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003792
3793 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3794 float pressure) {
3795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3796 0.0f, 0.0f, 0.0f, EPSILON));
3797 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003798};
3799
3800const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3801
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003802void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3803 int32_t originalY, int32_t rotatedX,
3804 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003805 NotifyMotionArgs args;
3806
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003812 ASSERT_NO_FATAL_FAILURE(
3813 assertCursorPointerCoords(args.pointerCoords[0],
3814 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3815 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003816}
3817
3818TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003819 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003820 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003821
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003822 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003823}
3824
3825TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003826 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003827 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003828
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003829 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003830}
3831
3832TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003833 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003834 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003835
3836 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003837 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003838
3839 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003840 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3841 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003842 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3843 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3844
3845 // When the bounds are set, then there should be a valid motion range.
3846 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3847
3848 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003849 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003850
3851 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3852 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3853 1, 800 - 1, 0.0f, 0.0f));
3854 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3855 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3856 2, 480 - 1, 0.0f, 0.0f));
3857 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3858 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3859 0.0f, 1.0f, 0.0f, 0.0f));
3860}
3861
3862TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003864 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865
3866 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00003867 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003868
3869 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3870 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3871 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3872 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3873 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3874 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3875 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3876 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3877 0.0f, 1.0f, 0.0f, 0.0f));
3878}
3879
3880TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003882 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003883
arthurhungdcef2dc2020-08-11 14:47:50 +08003884 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003885
3886 NotifyMotionArgs args;
3887
3888 // Button press.
3889 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003890 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3893 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3894 ASSERT_EQ(DEVICE_ID, args.deviceId);
3895 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3896 ASSERT_EQ(uint32_t(0), args.policyFlags);
3897 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3898 ASSERT_EQ(0, args.flags);
3899 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3900 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3901 ASSERT_EQ(0, args.edgeFlags);
3902 ASSERT_EQ(uint32_t(1), args.pointerCount);
3903 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003904 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003905 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3907 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3908 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3909
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3911 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3912 ASSERT_EQ(DEVICE_ID, args.deviceId);
3913 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3914 ASSERT_EQ(uint32_t(0), args.policyFlags);
3915 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3916 ASSERT_EQ(0, args.flags);
3917 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3918 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3919 ASSERT_EQ(0, args.edgeFlags);
3920 ASSERT_EQ(uint32_t(1), args.pointerCount);
3921 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003922 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003923 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003924 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3925 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3926 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3927
Michael Wrightd02c5b62014-02-10 15:10:22 -08003928 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003929 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3930 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3932 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3933 ASSERT_EQ(DEVICE_ID, args.deviceId);
3934 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3935 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003936 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3937 ASSERT_EQ(0, args.flags);
3938 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3939 ASSERT_EQ(0, args.buttonState);
3940 ASSERT_EQ(0, args.edgeFlags);
3941 ASSERT_EQ(uint32_t(1), args.pointerCount);
3942 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003943 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003944 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003945 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3946 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3947 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3948
3949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3950 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3951 ASSERT_EQ(DEVICE_ID, args.deviceId);
3952 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3953 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003954 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3955 ASSERT_EQ(0, args.flags);
3956 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3957 ASSERT_EQ(0, args.buttonState);
3958 ASSERT_EQ(0, args.edgeFlags);
3959 ASSERT_EQ(uint32_t(1), args.pointerCount);
3960 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07003961 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003962 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003963 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3964 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3965 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3966}
3967
3968TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003970 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003971
3972 NotifyMotionArgs args;
3973
3974 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3978 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003979 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3980 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
3981 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982
3983 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3985 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3987 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003988 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
3989 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003990}
3991
3992TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003993 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003994 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003995
3996 NotifyMotionArgs args;
3997
3998 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4002 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004003 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004004
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4006 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004007 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004008
Michael Wrightd02c5b62014-02-10 15:10:22 -08004009 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004013 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004014 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004015
4016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004018 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019}
4020
4021TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004023 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004024
4025 NotifyMotionArgs args;
4026
4027 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4033 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004034 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4035 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4036 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4039 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004040 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4041 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4042 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004043
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4046 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004050 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4051 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4052 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004053
4054 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004055 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004058 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004059 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004060
4061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004063 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064}
4065
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004066TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004067 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004069 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4070 // need to be rotated.
4071 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004072 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073
Michael Wrighta9cf4192022-12-01 23:46:39 +00004074 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4076 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4077 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4078 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4079 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4080 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4081 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4082 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4083}
4084
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004085TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004086 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004088 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4089 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004090 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004092 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004093 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4095 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4096 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4097 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4098 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4099 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4100 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4101 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4102
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004103 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004104 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004105 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4106 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4107 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4108 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4109 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4110 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4111 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4112 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004114 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004115 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004116 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4117 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4118 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4119 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4120 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4121 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4122 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4123 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4124
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004125 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004126 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4128 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4129 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4130 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4133 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4134 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004135}
4136
4137TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004139 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140
4141 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4142 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143
4144 NotifyMotionArgs motionArgs;
4145 NotifyKeyArgs keyArgs;
4146
4147 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4151 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4152 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004153 ASSERT_NO_FATAL_FAILURE(
4154 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4157 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4158 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004159 ASSERT_NO_FATAL_FAILURE(
4160 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004161
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004165 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004167 ASSERT_NO_FATAL_FAILURE(
4168 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004169
4170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004171 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004173 ASSERT_NO_FATAL_FAILURE(
4174 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004175
4176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004178 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004179 ASSERT_NO_FATAL_FAILURE(
4180 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181
4182 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4187 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4188 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004189 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004190 ASSERT_NO_FATAL_FAILURE(
4191 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004192
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4194 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4195 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004196 ASSERT_NO_FATAL_FAILURE(
4197 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004198
4199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4200 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4201 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004202 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004203 ASSERT_NO_FATAL_FAILURE(
4204 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004205
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004209 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004211 ASSERT_NO_FATAL_FAILURE(
4212 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004213
4214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004216 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004217 ASSERT_NO_FATAL_FAILURE(
4218 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4221 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004223 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4224 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004225 ASSERT_NO_FATAL_FAILURE(
4226 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004229
4230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004233 ASSERT_NO_FATAL_FAILURE(
4234 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004235
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4237 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004239 ASSERT_NO_FATAL_FAILURE(
4240 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241
4242 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004243 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4246 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4247 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004248
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004250 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004252 ASSERT_NO_FATAL_FAILURE(
4253 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004254
4255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4256 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4257 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004258 ASSERT_NO_FATAL_FAILURE(
4259 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004264 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004265 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004266 ASSERT_NO_FATAL_FAILURE(
4267 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004268
4269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004271 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004272
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004273 ASSERT_NO_FATAL_FAILURE(
4274 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4276 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4277 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4278
4279 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4283 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4284 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004285
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004287 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004289 ASSERT_NO_FATAL_FAILURE(
4290 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004291
4292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4294 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004295 ASSERT_NO_FATAL_FAILURE(
4296 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004301 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004303 ASSERT_NO_FATAL_FAILURE(
4304 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004305
4306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4307 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4308 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004309 ASSERT_NO_FATAL_FAILURE(
4310 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004311
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4313 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4314 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4315
4316 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4320 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4321 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004322
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004324 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004326 ASSERT_NO_FATAL_FAILURE(
4327 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004328
4329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4330 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4331 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004332 ASSERT_NO_FATAL_FAILURE(
4333 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004338 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004340 ASSERT_NO_FATAL_FAILURE(
4341 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004342
4343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4344 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4345 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004346 ASSERT_NO_FATAL_FAILURE(
4347 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004348
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4350 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4351 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4352
4353 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4357 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4358 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004359
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004361 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004363 ASSERT_NO_FATAL_FAILURE(
4364 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004365
4366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4367 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4368 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004369 ASSERT_NO_FATAL_FAILURE(
4370 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004375 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004377 ASSERT_NO_FATAL_FAILURE(
4378 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004379
4380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4381 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4382 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004383 ASSERT_NO_FATAL_FAILURE(
4384 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004385
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4387 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4388 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4389}
4390
4391TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004393 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004394
4395 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4396 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397
4398 NotifyMotionArgs args;
4399
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004404 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4405 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4407 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 +00004408 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004409}
4410
4411TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004412 addConfigurationProperty("cursor.mode", "pointer");
4413 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004414 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004415
4416 NotifyDeviceResetArgs resetArgs;
4417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4418 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4419 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4420
4421 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4422 mFakePointerController->setPosition(100, 200);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004423
4424 NotifyMotionArgs args;
4425
4426 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4431 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4432 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4434 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 +00004435 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004436
4437 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4441 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4442 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4444 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4446 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4447 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4449 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4450
4451 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004452 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4453 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4455 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4456 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4458 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4460 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4461 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4463 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4464
4465 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4470 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4473 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 +00004474 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004475
4476 // Disable pointer capture and check that the device generation got bumped
4477 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004478 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004479 mFakePolicy->setPointerCapture(false);
4480 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004481 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004482
4483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004484 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4485
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004486 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4487 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4490 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4493 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 +00004494 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495}
4496
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004497/**
4498 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4499 * pointer acceleration or speed processing should not be applied.
4500 */
4501TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4502 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004503 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4504 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004505 mFakePolicy->setVelocityControlParams(testParams);
4506 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4507
4508 NotifyDeviceResetArgs resetArgs;
4509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4510 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4511 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4512
4513 NotifyMotionArgs args;
4514
4515 // Move and verify scale is applied.
4516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4520 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4521 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4522 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4523 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4524 ASSERT_GT(relX, 10);
4525 ASSERT_GT(relY, 20);
4526
4527 // Enable Pointer Capture
4528 mFakePolicy->setPointerCapture(true);
4529 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4530 NotifyPointerCaptureChangedArgs captureArgs;
4531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4532 ASSERT_TRUE(captureArgs.request.enable);
4533
4534 // Move and verify scale is not applied.
4535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4539 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4540 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4541 ASSERT_EQ(10, args.pointerCoords[0].getX());
4542 ASSERT_EQ(20, args.pointerCoords[0].getY());
4543}
4544
Prabir Pradhan208360b2022-06-24 18:37:04 +00004545TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4546 addConfigurationProperty("cursor.mode", "pointer");
4547 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4548
4549 NotifyDeviceResetArgs resetArgs;
4550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4551 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4552 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4553
4554 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004555 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004556
4557 NotifyMotionArgs args;
4558
4559 // Verify that the coordinates are rotated.
4560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4561 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4564 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4565 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4566 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4567 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4568
4569 // Enable Pointer Capture.
4570 mFakePolicy->setPointerCapture(true);
4571 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4572 NotifyPointerCaptureChangedArgs captureArgs;
4573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4574 ASSERT_TRUE(captureArgs.request.enable);
4575
4576 // Move and verify rotation is not applied.
4577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4579 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4581 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4583 ASSERT_EQ(10, args.pointerCoords[0].getX());
4584 ASSERT_EQ(20, args.pointerCoords[0].getY());
4585}
4586
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004587TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004588 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004589
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004590 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004591 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004592
4593 // Set up the secondary display as the display on which the pointer should be shown.
4594 // The InputDevice is not associated with any display.
4595 prepareSecondaryDisplay();
4596 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08004597 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4598
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004599 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004600 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004601
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004602 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004603 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4604 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004607 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4608 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4609 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004610 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004611}
4612
4613TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4614 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4615
4616 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004617 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004618
4619 // Set up the secondary display as the display on which the pointer should be shown,
4620 // and associate the InputDevice with the secondary display.
4621 prepareSecondaryDisplay();
4622 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4623 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4624 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4625
4626 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4627 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004628
4629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004633 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4634 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4635 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004636 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004637}
4638
4639TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4640 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4641
4642 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004643 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004644 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4645
4646 // Associate the InputDevice with the secondary display.
4647 prepareSecondaryDisplay();
4648 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4649 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4650
4651 // The mapper should not generate any events because it is associated with a display that is
4652 // different from the pointer display.
4653 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004657}
4658
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004659// --- BluetoothCursorInputMapperTest ---
4660
4661class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4662protected:
4663 void SetUp() override {
4664 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4665
4666 mFakePointerController = std::make_shared<FakePointerController>();
4667 mFakePolicy->setPointerController(mFakePointerController);
4668 }
4669};
4670
4671TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4672 addConfigurationProperty("cursor.mode", "pointer");
4673 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4674
4675 nsecs_t kernelEventTime = ARBITRARY_TIME;
4676 nsecs_t expectedEventTime = ARBITRARY_TIME;
4677 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4678 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4680 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4681 WithEventTime(expectedEventTime))));
4682
4683 // Process several events that come in quick succession, according to their timestamps.
4684 for (int i = 0; i < 3; i++) {
4685 constexpr static nsecs_t delta = ms2ns(1);
4686 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4687 kernelEventTime += delta;
4688 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4689
4690 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4691 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4693 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4694 WithEventTime(expectedEventTime))));
4695 }
4696}
4697
4698TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4699 addConfigurationProperty("cursor.mode", "pointer");
4700 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4701
4702 nsecs_t expectedEventTime = ARBITRARY_TIME;
4703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4706 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4707 WithEventTime(expectedEventTime))));
4708
4709 // Process several events with the same timestamp from the kernel.
4710 // Ensure that we do not generate events too far into the future.
4711 constexpr static int32_t numEvents =
4712 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4713 for (int i = 0; i < numEvents; i++) {
4714 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4715
4716 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4717 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4719 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4720 WithEventTime(expectedEventTime))));
4721 }
4722
4723 // By processing more events with the same timestamp, we should not generate events with a
4724 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4725 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4726 for (int i = 0; i < 3; i++) {
4727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4730 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4731 WithEventTime(cappedEventTime))));
4732 }
4733}
4734
4735TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4736 addConfigurationProperty("cursor.mode", "pointer");
4737 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4738
4739 nsecs_t kernelEventTime = ARBITRARY_TIME;
4740 nsecs_t expectedEventTime = ARBITRARY_TIME;
4741 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4742 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4744 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4745 WithEventTime(expectedEventTime))));
4746
4747 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4748 // smoothening is not needed, its timestamp is not affected.
4749 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4750 expectedEventTime = kernelEventTime;
4751
4752 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4753 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4755 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4756 WithEventTime(expectedEventTime))));
4757}
4758
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759// --- TouchInputMapperTest ---
4760
4761class TouchInputMapperTest : public InputMapperTest {
4762protected:
4763 static const int32_t RAW_X_MIN;
4764 static const int32_t RAW_X_MAX;
4765 static const int32_t RAW_Y_MIN;
4766 static const int32_t RAW_Y_MAX;
4767 static const int32_t RAW_TOUCH_MIN;
4768 static const int32_t RAW_TOUCH_MAX;
4769 static const int32_t RAW_TOOL_MIN;
4770 static const int32_t RAW_TOOL_MAX;
4771 static const int32_t RAW_PRESSURE_MIN;
4772 static const int32_t RAW_PRESSURE_MAX;
4773 static const int32_t RAW_ORIENTATION_MIN;
4774 static const int32_t RAW_ORIENTATION_MAX;
4775 static const int32_t RAW_DISTANCE_MIN;
4776 static const int32_t RAW_DISTANCE_MAX;
4777 static const int32_t RAW_TILT_MIN;
4778 static const int32_t RAW_TILT_MAX;
4779 static const int32_t RAW_ID_MIN;
4780 static const int32_t RAW_ID_MAX;
4781 static const int32_t RAW_SLOT_MIN;
4782 static const int32_t RAW_SLOT_MAX;
4783 static const float X_PRECISION;
4784 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004785 static const float X_PRECISION_VIRTUAL;
4786 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787
4788 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004789 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790
4791 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4792
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004793 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004794 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004795
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796 enum Axes {
4797 POSITION = 1 << 0,
4798 TOUCH = 1 << 1,
4799 TOOL = 1 << 2,
4800 PRESSURE = 1 << 3,
4801 ORIENTATION = 1 << 4,
4802 MINOR = 1 << 5,
4803 ID = 1 << 6,
4804 DISTANCE = 1 << 7,
4805 TILT = 1 << 8,
4806 SLOT = 1 << 9,
4807 TOOL_TYPE = 1 << 10,
4808 };
4809
Michael Wrighta9cf4192022-12-01 23:46:39 +00004810 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004811 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004812 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004814 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815 int32_t toRawX(float displayX);
4816 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004817 int32_t toRotatedRawX(float displayX);
4818 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004819 float toCookedX(float rawX, float rawY);
4820 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004822 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004824 float toDisplayY(int32_t rawY, int32_t displayHeight);
4825
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826};
4827
4828const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4829const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4830const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4831const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4832const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4833const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4834const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4835const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004836const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4837const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4839const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4840const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4841const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4842const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4843const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4844const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4845const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4846const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4847const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4848const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4849const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004850const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4851 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4852const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4853 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004854const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4855 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004856
4857const float TouchInputMapperTest::GEOMETRIC_SCALE =
4858 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4859 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4860
4861const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4862 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4863 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4864};
4865
Michael Wrighta9cf4192022-12-01 23:46:39 +00004866void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004867 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4868 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004869}
4870
4871void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4872 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004873 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874}
4875
Michael Wrighta9cf4192022-12-01 23:46:39 +00004876void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004877 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4878 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4879 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004880}
4881
Michael Wrightd02c5b62014-02-10 15:10:22 -08004882void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004883 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4884 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4885 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4886 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004887}
4888
Jason Gerecke489fda82012-09-07 17:19:40 -07004889void TouchInputMapperTest::prepareLocationCalibration() {
4890 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4891}
4892
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893int32_t TouchInputMapperTest::toRawX(float displayX) {
4894 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4895}
4896
4897int32_t TouchInputMapperTest::toRawY(float displayY) {
4898 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4899}
4900
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004901int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4902 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4903}
4904
4905int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4906 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4907}
4908
Jason Gerecke489fda82012-09-07 17:19:40 -07004909float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4910 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4911 return rawX;
4912}
4913
4914float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4915 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4916 return rawY;
4917}
4918
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004920 return toDisplayX(rawX, DISPLAY_WIDTH);
4921}
4922
4923float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4924 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925}
4926
4927float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004928 return toDisplayY(rawY, DISPLAY_HEIGHT);
4929}
4930
4931float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4932 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933}
4934
4935
4936// --- SingleTouchInputMapperTest ---
4937
4938class SingleTouchInputMapperTest : public TouchInputMapperTest {
4939protected:
4940 void prepareButtons();
4941 void prepareAxes(int axes);
4942
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004943 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4944 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4945 void processUp(SingleTouchInputMapper& mappery);
4946 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4947 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4948 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4949 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4950 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4951 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952};
4953
4954void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004955 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956}
4957
4958void SingleTouchInputMapperTest::prepareAxes(int axes) {
4959 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004960 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4961 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962 }
4963 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004964 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4965 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966 }
4967 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004968 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4969 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004970 }
4971 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004972 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4973 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974 }
4975 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004976 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4977 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978 }
4979}
4980
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004981void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004982 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004985}
4986
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004987void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004988 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4989 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004990}
4991
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004992void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994}
4995
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004996void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998}
4999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005000void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5001 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005003}
5004
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005005void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005006 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005007}
5008
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005009void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5010 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013}
5014
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005015void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5016 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005018}
5019
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005020void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022}
5023
Michael Wrightd02c5b62014-02-10 15:10:22 -08005024TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005025 prepareButtons();
5026 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005027 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005028
Josep del Río2d8c79a2023-01-23 19:33:50 +00005029 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030}
5031
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033 prepareButtons();
5034 prepareAxes(POSITION);
5035 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005036 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005037
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005038 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039}
5040
5041TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005043 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005044 prepareButtons();
5045 prepareAxes(POSITION);
5046 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005047 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005048
5049 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005050 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005051
5052 // Virtual key is down.
5053 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5054 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5055 processDown(mapper, x, y);
5056 processSync(mapper);
5057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005059 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060
5061 // Virtual key is up.
5062 processUp(mapper);
5063 processSync(mapper);
5064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5065
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005066 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005067}
5068
5069TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005071 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072 prepareButtons();
5073 prepareAxes(POSITION);
5074 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005075 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005076
5077 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005078 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079
5080 // Virtual key is down.
5081 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5082 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5083 processDown(mapper, x, y);
5084 processSync(mapper);
5085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5086
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005087 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088
5089 // Virtual key is up.
5090 processUp(mapper);
5091 processSync(mapper);
5092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5093
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005094 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095}
5096
5097TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005098 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005099 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100 prepareButtons();
5101 prepareAxes(POSITION);
5102 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005103 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005104
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005106 ASSERT_TRUE(
5107 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108 ASSERT_TRUE(flags[0]);
5109 ASSERT_FALSE(flags[1]);
5110}
5111
5112TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005114 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115 prepareButtons();
5116 prepareAxes(POSITION);
5117 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005118 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119
arthurhungdcef2dc2020-08-11 14:47:50 +08005120 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005121
5122 NotifyKeyArgs args;
5123
5124 // Press virtual key.
5125 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5126 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5127 processDown(mapper, x, y);
5128 processSync(mapper);
5129
5130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5131 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5132 ASSERT_EQ(DEVICE_ID, args.deviceId);
5133 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5134 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5135 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5136 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5137 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5138 ASSERT_EQ(KEY_HOME, args.scanCode);
5139 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5140 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5141
5142 // Release virtual key.
5143 processUp(mapper);
5144 processSync(mapper);
5145
5146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5147 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5148 ASSERT_EQ(DEVICE_ID, args.deviceId);
5149 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5150 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5151 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5152 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5153 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5154 ASSERT_EQ(KEY_HOME, args.scanCode);
5155 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5156 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5157
5158 // Should not have sent any motions.
5159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5160}
5161
5162TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005163 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005164 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005165 prepareButtons();
5166 prepareAxes(POSITION);
5167 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005168 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169
arthurhungdcef2dc2020-08-11 14:47:50 +08005170 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005171
5172 NotifyKeyArgs keyArgs;
5173
5174 // Press virtual key.
5175 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5176 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5177 processDown(mapper, x, y);
5178 processSync(mapper);
5179
5180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5181 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5182 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5183 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5184 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5185 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5186 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5187 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5188 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5189 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5190 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5191
5192 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5193 // into the display area.
5194 y -= 100;
5195 processMove(mapper, x, y);
5196 processSync(mapper);
5197
5198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5199 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5200 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5201 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5202 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5203 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5204 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5205 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5206 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5207 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5208 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5209 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5210
5211 NotifyMotionArgs motionArgs;
5212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5213 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5214 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5215 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5216 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5217 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5218 ASSERT_EQ(0, motionArgs.flags);
5219 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5220 ASSERT_EQ(0, motionArgs.buttonState);
5221 ASSERT_EQ(0, motionArgs.edgeFlags);
5222 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5223 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005224 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5226 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5227 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5228 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5229 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5230
5231 // Keep moving out of bounds. Should generate a pointer move.
5232 y -= 50;
5233 processMove(mapper, x, y);
5234 processSync(mapper);
5235
5236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5237 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5238 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5239 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5240 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5242 ASSERT_EQ(0, motionArgs.flags);
5243 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5244 ASSERT_EQ(0, motionArgs.buttonState);
5245 ASSERT_EQ(0, motionArgs.edgeFlags);
5246 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5247 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005248 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5250 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5251 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5252 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5253 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5254
5255 // Release out of bounds. Should generate a pointer up.
5256 processUp(mapper);
5257 processSync(mapper);
5258
5259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5260 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5261 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5262 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5263 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5264 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5265 ASSERT_EQ(0, motionArgs.flags);
5266 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5267 ASSERT_EQ(0, motionArgs.buttonState);
5268 ASSERT_EQ(0, motionArgs.edgeFlags);
5269 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5270 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005271 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5273 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5274 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5275 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5276 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5277
5278 // Should not have sent any more keys or motions.
5279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5281}
5282
5283TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005284 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005285 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005286 prepareButtons();
5287 prepareAxes(POSITION);
5288 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005289 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290
arthurhungdcef2dc2020-08-11 14:47:50 +08005291 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005292
5293 NotifyMotionArgs motionArgs;
5294
5295 // Initially go down out of bounds.
5296 int32_t x = -10;
5297 int32_t y = -10;
5298 processDown(mapper, x, y);
5299 processSync(mapper);
5300
5301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5302
5303 // Move into the display area. Should generate a pointer down.
5304 x = 50;
5305 y = 75;
5306 processMove(mapper, x, y);
5307 processSync(mapper);
5308
5309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5310 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5311 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5312 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5313 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5314 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5315 ASSERT_EQ(0, motionArgs.flags);
5316 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5317 ASSERT_EQ(0, motionArgs.buttonState);
5318 ASSERT_EQ(0, motionArgs.edgeFlags);
5319 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5320 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005321 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5323 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5324 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5325 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5326 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5327
5328 // Release. Should generate a pointer up.
5329 processUp(mapper);
5330 processSync(mapper);
5331
5332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5333 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5334 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5335 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5336 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5337 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5338 ASSERT_EQ(0, motionArgs.flags);
5339 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5340 ASSERT_EQ(0, motionArgs.buttonState);
5341 ASSERT_EQ(0, motionArgs.edgeFlags);
5342 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5343 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005344 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5346 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5347 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5348 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5349 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5350
5351 // Should not have sent any more keys or motions.
5352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5354}
5355
Santos Cordonfa5cf462017-04-05 10:37:00 -07005356TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005357 addConfigurationProperty("touch.deviceType", "touchScreen");
5358 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5359
Michael Wrighta9cf4192022-12-01 23:46:39 +00005360 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005361 prepareButtons();
5362 prepareAxes(POSITION);
5363 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005364 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005365
arthurhungdcef2dc2020-08-11 14:47:50 +08005366 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005367
5368 NotifyMotionArgs motionArgs;
5369
5370 // Down.
5371 int32_t x = 100;
5372 int32_t y = 125;
5373 processDown(mapper, x, y);
5374 processSync(mapper);
5375
5376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5377 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5378 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5379 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5380 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5381 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5382 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5383 ASSERT_EQ(0, motionArgs.flags);
5384 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5385 ASSERT_EQ(0, motionArgs.buttonState);
5386 ASSERT_EQ(0, motionArgs.edgeFlags);
5387 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5388 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005389 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5391 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5392 1, 0, 0, 0, 0, 0, 0, 0));
5393 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5394 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5395 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5396
5397 // Move.
5398 x += 50;
5399 y += 75;
5400 processMove(mapper, x, y);
5401 processSync(mapper);
5402
5403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5404 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5405 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5406 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5407 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5408 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5410 ASSERT_EQ(0, motionArgs.flags);
5411 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5412 ASSERT_EQ(0, motionArgs.buttonState);
5413 ASSERT_EQ(0, motionArgs.edgeFlags);
5414 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5415 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005416 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5418 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5419 1, 0, 0, 0, 0, 0, 0, 0));
5420 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5421 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5422 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5423
5424 // Up.
5425 processUp(mapper);
5426 processSync(mapper);
5427
5428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5429 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5430 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5431 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5432 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5433 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5434 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5435 ASSERT_EQ(0, motionArgs.flags);
5436 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5437 ASSERT_EQ(0, motionArgs.buttonState);
5438 ASSERT_EQ(0, motionArgs.edgeFlags);
5439 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5440 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005441 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5443 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5444 1, 0, 0, 0, 0, 0, 0, 0));
5445 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5446 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5447 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5448
5449 // Should not have sent any more keys or motions.
5450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5452}
5453
Michael Wrightd02c5b62014-02-10 15:10:22 -08005454TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005456 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005457 prepareButtons();
5458 prepareAxes(POSITION);
5459 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005460 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461
arthurhungdcef2dc2020-08-11 14:47:50 +08005462 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005463
5464 NotifyMotionArgs motionArgs;
5465
5466 // Down.
5467 int32_t x = 100;
5468 int32_t y = 125;
5469 processDown(mapper, x, y);
5470 processSync(mapper);
5471
5472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5473 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5474 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5475 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5476 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5477 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5478 ASSERT_EQ(0, motionArgs.flags);
5479 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5480 ASSERT_EQ(0, motionArgs.buttonState);
5481 ASSERT_EQ(0, motionArgs.edgeFlags);
5482 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5483 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005484 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5486 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5487 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5488 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5489 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5490
5491 // Move.
5492 x += 50;
5493 y += 75;
5494 processMove(mapper, x, y);
5495 processSync(mapper);
5496
5497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5498 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5499 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5500 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5501 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5503 ASSERT_EQ(0, motionArgs.flags);
5504 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5505 ASSERT_EQ(0, motionArgs.buttonState);
5506 ASSERT_EQ(0, motionArgs.edgeFlags);
5507 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5508 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005509 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5511 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5512 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5513 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5514 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5515
5516 // Up.
5517 processUp(mapper);
5518 processSync(mapper);
5519
5520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5521 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5522 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5523 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5524 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5525 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5526 ASSERT_EQ(0, motionArgs.flags);
5527 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5528 ASSERT_EQ(0, motionArgs.buttonState);
5529 ASSERT_EQ(0, motionArgs.edgeFlags);
5530 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5531 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005532 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5534 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5535 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5536 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5537 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5538
5539 // Should not have sent any more keys or motions.
5540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5542}
5543
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005544TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005545 addConfigurationProperty("touch.deviceType", "touchScreen");
5546 prepareButtons();
5547 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005548 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5549 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005550 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005551
5552 NotifyMotionArgs args;
5553
5554 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005555 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556 processDown(mapper, toRawX(50), toRawY(75));
5557 processSync(mapper);
5558
5559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5560 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5561 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5562
5563 processUp(mapper);
5564 processSync(mapper);
5565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5566}
5567
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005568TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005569 addConfigurationProperty("touch.deviceType", "touchScreen");
5570 prepareButtons();
5571 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005572 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5573 // orientation-aware are affected by display rotation.
5574 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005575 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005576
5577 NotifyMotionArgs args;
5578
5579 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005580 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005581 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005582 processDown(mapper, toRawX(50), toRawY(75));
5583 processSync(mapper);
5584
5585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5586 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5587 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5588
5589 processUp(mapper);
5590 processSync(mapper);
5591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5592
5593 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005594 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005595 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005596 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005597 processSync(mapper);
5598
5599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5600 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5601 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5602
5603 processUp(mapper);
5604 processSync(mapper);
5605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5606
5607 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005608 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005609 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005610 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5611 processSync(mapper);
5612
5613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5614 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5615 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5616
5617 processUp(mapper);
5618 processSync(mapper);
5619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5620
5621 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005622 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005623 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005624 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005625 processSync(mapper);
5626
5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5628 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5629 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5630
5631 processUp(mapper);
5632 processSync(mapper);
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5634}
5635
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005636TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5637 addConfigurationProperty("touch.deviceType", "touchScreen");
5638 prepareButtons();
5639 prepareAxes(POSITION);
5640 addConfigurationProperty("touch.orientationAware", "1");
5641 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5642 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005643 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005644 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5645 NotifyMotionArgs args;
5646
5647 // Orientation 0.
5648 processDown(mapper, toRawX(50), toRawY(75));
5649 processSync(mapper);
5650
5651 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5652 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5653 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5654
5655 processUp(mapper);
5656 processSync(mapper);
5657 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5658}
5659
5660TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5661 addConfigurationProperty("touch.deviceType", "touchScreen");
5662 prepareButtons();
5663 prepareAxes(POSITION);
5664 addConfigurationProperty("touch.orientationAware", "1");
5665 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5666 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005667 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005668 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5669 NotifyMotionArgs args;
5670
5671 // Orientation 90.
5672 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5673 processSync(mapper);
5674
5675 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5676 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5677 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5678
5679 processUp(mapper);
5680 processSync(mapper);
5681 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5682}
5683
5684TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5685 addConfigurationProperty("touch.deviceType", "touchScreen");
5686 prepareButtons();
5687 prepareAxes(POSITION);
5688 addConfigurationProperty("touch.orientationAware", "1");
5689 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5690 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005691 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005692 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5693 NotifyMotionArgs args;
5694
5695 // Orientation 180.
5696 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5697 processSync(mapper);
5698
5699 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5700 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5701 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5702
5703 processUp(mapper);
5704 processSync(mapper);
5705 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5706}
5707
5708TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5709 addConfigurationProperty("touch.deviceType", "touchScreen");
5710 prepareButtons();
5711 prepareAxes(POSITION);
5712 addConfigurationProperty("touch.orientationAware", "1");
5713 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5714 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005715 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005716 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5717 NotifyMotionArgs args;
5718
5719 // Orientation 270.
5720 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5721 processSync(mapper);
5722
5723 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5724 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5725 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5726
5727 processUp(mapper);
5728 processSync(mapper);
5729 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5730}
5731
5732TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5733 addConfigurationProperty("touch.deviceType", "touchScreen");
5734 prepareButtons();
5735 prepareAxes(POSITION);
5736 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5737 // orientation-aware are affected by display rotation.
5738 addConfigurationProperty("touch.orientationAware", "0");
5739 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5740 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5741
5742 NotifyMotionArgs args;
5743
5744 // Orientation 90, Rotation 0.
5745 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005746 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005747 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5748 processSync(mapper);
5749
5750 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5751 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5752 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5753
5754 processUp(mapper);
5755 processSync(mapper);
5756 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5757
5758 // Orientation 90, Rotation 90.
5759 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005760 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005761 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005762 processSync(mapper);
5763
5764 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5765 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5766 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5767
5768 processUp(mapper);
5769 processSync(mapper);
5770 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5771
5772 // Orientation 90, Rotation 180.
5773 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005774 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005775 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5776 processSync(mapper);
5777
5778 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5779 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5780 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5781
5782 processUp(mapper);
5783 processSync(mapper);
5784 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5785
5786 // Orientation 90, Rotation 270.
5787 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005788 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005789 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 -07005790 processSync(mapper);
5791
5792 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5793 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5794 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5795
5796 processUp(mapper);
5797 processSync(mapper);
5798 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5799}
5800
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005801TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5802 addConfigurationProperty("touch.deviceType", "touchScreen");
5803 prepareButtons();
5804 prepareAxes(POSITION);
5805 addConfigurationProperty("touch.orientationAware", "1");
5806 prepareDisplay(ui::ROTATION_0);
5807 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5808
5809 // Set a physical frame in the display viewport.
5810 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5811 viewport->physicalLeft = 20;
5812 viewport->physicalTop = 600;
5813 viewport->physicalRight = 30;
5814 viewport->physicalBottom = 610;
5815 mFakePolicy->updateViewport(*viewport);
5816 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5817
5818 // Start the touch.
5819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5820 processSync(mapper);
5821
5822 // Expect all input starting outside the physical frame to be ignored.
5823 const std::array<Point, 6> outsidePoints = {
5824 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5825 for (const auto& p : outsidePoints) {
5826 processMove(mapper, toRawX(p.x), toRawY(p.y));
5827 processSync(mapper);
5828 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5829 }
5830
5831 // Move the touch into the physical frame.
5832 processMove(mapper, toRawX(25), toRawY(605));
5833 processSync(mapper);
5834 NotifyMotionArgs args;
5835 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5836 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5837 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5838 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5839
5840 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5841 for (const auto& p : outsidePoints) {
5842 processMove(mapper, toRawX(p.x), toRawY(p.y));
5843 processSync(mapper);
5844 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5845 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5846 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5847 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5848 }
5849
5850 processUp(mapper);
5851 processSync(mapper);
5852 EXPECT_NO_FATAL_FAILURE(
5853 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5854}
5855
Michael Wrightd02c5b62014-02-10 15:10:22 -08005856TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005857 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005858 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859 prepareButtons();
5860 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005861 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862
5863 // These calculations are based on the input device calibration documentation.
5864 int32_t rawX = 100;
5865 int32_t rawY = 200;
5866 int32_t rawPressure = 10;
5867 int32_t rawToolMajor = 12;
5868 int32_t rawDistance = 2;
5869 int32_t rawTiltX = 30;
5870 int32_t rawTiltY = 110;
5871
5872 float x = toDisplayX(rawX);
5873 float y = toDisplayY(rawY);
5874 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5875 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5876 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5877 float distance = float(rawDistance);
5878
5879 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5880 float tiltScale = M_PI / 180;
5881 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5882 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5883 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5884 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5885
5886 processDown(mapper, rawX, rawY);
5887 processPressure(mapper, rawPressure);
5888 processToolMajor(mapper, rawToolMajor);
5889 processDistance(mapper, rawDistance);
5890 processTilt(mapper, rawTiltX, rawTiltY);
5891 processSync(mapper);
5892
5893 NotifyMotionArgs args;
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5896 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5897 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5898}
5899
Jason Gerecke489fda82012-09-07 17:19:40 -07005900TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005901 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005902 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005903 prepareLocationCalibration();
5904 prepareButtons();
5905 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005906 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005907
5908 int32_t rawX = 100;
5909 int32_t rawY = 200;
5910
5911 float x = toDisplayX(toCookedX(rawX, rawY));
5912 float y = toDisplayY(toCookedY(rawX, rawY));
5913
5914 processDown(mapper, rawX, rawY);
5915 processSync(mapper);
5916
5917 NotifyMotionArgs args;
5918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5920 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5921}
5922
Michael Wrightd02c5b62014-02-10 15:10:22 -08005923TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005924 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005925 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005926 prepareButtons();
5927 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005928 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005929
5930 NotifyMotionArgs motionArgs;
5931 NotifyKeyArgs keyArgs;
5932
5933 processDown(mapper, 100, 200);
5934 processSync(mapper);
5935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5936 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5937 ASSERT_EQ(0, motionArgs.buttonState);
5938
5939 // press BTN_LEFT, release BTN_LEFT
5940 processKey(mapper, BTN_LEFT, 1);
5941 processSync(mapper);
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5944 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5945
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5948 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5949
Michael Wrightd02c5b62014-02-10 15:10:22 -08005950 processKey(mapper, BTN_LEFT, 0);
5951 processSync(mapper);
5952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005953 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005954 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005955
5956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005958 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005959
5960 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5961 processKey(mapper, BTN_RIGHT, 1);
5962 processKey(mapper, BTN_MIDDLE, 1);
5963 processSync(mapper);
5964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5966 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5967 motionArgs.buttonState);
5968
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5970 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5971 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5972
5973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5974 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5975 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5976 motionArgs.buttonState);
5977
Michael Wrightd02c5b62014-02-10 15:10:22 -08005978 processKey(mapper, BTN_RIGHT, 0);
5979 processSync(mapper);
5980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005981 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005983
5984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005985 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005986 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005987
5988 processKey(mapper, BTN_MIDDLE, 0);
5989 processSync(mapper);
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005991 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005992 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005993
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005996 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997
5998 // press BTN_BACK, release BTN_BACK
5999 processKey(mapper, BTN_BACK, 1);
6000 processSync(mapper);
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6002 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6003 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006004
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006007 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6008
6009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6010 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6011 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006012
6013 processKey(mapper, BTN_BACK, 0);
6014 processSync(mapper);
6015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006016 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006017 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006018
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006021 ASSERT_EQ(0, motionArgs.buttonState);
6022
Michael Wrightd02c5b62014-02-10 15:10:22 -08006023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6024 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6025 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6026
6027 // press BTN_SIDE, release BTN_SIDE
6028 processKey(mapper, BTN_SIDE, 1);
6029 processSync(mapper);
6030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6031 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6032 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006033
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006036 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6037
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6039 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6040 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041
6042 processKey(mapper, BTN_SIDE, 0);
6043 processSync(mapper);
6044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006045 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006046 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006047
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006050 ASSERT_EQ(0, motionArgs.buttonState);
6051
Michael Wrightd02c5b62014-02-10 15:10:22 -08006052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6053 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6054 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6055
6056 // press BTN_FORWARD, release BTN_FORWARD
6057 processKey(mapper, BTN_FORWARD, 1);
6058 processSync(mapper);
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6060 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6061 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006062
Michael Wrightd02c5b62014-02-10 15:10:22 -08006063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006065 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6066
6067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6068 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6069 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006070
6071 processKey(mapper, BTN_FORWARD, 0);
6072 processSync(mapper);
6073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006074 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006075 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006076
6077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006078 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006079 ASSERT_EQ(0, motionArgs.buttonState);
6080
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6082 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6083 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6084
6085 // press BTN_EXTRA, release BTN_EXTRA
6086 processKey(mapper, BTN_EXTRA, 1);
6087 processSync(mapper);
6088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6089 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6090 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006091
Michael Wrightd02c5b62014-02-10 15:10:22 -08006092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006093 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006094 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6095
6096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6097 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6098 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006099
6100 processKey(mapper, BTN_EXTRA, 0);
6101 processSync(mapper);
6102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006103 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006104 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006105
6106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006108 ASSERT_EQ(0, motionArgs.buttonState);
6109
Michael Wrightd02c5b62014-02-10 15:10:22 -08006110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6111 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6112 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6113
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6115
Michael Wrightd02c5b62014-02-10 15:10:22 -08006116 // press BTN_STYLUS, release BTN_STYLUS
6117 processKey(mapper, BTN_STYLUS, 1);
6118 processSync(mapper);
6119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006121 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6122
6123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6125 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126
6127 processKey(mapper, BTN_STYLUS, 0);
6128 processSync(mapper);
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006130 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006131 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006132
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006135 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136
6137 // press BTN_STYLUS2, release BTN_STYLUS2
6138 processKey(mapper, BTN_STYLUS2, 1);
6139 processSync(mapper);
6140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006142 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6143
6144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6145 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6146 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006147
6148 processKey(mapper, BTN_STYLUS2, 0);
6149 processSync(mapper);
6150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006151 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006152 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006153
6154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006156 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006157
6158 // release touch
6159 processUp(mapper);
6160 processSync(mapper);
6161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6162 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6163 ASSERT_EQ(0, motionArgs.buttonState);
6164}
6165
6166TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006168 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169 prepareButtons();
6170 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006171 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006172
6173 NotifyMotionArgs motionArgs;
6174
6175 // default tool type is finger
6176 processDown(mapper, 100, 200);
6177 processSync(mapper);
6178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6179 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006180 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006181
6182 // eraser
6183 processKey(mapper, BTN_TOOL_RUBBER, 1);
6184 processSync(mapper);
6185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006187 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006188
6189 // stylus
6190 processKey(mapper, BTN_TOOL_RUBBER, 0);
6191 processKey(mapper, BTN_TOOL_PEN, 1);
6192 processSync(mapper);
6193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006195 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006196
6197 // brush
6198 processKey(mapper, BTN_TOOL_PEN, 0);
6199 processKey(mapper, BTN_TOOL_BRUSH, 1);
6200 processSync(mapper);
6201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006203 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006204
6205 // pencil
6206 processKey(mapper, BTN_TOOL_BRUSH, 0);
6207 processKey(mapper, BTN_TOOL_PENCIL, 1);
6208 processSync(mapper);
6209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006211 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006212
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006213 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214 processKey(mapper, BTN_TOOL_PENCIL, 0);
6215 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6216 processSync(mapper);
6217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006219 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220
6221 // mouse
6222 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6223 processKey(mapper, BTN_TOOL_MOUSE, 1);
6224 processSync(mapper);
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006227 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006228
6229 // lens
6230 processKey(mapper, BTN_TOOL_MOUSE, 0);
6231 processKey(mapper, BTN_TOOL_LENS, 1);
6232 processSync(mapper);
6233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006235 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236
6237 // double-tap
6238 processKey(mapper, BTN_TOOL_LENS, 0);
6239 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6240 processSync(mapper);
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006243 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244
6245 // triple-tap
6246 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6247 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6248 processSync(mapper);
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006251 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252
6253 // quad-tap
6254 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6255 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6256 processSync(mapper);
6257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006259 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006260
6261 // finger
6262 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6263 processKey(mapper, BTN_TOOL_FINGER, 1);
6264 processSync(mapper);
6265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006267 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006268
6269 // stylus trumps finger
6270 processKey(mapper, BTN_TOOL_PEN, 1);
6271 processSync(mapper);
6272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006274 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275
6276 // eraser trumps stylus
6277 processKey(mapper, BTN_TOOL_RUBBER, 1);
6278 processSync(mapper);
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6280 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006281 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282
6283 // mouse trumps eraser
6284 processKey(mapper, BTN_TOOL_MOUSE, 1);
6285 processSync(mapper);
6286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006288 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289
6290 // back to default tool type
6291 processKey(mapper, BTN_TOOL_MOUSE, 0);
6292 processKey(mapper, BTN_TOOL_RUBBER, 0);
6293 processKey(mapper, BTN_TOOL_PEN, 0);
6294 processKey(mapper, BTN_TOOL_FINGER, 0);
6295 processSync(mapper);
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006298 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006299}
6300
6301TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006302 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006303 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 prepareButtons();
6305 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006306 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006307 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308
6309 NotifyMotionArgs motionArgs;
6310
6311 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6312 processKey(mapper, BTN_TOOL_FINGER, 1);
6313 processMove(mapper, 100, 200);
6314 processSync(mapper);
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6316 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6318 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6319
6320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6321 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6323 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6324
6325 // move a little
6326 processMove(mapper, 150, 250);
6327 processSync(mapper);
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6329 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6331 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6332
6333 // down when BTN_TOUCH is pressed, pressure defaults to 1
6334 processKey(mapper, BTN_TOUCH, 1);
6335 processSync(mapper);
6336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6337 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6339 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6340
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6342 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6344 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6345
6346 // up when BTN_TOUCH is released, hover restored
6347 processKey(mapper, BTN_TOUCH, 0);
6348 processSync(mapper);
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6350 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6352 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6353
6354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6355 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6357 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6358
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6360 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6362 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6363
6364 // exit hover when pointer goes away
6365 processKey(mapper, BTN_TOOL_FINGER, 0);
6366 processSync(mapper);
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6368 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6370 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6371}
6372
6373TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006374 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006375 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006376 prepareButtons();
6377 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006378 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006379
6380 NotifyMotionArgs motionArgs;
6381
6382 // initially hovering because pressure is 0
6383 processDown(mapper, 100, 200);
6384 processPressure(mapper, 0);
6385 processSync(mapper);
6386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6387 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6389 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6390
6391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6392 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6394 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6395
6396 // move a little
6397 processMove(mapper, 150, 250);
6398 processSync(mapper);
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6400 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6402 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6403
6404 // down when pressure is non-zero
6405 processPressure(mapper, RAW_PRESSURE_MAX);
6406 processSync(mapper);
6407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6408 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6410 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6411
6412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6413 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6415 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6416
6417 // up when pressure becomes 0, hover restored
6418 processPressure(mapper, 0);
6419 processSync(mapper);
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6421 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6423 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6424
6425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6426 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6428 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6429
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6431 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6433 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6434
6435 // exit hover when pointer goes away
6436 processUp(mapper);
6437 processSync(mapper);
6438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6439 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6441 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6442}
6443
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006444TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6445 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006446 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006447 prepareButtons();
6448 prepareAxes(POSITION | PRESSURE);
6449 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6450
6451 // Touch down.
6452 processDown(mapper, 100, 200);
6453 processPressure(mapper, 1);
6454 processSync(mapper);
6455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6456 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6457
6458 // Reset the mapper. This should cancel the ongoing gesture.
6459 resetMapper(mapper, ARBITRARY_TIME);
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6461 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6462
6463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6464}
6465
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006466TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6467 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006468 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006469 prepareButtons();
6470 prepareAxes(POSITION | PRESSURE);
6471 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6472
6473 // Set the initial state for the touch pointer.
6474 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6475 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6476 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6477 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6478
6479 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006480 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6481 // does not generate any events.
6482 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006483
6484 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6485 // the recreated touch state to generate a down event.
6486 processSync(mapper);
6487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6488 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6489
6490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6491}
6492
lilinnan687e58f2022-07-19 16:00:50 +08006493TEST_F(SingleTouchInputMapperTest,
6494 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6495 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006496 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006497 prepareButtons();
6498 prepareAxes(POSITION);
6499 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6500 NotifyMotionArgs motionArgs;
6501
6502 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006503 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006504 processSync(mapper);
6505
6506 // We should receive a down event
6507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6508 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6509
6510 // Change display id
6511 clearViewports();
6512 prepareSecondaryDisplay(ViewportType::INTERNAL);
6513
6514 // We should receive a cancel event
6515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6516 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6517 // Then receive reset called
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6519}
6520
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006521TEST_F(SingleTouchInputMapperTest,
6522 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6523 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006524 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006525 prepareButtons();
6526 prepareAxes(POSITION);
6527 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6529 NotifyMotionArgs motionArgs;
6530
6531 // Start a new gesture.
6532 processDown(mapper, 100, 200);
6533 processSync(mapper);
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6535 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6536
6537 // Make the viewport inactive. This will put the device in disabled mode.
6538 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6539 viewport->isActive = false;
6540 mFakePolicy->updateViewport(*viewport);
6541 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6542
6543 // We should receive a cancel event for the ongoing gesture.
6544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6545 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6546 // Then we should be notified that the device was reset.
6547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6548
6549 // No events are generated while the viewport is inactive.
6550 processMove(mapper, 101, 201);
6551 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006552 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006553 processSync(mapper);
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6555
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006556 // Start a new gesture while the viewport is still inactive.
6557 processDown(mapper, 300, 400);
6558 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6559 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6560 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6561 processSync(mapper);
6562
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006563 // Make the viewport active again. The device should resume processing events.
6564 viewport->isActive = true;
6565 mFakePolicy->updateViewport(*viewport);
6566 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6567
6568 // The device is reset because it changes back to direct mode, without generating any events.
6569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6571
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006572 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006573 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6575 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006576
6577 // No more events.
6578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6580}
6581
Prabir Pradhan211ba622022-10-31 21:09:21 +00006582TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6583 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006584 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006585 prepareButtons();
6586 prepareAxes(POSITION);
6587 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6589
6590 // Press a stylus button.
6591 processKey(mapper, BTN_STYLUS, 1);
6592 processSync(mapper);
6593
6594 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6595 processDown(mapper, 100, 200);
6596 processSync(mapper);
6597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6598 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6599 WithCoords(toDisplayX(100), toDisplayY(200)),
6600 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6602 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6603 WithCoords(toDisplayX(100), toDisplayY(200)),
6604 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6605
6606 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6607 // the button has not actually been released, since there will be no pointers through which the
6608 // button state can be reported. The event is generated at the location of the pointer before
6609 // it went up.
6610 processUp(mapper);
6611 processSync(mapper);
6612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6613 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6614 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6616 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6617 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6618}
6619
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006620TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6621 addConfigurationProperty("touch.deviceType", "touchScreen");
6622 prepareDisplay(ui::ROTATION_0);
6623 prepareButtons();
6624 prepareAxes(POSITION);
6625
6626 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6627
6628 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6630
6631 // Press a stylus button.
6632 processKey(mapper, BTN_STYLUS, 1);
6633 processSync(mapper);
6634
6635 // Start a touch gesture and ensure that the stylus button is not reported.
6636 processDown(mapper, 100, 200);
6637 processSync(mapper);
6638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6639 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6640
6641 // Release and press the stylus button again.
6642 processKey(mapper, BTN_STYLUS, 0);
6643 processSync(mapper);
6644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6645 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6646 processKey(mapper, BTN_STYLUS, 1);
6647 processSync(mapper);
6648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6649 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6650
6651 // Release the touch gesture.
6652 processUp(mapper);
6653 processSync(mapper);
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6655 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6656
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6658}
6659
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006660TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6661 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6662 prepareDisplay(ui::ROTATION_0);
6663 prepareButtons();
6664 prepareAxes(POSITION);
6665 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6667
6668 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6669}
6670
Seunghwan Choi356026c2023-02-01 14:37:25 +09006671TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6672 std::shared_ptr<FakePointerController> fakePointerController =
6673 std::make_shared<FakePointerController>();
6674 addConfigurationProperty("touch.deviceType", "touchScreen");
6675 prepareDisplay(ui::ROTATION_0);
6676 prepareButtons();
6677 prepareAxes(POSITION);
6678 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6679 mFakePolicy->setPointerController(fakePointerController);
6680 mFakePolicy->setStylusPointerIconEnabled(true);
6681 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6682
6683 processKey(mapper, BTN_TOOL_PEN, 1);
6684 processMove(mapper, 100, 200);
6685 processSync(mapper);
6686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6687 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006688 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006689 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6690 ASSERT_TRUE(fakePointerController->isPointerShown());
6691 ASSERT_NO_FATAL_FAILURE(
6692 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6693}
6694
6695TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6696 std::shared_ptr<FakePointerController> fakePointerController =
6697 std::make_shared<FakePointerController>();
6698 addConfigurationProperty("touch.deviceType", "touchScreen");
6699 prepareDisplay(ui::ROTATION_0);
6700 prepareButtons();
6701 prepareAxes(POSITION);
6702 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6703 mFakePolicy->setPointerController(fakePointerController);
6704 mFakePolicy->setStylusPointerIconEnabled(false);
6705 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6706
6707 processKey(mapper, BTN_TOOL_PEN, 1);
6708 processMove(mapper, 100, 200);
6709 processSync(mapper);
6710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6711 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006712 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006713 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6714 ASSERT_FALSE(fakePointerController->isPointerShown());
6715}
6716
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006717TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6718 // Initialize the device without setting device source to touch navigation.
6719 addConfigurationProperty("touch.deviceType", "touchScreen");
6720 prepareDisplay(ui::ROTATION_0);
6721 prepareButtons();
6722 prepareAxes(POSITION);
6723 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6724
6725 // Ensure that the device is created as a touchscreen, not touch navigation.
6726 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6727
6728 // Add device type association after the device was created.
6729 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6730
6731 // Send update to the mapper.
6732 std::list<NotifyArgs> unused2 =
6733 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6734 InputReaderConfiguration::CHANGE_DEVICE_TYPE /*changes*/);
6735
6736 // Check whether device type update was successful.
6737 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6738}
6739
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006740TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6741 // Initialize the device without setting device source to touch navigation.
6742 addConfigurationProperty("touch.deviceType", "touchScreen");
6743 prepareDisplay(ui::ROTATION_0);
6744 prepareButtons();
6745 prepareAxes(POSITION);
6746 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6747
6748 // Set a physical frame in the display viewport.
6749 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6750 viewport->physicalLeft = 0;
6751 viewport->physicalTop = 0;
6752 viewport->physicalRight = DISPLAY_WIDTH / 2;
6753 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6754 mFakePolicy->updateViewport(*viewport);
6755 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6756
6757 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6758
6759 // Hovering inside the physical frame produces events.
6760 processKey(mapper, BTN_TOOL_PEN, 1);
6761 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6762 processSync(mapper);
6763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6764 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6766 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6767
6768 // Leaving the physical frame ends the hovering gesture.
6769 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6770 processSync(mapper);
6771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6772 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6773
6774 // Moving outside the physical frame does not produce events.
6775 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6776 processSync(mapper);
6777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6778
6779 // Re-entering the physical frame produces events.
6780 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6781 processSync(mapper);
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6783 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6785 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6786}
6787
Prabir Pradhan5632d622021-09-06 07:57:20 -07006788// --- TouchDisplayProjectionTest ---
6789
6790class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6791public:
6792 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6793 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6794 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006795 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6796 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6797 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006798 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006799 auto rotatedWidth = naturalDisplayWidth;
6800 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006801 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006802 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006803 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006804 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006805 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006806 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006807 inverseRotationFlags = ui::Transform::ROT_180;
6808 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006809 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006810 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006811 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006812 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006813 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006814 inverseRotationFlags = ui::Transform::ROT_0;
6815 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006816 }
6817
Prabir Pradhana9df3162022-12-05 23:57:27 +00006818 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006819 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6820
6821 std::optional<DisplayViewport> internalViewport =
6822 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6823 DisplayViewport& v = *internalViewport;
6824 v.displayId = DISPLAY_ID;
6825 v.orientation = orientation;
6826
6827 v.logicalLeft = 0;
6828 v.logicalTop = 0;
6829 v.logicalRight = 100;
6830 v.logicalBottom = 100;
6831
6832 v.physicalLeft = rotatedPhysicalDisplay.left;
6833 v.physicalTop = rotatedPhysicalDisplay.top;
6834 v.physicalRight = rotatedPhysicalDisplay.right;
6835 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6836
Prabir Pradhana9df3162022-12-05 23:57:27 +00006837 v.deviceWidth = rotatedWidth;
6838 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006839
6840 v.isActive = true;
6841 v.uniqueId = UNIQUE_ID;
6842 v.type = ViewportType::INTERNAL;
6843 mFakePolicy->updateViewport(v);
6844 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6845 }
6846
6847 void assertReceivedMove(const Point& point) {
6848 NotifyMotionArgs motionArgs;
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6851 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6853 1, 0, 0, 0, 0, 0, 0, 0));
6854 }
6855};
6856
6857TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6858 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006859 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006860
6861 prepareButtons();
6862 prepareAxes(POSITION);
6863 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6864
6865 NotifyMotionArgs motionArgs;
6866
6867 // Configure the DisplayViewport such that the logical display maps to a subsection of
6868 // the display panel called the physical display. Here, the physical display is bounded by the
6869 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6870 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6871 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6872 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6873
Michael Wrighta9cf4192022-12-01 23:46:39 +00006874 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006875 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6876
6877 // Touches outside the physical display should be ignored, and should not generate any
6878 // events. Ensure touches at the following points that lie outside of the physical display
6879 // area do not generate any events.
6880 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6881 processDown(mapper, toRawX(point.x), toRawY(point.y));
6882 processSync(mapper);
6883 processUp(mapper);
6884 processSync(mapper);
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6886 << "Unexpected event generated for touch outside physical display at point: "
6887 << point.x << ", " << point.y;
6888 }
6889 }
6890}
6891
6892TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6893 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006894 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006895
6896 prepareButtons();
6897 prepareAxes(POSITION);
6898 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6899
6900 NotifyMotionArgs motionArgs;
6901
6902 // Configure the DisplayViewport such that the logical display maps to a subsection of
6903 // the display panel called the physical display. Here, the physical display is bounded by the
6904 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6905 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6906
Michael Wrighta9cf4192022-12-01 23:46:39 +00006907 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006908 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6909
6910 // Touches that start outside the physical display should be ignored until it enters the
6911 // physical display bounds, at which point it should generate a down event. Start a touch at
6912 // the point (5, 100), which is outside the physical display bounds.
6913 static const Point kOutsidePoint{5, 100};
6914 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6915 processSync(mapper);
6916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6917
6918 // Move the touch into the physical display area. This should generate a pointer down.
6919 processMove(mapper, toRawX(11), toRawY(21));
6920 processSync(mapper);
6921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6922 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6923 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6924 ASSERT_NO_FATAL_FAILURE(
6925 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6926
6927 // Move the touch inside the physical display area. This should generate a pointer move.
6928 processMove(mapper, toRawX(69), toRawY(159));
6929 processSync(mapper);
6930 assertReceivedMove({69, 159});
6931
6932 // Move outside the physical display area. Since the pointer is already down, this should
6933 // now continue generating events.
6934 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6935 processSync(mapper);
6936 assertReceivedMove(kOutsidePoint);
6937
6938 // Release. This should generate a pointer up.
6939 processUp(mapper);
6940 processSync(mapper);
6941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6942 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6944 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6945
6946 // Ensure no more events were generated.
6947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6949 }
6950}
6951
Prabir Pradhana9df3162022-12-05 23:57:27 +00006952// --- TouchscreenPrecisionTests ---
6953
6954// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6955// in various orientations and with different display rotations. We configure the touchscreen to
6956// have a higher resolution than that of the display by an integer scale factor in each axis so that
6957// we can enforce that coordinates match precisely as expected.
6958class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6959 public ::testing::WithParamInterface<ui::Rotation> {
6960public:
6961 void SetUp() override {
6962 SingleTouchInputMapperTest::SetUp();
6963
6964 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6965 // four times the resolution of the display in the Y axis.
6966 prepareButtons();
6967 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006968 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6969 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006970 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006971 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6972 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006973 }
6974
6975 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6976 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6977 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6978 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6979
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006980 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6981 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6982
6983 static const int32_t PRECISION_RAW_X_FLAT = 16;
6984 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6985
6986 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6987 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6988
Prabir Pradhana9df3162022-12-05 23:57:27 +00006989 static const std::array<Point, 4> kRawCorners;
6990};
6991
6992const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6993 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6994 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6995 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6996 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6997}};
6998
6999// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7000// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7001// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7002TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7003 enum class Orientation {
7004 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7005 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7006 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7007 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7008 ftl_last = ORIENTATION_270,
7009 };
7010 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7011 Orientation::ORIENTATION_270;
7012 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7013 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7014 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7015 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7016 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7017 };
7018
7019 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7020
7021 // Configure the touchscreen as being installed in the one of the four different orientations
7022 // relative to the display.
7023 addConfigurationProperty("touch.deviceType", "touchScreen");
7024 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7025 prepareDisplay(ui::ROTATION_0);
7026
7027 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7028
7029 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7030 // orientations of either 90 or 270) this means the display's natural resolution will be
7031 // flipped.
7032 const bool displayRotated =
7033 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7034 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7035 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7036 const Rect physicalFrame{0, 0, width, height};
7037 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7038
7039 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7040 const float expectedPrecisionX = displayRotated ? 4 : 2;
7041 const float expectedPrecisionY = displayRotated ? 2 : 4;
7042
7043 // Test all four corners.
7044 for (int i = 0; i < 4; i++) {
7045 const auto& raw = kRawCorners[i];
7046 processDown(mapper, raw.x, raw.y);
7047 processSync(mapper);
7048 const auto& expected = expectedPoints[i];
7049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7050 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7051 WithCoords(expected.x, expected.y),
7052 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7053 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7054 << "with touchscreen orientation "
7055 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7056 << expected.x << ", " << expected.y << ").";
7057 processUp(mapper);
7058 processSync(mapper);
7059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7060 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7061 WithCoords(expected.x, expected.y))));
7062 }
7063}
7064
Prabir Pradhan82687402022-12-06 01:32:53 +00007065TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7066 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7067 kMappedCorners = {
7068 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7069 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7070 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7071 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7072 };
7073
7074 const ui::Rotation displayRotation = GetParam();
7075
7076 addConfigurationProperty("touch.deviceType", "touchScreen");
7077 prepareDisplay(displayRotation);
7078
7079 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7080
7081 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7082
7083 // Test all four corners.
7084 for (int i = 0; i < 4; i++) {
7085 const auto& expected = expectedPoints[i];
7086 const auto& raw = kRawCorners[i];
7087 processDown(mapper, raw.x, raw.y);
7088 processSync(mapper);
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7090 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7091 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7092 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7093 << "with display rotation " << ui::toCString(displayRotation)
7094 << ", expected point (" << expected.x << ", " << expected.y << ").";
7095 processUp(mapper);
7096 processSync(mapper);
7097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7098 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7099 WithCoords(expected.x, expected.y))));
7100 }
7101}
7102
Prabir Pradhan3e798762022-12-02 21:02:11 +00007103TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7104 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7105 kMappedCorners = {
7106 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7107 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7108 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7109 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7110 };
7111
7112 const ui::Rotation displayRotation = GetParam();
7113
7114 addConfigurationProperty("touch.deviceType", "touchScreen");
7115 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7116
7117 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7118
7119 // Ori 270, so width and height swapped
7120 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7121 prepareDisplay(displayRotation);
7122 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7123
7124 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7125
7126 // Test all four corners.
7127 for (int i = 0; i < 4; i++) {
7128 const auto& expected = expectedPoints[i];
7129 const auto& raw = kRawCorners[i];
7130 processDown(mapper, raw.x, raw.y);
7131 processSync(mapper);
7132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7133 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7134 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7135 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7136 << "with display rotation " << ui::toCString(displayRotation)
7137 << ", expected point (" << expected.x << ", " << expected.y << ").";
7138 processUp(mapper);
7139 processSync(mapper);
7140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7141 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7142 WithCoords(expected.x, expected.y))));
7143 }
7144}
7145
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007146TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
7147 const ui::Rotation displayRotation = GetParam();
7148
7149 addConfigurationProperty("touch.deviceType", "touchScreen");
7150 prepareDisplay(displayRotation);
7151
7152 __attribute__((unused)) SingleTouchInputMapper& mapper =
7153 addMapperAndConfigure<SingleTouchInputMapper>();
7154
7155 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
7156 // MotionRanges use display pixels as their units
7157 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
7158 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
7159
7160 // The MotionRanges should be oriented in the rotated display's coordinate space
7161 const bool displayRotated =
7162 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
7163
7164 constexpr float MAX_X = 479.5;
7165 constexpr float MAX_Y = 799.75;
7166 EXPECT_EQ(xRange->min, 0.f);
7167 EXPECT_EQ(yRange->min, 0.f);
7168 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
7169 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
7170
7171 EXPECT_EQ(xRange->flat, 8.f);
7172 EXPECT_EQ(yRange->flat, 8.f);
7173
7174 EXPECT_EQ(xRange->fuzz, 2.f);
7175 EXPECT_EQ(yRange->fuzz, 2.f);
7176
7177 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
7178 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
7179}
7180
Prabir Pradhana9df3162022-12-05 23:57:27 +00007181// Run the precision tests for all rotations.
7182INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7183 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7184 ui::ROTATION_270),
7185 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7186 return ftl::enum_string(testParamInfo.param);
7187 });
7188
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007189// --- ExternalStylusFusionTest ---
7190
7191class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7192public:
7193 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7194 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007195 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007196 prepareButtons();
7197 prepareAxes(POSITION);
7198 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7199
7200 mStylusState.when = ARBITRARY_TIME;
7201 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007202 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007203 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
7204 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
7205 processExternalStylusState(mapper);
7206 return mapper;
7207 }
7208
7209 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7210 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7211 for (const NotifyArgs& args : generatedArgs) {
7212 mFakeListener->notify(args);
7213 }
7214 // Loop the reader to flush the input listener queue.
7215 mReader->loopOnce();
7216 return generatedArgs;
7217 }
7218
7219protected:
7220 StylusState mStylusState{};
7221 static constexpr uint32_t EXPECTED_SOURCE =
7222 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7223
7224 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7225 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007226 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007227
7228 // The first pointer is withheld.
7229 processDown(mapper, 100, 200);
7230 processSync(mapper);
7231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7232 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7233 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7234
7235 // The external stylus reports pressure. The withheld finger pointer is released as a
7236 // stylus.
7237 mStylusState.pressure = 1.f;
7238 processExternalStylusState(mapper);
7239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7240 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7241 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7242
7243 // Subsequent pointer events are not withheld.
7244 processMove(mapper, 101, 201);
7245 processSync(mapper);
7246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7247 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7248
7249 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7251 }
7252
7253 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7254 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7255
7256 // Releasing the touch pointer ends the gesture.
7257 processUp(mapper);
7258 processSync(mapper);
7259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7260 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007261 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007262
7263 mStylusState.pressure = 0.f;
7264 processExternalStylusState(mapper);
7265 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7267 }
7268
7269 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7270 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007271 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007272
7273 // The first pointer is withheld when an external stylus is connected,
7274 // and a timeout is requested.
7275 processDown(mapper, 100, 200);
7276 processSync(mapper);
7277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7278 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7279 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7280
7281 // If the timeout expires early, it is requested again.
7282 handleTimeout(mapper, ARBITRARY_TIME + 1);
7283 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7284 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7285
7286 // When the timeout expires, the withheld touch is released as a finger pointer.
7287 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7289 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7290
7291 // Subsequent pointer events are not withheld.
7292 processMove(mapper, 101, 201);
7293 processSync(mapper);
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7295 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7296 processUp(mapper);
7297 processSync(mapper);
7298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7299 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7300
7301 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7303 }
7304
7305private:
7306 InputDeviceInfo mExternalStylusDeviceInfo{};
7307};
7308
7309TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7310 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7311 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7312}
7313
7314TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7315 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7316 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7317}
7318
7319TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7320 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7321 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7322}
7323
7324// Test a successful stylus fusion gesture where the pressure is reported by the external
7325// before the touch is reported by the touchscreen.
7326TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7327 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7328 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007329 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007330
7331 // The external stylus reports pressure first. It is ignored for now.
7332 mStylusState.pressure = 1.f;
7333 processExternalStylusState(mapper);
7334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7335 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7336
7337 // When the touch goes down afterwards, it is reported as a stylus pointer.
7338 processDown(mapper, 100, 200);
7339 processSync(mapper);
7340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7341 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7342 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7343
7344 processMove(mapper, 101, 201);
7345 processSync(mapper);
7346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7347 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7348 processUp(mapper);
7349 processSync(mapper);
7350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7351 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7352
7353 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7355}
7356
7357TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7358 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7359
7360 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7361 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7362
7363 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7364 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7365 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7366 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7367}
7368
7369TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7370 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7371 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007372 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007373
7374 mStylusState.pressure = 0.8f;
7375 processExternalStylusState(mapper);
7376 processDown(mapper, 100, 200);
7377 processSync(mapper);
7378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7379 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7380 WithPressure(0.8f))));
7381 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7382
7383 // The external stylus reports a pressure change. We wait for some time for a touch event.
7384 mStylusState.pressure = 0.6f;
7385 processExternalStylusState(mapper);
7386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7387 ASSERT_NO_FATAL_FAILURE(
7388 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7389
7390 // If a touch is reported within the timeout, it reports the updated pressure.
7391 processMove(mapper, 101, 201);
7392 processSync(mapper);
7393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7394 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7395 WithPressure(0.6f))));
7396 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7397
7398 // There is another pressure change.
7399 mStylusState.pressure = 0.5f;
7400 processExternalStylusState(mapper);
7401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7402 ASSERT_NO_FATAL_FAILURE(
7403 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7404
7405 // If a touch is not reported within the timeout, a move event is generated to report
7406 // the new pressure.
7407 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7409 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7410 WithPressure(0.5f))));
7411
7412 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7413 // repeated indefinitely.
7414 mStylusState.pressure = 0.0f;
7415 processExternalStylusState(mapper);
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7417 ASSERT_NO_FATAL_FAILURE(
7418 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7419 processMove(mapper, 102, 202);
7420 processSync(mapper);
7421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7422 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7423 WithPressure(0.5f))));
7424 processMove(mapper, 103, 203);
7425 processSync(mapper);
7426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7427 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7428 WithPressure(0.5f))));
7429
7430 processUp(mapper);
7431 processSync(mapper);
7432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7433 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007434 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007435
7436 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7438}
7439
7440TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7441 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7442 auto source = WithSource(EXPECTED_SOURCE);
7443
7444 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007445 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007446 processExternalStylusState(mapper);
7447 processDown(mapper, 100, 200);
7448 processSync(mapper);
7449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7450 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007451 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007452 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7453
7454 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007455 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007456 processExternalStylusState(mapper);
7457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7458 ASSERT_NO_FATAL_FAILURE(
7459 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7460
7461 // If a touch is reported within the timeout, it reports the updated pressure.
7462 processMove(mapper, 101, 201);
7463 processSync(mapper);
7464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7465 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007466 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007467 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7468
7469 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007470 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007471 processExternalStylusState(mapper);
7472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7473 ASSERT_NO_FATAL_FAILURE(
7474 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7475
7476 // If a touch is not reported within the timeout, a move event is generated to report
7477 // the new tool type.
7478 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7480 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007481 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007482
7483 processUp(mapper);
7484 processSync(mapper);
7485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7486 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007487 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007488
7489 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7491}
7492
7493TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7494 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7495 auto toolTypeSource =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007496 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007497
7498 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7499
7500 // The external stylus reports a button change. We wait for some time for a touch event.
7501 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7502 processExternalStylusState(mapper);
7503 ASSERT_NO_FATAL_FAILURE(
7504 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7505
7506 // If a touch is reported within the timeout, it reports the updated button state.
7507 processMove(mapper, 101, 201);
7508 processSync(mapper);
7509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7510 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7511 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7513 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7514 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7515 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7516
7517 // The button is now released.
7518 mStylusState.buttons = 0;
7519 processExternalStylusState(mapper);
7520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7521 ASSERT_NO_FATAL_FAILURE(
7522 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7523
7524 // If a touch is not reported within the timeout, a move event is generated to report
7525 // the new button state.
7526 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7528 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7529 WithButtonState(0))));
7530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007531 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7532 WithButtonState(0))));
7533
7534 processUp(mapper);
7535 processSync(mapper);
7536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007537 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7538
7539 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7541}
7542
Michael Wrightd02c5b62014-02-10 15:10:22 -08007543// --- MultiTouchInputMapperTest ---
7544
7545class MultiTouchInputMapperTest : public TouchInputMapperTest {
7546protected:
7547 void prepareAxes(int axes);
7548
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007549 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7550 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7551 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7552 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7553 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7554 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7555 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7556 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7557 void processId(MultiTouchInputMapper& mapper, int32_t id);
7558 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7559 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7560 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007561 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007562 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007563 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7564 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007565};
7566
7567void MultiTouchInputMapperTest::prepareAxes(int axes) {
7568 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007569 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7570 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007571 }
7572 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007573 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7574 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007575 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007576 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7577 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007578 }
7579 }
7580 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007581 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7582 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007583 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007584 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007585 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007586 }
7587 }
7588 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007589 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7590 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007591 }
7592 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007593 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7594 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007595 }
7596 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007597 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7598 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007599 }
7600 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007601 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7602 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007603 }
7604 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007605 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7606 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007607 }
7608 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007609 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007610 }
7611}
7612
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007613void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7614 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7616 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007617}
7618
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007619void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7620 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007621 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007622}
7623
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007624void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7625 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007627}
7628
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007629void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007631}
7632
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007633void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007635}
7636
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007637void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7638 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007640}
7641
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007642void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007644}
7645
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007646void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007648}
7649
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007650void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007652}
7653
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007654void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007656}
7657
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007658void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007660}
7661
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007662void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7663 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007665}
7666
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007667void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7668 int32_t value) {
7669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7671}
7672
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007673void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007675}
7676
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007677void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7678 nsecs_t readTime) {
7679 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007680}
7681
Michael Wrightd02c5b62014-02-10 15:10:22 -08007682TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007683 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007684 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007685 prepareAxes(POSITION);
7686 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007687 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007688
arthurhungdcef2dc2020-08-11 14:47:50 +08007689 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007690
7691 NotifyMotionArgs motionArgs;
7692
7693 // Two fingers down at once.
7694 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
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_DOWN, 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(1), 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_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7715 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7716 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7717 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7718 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7719
7720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7721 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7722 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7723 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7724 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007725 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007726 ASSERT_EQ(0, motionArgs.flags);
7727 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7728 ASSERT_EQ(0, motionArgs.buttonState);
7729 ASSERT_EQ(0, motionArgs.edgeFlags);
7730 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7731 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007732 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007733 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007734 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7736 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7738 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7739 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7740 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7741 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7742
7743 // Move.
7744 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7745 processPosition(mapper, x1, y1);
7746 processMTSync(mapper);
7747 processPosition(mapper, x2, y2);
7748 processMTSync(mapper);
7749 processSync(mapper);
7750
7751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7752 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7753 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7754 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7755 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7756 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7757 ASSERT_EQ(0, motionArgs.flags);
7758 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7759 ASSERT_EQ(0, motionArgs.buttonState);
7760 ASSERT_EQ(0, motionArgs.edgeFlags);
7761 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7762 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007763 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007764 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007765 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7767 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7769 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7770 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7771 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7772 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7773
7774 // First finger up.
7775 x2 += 15; y2 -= 20;
7776 processPosition(mapper, x2, y2);
7777 processMTSync(mapper);
7778 processSync(mapper);
7779
7780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7781 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7782 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7783 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7784 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007785 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007786 ASSERT_EQ(0, motionArgs.flags);
7787 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7788 ASSERT_EQ(0, motionArgs.buttonState);
7789 ASSERT_EQ(0, motionArgs.edgeFlags);
7790 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7791 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007792 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007793 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007794 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7796 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7798 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7799 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7800 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7801 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7802
7803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7804 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7805 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7806 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7807 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7808 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7809 ASSERT_EQ(0, motionArgs.flags);
7810 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7811 ASSERT_EQ(0, motionArgs.buttonState);
7812 ASSERT_EQ(0, motionArgs.edgeFlags);
7813 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7814 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007815 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7817 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7818 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7819 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7820 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7821
7822 // Move.
7823 x2 += 20; y2 -= 25;
7824 processPosition(mapper, x2, y2);
7825 processMTSync(mapper);
7826 processSync(mapper);
7827
7828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7829 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7830 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7831 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7832 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7834 ASSERT_EQ(0, motionArgs.flags);
7835 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7836 ASSERT_EQ(0, motionArgs.buttonState);
7837 ASSERT_EQ(0, motionArgs.edgeFlags);
7838 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7839 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007840 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7842 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7843 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7844 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7845 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7846
7847 // New finger down.
7848 int32_t x3 = 700, y3 = 300;
7849 processPosition(mapper, x2, y2);
7850 processMTSync(mapper);
7851 processPosition(mapper, x3, y3);
7852 processMTSync(mapper);
7853 processSync(mapper);
7854
7855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7856 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7857 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7858 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7859 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007860 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007861 ASSERT_EQ(0, motionArgs.flags);
7862 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7863 ASSERT_EQ(0, motionArgs.buttonState);
7864 ASSERT_EQ(0, motionArgs.edgeFlags);
7865 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7866 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007867 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007868 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007869 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].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_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7873 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7874 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7875 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7876 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7877
7878 // Second finger up.
7879 x3 += 30; y3 -= 20;
7880 processPosition(mapper, x3, y3);
7881 processMTSync(mapper);
7882 processSync(mapper);
7883
7884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7885 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7886 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7887 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7888 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007889 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007890 ASSERT_EQ(0, motionArgs.flags);
7891 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7892 ASSERT_EQ(0, motionArgs.buttonState);
7893 ASSERT_EQ(0, motionArgs.edgeFlags);
7894 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7895 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007896 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007897 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007898 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007899 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7900 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7902 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7903 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7904 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7905 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7906
7907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7908 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7909 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7910 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7911 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7913 ASSERT_EQ(0, motionArgs.flags);
7914 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7915 ASSERT_EQ(0, motionArgs.buttonState);
7916 ASSERT_EQ(0, motionArgs.edgeFlags);
7917 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7918 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007919 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007920 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7921 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7922 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7923 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7924 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7925
7926 // Last finger up.
7927 processMTSync(mapper);
7928 processSync(mapper);
7929
7930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7931 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7932 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7933 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7934 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7935 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7936 ASSERT_EQ(0, motionArgs.flags);
7937 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7938 ASSERT_EQ(0, motionArgs.buttonState);
7939 ASSERT_EQ(0, motionArgs.edgeFlags);
7940 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7941 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007942 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7944 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7945 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7946 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7947 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7948
7949 // Should not have sent any more keys or motions.
7950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7952}
7953
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007954TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7955 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007956 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007957
7958 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7959 /*fuzz*/ 0, /*resolution*/ 10);
7960 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7961 /*fuzz*/ 0, /*resolution*/ 11);
7962 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7963 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7964 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7965 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7966 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7967 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7968 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7969 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7970
7971 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7972
7973 // X and Y axes
7974 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7975 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7976 // Touch major and minor
7977 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7978 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7979 // Tool major and minor
7980 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7981 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7982}
7983
7984TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7985 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007986 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007987
7988 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7989 /*fuzz*/ 0, /*resolution*/ 10);
7990 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7991 /*fuzz*/ 0, /*resolution*/ 11);
7992
7993 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7994
7995 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7996
7997 // Touch major and minor
7998 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7999 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8000 // Tool major and minor
8001 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8002 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8003}
8004
Michael Wrightd02c5b62014-02-10 15:10:22 -08008005TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008006 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008007 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008008 prepareAxes(POSITION | ID);
8009 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008010 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008011
arthurhungdcef2dc2020-08-11 14:47:50 +08008012 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008013
8014 NotifyMotionArgs motionArgs;
8015
8016 // Two fingers down at once.
8017 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8018 processPosition(mapper, x1, y1);
8019 processId(mapper, 1);
8020 processMTSync(mapper);
8021 processPosition(mapper, x2, y2);
8022 processId(mapper, 2);
8023 processMTSync(mapper);
8024 processSync(mapper);
8025
8026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8027 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8028 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8029 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008030 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8032 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8033
8034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008035 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008036 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8037 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008038 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008040 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8042 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8044 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8045
8046 // Move.
8047 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8048 processPosition(mapper, x1, y1);
8049 processId(mapper, 1);
8050 processMTSync(mapper);
8051 processPosition(mapper, x2, y2);
8052 processId(mapper, 2);
8053 processMTSync(mapper);
8054 processSync(mapper);
8055
8056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8057 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8058 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8059 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008060 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008061 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008062 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8064 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8066 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8067
8068 // First finger up.
8069 x2 += 15; y2 -= 20;
8070 processPosition(mapper, x2, y2);
8071 processId(mapper, 2);
8072 processMTSync(mapper);
8073 processSync(mapper);
8074
8075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008076 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8078 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008079 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008080 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008081 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008082 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8083 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8085 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8086
8087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8089 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8090 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008091 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8093 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8094
8095 // Move.
8096 x2 += 20; y2 -= 25;
8097 processPosition(mapper, x2, y2);
8098 processId(mapper, 2);
8099 processMTSync(mapper);
8100 processSync(mapper);
8101
8102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8103 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8104 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8105 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008106 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8108 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8109
8110 // New finger down.
8111 int32_t x3 = 700, y3 = 300;
8112 processPosition(mapper, x2, y2);
8113 processId(mapper, 2);
8114 processMTSync(mapper);
8115 processPosition(mapper, x3, y3);
8116 processId(mapper, 3);
8117 processMTSync(mapper);
8118 processSync(mapper);
8119
8120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008121 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008122 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8123 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008124 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008125 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008126 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8128 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8129 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8130 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8131
8132 // Second finger up.
8133 x3 += 30; y3 -= 20;
8134 processPosition(mapper, x3, y3);
8135 processId(mapper, 3);
8136 processMTSync(mapper);
8137 processSync(mapper);
8138
8139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008140 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008141 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8142 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008143 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008144 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008145 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8147 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8149 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8150
8151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8153 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8154 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008155 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8157 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8158
8159 // Last finger up.
8160 processMTSync(mapper);
8161 processSync(mapper);
8162
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8164 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8165 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8166 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008167 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8169 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8170
8171 // Should not have sent any more keys or motions.
8172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8174}
8175
8176TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008177 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008178 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179 prepareAxes(POSITION | ID | SLOT);
8180 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008181 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008182
arthurhungdcef2dc2020-08-11 14:47:50 +08008183 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008184
8185 NotifyMotionArgs motionArgs;
8186
8187 // Two fingers down at once.
8188 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8189 processPosition(mapper, x1, y1);
8190 processId(mapper, 1);
8191 processSlot(mapper, 1);
8192 processPosition(mapper, x2, y2);
8193 processId(mapper, 2);
8194 processSync(mapper);
8195
8196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8197 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8198 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8199 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008200 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].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
8204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008205 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008206 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8207 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008208 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008209 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008210 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8212 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8214 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8215
8216 // Move.
8217 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8218 processSlot(mapper, 0);
8219 processPosition(mapper, x1, y1);
8220 processSlot(mapper, 1);
8221 processPosition(mapper, x2, y2);
8222 processSync(mapper);
8223
8224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8226 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8227 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008228 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008229 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008230 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8232 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8234 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8235
8236 // First finger up.
8237 x2 += 15; y2 -= 20;
8238 processSlot(mapper, 0);
8239 processId(mapper, -1);
8240 processSlot(mapper, 1);
8241 processPosition(mapper, x2, y2);
8242 processSync(mapper);
8243
8244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008245 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008246 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8247 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008248 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008250 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8252 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8254 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8255
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8258 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8259 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008260 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8262 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8263
8264 // Move.
8265 x2 += 20; y2 -= 25;
8266 processPosition(mapper, x2, y2);
8267 processSync(mapper);
8268
8269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8271 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8272 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008273 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8275 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8276
8277 // New finger down.
8278 int32_t x3 = 700, y3 = 300;
8279 processPosition(mapper, x2, y2);
8280 processSlot(mapper, 0);
8281 processId(mapper, 3);
8282 processPosition(mapper, x3, y3);
8283 processSync(mapper);
8284
8285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008286 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008287 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8288 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008289 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008290 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008291 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8293 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8295 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8296
8297 // Second finger up.
8298 x3 += 30; y3 -= 20;
8299 processSlot(mapper, 1);
8300 processId(mapper, -1);
8301 processSlot(mapper, 0);
8302 processPosition(mapper, x3, y3);
8303 processSync(mapper);
8304
8305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008306 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008307 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8308 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008309 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008310 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008311 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8313 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8315 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8316
8317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8319 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8320 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008321 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8323 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8324
8325 // Last finger up.
8326 processId(mapper, -1);
8327 processSync(mapper);
8328
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8330 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8331 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8332 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008333 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8335 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8336
8337 // Should not have sent any more keys or motions.
8338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8340}
8341
8342TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008343 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008344 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008345 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
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 = 7;
8352 int32_t rawTouchMinor = 6;
8353 int32_t rawToolMajor = 9;
8354 int32_t rawToolMinor = 8;
8355 int32_t rawPressure = 11;
8356 int32_t rawDistance = 0;
8357 int32_t rawOrientation = 3;
8358 int32_t id = 5;
8359
8360 float x = toDisplayX(rawX);
8361 float y = toDisplayY(rawY);
8362 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8363 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8364 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8365 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8366 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8367 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8368 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8369 float distance = float(rawDistance);
8370
8371 processPosition(mapper, rawX, rawY);
8372 processTouchMajor(mapper, rawTouchMajor);
8373 processTouchMinor(mapper, rawTouchMinor);
8374 processToolMajor(mapper, rawToolMajor);
8375 processToolMinor(mapper, rawToolMinor);
8376 processPressure(mapper, rawPressure);
8377 processOrientation(mapper, rawOrientation);
8378 processDistance(mapper, rawDistance);
8379 processId(mapper, id);
8380 processMTSync(mapper);
8381 processSync(mapper);
8382
8383 NotifyMotionArgs args;
8384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8385 ASSERT_EQ(0, args.pointerProperties[0].id);
8386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8387 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8388 orientation, distance));
8389}
8390
8391TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008392 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008393 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008394 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8395 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008396 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008397
8398 // These calculations are based on the input device calibration documentation.
8399 int32_t rawX = 100;
8400 int32_t rawY = 200;
8401 int32_t rawTouchMajor = 140;
8402 int32_t rawTouchMinor = 120;
8403 int32_t rawToolMajor = 180;
8404 int32_t rawToolMinor = 160;
8405
8406 float x = toDisplayX(rawX);
8407 float y = toDisplayY(rawY);
8408 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8409 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8410 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8411 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8412 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8413
8414 processPosition(mapper, rawX, rawY);
8415 processTouchMajor(mapper, rawTouchMajor);
8416 processTouchMinor(mapper, rawTouchMinor);
8417 processToolMajor(mapper, rawToolMajor);
8418 processToolMinor(mapper, rawToolMinor);
8419 processMTSync(mapper);
8420 processSync(mapper);
8421
8422 NotifyMotionArgs args;
8423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8425 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8426}
8427
8428TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008429 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008430 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008431 prepareAxes(POSITION | TOUCH | TOOL);
8432 addConfigurationProperty("touch.size.calibration", "diameter");
8433 addConfigurationProperty("touch.size.scale", "10");
8434 addConfigurationProperty("touch.size.bias", "160");
8435 addConfigurationProperty("touch.size.isSummed", "1");
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 // Note: We only provide a single common touch/tool value because the device is assumed
8440 // not to emit separate values for each pointer (isSummed = 1).
8441 int32_t rawX = 100;
8442 int32_t rawY = 200;
8443 int32_t rawX2 = 150;
8444 int32_t rawY2 = 250;
8445 int32_t rawTouchMajor = 5;
8446 int32_t rawToolMajor = 8;
8447
8448 float x = toDisplayX(rawX);
8449 float y = toDisplayY(rawY);
8450 float x2 = toDisplayX(rawX2);
8451 float y2 = toDisplayY(rawY2);
8452 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8453 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8454 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8455
8456 processPosition(mapper, rawX, rawY);
8457 processTouchMajor(mapper, rawTouchMajor);
8458 processToolMajor(mapper, rawToolMajor);
8459 processMTSync(mapper);
8460 processPosition(mapper, rawX2, rawY2);
8461 processTouchMajor(mapper, rawTouchMajor);
8462 processToolMajor(mapper, rawToolMajor);
8463 processMTSync(mapper);
8464 processSync(mapper);
8465
8466 NotifyMotionArgs args;
8467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8468 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8469
8470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008471 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008472 ASSERT_EQ(size_t(2), args.pointerCount);
8473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8474 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8476 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8477}
8478
8479TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008480 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008481 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008482 prepareAxes(POSITION | TOUCH | TOOL);
8483 addConfigurationProperty("touch.size.calibration", "area");
8484 addConfigurationProperty("touch.size.scale", "43");
8485 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008486 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008487
8488 // These calculations are based on the input device calibration documentation.
8489 int32_t rawX = 100;
8490 int32_t rawY = 200;
8491 int32_t rawTouchMajor = 5;
8492 int32_t rawToolMajor = 8;
8493
8494 float x = toDisplayX(rawX);
8495 float y = toDisplayY(rawY);
8496 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8497 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8498 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8499
8500 processPosition(mapper, rawX, rawY);
8501 processTouchMajor(mapper, rawTouchMajor);
8502 processToolMajor(mapper, rawToolMajor);
8503 processMTSync(mapper);
8504 processSync(mapper);
8505
8506 NotifyMotionArgs args;
8507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8509 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8510}
8511
8512TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008513 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008514 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008515 prepareAxes(POSITION | PRESSURE);
8516 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8517 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008518 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008519
Michael Wrightaa449c92017-12-13 21:21:43 +00008520 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008521 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008522 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8523 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8524 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8525
Michael Wrightd02c5b62014-02-10 15:10:22 -08008526 // These calculations are based on the input device calibration documentation.
8527 int32_t rawX = 100;
8528 int32_t rawY = 200;
8529 int32_t rawPressure = 60;
8530
8531 float x = toDisplayX(rawX);
8532 float y = toDisplayY(rawY);
8533 float pressure = float(rawPressure) * 0.01f;
8534
8535 processPosition(mapper, rawX, rawY);
8536 processPressure(mapper, rawPressure);
8537 processMTSync(mapper);
8538 processSync(mapper);
8539
8540 NotifyMotionArgs args;
8541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8543 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8544}
8545
8546TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008547 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008548 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008549 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008550 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008551
8552 NotifyMotionArgs motionArgs;
8553 NotifyKeyArgs keyArgs;
8554
8555 processId(mapper, 1);
8556 processPosition(mapper, 100, 200);
8557 processSync(mapper);
8558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8559 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8560 ASSERT_EQ(0, motionArgs.buttonState);
8561
8562 // press BTN_LEFT, release BTN_LEFT
8563 processKey(mapper, BTN_LEFT, 1);
8564 processSync(mapper);
8565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8567 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8568
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8570 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8571 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8572
Michael Wrightd02c5b62014-02-10 15:10:22 -08008573 processKey(mapper, BTN_LEFT, 0);
8574 processSync(mapper);
8575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008576 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008577 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008578
8579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008581 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008582
8583 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8584 processKey(mapper, BTN_RIGHT, 1);
8585 processKey(mapper, BTN_MIDDLE, 1);
8586 processSync(mapper);
8587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8589 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8590 motionArgs.buttonState);
8591
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8593 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8594 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8595
8596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8597 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8598 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8599 motionArgs.buttonState);
8600
Michael Wrightd02c5b62014-02-10 15:10:22 -08008601 processKey(mapper, BTN_RIGHT, 0);
8602 processSync(mapper);
8603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008604 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008605 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008606
8607 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_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008610
8611 processKey(mapper, BTN_MIDDLE, 0);
8612 processSync(mapper);
8613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008614 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008615 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008616
8617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008619 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008620
8621 // press BTN_BACK, release BTN_BACK
8622 processKey(mapper, BTN_BACK, 1);
8623 processSync(mapper);
8624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8625 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8626 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008627
Michael Wrightd02c5b62014-02-10 15:10:22 -08008628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008630 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8631
8632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8633 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8634 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008635
8636 processKey(mapper, BTN_BACK, 0);
8637 processSync(mapper);
8638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008639 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008640 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008641
8642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008643 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008644 ASSERT_EQ(0, motionArgs.buttonState);
8645
Michael Wrightd02c5b62014-02-10 15:10:22 -08008646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8647 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8648 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8649
8650 // press BTN_SIDE, release BTN_SIDE
8651 processKey(mapper, BTN_SIDE, 1);
8652 processSync(mapper);
8653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8654 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8655 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008656
Michael Wrightd02c5b62014-02-10 15:10:22 -08008657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008659 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8660
8661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8662 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8663 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008664
8665 processKey(mapper, BTN_SIDE, 0);
8666 processSync(mapper);
8667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008668 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008669 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008670
8671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008673 ASSERT_EQ(0, motionArgs.buttonState);
8674
Michael Wrightd02c5b62014-02-10 15:10:22 -08008675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8676 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8677 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8678
8679 // press BTN_FORWARD, release BTN_FORWARD
8680 processKey(mapper, BTN_FORWARD, 1);
8681 processSync(mapper);
8682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8683 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8684 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008685
Michael Wrightd02c5b62014-02-10 15:10:22 -08008686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008688 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8689
8690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8691 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8692 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008693
8694 processKey(mapper, BTN_FORWARD, 0);
8695 processSync(mapper);
8696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008697 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008698 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008699
8700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008702 ASSERT_EQ(0, motionArgs.buttonState);
8703
Michael Wrightd02c5b62014-02-10 15:10:22 -08008704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8705 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8706 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8707
8708 // press BTN_EXTRA, release BTN_EXTRA
8709 processKey(mapper, BTN_EXTRA, 1);
8710 processSync(mapper);
8711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8712 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8713 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008714
Michael Wrightd02c5b62014-02-10 15:10:22 -08008715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008717 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8718
8719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8720 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8721 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008722
8723 processKey(mapper, BTN_EXTRA, 0);
8724 processSync(mapper);
8725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008726 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008727 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008728
8729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008730 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008731 ASSERT_EQ(0, motionArgs.buttonState);
8732
Michael Wrightd02c5b62014-02-10 15:10:22 -08008733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8734 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8735 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8736
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8738
Michael Wrightd02c5b62014-02-10 15:10:22 -08008739 // press BTN_STYLUS, release BTN_STYLUS
8740 processKey(mapper, BTN_STYLUS, 1);
8741 processSync(mapper);
8742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008744 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8745
8746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8747 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8748 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008749
8750 processKey(mapper, BTN_STYLUS, 0);
8751 processSync(mapper);
8752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008753 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008754 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008755
8756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008758 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008759
8760 // press BTN_STYLUS2, release BTN_STYLUS2
8761 processKey(mapper, BTN_STYLUS2, 1);
8762 processSync(mapper);
8763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8764 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008765 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8766
8767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8768 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8769 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008770
8771 processKey(mapper, BTN_STYLUS2, 0);
8772 processSync(mapper);
8773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008774 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008775 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008776
8777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008779 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008780
8781 // release touch
8782 processId(mapper, -1);
8783 processSync(mapper);
8784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8785 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8786 ASSERT_EQ(0, motionArgs.buttonState);
8787}
8788
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008789TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8790 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008791 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008792 prepareAxes(POSITION | ID | SLOT);
8793 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8794
8795 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8796 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8797
8798 // Touch down.
8799 processId(mapper, 1);
8800 processPosition(mapper, 100, 200);
8801 processSync(mapper);
8802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8803 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8804
8805 // Press and release button mapped to the primary stylus button.
8806 processKey(mapper, BTN_A, 1);
8807 processSync(mapper);
8808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8809 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8810 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8812 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8813 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8814
8815 processKey(mapper, BTN_A, 0);
8816 processSync(mapper);
8817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8818 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8820 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8821
8822 // Press and release the HID usage mapped to the secondary stylus button.
8823 processHidUsage(mapper, 0xabcd, 1);
8824 processSync(mapper);
8825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8826 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8827 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8829 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8830 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8831
8832 processHidUsage(mapper, 0xabcd, 0);
8833 processSync(mapper);
8834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8835 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8837 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8838
8839 // Release touch.
8840 processId(mapper, -1);
8841 processSync(mapper);
8842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8843 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8844}
8845
Michael Wrightd02c5b62014-02-10 15:10:22 -08008846TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008847 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008848 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008849 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008850 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008851
8852 NotifyMotionArgs motionArgs;
8853
8854 // default tool type is finger
8855 processId(mapper, 1);
8856 processPosition(mapper, 100, 200);
8857 processSync(mapper);
8858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8859 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008860 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008861
8862 // eraser
8863 processKey(mapper, BTN_TOOL_RUBBER, 1);
8864 processSync(mapper);
8865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008867 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008868
8869 // stylus
8870 processKey(mapper, BTN_TOOL_RUBBER, 0);
8871 processKey(mapper, BTN_TOOL_PEN, 1);
8872 processSync(mapper);
8873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008875 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008876
8877 // brush
8878 processKey(mapper, BTN_TOOL_PEN, 0);
8879 processKey(mapper, BTN_TOOL_BRUSH, 1);
8880 processSync(mapper);
8881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008883 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008884
8885 // pencil
8886 processKey(mapper, BTN_TOOL_BRUSH, 0);
8887 processKey(mapper, BTN_TOOL_PENCIL, 1);
8888 processSync(mapper);
8889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008891 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008892
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008893 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008894 processKey(mapper, BTN_TOOL_PENCIL, 0);
8895 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8896 processSync(mapper);
8897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008899 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008900
8901 // mouse
8902 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8903 processKey(mapper, BTN_TOOL_MOUSE, 1);
8904 processSync(mapper);
8905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008907 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008908
8909 // lens
8910 processKey(mapper, BTN_TOOL_MOUSE, 0);
8911 processKey(mapper, BTN_TOOL_LENS, 1);
8912 processSync(mapper);
8913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8914 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008915 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008916
8917 // double-tap
8918 processKey(mapper, BTN_TOOL_LENS, 0);
8919 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8920 processSync(mapper);
8921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8922 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008923 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008924
8925 // triple-tap
8926 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8927 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8928 processSync(mapper);
8929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008931 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008932
8933 // quad-tap
8934 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8935 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8936 processSync(mapper);
8937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008939 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008940
8941 // finger
8942 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8943 processKey(mapper, BTN_TOOL_FINGER, 1);
8944 processSync(mapper);
8945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008947 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008948
8949 // stylus trumps finger
8950 processKey(mapper, BTN_TOOL_PEN, 1);
8951 processSync(mapper);
8952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008954 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008955
8956 // eraser trumps stylus
8957 processKey(mapper, BTN_TOOL_RUBBER, 1);
8958 processSync(mapper);
8959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8960 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008961 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008962
8963 // mouse trumps eraser
8964 processKey(mapper, BTN_TOOL_MOUSE, 1);
8965 processSync(mapper);
8966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008968 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008969
8970 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8971 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8972 processSync(mapper);
8973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008975 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008976
8977 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8978 processToolType(mapper, MT_TOOL_PEN);
8979 processSync(mapper);
8980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008982 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008983
8984 // back to default tool type
8985 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8986 processKey(mapper, BTN_TOOL_MOUSE, 0);
8987 processKey(mapper, BTN_TOOL_RUBBER, 0);
8988 processKey(mapper, BTN_TOOL_PEN, 0);
8989 processKey(mapper, BTN_TOOL_FINGER, 0);
8990 processSync(mapper);
8991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8992 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008993 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008994}
8995
8996TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008997 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008998 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008999 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009000 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009001 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009002
9003 NotifyMotionArgs motionArgs;
9004
9005 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9006 processId(mapper, 1);
9007 processPosition(mapper, 100, 200);
9008 processSync(mapper);
9009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9010 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9012 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9013
9014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9015 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9017 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9018
9019 // move a little
9020 processPosition(mapper, 150, 250);
9021 processSync(mapper);
9022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9023 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9025 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9026
9027 // down when BTN_TOUCH is pressed, pressure defaults to 1
9028 processKey(mapper, BTN_TOUCH, 1);
9029 processSync(mapper);
9030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9031 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9033 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9034
9035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9036 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9038 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9039
9040 // up when BTN_TOUCH is released, hover restored
9041 processKey(mapper, BTN_TOUCH, 0);
9042 processSync(mapper);
9043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9044 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9046 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9047
9048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9049 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9051 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9052
9053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9054 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9056 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9057
9058 // exit hover when pointer goes away
9059 processId(mapper, -1);
9060 processSync(mapper);
9061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9062 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9064 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9065}
9066
9067TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009068 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009069 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009070 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009071 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009072
9073 NotifyMotionArgs motionArgs;
9074
9075 // initially hovering because pressure is 0
9076 processId(mapper, 1);
9077 processPosition(mapper, 100, 200);
9078 processPressure(mapper, 0);
9079 processSync(mapper);
9080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9081 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9082 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9083 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9084
9085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9086 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9088 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9089
9090 // move a little
9091 processPosition(mapper, 150, 250);
9092 processSync(mapper);
9093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9094 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9095 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9096 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9097
9098 // down when pressure becomes non-zero
9099 processPressure(mapper, RAW_PRESSURE_MAX);
9100 processSync(mapper);
9101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9102 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9104 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9105
9106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9107 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9109 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9110
9111 // up when pressure becomes 0, hover restored
9112 processPressure(mapper, 0);
9113 processSync(mapper);
9114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9115 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9117 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9118
9119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9120 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9122 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9123
9124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9125 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9127 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9128
9129 // exit hover when pointer goes away
9130 processId(mapper, -1);
9131 processSync(mapper);
9132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9133 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9135 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9136}
9137
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009138/**
9139 * Set the input device port <--> display port associations, and check that the
9140 * events are routed to the display that matches the display port.
9141 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9142 */
9143TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009144 const std::string usb2 = "USB2";
9145 const uint8_t hdmi1 = 0;
9146 const uint8_t hdmi2 = 1;
9147 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009148 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009149
9150 addConfigurationProperty("touch.deviceType", "touchScreen");
9151 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009152 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009153
9154 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9155 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9156
9157 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9158 // for this input device is specified, and the matching viewport is not present,
9159 // the input device should be disabled (at the mapper level).
9160
9161 // Add viewport for display 2 on hdmi2
9162 prepareSecondaryDisplay(type, hdmi2);
9163 // Send a touch event
9164 processPosition(mapper, 100, 100);
9165 processSync(mapper);
9166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9167
9168 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009169 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009170 // Send a touch event again
9171 processPosition(mapper, 100, 100);
9172 processSync(mapper);
9173
9174 NotifyMotionArgs args;
9175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9176 ASSERT_EQ(DISPLAY_ID, args.displayId);
9177}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009178
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009179TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9180 addConfigurationProperty("touch.deviceType", "touchScreen");
9181 prepareAxes(POSITION);
9182 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9183
9184 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9185
Michael Wrighta9cf4192022-12-01 23:46:39 +00009186 prepareDisplay(ui::ROTATION_0);
9187 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009188
9189 // Send a touch event
9190 processPosition(mapper, 100, 100);
9191 processSync(mapper);
9192
9193 NotifyMotionArgs args;
9194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9195 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9196}
9197
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009198TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009199 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009200 std::shared_ptr<FakePointerController> fakePointerController =
9201 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009202 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009203 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009204 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009205
Garfield Tan888a6a42020-01-09 11:39:16 -08009206 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009207 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009208
Michael Wrighta9cf4192022-12-01 23:46:39 +00009209 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009210 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009211 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009212
Josep del Río2d8c79a2023-01-23 19:33:50 +00009213 // Check source is mouse that would obtain the PointerController.
9214 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009215
9216 NotifyMotionArgs motionArgs;
9217 processPosition(mapper, 100, 100);
9218 processSync(mapper);
9219
9220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9221 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9222 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9223}
9224
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009225/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009226 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9227 */
9228TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9229 addConfigurationProperty("touch.deviceType", "touchScreen");
9230 prepareAxes(POSITION);
9231 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9232
Michael Wrighta9cf4192022-12-01 23:46:39 +00009233 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009234 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9235 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9236 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9237 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009238
9239 NotifyMotionArgs args;
9240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9241 ASSERT_EQ(26, args.readTime);
9242
Harry Cutts33476232023-01-30 19:57:29 +00009243 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9244 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9245 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009246
9247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9248 ASSERT_EQ(33, args.readTime);
9249}
9250
9251/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009252 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9253 * events should not be delivered to the listener.
9254 */
9255TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9256 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009257 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009258 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009259 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009260 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9261 prepareAxes(POSITION);
9262 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9263
9264 NotifyMotionArgs motionArgs;
9265 processPosition(mapper, 100, 100);
9266 processSync(mapper);
9267
9268 mFakeListener->assertNotifyMotionWasNotCalled();
9269}
9270
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009271/**
9272 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9273 * the touch mapper can process the events and the events can be delivered to the listener.
9274 */
9275TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9276 addConfigurationProperty("touch.deviceType", "touchScreen");
9277 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009278 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009279 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009280 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9281 prepareAxes(POSITION);
9282 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9283
9284 NotifyMotionArgs motionArgs;
9285 processPosition(mapper, 100, 100);
9286 processSync(mapper);
9287
9288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9289 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9290}
9291
Garfield Tanc734e4f2021-01-15 20:01:39 -08009292TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9293 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009294 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009295 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009296 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009297 std::optional<DisplayViewport> optionalDisplayViewport =
9298 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9299 ASSERT_TRUE(optionalDisplayViewport.has_value());
9300 DisplayViewport displayViewport = *optionalDisplayViewport;
9301
9302 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9303 prepareAxes(POSITION);
9304 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9305
9306 // Finger down
9307 int32_t x = 100, y = 100;
9308 processPosition(mapper, x, y);
9309 processSync(mapper);
9310
9311 NotifyMotionArgs motionArgs;
9312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9313 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9314
9315 // Deactivate display viewport
9316 displayViewport.isActive = false;
9317 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9318 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9319
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009320 // The ongoing touch should be canceled immediately
9321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9322 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9323
9324 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009325 x += 10, y += 10;
9326 processPosition(mapper, x, y);
9327 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009329
9330 // Reactivate display viewport
9331 displayViewport.isActive = true;
9332 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9333 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9334
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009335 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009336 x += 10, y += 10;
9337 processPosition(mapper, x, y);
9338 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9340 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009341}
9342
Arthur Hung7c645402019-01-25 17:45:42 +08009343TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9344 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009345 prepareAxes(POSITION | ID | SLOT);
9346 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009347 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009348
9349 // Create the second touch screen device, and enable multi fingers.
9350 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009351 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009352 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009353 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009354 std::shared_ptr<InputDevice> device2 =
9355 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009356 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009357
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009358 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009359 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009360 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009361 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009362 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009363 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009364 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009365 /*flat=*/0, /*fuzz=*/0);
9366 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009367 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9368 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009369
9370 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009371 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009372 std::list<NotifyArgs> unused =
9373 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00009374 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009375 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009376
9377 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009378 std::shared_ptr<FakePointerController> fakePointerController =
9379 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009380 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009381
9382 // Setup policy for associated displays and show touches.
9383 const uint8_t hdmi1 = 0;
9384 const uint8_t hdmi2 = 1;
9385 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9386 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9387 mFakePolicy->setShowTouches(true);
9388
9389 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009390 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009391 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009392
9393 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009394 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9395 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9396 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009397
9398 // Two fingers down at default display.
9399 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9400 processPosition(mapper, x1, y1);
9401 processId(mapper, 1);
9402 processSlot(mapper, 1);
9403 processPosition(mapper, x2, y2);
9404 processId(mapper, 2);
9405 processSync(mapper);
9406
9407 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9408 fakePointerController->getSpots().find(DISPLAY_ID);
9409 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9410 ASSERT_EQ(size_t(2), iter->second.size());
9411
9412 // Two fingers down at second display.
9413 processPosition(mapper2, x1, y1);
9414 processId(mapper2, 1);
9415 processSlot(mapper2, 1);
9416 processPosition(mapper2, x2, y2);
9417 processId(mapper2, 2);
9418 processSync(mapper2);
9419
9420 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9421 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9422 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009423
9424 // Disable the show touches configuration and ensure the spots are cleared.
9425 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009426 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9427 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009428
9429 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009430}
9431
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009432TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009433 prepareAxes(POSITION);
9434 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009435 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009436 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009437
9438 NotifyMotionArgs motionArgs;
9439 // Unrotated video frame
9440 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9441 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009442 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009443 processPosition(mapper, 100, 200);
9444 processSync(mapper);
9445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9446 ASSERT_EQ(frames, motionArgs.videoFrames);
9447
9448 // Subsequent touch events should not have any videoframes
9449 // This is implemented separately in FakeEventHub,
9450 // but that should match the behaviour of TouchVideoDevice.
9451 processPosition(mapper, 200, 200);
9452 processSync(mapper);
9453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9454 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9455}
9456
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009457TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009458 prepareAxes(POSITION);
9459 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009460 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009461 // Unrotated video frame
9462 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9463 NotifyMotionArgs motionArgs;
9464
9465 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009466 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009467 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9468 clearViewports();
9469 prepareDisplay(orientation);
9470 std::vector<TouchVideoFrame> frames{frame};
9471 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9472 processPosition(mapper, 100, 200);
9473 processSync(mapper);
9474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9475 ASSERT_EQ(frames, motionArgs.videoFrames);
9476 }
9477}
9478
9479TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9480 prepareAxes(POSITION);
9481 addConfigurationProperty("touch.deviceType", "touchScreen");
9482 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9483 // orientation-aware are affected by display rotation.
9484 addConfigurationProperty("touch.orientationAware", "0");
9485 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9486 // Unrotated video frame
9487 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9488 NotifyMotionArgs motionArgs;
9489
9490 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009491 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009492 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9493 clearViewports();
9494 prepareDisplay(orientation);
9495 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009496 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009497 processPosition(mapper, 100, 200);
9498 processSync(mapper);
9499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009500 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9501 // compared to the display. This is so that when the window transform (which contains the
9502 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9503 // window's coordinate space.
9504 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009505 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009506
9507 // Release finger.
9508 processSync(mapper);
9509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009510 }
9511}
9512
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009513TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009514 prepareAxes(POSITION);
9515 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009516 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009517 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9518 // so mix these.
9519 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9520 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9521 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9522 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9523 NotifyMotionArgs motionArgs;
9524
Michael Wrighta9cf4192022-12-01 23:46:39 +00009525 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009526 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009527 processPosition(mapper, 100, 200);
9528 processSync(mapper);
9529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009530 ASSERT_EQ(frames, motionArgs.videoFrames);
9531}
9532
9533TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9534 prepareAxes(POSITION);
9535 addConfigurationProperty("touch.deviceType", "touchScreen");
9536 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9537 // orientation-aware are affected by display rotation.
9538 addConfigurationProperty("touch.orientationAware", "0");
9539 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9540 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9541 // so mix these.
9542 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9543 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9544 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9545 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9546 NotifyMotionArgs motionArgs;
9547
Michael Wrighta9cf4192022-12-01 23:46:39 +00009548 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009549 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9550 processPosition(mapper, 100, 200);
9551 processSync(mapper);
9552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9553 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9554 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9555 // compared to the display. This is so that when the window transform (which contains the
9556 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9557 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009558 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009559 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009560 ASSERT_EQ(frames, motionArgs.videoFrames);
9561}
9562
Arthur Hung9da14732019-09-02 16:16:58 +08009563/**
9564 * If we had defined port associations, but the viewport is not ready, the touch device would be
9565 * expected to be disabled, and it should be enabled after the viewport has found.
9566 */
9567TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009568 constexpr uint8_t hdmi2 = 1;
9569 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009570 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009571
9572 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9573
9574 addConfigurationProperty("touch.deviceType", "touchScreen");
9575 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009576 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009577
9578 ASSERT_EQ(mDevice->isEnabled(), false);
9579
9580 // Add display on hdmi2, the device should be enabled and can receive touch event.
9581 prepareSecondaryDisplay(type, hdmi2);
9582 ASSERT_EQ(mDevice->isEnabled(), true);
9583
9584 // Send a touch event.
9585 processPosition(mapper, 100, 100);
9586 processSync(mapper);
9587
9588 NotifyMotionArgs args;
9589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9590 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9591}
9592
Arthur Hung421eb1c2020-01-16 00:09:42 +08009593TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009594 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009595 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009596 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009597 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009598
9599 NotifyMotionArgs motionArgs;
9600
9601 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9602 // finger down
9603 processId(mapper, 1);
9604 processPosition(mapper, x1, y1);
9605 processSync(mapper);
9606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9607 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009608 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009609
9610 // finger move
9611 processId(mapper, 1);
9612 processPosition(mapper, x2, y2);
9613 processSync(mapper);
9614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9615 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009616 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009617
9618 // finger up.
9619 processId(mapper, -1);
9620 processSync(mapper);
9621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9622 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009623 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009624
9625 // new finger down
9626 processId(mapper, 1);
9627 processPosition(mapper, x3, y3);
9628 processSync(mapper);
9629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9630 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009631 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009632}
9633
9634/**
arthurhungcc7f9802020-04-30 17:55:40 +08009635 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9636 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009637 */
arthurhungcc7f9802020-04-30 17:55:40 +08009638TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009639 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009640 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009641 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009642 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009643
9644 NotifyMotionArgs motionArgs;
9645
9646 // default tool type is finger
9647 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009648 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009649 processPosition(mapper, x1, y1);
9650 processSync(mapper);
9651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9652 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009653 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009654
9655 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9656 processToolType(mapper, MT_TOOL_PALM);
9657 processSync(mapper);
9658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9659 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9660
9661 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009662 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009663 processPosition(mapper, x2, y2);
9664 processSync(mapper);
9665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9666
9667 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009668 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009669 processSync(mapper);
9670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9671
9672 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009673 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009674 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009675 processPosition(mapper, x3, y3);
9676 processSync(mapper);
9677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9678 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009679 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009680}
9681
arthurhungbf89a482020-04-17 17:37:55 +08009682/**
arthurhungcc7f9802020-04-30 17:55:40 +08009683 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9684 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009685 */
arthurhungcc7f9802020-04-30 17:55:40 +08009686TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009687 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009688 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009689 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9690 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9691
9692 NotifyMotionArgs motionArgs;
9693
9694 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009695 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9696 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009697 processPosition(mapper, x1, y1);
9698 processSync(mapper);
9699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9700 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009701 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009702
9703 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009704 processSlot(mapper, SECOND_SLOT);
9705 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009706 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009707 processSync(mapper);
9708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009709 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009710 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009711
9712 // If the tool type of the first finger changes to MT_TOOL_PALM,
9713 // we expect to receive ACTION_POINTER_UP with cancel flag.
9714 processSlot(mapper, FIRST_SLOT);
9715 processId(mapper, FIRST_TRACKING_ID);
9716 processToolType(mapper, MT_TOOL_PALM);
9717 processSync(mapper);
9718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009719 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009720 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9721
9722 // The following MOVE events of second finger should be processed.
9723 processSlot(mapper, SECOND_SLOT);
9724 processId(mapper, SECOND_TRACKING_ID);
9725 processPosition(mapper, x2 + 1, y2 + 1);
9726 processSync(mapper);
9727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9729 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9730
9731 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9732 // it. Second finger receive move.
9733 processSlot(mapper, FIRST_SLOT);
9734 processId(mapper, INVALID_TRACKING_ID);
9735 processSync(mapper);
9736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9737 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9738 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9739
9740 // Second finger keeps moving.
9741 processSlot(mapper, SECOND_SLOT);
9742 processId(mapper, SECOND_TRACKING_ID);
9743 processPosition(mapper, x2 + 2, y2 + 2);
9744 processSync(mapper);
9745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9747 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9748
9749 // Second finger up.
9750 processId(mapper, INVALID_TRACKING_ID);
9751 processSync(mapper);
9752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9753 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9754 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9755}
9756
9757/**
9758 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9759 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9760 */
9761TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9762 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009763 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009764 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9765 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9766
9767 NotifyMotionArgs motionArgs;
9768
9769 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9770 // First finger down.
9771 processId(mapper, FIRST_TRACKING_ID);
9772 processPosition(mapper, x1, y1);
9773 processSync(mapper);
9774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9775 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009776 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009777
9778 // Second finger down.
9779 processSlot(mapper, SECOND_SLOT);
9780 processId(mapper, SECOND_TRACKING_ID);
9781 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009782 processSync(mapper);
9783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009784 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009785 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009786
arthurhungcc7f9802020-04-30 17:55:40 +08009787 // If the tool type of the first finger changes to MT_TOOL_PALM,
9788 // we expect to receive ACTION_POINTER_UP with cancel flag.
9789 processSlot(mapper, FIRST_SLOT);
9790 processId(mapper, FIRST_TRACKING_ID);
9791 processToolType(mapper, MT_TOOL_PALM);
9792 processSync(mapper);
9793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009794 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009795 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9796
9797 // Second finger keeps moving.
9798 processSlot(mapper, SECOND_SLOT);
9799 processId(mapper, SECOND_TRACKING_ID);
9800 processPosition(mapper, x2 + 1, y2 + 1);
9801 processSync(mapper);
9802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9804
9805 // second finger becomes palm, receive cancel due to only 1 finger is active.
9806 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009807 processToolType(mapper, MT_TOOL_PALM);
9808 processSync(mapper);
9809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9810 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9811
arthurhungcc7f9802020-04-30 17:55:40 +08009812 // third finger down.
9813 processSlot(mapper, THIRD_SLOT);
9814 processId(mapper, THIRD_TRACKING_ID);
9815 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009816 processPosition(mapper, x3, y3);
9817 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9819 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009820 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009821 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9822
9823 // third finger move
9824 processId(mapper, THIRD_TRACKING_ID);
9825 processPosition(mapper, x3 + 1, y3 + 1);
9826 processSync(mapper);
9827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9829
9830 // first finger up, third finger receive move.
9831 processSlot(mapper, FIRST_SLOT);
9832 processId(mapper, INVALID_TRACKING_ID);
9833 processSync(mapper);
9834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9836 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9837
9838 // second finger up, third finger receive move.
9839 processSlot(mapper, SECOND_SLOT);
9840 processId(mapper, INVALID_TRACKING_ID);
9841 processSync(mapper);
9842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9844 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9845
9846 // third finger up.
9847 processSlot(mapper, THIRD_SLOT);
9848 processId(mapper, INVALID_TRACKING_ID);
9849 processSync(mapper);
9850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9851 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9852 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9853}
9854
9855/**
9856 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9857 * and the active finger could still be allowed to receive the events
9858 */
9859TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9860 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009861 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009862 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9863 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9864
9865 NotifyMotionArgs motionArgs;
9866
9867 // default tool type is finger
9868 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9869 processId(mapper, FIRST_TRACKING_ID);
9870 processPosition(mapper, x1, y1);
9871 processSync(mapper);
9872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9873 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009874 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009875
9876 // Second finger down.
9877 processSlot(mapper, SECOND_SLOT);
9878 processId(mapper, SECOND_TRACKING_ID);
9879 processPosition(mapper, x2, y2);
9880 processSync(mapper);
9881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009882 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009883 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009884
9885 // If the tool type of the second finger changes to MT_TOOL_PALM,
9886 // we expect to receive ACTION_POINTER_UP with cancel flag.
9887 processId(mapper, SECOND_TRACKING_ID);
9888 processToolType(mapper, MT_TOOL_PALM);
9889 processSync(mapper);
9890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009891 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009892 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9893
9894 // The following MOVE event should be processed.
9895 processSlot(mapper, FIRST_SLOT);
9896 processId(mapper, FIRST_TRACKING_ID);
9897 processPosition(mapper, x1 + 1, y1 + 1);
9898 processSync(mapper);
9899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9901 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9902
9903 // second finger up.
9904 processSlot(mapper, SECOND_SLOT);
9905 processId(mapper, INVALID_TRACKING_ID);
9906 processSync(mapper);
9907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9909
9910 // first finger keep moving
9911 processSlot(mapper, FIRST_SLOT);
9912 processId(mapper, FIRST_TRACKING_ID);
9913 processPosition(mapper, x1 + 2, y1 + 2);
9914 processSync(mapper);
9915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9917
9918 // first finger up.
9919 processId(mapper, INVALID_TRACKING_ID);
9920 processSync(mapper);
9921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9922 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9923 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009924}
9925
Arthur Hung9ad18942021-06-19 02:04:46 +00009926/**
9927 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9928 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9929 * cause slot be valid again.
9930 */
9931TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9932 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009933 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009934 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9935 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9936
9937 NotifyMotionArgs motionArgs;
9938
9939 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9940 // First finger down.
9941 processId(mapper, FIRST_TRACKING_ID);
9942 processPosition(mapper, x1, y1);
9943 processPressure(mapper, RAW_PRESSURE_MAX);
9944 processSync(mapper);
9945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9946 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9947 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9948
9949 // First finger move.
9950 processId(mapper, FIRST_TRACKING_ID);
9951 processPosition(mapper, x1 + 1, y1 + 1);
9952 processPressure(mapper, RAW_PRESSURE_MAX);
9953 processSync(mapper);
9954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9956 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9957
9958 // Second finger down.
9959 processSlot(mapper, SECOND_SLOT);
9960 processId(mapper, SECOND_TRACKING_ID);
9961 processPosition(mapper, x2, y2);
9962 processPressure(mapper, RAW_PRESSURE_MAX);
9963 processSync(mapper);
9964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009965 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009966 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9967
9968 // second finger up with some unexpected data.
9969 processSlot(mapper, SECOND_SLOT);
9970 processId(mapper, INVALID_TRACKING_ID);
9971 processPosition(mapper, x2, y2);
9972 processSync(mapper);
9973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009974 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009975 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9976
9977 // first finger up with some unexpected data.
9978 processSlot(mapper, FIRST_SLOT);
9979 processId(mapper, INVALID_TRACKING_ID);
9980 processPosition(mapper, x2, y2);
9981 processPressure(mapper, RAW_PRESSURE_MAX);
9982 processSync(mapper);
9983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9984 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9985 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9986}
9987
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009988TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9989 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009990 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009991 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9992 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9993
9994 // First finger down.
9995 processId(mapper, FIRST_TRACKING_ID);
9996 processPosition(mapper, 100, 200);
9997 processPressure(mapper, RAW_PRESSURE_MAX);
9998 processSync(mapper);
9999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10000 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10001
10002 // Second finger down.
10003 processSlot(mapper, SECOND_SLOT);
10004 processId(mapper, SECOND_TRACKING_ID);
10005 processPosition(mapper, 300, 400);
10006 processPressure(mapper, RAW_PRESSURE_MAX);
10007 processSync(mapper);
10008 ASSERT_NO_FATAL_FAILURE(
10009 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10010
10011 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010012 // preserved. Resetting should cancel the ongoing gesture.
10013 resetMapper(mapper, ARBITRARY_TIME);
10014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10015 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010016
10017 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10018 // the existing touch state to generate a down event.
10019 processPosition(mapper, 301, 302);
10020 processSync(mapper);
10021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10022 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10024 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10025
10026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10027}
10028
10029TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10030 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010031 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010032 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10033 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10034
10035 // First finger touches down and releases.
10036 processId(mapper, FIRST_TRACKING_ID);
10037 processPosition(mapper, 100, 200);
10038 processPressure(mapper, RAW_PRESSURE_MAX);
10039 processSync(mapper);
10040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10041 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10042 processId(mapper, INVALID_TRACKING_ID);
10043 processSync(mapper);
10044 ASSERT_NO_FATAL_FAILURE(
10045 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10046
10047 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10048 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010049 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10051
10052 // Send an empty sync frame. Since there are no pointers, no events are generated.
10053 processSync(mapper);
10054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10055}
10056
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010057TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010058 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010059 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010060 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
10061 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010063
10064 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10065 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10066 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10067 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10068 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10069
10070 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010071 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010072 processId(mapper, FIRST_TRACKING_ID);
10073 processToolType(mapper, MT_TOOL_PEN);
10074 processPosition(mapper, 100, 200);
10075 processPressure(mapper, RAW_PRESSURE_MAX);
10076 processSync(mapper);
10077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10078 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10079 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010080 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010081
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010082 // Now that we know the device supports styluses, ensure that the device is re-configured with
10083 // the stylus source.
10084 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10085 {
10086 const auto& devices = mReader->getInputDevices();
10087 auto deviceInfo =
10088 std::find_if(devices.begin(), devices.end(),
10089 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10090 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10091 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10092 }
10093
10094 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10096
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010097 processId(mapper, INVALID_TRACKING_ID);
10098 processSync(mapper);
10099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10100 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10101 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010102 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010103}
10104
Seunghwan Choi356026c2023-02-01 14:37:25 +090010105TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10106 addConfigurationProperty("touch.deviceType", "touchScreen");
10107 prepareDisplay(ui::ROTATION_0);
10108 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10109 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10110 // indicate stylus presence dynamically.
10111 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10112 std::shared_ptr<FakePointerController> fakePointerController =
10113 std::make_shared<FakePointerController>();
10114 mFakePolicy->setPointerController(fakePointerController);
10115 mFakePolicy->setStylusPointerIconEnabled(true);
10116 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10117
10118 processId(mapper, FIRST_TRACKING_ID);
10119 processPressure(mapper, RAW_PRESSURE_MIN);
10120 processPosition(mapper, 100, 200);
10121 processToolType(mapper, MT_TOOL_PEN);
10122 processSync(mapper);
10123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10124 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010125 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010126 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10127 ASSERT_TRUE(fakePointerController->isPointerShown());
10128 ASSERT_NO_FATAL_FAILURE(
10129 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10130}
10131
10132TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10133 addConfigurationProperty("touch.deviceType", "touchScreen");
10134 prepareDisplay(ui::ROTATION_0);
10135 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10136 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10137 // indicate stylus presence dynamically.
10138 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10139 std::shared_ptr<FakePointerController> fakePointerController =
10140 std::make_shared<FakePointerController>();
10141 mFakePolicy->setPointerController(fakePointerController);
10142 mFakePolicy->setStylusPointerIconEnabled(false);
10143 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10144
10145 processId(mapper, FIRST_TRACKING_ID);
10146 processPressure(mapper, RAW_PRESSURE_MIN);
10147 processPosition(mapper, 100, 200);
10148 processToolType(mapper, MT_TOOL_PEN);
10149 processSync(mapper);
10150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10151 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010152 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090010153 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10154 ASSERT_FALSE(fakePointerController->isPointerShown());
10155}
10156
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010157// --- MultiTouchInputMapperTest_ExternalDevice ---
10158
10159class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10160protected:
Chris Yea52ade12020-08-27 16:49:20 -070010161 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010162};
10163
10164/**
10165 * Expect fallback to internal viewport if device is external and external viewport is not present.
10166 */
10167TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10168 prepareAxes(POSITION);
10169 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010170 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010171 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10172
10173 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10174
10175 NotifyMotionArgs motionArgs;
10176
10177 // Expect the event to be sent to the internal viewport,
10178 // because an external viewport is not present.
10179 processPosition(mapper, 100, 100);
10180 processSync(mapper);
10181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10182 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10183
10184 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010185 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010186 processPosition(mapper, 100, 100);
10187 processSync(mapper);
10188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10189 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10190}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010191
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010192TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10193 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10194 std::shared_ptr<FakePointerController> fakePointerController =
10195 std::make_shared<FakePointerController>();
10196 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10197 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010198
10199 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010200 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010201 prepareAxes(POSITION | ID | SLOT);
10202 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10203 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10204 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010205 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010206 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10207
10208 // captured touchpad should be a touchpad source
10209 NotifyDeviceResetArgs resetArgs;
10210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10211 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10212
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010213 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010214
10215 const InputDeviceInfo::MotionRange* relRangeX =
10216 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10217 ASSERT_NE(relRangeX, nullptr);
10218 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10219 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10220 const InputDeviceInfo::MotionRange* relRangeY =
10221 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10222 ASSERT_NE(relRangeY, nullptr);
10223 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10224 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10225
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010226 // run captured pointer tests - note that this is unscaled, so input listener events should be
10227 // identical to what the hardware sends (accounting for any
10228 // calibration).
10229 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010230 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010231 processId(mapper, 1);
10232 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10233 processKey(mapper, BTN_TOUCH, 1);
10234 processSync(mapper);
10235
10236 // expect coord[0] to contain initial location of touch 0
10237 NotifyMotionArgs args;
10238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10239 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10240 ASSERT_EQ(1U, args.pointerCount);
10241 ASSERT_EQ(0, args.pointerProperties[0].id);
10242 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10243 ASSERT_NO_FATAL_FAILURE(
10244 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10245
10246 // FINGER 1 DOWN
10247 processSlot(mapper, 1);
10248 processId(mapper, 2);
10249 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10250 processSync(mapper);
10251
10252 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010254 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010255 ASSERT_EQ(2U, args.pointerCount);
10256 ASSERT_EQ(0, args.pointerProperties[0].id);
10257 ASSERT_EQ(1, args.pointerProperties[1].id);
10258 ASSERT_NO_FATAL_FAILURE(
10259 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10260 ASSERT_NO_FATAL_FAILURE(
10261 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10262
10263 // FINGER 1 MOVE
10264 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10265 processSync(mapper);
10266
10267 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10268 // from move
10269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10271 ASSERT_NO_FATAL_FAILURE(
10272 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10273 ASSERT_NO_FATAL_FAILURE(
10274 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10275
10276 // FINGER 0 MOVE
10277 processSlot(mapper, 0);
10278 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10279 processSync(mapper);
10280
10281 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10284 ASSERT_NO_FATAL_FAILURE(
10285 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10286 ASSERT_NO_FATAL_FAILURE(
10287 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10288
10289 // BUTTON DOWN
10290 processKey(mapper, BTN_LEFT, 1);
10291 processSync(mapper);
10292
10293 // touchinputmapper design sends a move before button press
10294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10295 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10298
10299 // BUTTON UP
10300 processKey(mapper, BTN_LEFT, 0);
10301 processSync(mapper);
10302
10303 // touchinputmapper design sends a move after button release
10304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10305 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10308
10309 // FINGER 0 UP
10310 processId(mapper, -1);
10311 processSync(mapper);
10312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10313 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10314
10315 // FINGER 1 MOVE
10316 processSlot(mapper, 1);
10317 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10318 processSync(mapper);
10319
10320 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10322 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10323 ASSERT_EQ(1U, args.pointerCount);
10324 ASSERT_EQ(1, args.pointerProperties[0].id);
10325 ASSERT_NO_FATAL_FAILURE(
10326 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10327
10328 // FINGER 1 UP
10329 processId(mapper, -1);
10330 processKey(mapper, BTN_TOUCH, 0);
10331 processSync(mapper);
10332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10333 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10334
Josep del Río2d8c79a2023-01-23 19:33:50 +000010335 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010336 mFakePolicy->setPointerCapture(false);
10337 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010339 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010340}
10341
10342TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10343 std::shared_ptr<FakePointerController> fakePointerController =
10344 std::make_shared<FakePointerController>();
10345 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10346 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010347
10348 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010349 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010350 prepareAxes(POSITION | ID | SLOT);
10351 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10352 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010353 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010354 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10355 // run uncaptured pointer tests - pushes out generic events
10356 // FINGER 0 DOWN
10357 processId(mapper, 3);
10358 processPosition(mapper, 100, 100);
10359 processKey(mapper, BTN_TOUCH, 1);
10360 processSync(mapper);
10361
10362 // start at (100,100), cursor should be at (0,0) * scale
10363 NotifyMotionArgs args;
10364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10365 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10366 ASSERT_NO_FATAL_FAILURE(
10367 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10368
10369 // FINGER 0 MOVE
10370 processPosition(mapper, 200, 200);
10371 processSync(mapper);
10372
10373 // compute scaling to help with touch position checking
10374 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10375 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10376 float scale =
10377 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10378
10379 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10381 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10383 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010384
10385 // BUTTON DOWN
10386 processKey(mapper, BTN_LEFT, 1);
10387 processSync(mapper);
10388
10389 // touchinputmapper design sends a move before button press
10390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10391 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10393 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10394
10395 // BUTTON UP
10396 processKey(mapper, BTN_LEFT, 0);
10397 processSync(mapper);
10398
10399 // touchinputmapper design sends a move after button release
10400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10401 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10403 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010404}
10405
10406TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10407 std::shared_ptr<FakePointerController> fakePointerController =
10408 std::make_shared<FakePointerController>();
10409
Michael Wrighta9cf4192022-12-01 23:46:39 +000010410 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010411 prepareAxes(POSITION | ID | SLOT);
10412 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010413 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010414 mFakePolicy->setPointerCapture(false);
10415 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10416
Josep del Río2d8c79a2023-01-23 19:33:50 +000010417 // uncaptured touchpad should be a pointer device
10418 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010419
Josep del Río2d8c79a2023-01-23 19:33:50 +000010420 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010421 mFakePolicy->setPointerCapture(true);
10422 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10423 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10424}
10425
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010426// --- BluetoothMultiTouchInputMapperTest ---
10427
10428class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10429protected:
10430 void SetUp() override {
10431 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10432 }
10433};
10434
10435TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10436 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010437 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010438 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10439 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10440
10441 nsecs_t kernelEventTime = ARBITRARY_TIME;
10442 nsecs_t expectedEventTime = ARBITRARY_TIME;
10443 // Touch down.
10444 processId(mapper, FIRST_TRACKING_ID);
10445 processPosition(mapper, 100, 200);
10446 processPressure(mapper, RAW_PRESSURE_MAX);
10447 processSync(mapper, ARBITRARY_TIME);
10448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10449 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10450
10451 // Process several events that come in quick succession, according to their timestamps.
10452 for (int i = 0; i < 3; i++) {
10453 constexpr static nsecs_t delta = ms2ns(1);
10454 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10455 kernelEventTime += delta;
10456 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10457
10458 processPosition(mapper, 101 + i, 201 + i);
10459 processSync(mapper, kernelEventTime);
10460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10461 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10462 WithEventTime(expectedEventTime))));
10463 }
10464
10465 // Release the touch.
10466 processId(mapper, INVALID_TRACKING_ID);
10467 processPressure(mapper, RAW_PRESSURE_MIN);
10468 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10470 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10471 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10472}
10473
10474// --- MultiTouchPointerModeTest ---
10475
HQ Liue6983c72022-04-19 22:14:56 +000010476class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10477protected:
10478 float mPointerMovementScale;
10479 float mPointerXZoomScale;
10480 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10481 addConfigurationProperty("touch.deviceType", "pointer");
10482 std::shared_ptr<FakePointerController> fakePointerController =
10483 std::make_shared<FakePointerController>();
10484 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10485 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010486 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010487
10488 prepareAxes(POSITION);
10489 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10490 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10491 // needs to be disabled, and the pointer gesture needs to be enabled.
10492 mFakePolicy->setPointerCapture(false);
10493 mFakePolicy->setPointerGestureEnabled(true);
10494 mFakePolicy->setPointerController(fakePointerController);
10495
10496 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10497 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10498 mPointerMovementScale =
10499 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10500 mPointerXZoomScale =
10501 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10502 }
10503
10504 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10505 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10506 /*flat*/ 0,
10507 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10508 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10509 /*flat*/ 0,
10510 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10511 }
10512};
10513
10514/**
10515 * Two fingers down on a pointer mode touch pad. The width
10516 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10517 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10518 * be greater than the both value to be freeform gesture, so that after two
10519 * fingers start to move downwards, the gesture should be swipe.
10520 */
10521TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10522 // The min freeform gesture width is 25units/mm x 30mm = 750
10523 // which is greater than fraction of the diagnal length of the touchpad (349).
10524 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010525 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010526 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10527 NotifyMotionArgs motionArgs;
10528
10529 // Two fingers down at once.
10530 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10531 // Pointer's initial position is used the [0,0] coordinate.
10532 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10533
10534 processId(mapper, FIRST_TRACKING_ID);
10535 processPosition(mapper, x1, y1);
10536 processMTSync(mapper);
10537 processId(mapper, SECOND_TRACKING_ID);
10538 processPosition(mapper, x2, y2);
10539 processMTSync(mapper);
10540 processSync(mapper);
10541
10542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10543 ASSERT_EQ(1U, motionArgs.pointerCount);
10544 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010545 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010546 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010547 ASSERT_NO_FATAL_FAILURE(
10548 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10549
10550 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10551 // that there should be 1 pointer.
10552 int32_t movingDistance = 200;
10553 y1 += movingDistance;
10554 y2 += movingDistance;
10555
10556 processId(mapper, FIRST_TRACKING_ID);
10557 processPosition(mapper, x1, y1);
10558 processMTSync(mapper);
10559 processId(mapper, SECOND_TRACKING_ID);
10560 processPosition(mapper, x2, y2);
10561 processMTSync(mapper);
10562 processSync(mapper);
10563
10564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10565 ASSERT_EQ(1U, motionArgs.pointerCount);
10566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010567 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010568 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10570 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10571 0, 0, 0, 0));
10572}
10573
10574/**
10575 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10576 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10577 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10578 * value to be freeform gesture, so that after two fingers start to move downwards,
10579 * the gesture should be swipe.
10580 */
10581TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10582 // The min freeform gesture width is 5units/mm x 30mm = 150
10583 // which is greater than fraction of the diagnal length of the touchpad (349).
10584 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010585 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
HQ Liue6983c72022-04-19 22:14:56 +000010586 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10587 NotifyMotionArgs motionArgs;
10588
10589 // Two fingers down at once.
10590 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10591 // Pointer's initial position is used the [0,0] coordinate.
10592 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10593
10594 processId(mapper, FIRST_TRACKING_ID);
10595 processPosition(mapper, x1, y1);
10596 processMTSync(mapper);
10597 processId(mapper, SECOND_TRACKING_ID);
10598 processPosition(mapper, x2, y2);
10599 processMTSync(mapper);
10600 processSync(mapper);
10601
10602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10603 ASSERT_EQ(1U, motionArgs.pointerCount);
10604 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010605 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010606 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010607 ASSERT_NO_FATAL_FAILURE(
10608 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10609
10610 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10611 // and there should be 1 pointer.
10612 int32_t movingDistance = 200;
10613 y1 += movingDistance;
10614 y2 += movingDistance;
10615
10616 processId(mapper, FIRST_TRACKING_ID);
10617 processPosition(mapper, x1, y1);
10618 processMTSync(mapper);
10619 processId(mapper, SECOND_TRACKING_ID);
10620 processPosition(mapper, x2, y2);
10621 processMTSync(mapper);
10622 processSync(mapper);
10623
10624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10625 ASSERT_EQ(1U, motionArgs.pointerCount);
10626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010627 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010628 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010629 // New coordinate is the scaled relative coordinate from the initial coordinate.
10630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10631 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10632 0, 0, 0, 0));
10633}
10634
10635/**
10636 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10637 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10638 * freeform gestures after two fingers start to move downwards.
10639 */
10640TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010641 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010642 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10643
10644 NotifyMotionArgs motionArgs;
10645
10646 // Two fingers down at once. Wider than the max swipe width.
10647 // The gesture is expected to be PRESS, then transformed to FREEFORM
10648 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10649
10650 processId(mapper, FIRST_TRACKING_ID);
10651 processPosition(mapper, x1, y1);
10652 processMTSync(mapper);
10653 processId(mapper, SECOND_TRACKING_ID);
10654 processPosition(mapper, x2, y2);
10655 processMTSync(mapper);
10656 processSync(mapper);
10657
10658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10659 ASSERT_EQ(1U, motionArgs.pointerCount);
10660 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010661 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010662 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010663 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10664 ASSERT_NO_FATAL_FAILURE(
10665 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10666
10667 int32_t movingDistance = 200;
10668
10669 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10670 // then two down events for two pointers.
10671 y1 += movingDistance;
10672 y2 += movingDistance;
10673
10674 processId(mapper, FIRST_TRACKING_ID);
10675 processPosition(mapper, x1, y1);
10676 processMTSync(mapper);
10677 processId(mapper, SECOND_TRACKING_ID);
10678 processPosition(mapper, x2, y2);
10679 processMTSync(mapper);
10680 processSync(mapper);
10681
10682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10683 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10684 ASSERT_EQ(1U, motionArgs.pointerCount);
10685 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010687 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
HQ Liue6983c72022-04-19 22:14:56 +000010688 ASSERT_EQ(1U, motionArgs.pointerCount);
10689 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010691 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010692 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010693 ASSERT_EQ(2U, motionArgs.pointerCount);
10694 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010695 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010696 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010697 // Two pointers' scaled relative coordinates from their initial centroid.
10698 // Initial y coordinates are 0 as y1 and y2 have the same value.
10699 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10700 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10701 // When pointers move, the new coordinates equal to the initial coordinates plus
10702 // scaled moving distance.
10703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10704 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10705 0, 0, 0, 0));
10706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10707 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10708 0, 0, 0, 0));
10709
10710 // Move two fingers down again, expect one MOVE motion event.
10711 y1 += movingDistance;
10712 y2 += movingDistance;
10713
10714 processId(mapper, FIRST_TRACKING_ID);
10715 processPosition(mapper, x1, y1);
10716 processMTSync(mapper);
10717 processId(mapper, SECOND_TRACKING_ID);
10718 processPosition(mapper, x2, y2);
10719 processMTSync(mapper);
10720 processSync(mapper);
10721
10722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10723 ASSERT_EQ(2U, motionArgs.pointerCount);
10724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010725 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010726 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10728 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10729 0, 0, 0, 0, 0));
10730 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10731 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10732 0, 0, 0, 0, 0));
10733}
10734
Harry Cutts39b7ca22022-10-05 15:55:48 +000010735TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010736 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Harry Cutts39b7ca22022-10-05 15:55:48 +000010737 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10738 NotifyMotionArgs motionArgs;
10739
10740 // Place two fingers down.
10741 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10742
10743 processId(mapper, FIRST_TRACKING_ID);
10744 processPosition(mapper, x1, y1);
10745 processMTSync(mapper);
10746 processId(mapper, SECOND_TRACKING_ID);
10747 processPosition(mapper, x2, y2);
10748 processMTSync(mapper);
10749 processSync(mapper);
10750
10751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10752 ASSERT_EQ(1U, motionArgs.pointerCount);
10753 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10754 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10755 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10756 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10757
10758 // Move the two fingers down and to the left.
10759 int32_t movingDistance = 200;
10760 x1 -= movingDistance;
10761 y1 += movingDistance;
10762 x2 -= movingDistance;
10763 y2 += movingDistance;
10764
10765 processId(mapper, FIRST_TRACKING_ID);
10766 processPosition(mapper, x1, y1);
10767 processMTSync(mapper);
10768 processId(mapper, SECOND_TRACKING_ID);
10769 processPosition(mapper, x2, y2);
10770 processMTSync(mapper);
10771 processSync(mapper);
10772
10773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10774 ASSERT_EQ(1U, motionArgs.pointerCount);
10775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10776 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10777 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10778 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10779}
10780
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010781TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010782 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010783 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10784 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10786
10787 // Start a stylus gesture.
10788 processKey(mapper, BTN_TOOL_PEN, 1);
10789 processId(mapper, FIRST_TRACKING_ID);
10790 processPosition(mapper, 100, 200);
10791 processSync(mapper);
10792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10793 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10794 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010795 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010796 // TODO(b/257078296): Pointer mode generates extra event.
10797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10798 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10799 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010800 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10802
10803 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10804 // gesture should be disabled.
10805 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10806 viewport->isActive = false;
10807 mFakePolicy->updateViewport(*viewport);
10808 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10810 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10811 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010812 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010813 // TODO(b/257078296): Pointer mode generates extra event.
10814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10815 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10816 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010817 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10819}
10820
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010821// --- JoystickInputMapperTest ---
10822
10823class JoystickInputMapperTest : public InputMapperTest {
10824protected:
10825 static const int32_t RAW_X_MIN;
10826 static const int32_t RAW_X_MAX;
10827 static const int32_t RAW_Y_MIN;
10828 static const int32_t RAW_Y_MAX;
10829
10830 void SetUp() override {
10831 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10832 }
10833 void prepareAxes() {
10834 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10835 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10836 }
10837
10838 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10840 }
10841
10842 void processSync(JoystickInputMapper& mapper) {
10843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10844 }
10845
Michael Wrighta9cf4192022-12-01 23:46:39 +000010846 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010847 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10848 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10849 NO_PORT, ViewportType::VIRTUAL);
10850 }
10851};
10852
10853const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10854const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10855const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10856const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10857
10858TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10859 prepareAxes();
10860 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10861
10862 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10863
Michael Wrighta9cf4192022-12-01 23:46:39 +000010864 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010865
10866 // Send an axis event
10867 processAxis(mapper, ABS_X, 100);
10868 processSync(mapper);
10869
10870 NotifyMotionArgs args;
10871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10872 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10873
10874 // Send another axis event
10875 processAxis(mapper, ABS_Y, 100);
10876 processSync(mapper);
10877
10878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10879 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10880}
10881
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010882// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010883
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010884class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010885protected:
10886 static const char* DEVICE_NAME;
10887 static const char* DEVICE_LOCATION;
10888 static const int32_t DEVICE_ID;
10889 static const int32_t DEVICE_GENERATION;
10890 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010891 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010892 static const int32_t EVENTHUB_ID;
10893
10894 std::shared_ptr<FakeEventHub> mFakeEventHub;
10895 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010896 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010897 std::unique_ptr<InstrumentedInputReader> mReader;
10898 std::shared_ptr<InputDevice> mDevice;
10899
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010900 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010901 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010902 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010903 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010904 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010905 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010906 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10907 }
10908
10909 void SetUp() override { SetUp(DEVICE_CLASSES); }
10910
10911 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010912 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010913 mFakePolicy.clear();
10914 }
10915
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010916 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010917 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10918 mReader->requestRefreshConfiguration(changes);
10919 mReader->loopOnce();
10920 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010921 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010922 }
10923
10924 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10925 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010926 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010927 InputDeviceIdentifier identifier;
10928 identifier.name = name;
10929 identifier.location = location;
10930 std::shared_ptr<InputDevice> device =
10931 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10932 identifier);
10933 mReader->pushNextDevice(device);
10934 mFakeEventHub->addDevice(eventHubId, name, classes);
10935 mReader->loopOnce();
10936 return device;
10937 }
10938
10939 template <class T, typename... Args>
10940 T& addControllerAndConfigure(Args... args) {
10941 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10942
10943 return controller;
10944 }
10945};
10946
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010947const char* PeripheralControllerTest::DEVICE_NAME = "device";
10948const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10949const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10950const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10951const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010952const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10953 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010954const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010955
10956// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010957class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010958protected:
10959 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010960 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010961 }
10962};
10963
10964TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010965 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010966
Harry Cuttsa5b71292022-11-28 12:56:17 +000010967 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10968 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10969 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010970}
10971
10972TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010973 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010974
Harry Cuttsa5b71292022-11-28 12:56:17 +000010975 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10976 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10977 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010978}
10979
10980// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010981class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010982protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010983 void SetUp() override {
10984 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10985 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010986};
10987
Chris Ye85758332021-05-16 23:05:17 -070010988TEST_F(LightControllerTest, MonoLight) {
10989 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010990 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010991 .maxBrightness = 255,
10992 .flags = InputLightClass::BRIGHTNESS,
10993 .path = ""};
10994 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010995
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010996 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010997 InputDeviceInfo info;
10998 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010999 std::vector<InputDeviceLightInfo> lights = info.getLights();
11000 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011001 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11002 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11003
11004 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11005 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11006}
11007
11008TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11009 RawLightInfo infoMono = {.id = 1,
11010 .name = "mono_keyboard_backlight",
11011 .maxBrightness = 255,
11012 .flags = InputLightClass::BRIGHTNESS |
11013 InputLightClass::KEYBOARD_BACKLIGHT,
11014 .path = ""};
11015 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11016
11017 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11018 InputDeviceInfo info;
11019 controller.populateDeviceInfo(&info);
11020 std::vector<InputDeviceLightInfo> lights = info.getLights();
11021 ASSERT_EQ(1U, lights.size());
11022 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11023 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011024
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011025 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11026 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011027}
11028
11029TEST_F(LightControllerTest, RGBLight) {
11030 RawLightInfo infoRed = {.id = 1,
11031 .name = "red",
11032 .maxBrightness = 255,
11033 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11034 .path = ""};
11035 RawLightInfo infoGreen = {.id = 2,
11036 .name = "green",
11037 .maxBrightness = 255,
11038 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11039 .path = ""};
11040 RawLightInfo infoBlue = {.id = 3,
11041 .name = "blue",
11042 .maxBrightness = 255,
11043 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11044 .path = ""};
11045 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11046 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11047 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11048
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011049 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011050 InputDeviceInfo info;
11051 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011052 std::vector<InputDeviceLightInfo> lights = info.getLights();
11053 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011054 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11055 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11056 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11057
11058 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11059 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11060}
11061
11062TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11063 RawLightInfo infoRed = {.id = 1,
11064 .name = "red_keyboard_backlight",
11065 .maxBrightness = 255,
11066 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11067 InputLightClass::KEYBOARD_BACKLIGHT,
11068 .path = ""};
11069 RawLightInfo infoGreen = {.id = 2,
11070 .name = "green_keyboard_backlight",
11071 .maxBrightness = 255,
11072 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11073 InputLightClass::KEYBOARD_BACKLIGHT,
11074 .path = ""};
11075 RawLightInfo infoBlue = {.id = 3,
11076 .name = "blue_keyboard_backlight",
11077 .maxBrightness = 255,
11078 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11079 InputLightClass::KEYBOARD_BACKLIGHT,
11080 .path = ""};
11081 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11082 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11083 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11084
11085 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11086 InputDeviceInfo info;
11087 controller.populateDeviceInfo(&info);
11088 std::vector<InputDeviceLightInfo> lights = info.getLights();
11089 ASSERT_EQ(1U, lights.size());
11090 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11091 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11092 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11093
11094 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11095 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11096}
11097
11098TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11099 RawLightInfo infoRed = {.id = 1,
11100 .name = "red",
11101 .maxBrightness = 255,
11102 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11103 .path = ""};
11104 RawLightInfo infoGreen = {.id = 2,
11105 .name = "green",
11106 .maxBrightness = 255,
11107 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11108 .path = ""};
11109 RawLightInfo infoBlue = {.id = 3,
11110 .name = "blue",
11111 .maxBrightness = 255,
11112 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11113 .path = ""};
11114 RawLightInfo infoGlobal = {.id = 3,
11115 .name = "global_keyboard_backlight",
11116 .maxBrightness = 255,
11117 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11118 InputLightClass::KEYBOARD_BACKLIGHT,
11119 .path = ""};
11120 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11121 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11122 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11123 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11124
11125 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11126 InputDeviceInfo info;
11127 controller.populateDeviceInfo(&info);
11128 std::vector<InputDeviceLightInfo> lights = info.getLights();
11129 ASSERT_EQ(1U, lights.size());
11130 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11131 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11132 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011133
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011134 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11135 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011136}
11137
11138TEST_F(LightControllerTest, MultiColorRGBLight) {
11139 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011140 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011141 .maxBrightness = 255,
11142 .flags = InputLightClass::BRIGHTNESS |
11143 InputLightClass::MULTI_INTENSITY |
11144 InputLightClass::MULTI_INDEX,
11145 .path = ""};
11146
11147 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11148
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011149 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011150 InputDeviceInfo info;
11151 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011152 std::vector<InputDeviceLightInfo> lights = info.getLights();
11153 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011154 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11155 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11156 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11157
11158 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11159 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11160}
11161
11162TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11163 RawLightInfo infoColor = {.id = 1,
11164 .name = "multi_color_keyboard_backlight",
11165 .maxBrightness = 255,
11166 .flags = InputLightClass::BRIGHTNESS |
11167 InputLightClass::MULTI_INTENSITY |
11168 InputLightClass::MULTI_INDEX |
11169 InputLightClass::KEYBOARD_BACKLIGHT,
11170 .path = ""};
11171
11172 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11173
11174 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11175 InputDeviceInfo info;
11176 controller.populateDeviceInfo(&info);
11177 std::vector<InputDeviceLightInfo> lights = info.getLights();
11178 ASSERT_EQ(1U, lights.size());
11179 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11180 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11181 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011182
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011183 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11184 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011185}
11186
11187TEST_F(LightControllerTest, PlayerIdLight) {
11188 RawLightInfo info1 = {.id = 1,
11189 .name = "player1",
11190 .maxBrightness = 255,
11191 .flags = InputLightClass::BRIGHTNESS,
11192 .path = ""};
11193 RawLightInfo info2 = {.id = 2,
11194 .name = "player2",
11195 .maxBrightness = 255,
11196 .flags = InputLightClass::BRIGHTNESS,
11197 .path = ""};
11198 RawLightInfo info3 = {.id = 3,
11199 .name = "player3",
11200 .maxBrightness = 255,
11201 .flags = InputLightClass::BRIGHTNESS,
11202 .path = ""};
11203 RawLightInfo info4 = {.id = 4,
11204 .name = "player4",
11205 .maxBrightness = 255,
11206 .flags = InputLightClass::BRIGHTNESS,
11207 .path = ""};
11208 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11209 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11210 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11211 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11212
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011213 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011214 InputDeviceInfo info;
11215 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011216 std::vector<InputDeviceLightInfo> lights = info.getLights();
11217 ASSERT_EQ(1U, lights.size());
11218 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011219 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11220 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011221
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011222 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11223 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11224 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011225}
11226
Michael Wrightd02c5b62014-02-10 15:10:22 -080011227} // namespace android